Changes
[iotcloud.git] / version2 / src / java / light_fan_embed_benchmark / WemoController.java
1 import iotcloud.*;
2 import java.util.*;
3
4
5 class WemoController {
6     public static void main(String[] args) throws Exception {
7
8         Table t1 = new Table("http://dc-6.calit2.uci.edu/test.iotcloud/", "reallysecret", 351, -1);
9         t1.rebuild();
10
11
12         String a = "fan";
13         IoTString ia = new IoTString(a);
14         t1.createNewKey(ia, 351);
15
16
17         String a1 = "wemo1";
18         String a2 = "wemo2";
19
20         IoTString ia1 = new IoTString(a1);
21         IoTString ia2 = new IoTString(a2);
22
23
24         List<IoTString> keys = new ArrayList<IoTString>();
25         keys.add(ia1);
26         keys.add(ia2);
27
28
29         IoTDeviceAddress devAddr1 = new IoTDeviceAddress("192.168.1.16", 49153, 49153, false, false);
30         Wemo wemo1 = new Wemo(devAddr1);
31
32         IoTDeviceAddress devAddr2 = new IoTDeviceAddress("192.168.1.34", 49154, 49153, false, false);
33         Wemo wemo2 = new Wemo(devAddr2);
34
35         List<Wemo> wemos = new ArrayList<Wemo>();
36         wemos.add(wemo1);
37         wemos.add(wemo2);
38
39
40         String pingTimerKey = "wemoController";
41         IoTString ipingTimerKey = new IoTString(pingTimerKey);
42
43
44         String valueA = "on";
45         IoTString iValueA = new IoTString(valueA);
46
47         System.out.println("Starting System");
48         int counter = 0;
49
50
51         while (true) {
52             try {
53                 String pingTimer = Long.toString(System.currentTimeMillis());
54                 IoTString ipingTimer = new IoTString(pingTimer);
55
56                 t1.update();
57                 t1.startTransaction();
58                 t1.addKV(ipingTimerKey, ipingTimer);
59                 t1.commitTransaction();
60
61
62                 t1.update();
63                 Thread.sleep(1000);
64
65                 for (int i = 0; i < 2; i++) {
66                     IoTString testValA1 = t1.getCommitted(keys.get(i));
67                     if ((testValA1 != null) && (testValA1.equals(iValueA) == true)) {
68                         wemos.get(i).turnOn();
69                     } else {
70                         wemos.get(i).turnOff();
71                     }
72                 }
73
74             } catch (Error e) {
75                 e.printStackTrace();
76
77                 Runtime runTime = Runtime.getRuntime();
78                 runTime.exec("gpio mode 4 out");
79
80
81                 while (true) {
82                     runTime.exec("gpio write 4 1");
83                     Thread.sleep(500);
84                     runTime.exec("gpio write 4 0");
85                     Thread.sleep(500);
86                 }
87             }
88         }
89     }
90 }