+ /** Method to initialize IoTCloud server (dc-6.calit2.uci.edu)
+ *
+ * @return [void] None.
+ */
+ private void initIoTCloudServer() {
+
+ try {
+ System.out.println("DEBUG: Initialize IoTCloud table!");
+ // Get and init the IoTCloud server address
+ // Setup table
+ t1 = new Table(BASE_URL, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT);
+ t1.initTable();
+ // Setup is done somewhere else, we just do rebuild()
+ //t1.rebuild();
+ System.out.println("DEBUG: Table initialized!");
+ // Initialize sensors!
+ // TODO: Still deciding whether to initialize all devices here or inside each init method
+ int id = 0;
+ // Initialize alarms! One alarm for now
+ for(AlarmSmart alarm : alarmSet.values()) {
+ createKeyIoTCloud("alarm", NOT_ACTIVE);
+ System.out.println("DEBUG: Setting alarm to NOT-ACTIVE!");
+ }
+ // TODO: Just use alarm for now as a status to cloud
+ /*for(SmartthingsSensorSmart smartSensor : smartSensorsSet.values()) {
+
+ createKeyIoTCloud("sensor" + Integer.toString(id++), NOT_ACTIVE);
+ System.out.println("DEBUG: Setting sensor" + id + " to NOT-ACTIVE!");
+ }
+ // Initialize cameras! One camera for now...
+ for(CameraSmart cam : camSet.values()) {
+ createKeyIoTCloud("camera", NOT_ACTIVE);
+ System.out.println("DEBUG: Setting camera to NOT-ACTIVE!");
+ }
+ int doorId = 0;
+ for(SmartthingsActuatorSmart doorlock : doorlockSet.values()) {
+ createKeyIoTCloud("doorlock" + Integer.toString(doorId), NOT_ACTIVE);
+ System.out.println("DEBUG: Setting doorlock" + id + " to NOT-ACTIVE!");
+ }*/
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ System.out.println("DEBUG: Cloud server transactions committed successfully!");
+ }
+
+ /** Method to create key IoTCloud
+ *
+ * @param key [String] , encrypted key.
+ * @param val [IoTString] , encrypted value.
+ *
+ * @return [void] None.
+ */
+ private void createKeyIoTCloud(String key, IoTString val) {
+
+ try {
+ IoTString iotKey = new IoTString(key);
+ t1.update();
+ t1.createNewKey(iotKey, LOCAL_MACHINE_ID);
+ t1.startTransaction();
+ t1.addKV(iotKey, val);
+ t1.commitTransaction();
+ t1.update();
+ System.out.println("DEBUG: Successfully committed transaction for: " + key);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /** Method to update IoTCloud
+ *
+ * @param key [String] , encrypted key.
+ * @param val [IoTString] , encrypted value.
+ *
+ * @return [void] None.
+ */
+ private void updateIoTCloud(String key, IoTString val) {
+
+ try {
+ IoTString iotKey = new IoTString(key);
+ t1.update();
+ t1.startTransaction();
+ t1.addKV(iotKey, val);
+ t1.commitTransaction();
+ t1.update();
+ System.out.println("DEBUG: Successfully committed transaction for: " + key);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /** Method to read IoTCloud
+ *
+ * @param key [String] , encrypted key.
+ * @param val [IoTString] , encrypted value.
+ *
+ * @return [boolean] Check if it is ACTIVE or NOT_ACTIVE (true or false).
+ */
+ private boolean readIoTCloud(String key, IoTString iotTestVal) {
+
+ t1.update();
+ IoTString iotKey = new IoTString(key);
+ IoTString iotVal = t1.getCommitted(iotKey);
+
+ // Matching value and test value
+ if ((iotVal != null) && (iotVal.equals(iotTestVal) == true))
+ return true;
+ // Mismatch between value and test value
+ return false;
+ }