|
| 1 | +package com.maths22.laundryviewapi; |
| 2 | + |
| 3 | +import com.amazonaws.client.builder.AwsClientBuilder; |
| 4 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 5 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; |
| 6 | +import com.amazonaws.services.dynamodbv2.document.DynamoDB; |
| 7 | +import com.amazonaws.services.dynamodbv2.document.Item; |
| 8 | +import com.amazonaws.services.dynamodbv2.document.Table; |
| 9 | +import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec; |
| 10 | +import com.amazonaws.services.secretsmanager.AWSSecretsManager; |
| 11 | +import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder; |
| 12 | +import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest; |
| 13 | +import com.amazonaws.services.secretsmanager.model.GetSecretValueResult; |
| 14 | +import com.google.auth.oauth2.GoogleCredentials; |
| 15 | +import com.google.firebase.FirebaseApp; |
| 16 | +import com.google.firebase.FirebaseOptions; |
| 17 | +import com.google.firebase.messaging.*; |
| 18 | +import com.maths22.laundryviewapi.data.Machine; |
| 19 | +import com.maths22.laundryviewapi.data.RoomMachineStatus; |
| 20 | +import com.maths22.laundryviewapi.data.Status; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.Calendar; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +public class NotificationManager { |
| 32 | + private Table ddbTable; |
| 33 | + |
| 34 | + public NotificationManager() { |
| 35 | + AmazonDynamoDB client; |
| 36 | + if(System.getenv("dynamo-local") != null) { |
| 37 | + client = AmazonDynamoDBClientBuilder.standard() |
| 38 | + .withEndpointConfiguration( |
| 39 | + new AwsClientBuilder.EndpointConfiguration("http://ddblocal.docker:8000", "us-west-2")) |
| 40 | + .build(); |
| 41 | + } else { |
| 42 | + client = AmazonDynamoDBClientBuilder.standard() |
| 43 | + .build(); |
| 44 | + } |
| 45 | + DynamoDB db = new DynamoDB(client); |
| 46 | + ddbTable = db.getTable(System.getenv("notification_table")); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public void register(String machineId, String requesterId) { |
| 52 | + Calendar cal = Calendar.getInstance(); |
| 53 | + cal.add(Calendar.HOUR, 12); |
| 54 | + long expiry = cal.getTimeInMillis() / 1000L; |
| 55 | + Item item = new Item() |
| 56 | + .withPrimaryKey("RequesterId", requesterId, |
| 57 | + "MachineId", machineId) |
| 58 | + .withNumber("TimeToExist", expiry); |
| 59 | + ddbTable.putItem(item); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public void unregister(String machineId, String requesterId) { |
| 64 | + DeleteItemSpec spec = new DeleteItemSpec() |
| 65 | + .withPrimaryKey( |
| 66 | + "MachineId", |
| 67 | + machineId, |
| 68 | + "RequesterId", |
| 69 | + requesterId); |
| 70 | + |
| 71 | + |
| 72 | + ddbTable.deleteItem(spec); |
| 73 | + } |
| 74 | + |
| 75 | + private static String getGoogleKey() { |
| 76 | + if(System.getenv("googlekey-local") != null) { |
| 77 | + byte[] encoded = new byte[0]; |
| 78 | + try { |
| 79 | + encoded = Files.readAllBytes(Paths.get(System.getenv("googlekey-local"))); |
| 80 | + } catch (IOException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } |
| 83 | + return new String(encoded); |
| 84 | + } |
| 85 | + String secretName = System.getenv("firebase_secret_name"); |
| 86 | + |
| 87 | + // Create a Secrets Manager client |
| 88 | + AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard() |
| 89 | + .build(); |
| 90 | + |
| 91 | + String secret; |
| 92 | + GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest() |
| 93 | + .withSecretId(secretName); |
| 94 | + GetSecretValueResult getSecretValueResult; |
| 95 | + |
| 96 | + getSecretValueResult = client.getSecretValue(getSecretValueRequest); |
| 97 | + |
| 98 | + secret = getSecretValueResult.getSecretString(); |
| 99 | + return secret; |
| 100 | + } |
| 101 | + |
| 102 | + static boolean fbInitialized = false; |
| 103 | + |
| 104 | + private static void initFirebase() { |
| 105 | + if(fbInitialized) return; |
| 106 | + |
| 107 | + FirebaseOptions options; |
| 108 | + try { |
| 109 | + options = new FirebaseOptions.Builder() |
| 110 | + .setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream( getGoogleKey().getBytes() ))) |
| 111 | + |
| 112 | +// .setDatabaseUrl("https://numeric-drummer-119720.firebaseio.com/") |
| 113 | + .build(); |
| 114 | + } catch (IOException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + throw new RuntimeException(e); |
| 117 | + } |
| 118 | + |
| 119 | + FirebaseApp.initializeApp(options); |
| 120 | + fbInitialized = true; |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String args[]) { |
| 124 | + new NotificationManager().handleRequest(); |
| 125 | + } |
| 126 | + |
| 127 | + public void handleRequest() { |
| 128 | + initFirebase(); |
| 129 | + |
| 130 | + Map<String, RoomMachineStatus> statuses = new HashMap<>(); |
| 131 | + for(Item item : ddbTable.scan()) { |
| 132 | + try { |
| 133 | + String[] pieces = item.getString("MachineId").split("\\|"); |
| 134 | + String lrName = pieces[0]; |
| 135 | + String lrId = pieces[1]; |
| 136 | + String machineId = pieces[2]; |
| 137 | + RoomMachineStatus stat = statuses.get(lrId); |
| 138 | + if (stat == null) { |
| 139 | + stat = new LaundryViewEndpoint().machineStatus(lrId); |
| 140 | + statuses.put(lrId, stat); |
| 141 | + } |
| 142 | + String type = "WASHER"; |
| 143 | + Optional<Machine> machineOpt = stat.getWashers().stream().filter((m) -> m.getId().equals(machineId)).findFirst(); |
| 144 | + if (!machineOpt.isPresent()) { |
| 145 | + type = "DRYER"; |
| 146 | + machineOpt = stat.getDryers().stream().filter((m) -> m.getId().equals(machineId)).findFirst(); |
| 147 | + } |
| 148 | + if (!machineOpt.isPresent()) continue; |
| 149 | + |
| 150 | + Machine mac = machineOpt.get(); |
| 151 | + System.out.println(mac.getId() + " " + mac.getStatus()); |
| 152 | + if (mac.getStatus().equals(Status.AVAILBLE) || mac.getStatus().equals(Status.DONE)) { |
| 153 | + Message message = Message.builder() |
| 154 | + .setAndroidConfig(AndroidConfig.builder() |
| 155 | + .setNotification(AndroidNotification.builder() |
| 156 | + .setTitle(type.equals("WASHER") ? "Washing Machine Done" : "Dryer Done") |
| 157 | + .setBody(lrName + ": Machine #" + mac.getNumber()) |
| 158 | + .setIcon("washing_machine") |
| 159 | + .setSound("default") |
| 160 | + .setColor("#000075") |
| 161 | + .build()).build()) |
| 162 | + // .setToken(item.getString("RequesterId")) |
| 163 | + // .build(); |
| 164 | + // |
| 165 | + // FirebaseMessaging.getInstance().send(message); |
| 166 | + // |
| 167 | + // message = Message.builder() |
| 168 | + .putData("completed", item.getString("MachineId")) |
| 169 | + .setToken(item.getString("RequesterId")) |
| 170 | + .build(); |
| 171 | + |
| 172 | + try { |
| 173 | + FirebaseMessaging.getInstance().send(message); |
| 174 | + } catch (FirebaseMessagingException e) { |
| 175 | + e.printStackTrace(); |
| 176 | + } |
| 177 | + ddbTable.deleteItem("RequesterId", item.getString("RequesterId"), "MachineId", item.getString("MachineId")); |
| 178 | + } |
| 179 | + } catch (Exception e) { |
| 180 | + // We want to keep processing the other alerts |
| 181 | + e.printStackTrace(); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments