2992130f6c16b337bc8ee2ed64245ec0553ada4f
[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                                 try {
83                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
84                                 } catch (Exception e) {
85                                         e.printStackTrace();
86                                 }
87                         }
88
89                         while (!didConfigureReporting.get()) {
90                                 zigConnection.sendConfigureReportingCommand(0x0001, 0x0405, 0x0104, 0x01, 0x0000, 0x21, 0x0001, 0x0001, null);
91                                 try {
92                                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC);
93                                 } catch (Exception e) {
94                                         e.printStackTrace();
95                                 }
96                         }
97                 } catch (Exception e) {
98                         e.printStackTrace();
99                 }
100         }
101
102         public void close() {
103
104                 if (didAlreadyClose.compareAndSet(false, true) == false) {
105                         return; // already init
106                 }
107
108                 didAlreadyInit.set(false);
109
110
111                 try {
112                         zigConnection.close();
113                 } catch (Exception e) {
114                         e.printStackTrace();
115                 }
116         }
117
118         public void Finalize() {
119                 if (!didClose) {
120                         close();
121                 }
122         }
123
124         public void setId(int id) {
125
126                 sensorId = id;
127
128         }
129
130         public int getId() {
131
132                 return sensorId;
133
134         }
135
136         public float getMoisture() {
137
138                 float tmp = 0;
139                 try {
140                         gettingLatestDataMutex.acquire();
141                         tmp = humidity;
142
143                 } catch (Exception e) {
144                         e.printStackTrace();
145                 }
146                 gettingLatestDataMutex.release();
147
148                 return tmp;
149         }
150
151         public long getTimestampOfLastReading() {
152
153                 Date tmp = null;
154                 try {
155                         gettingLatestDataMutex.acquire();
156                         tmp = (Date)timestampOfLastHumidity.clone();
157
158                 } catch (Exception e) {
159                         e.printStackTrace();
160                 }
161                 gettingLatestDataMutex.release();
162                 long retLong = tmp.getTime();
163
164                 return retLong;
165         }
166
167         public void newMessageAvailable(IoTZigbeeMessage _zm) {
168
169                 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
170                         IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
171                         if (message.getSucceeded()) {
172                                 didBind.set(true);
173                         }
174
175                 } else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse) {
176                         IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
177                         if (message.getAllSuccess()) {
178                                 didConfigureReporting.set(true);
179                         }
180
181                 } else if (_zm instanceof IoTZigbeeMessageZclReportAttributes) {
182                         IoTZigbeeMessageZclReportAttributes message = (IoTZigbeeMessageZclReportAttributes)_zm;
183                         List <IoTZigbeeMessageZclReportAttributes.Attribute> attrList = message.getAttributes();
184
185                         if (attrList.size() == 1) {
186                                 if (attrList.get(0).getAttributeId() == 0) {
187                                         byte[] data = attrList.get(0).getData();
188
189                                         int value = (data[0] * 256) + data[1];
190
191                                         try {
192                                                 gettingLatestDataMutex.acquire();
193                                                 humidity = (float)value / (float)100.0;
194                                                 timestampOfLastHumidity = new Date();
195                                         } catch (Exception e) {
196                                                 e.printStackTrace();
197                                         }
198                                         gettingLatestDataMutex.release();
199
200                                         try {
201                                                 for (MoistureSensorSmartCallback cb : callbackList) {
202                                                         cb.newReadingAvailable(this.getId(), this.getMoisture(), this.getTimestampOfLastReading());
203                                                 }
204                                         } catch (Exception e) {
205                                                 e.printStackTrace();
206                                         }
207                                 }
208                         }
209
210                 }
211         }
212
213         public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
214                 callbackList.add(_callbackTo);
215         }
216 }