Changing LifxLightBulb config to default to running Lifxtest application, i.e. using...
[iot2.git] / benchmarks / other / ZigbeeTest / SpruceSensor.java
1 // Standard Java Packages
2 import java.util.Iterator;
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import java.util.concurrent.CopyOnWriteArrayList;
8 import java.util.concurrent.Semaphore;
9
10
11 // Checker annotations
12 //import iotchecker.qual.*;
13
14 // IoT Packages
15 import iotruntime.slave.*;
16 //import iotcode.interfaces.MoistureSensor;
17 //import iotcode.interfaces.MoistureSensorSmartCallback;
18 import iotruntime.zigbee.*;
19 //import iotcode.annotation.*;
20
21 public class SpruceSensor implements IoTZigbeeCallback, MoistureSensor {
22
23         private final int TIMEOUT_FOR_RESEND_MSEC = 1000;
24
25         private IoTZigbee zigConnection = null;
26         private boolean didClose;                                                                       // make sure that the clean up was done correctly
27
28         private float humidity = 0;
29         private Date timestampOfLastHumidity = null;
30
31         private AtomicBoolean didBind = new AtomicBoolean(false);
32         private AtomicBoolean didConfigureReporting = new AtomicBoolean(false);
33         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
34         private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
35         static Semaphore gettingLatestDataMutex = new Semaphore(1);
36
37         private List < MoistureSensorSmartCallback > callbackList = new CopyOnWriteArrayList < MoistureSensorSmartCallback > ();
38
39         private int sensorId = 0;
40
41         @config private IoTSet<IoTDeviceAddress> devUdpAddress;
42         @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
43
44         public SpruceSensor(IoTSet<IoTDeviceAddress> _devUdpAddress, IoTSet<IoTZigbeeAddress> _devZigbeeAddress) {
45                 devUdpAddress = _devUdpAddress;
46                 devZigbeeAddress = _devZigbeeAddress;
47         }
48
49         public void init() {
50
51                 if (didAlreadyInit.compareAndSet(false, true) == false) {
52                         return; // already init
53                 }
54
55                 didAlreadyClose.set(false);
56
57                 try {
58                         Iterator itrUdp = devUdpAddress.iterator();
59                         Iterator itrZig = devZigbeeAddress.iterator();
60
61                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
62
63                         // DEBUG
64                         System.out.println("DEBUG: Allocate iterators to print out addresses!");
65                         Iterator itrDebugUdp = devUdpAddress.iterator();
66                         IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
67                         System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
68                         System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
69                         System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
70
71                         Iterator itrDebugZig = devZigbeeAddress.iterator();
72                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
73                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
74
75                         zigConnection.registerCallback(this);
76                         System.out.println("Register callback!");
77                         zigConnection.init();
78                         System.out.println("Initialized!");
79
80                         while (!didBind.get()) {
81                                 zigConnection.sendBindRequest(0x0001, 0x0405, 0x01);
82                                 System.out.println("Sending bind request!");
83                                 try {
84                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
85                                 } catch (Exception e) {
86                                         e.printStackTrace();
87                                 }
88                         }
89
90                         while (!didConfigureReporting.get()) {
91                                 zigConnection.sendConfigureReportingCommand(0x0001, 0x0405, 0x0104, 0x01, 0x0000, 0x21, 0x0001, 0x0001, null);
92                                 try {
93                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
94                                 } catch (Exception e) {
95                                         e.printStackTrace();
96                                 }
97                         }
98                 } catch (Exception e) {
99                         e.printStackTrace();
100                 }
101         }
102
103         public void close() {
104
105                 if (didAlreadyClose.compareAndSet(false, true) == false) {
106                         return; // already init
107                 }
108
109                 didAlreadyInit.set(false);
110
111
112                 try {
113                         zigConnection.close();
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                 }
117         }
118
119         public void Finalize() {
120                 if (!didClose) {
121                         close();
122                 }
123         }
124
125         public void setId(int id) {
126
127                 sensorId = id;
128
129         }
130
131         public int getId() {
132
133                 return sensorId;
134
135         }
136
137         public float getMoisture() {
138
139                 float tmp = 0;
140                 try {
141                         gettingLatestDataMutex.acquire();
142                         tmp = humidity;
143
144                 } catch (Exception e) {
145                         e.printStackTrace();
146                 }
147                 gettingLatestDataMutex.release();
148
149                 return tmp;
150         }
151
152         public long getTimestampOfLastReading() {
153
154                 Date tmp = null;
155                 try {
156                         gettingLatestDataMutex.acquire();
157                         tmp = (Date)timestampOfLastHumidity.clone();
158
159                 } catch (Exception e) {
160                         e.printStackTrace();
161                 }
162                 gettingLatestDataMutex.release();
163                 long retLong = tmp.getTime();
164
165                 return retLong;
166         }
167
168         public void newMessageAvailable(IoTZigbeeMessage _zm) {
169
170                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
171                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
172                         if (message.getSucceeded()) {
173                                 didBind.set(true);
174                         }
175
176                 } else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse) {
177                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
178                         if (message.getAllSuccess()) {
179                                 didConfigureReporting.set(true);
180                         }
181
182                 } else if (_zm instanceof IoTZigbeeMessageZclReportAttributes) {
183                         IoTZigbeeMessageZclReportAttributes message = (IoTZigbeeMessageZclReportAttributes)_zm;
184                         List <IoTZigbeeMessageZclReportAttributes.Attribute> attrList = message.getAttributes();
185
186                         if (attrList.size() == 1) {
187                                 if (attrList.get(0).getAttributeId() == 0) {
188                                         byte[] data = attrList.get(0).getData();
189
190                                         int value = (data[0] * 256) + data[1];
191
192                                         try {
193                                                 gettingLatestDataMutex.acquire();
194                                                 humidity = (float)value / (float)100.0;
195                                                 timestampOfLastHumidity = new Date();
196                                         } catch (Exception e) {
197                                                 e.printStackTrace();
198                                         }
199                                         gettingLatestDataMutex.release();
200
201                                         try {
202                                                 for (MoistureSensorSmartCallback cb : callbackList) {
203                                                         cb.newReadingAvailable(this.getId(), this.getMoisture(), this.getTimestampOfLastReading());
204                                                 }
205                                         } catch (Exception e) {
206                                                 e.printStackTrace();
207                                         }
208                                 }
209                         }
210
211                 }
212         }
213
214         public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
215                 callbackList.add(_callbackTo);
216         }
217 }