Checking in new files for phone app and testing local control/communication.
[iotcloud.git] / version2 / src / java / light_fan_embed_benchmark_2bulbs / LightsController.java
1 import iotcloud.*;
2 import java.util.*;
3
4 class LightsController {
5
6     public static void main(String[] args) throws Exception {
7
8
9         // Bulb 1
10         byte[] bulbMacAdd1 = new byte[8];
11         bulbMacAdd1[0] = (byte)0xD0;
12         bulbMacAdd1[1] = (byte)0x73;
13         bulbMacAdd1[2] = (byte)0xD5;
14         bulbMacAdd1[3] = (byte)0x02;
15         bulbMacAdd1[4] = (byte)0x41;
16         bulbMacAdd1[5] = (byte)0xDA;
17         bulbMacAdd1[6] = (byte)0x00;
18         bulbMacAdd1[7] = (byte)0x00;
19
20         IoTDeviceAddress devAddr1 = new IoTDeviceAddress("192.168.1.232", 56700, 56700, false, false);
21         IoTUDP udp1 = new IoTUDP(devAddr1);
22         LightBulb bulb1 = new LifxLightBulb(udp1, bulbMacAdd1);
23
24
25
26         byte[] bulbMacAdd2 = new byte[8];
27         bulbMacAdd2[0] = (byte)0xD0;
28         bulbMacAdd2[1] = (byte)0x73;
29         bulbMacAdd2[2] = (byte)0xD5;
30         bulbMacAdd2[3] = (byte)0x12;
31         bulbMacAdd2[4] = (byte)0x8E;
32         bulbMacAdd2[5] = (byte)0x30;
33         bulbMacAdd2[6] = (byte)0x00;
34         bulbMacAdd2[7] = (byte)0x00;
35
36         IoTDeviceAddress devAddr2 = new IoTDeviceAddress("192.168.1.126", 56701, 56700, false, false);
37         IoTUDP udp2 = new IoTUDP(devAddr2);
38         LightBulb bulb2 = new LifxLightBulb(udp2, bulbMacAdd2);
39
40
41         List<LightBulb> bulbs = new ArrayList<LightBulb>();
42         bulbs.add(bulb1);
43         bulbs.add(bulb2);
44
45                 String a = "bulb";
46         //String a1 = "bulb1";
47         //String a2 = "bulb2";
48
49                 IoTString ia = new IoTString(a);
50         //IoTString ia1 = new IoTString(a1);
51         //IoTString ia2 = new IoTString(a2);
52
53         //List<IoTString> keys = new ArrayList<IoTString>();
54         //keys.add(ia1);
55         //keys.add(ia2);
56
57         String valueA = "on";
58         IoTString iValueA = new IoTString(valueA);
59
60         System.out.println("Starting System");
61         int counter = 0;
62
63
64         Table t1 = null;
65         try {
66             t1 = new Table("http://dc-6.calit2.uci.edu/test.iotcloud/", "reallysecret", 400, 6000);
67             // This is the controlling smartphone!
68             t1.addLocalCommunication(399, "192.168.1.108", 7000);
69
70             t1.rebuild();
71         } catch (Error e) {
72
73             e.printStackTrace();
74             for (int i = 0; i < bulbs.size(); i++) {
75                 bulbs.get(i).setColor(0, 100, 100);
76             }
77
78             while (true) {
79                 for (int i = 0; i < bulbs.size(); i++) {
80                     bulbs.get(i).turnOff();
81                 }
82                 Thread.sleep(1000);
83
84                 for (int i = 0; i < bulbs.size(); i++) {
85                     bulbs.get(i).turnOn();
86                 }
87                 Thread.sleep(1000);
88             }
89         }
90
91
92         while (true) {
93
94             try {
95
96                 System.out.println("Loop");
97
98                 for (int i = 0; i < bulbs.size(); i++) {
99                         t1.update();
100                         IoTString testValA1 = t1.getCommitted(ia);
101                     //IoTString testValA1 = t1.getCommitted(keys.get(i));
102                     //bulbs.get(i).setColor(200, 200, 200);
103                     if ((testValA1 != null) && (testValA1.equals(iValueA) == true)) {
104                         bulbs.get(i).turnOn();
105                                 System.out.println("Turning on bulbs");
106                     } else {
107                                 System.out.println("Turning off bulbs");
108                         bulbs.get(i).turnOff();
109                         
110                     }
111                 }
112
113                 Thread.sleep(1000);
114
115             } catch (Error e) {
116
117                 e.printStackTrace();
118                 for (int i = 0; i < bulbs.size(); i++) {
119                     bulbs.get(i).setColor(0, 100, 100);
120                 }
121
122
123                 while (true) {
124                     for (int i = 0; i < bulbs.size(); i++) {
125                         bulbs.get(i).turnOff();
126                     }
127                     Thread.sleep(1000);
128
129                     for (int i = 0; i < bulbs.size(); i++) {
130                         bulbs.get(i).turnOn();
131                     }
132                     Thread.sleep(1000);
133                 }
134             }
135         }
136     }
137 }