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         String a1 = "wemo1";
12         String a2 = "wemo2";
13
14         IoTString ia1 = new IoTString(a1);
15         IoTString ia2 = new IoTString(a2);
16
17
18         List<IoTString> keys = new ArrayList<IoTString>();
19         keys.add(ia1);
20         keys.add(ia2);
21
22
23         IoTDeviceAddress devAddr1 = new IoTDeviceAddress("192.168.2.145", 49153, 49153, false, false);
24         Wemo wemo1 = new Wemo(devAddr1);
25
26         IoTDeviceAddress devAddr2 = new IoTDeviceAddress("192.168.2.186", 49154, 49153, false, false);
27         Wemo wemo2 = new Wemo(devAddr2);
28
29         List<Wemo> wemos = new ArrayList<Wemo>();
30         wemos.add(wemo1);
31         wemos.add(wemo2);
32
33
34         String pingTimerKey = "wemoController";
35         IoTString ipingTimerKey = new IoTString(pingTimerKey);
36
37
38         String valueA = "on";
39         IoTString iValueA = new IoTString(valueA);
40
41         System.out.println("Starting System");
42         int counter = 0;
43
44
45         while (true) {
46             try {
47                 String pingTimer = Long.toString(System.currentTimeMillis());
48                 IoTString ipingTimer = new IoTString(pingTimer);
49
50                 t1.update();
51                 t1.startTransaction();
52                 t1.addKV(ipingTimerKey, ipingTimer);
53                 t1.commitTransaction();
54
55
56                 t1.update();
57                 Thread.sleep(1000);
58
59                 for (int i = 0; i < 2; i++) {
60                     IoTString testValA1 = t1.getCommitted(keys.get(i));
61                     if ((testValA1 != null) && (testValA1.equals(iValueA) == true)) {
62                         wemos.get(i).turnOn();
63                     } else {
64                         wemos.get(i).turnOff();
65                     }
66                 }
67
68             } catch (Error e) {
69                 e.printStackTrace();
70
71                 Runtime runTime = Runtime.getRuntime();
72                 runTime.exec("gpio mode 4 out");
73
74
75                 while (true) {
76                     runTime.exec("gpio write 4 1");
77                     Thread.sleep(500);
78                     runTime.exec("gpio write 4 0");
79                     Thread.sleep(500);
80                 }
81             }
82         }
83     }
84 }