8a56aabf035be5a9bd8e24cdfb04dfb3f4511bc0
[iot2.git] / benchmarks / other / ZigbeeTest / WaterLeakSensor.java
1 // Standard Java Packages
2 import java.util.*;
3 import java.util.concurrent.atomic.AtomicBoolean;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.Semaphore;
6
7 // Checker annotations
8 //import iotchecker.qual.*;
9 //import iotcode.annotation.*;
10
11 // IoT Packages
12 import iotruntime.slave.*;
13 import iotcode.interfaces.*;
14 import iotruntime.zigbee.*;
15
16 /** Class Smartthings sensor driver for Smartthings sensor devices.
17  *
18  * @author      Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
19  * @version     1.0
20  * @since       2016-12-01
21  */
22 public class WaterLeakSensor implements IoTZigbeeCallback, SmartthingsSensor {
23
24         private final int TIMEOUT_FOR_RESEND_MSEC = 900;
25
26         private IoTZigbee zigConnection = null;
27         private boolean didClose; // make sure that the clean up was done correctly
28         private boolean detectStatus = false;
29
30         private int detectedValue = 0;
31         private Date timestampOfLastDetecting = null;
32
33         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
34         private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
35         private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
36         private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
37         static Semaphore gettingLatestDataMutex = new Semaphore(1);
38
39         private List < SmartthingsSensorCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorCallback > ();
40
41         @config private IoTSet<IoTDeviceAddress> devUdpAddress;
42         @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
43
44         private int sensorId = 0;
45
46         public WaterLeakSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
47                 devUdpAddress = dSet;
48                 devZigbeeAddress = zigSet;
49         }
50
51         public void init() {
52
53                 if (didAlreadyInit.compareAndSet(false, true) == false) {
54                         return; // already init
55                 }
56
57                 didAlreadyClose.set(false);
58
59                 try {
60                         Iterator itrUdp = devUdpAddress.iterator();
61                         Iterator itrZig = devZigbeeAddress.iterator();
62
63                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
64
65                         // DEBUG
66                         System.out.println("DEBUG: Allocate iterators to print out addresses!");
67                         Iterator itrDebugUdp = devUdpAddress.iterator();
68                         IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
69                         System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
70                         System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
71                         System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
72
73                         Iterator itrDebugZig = devZigbeeAddress.iterator();
74                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
75                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
76
77                         zigConnection.registerCallback(this);
78                         System.out.println("Register callback!");
79                         zigConnection.init();
80                         System.out.println("Initialized!");
81
82                         //made by changwoo
83                         sleep(10);
84                         System.out.println("Sending Management Permit Joining Request");
85                         for(int z=0; z<3; z++){
86                                 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
87                                 sleep(0);
88                         }
89
90                         //made by changwoo
91                         while (!didWriteAttrb.get()) {
92                                 System.out.println("Sending Write Attribute Request");
93                                 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
94                                 sleep(0);
95                         }
96
97                         //made by changwoo
98                         System.out.println("Sending Enrollment Reponse");
99                         zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
100                         sleep(0);
101
102                 } catch (Exception e) {
103                         e.printStackTrace();
104                 }
105         }
106
107         //made by changwoo
108         private void sleep(int multipleTime){
109                 if(multipleTime<=0){
110                         multipleTime=1;
111                 }
112                 try{
113                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
114                 } catch(Exception e){
115                         e.printStackTrace();
116                 }
117         }
118
119         public void close() {
120
121                 if (didAlreadyClose.compareAndSet(false, true) == false) {
122                         return; // already init
123                 }
124
125                 didAlreadyInit.set(false);
126
127
128                 try {
129                         zigConnection.close();
130                 } catch (Exception e) {
131                         e.printStackTrace();
132                 }
133         }
134
135         public void Finalize() {
136                 if (!didClose) {
137                         close();
138                 }
139         }
140
141         public void setId(int id) {
142
143                 sensorId = id;
144
145         }
146
147         public int getId() {
148
149                 return sensorId;
150
151         }
152
153         public int getValue() {
154
155                 int tmp = 0;
156                 try {
157                         gettingLatestDataMutex.acquire();
158                         tmp = detectedValue;
159
160                 } catch (Exception e) {
161                         e.printStackTrace();
162                 }
163                 gettingLatestDataMutex.release();
164
165                 return tmp;
166         }
167
168         // WaterLeakSensor: 
169         // - 24 = no leak = false
170         // - 25 = leak = true
171         public boolean isActiveValue() {
172
173                 int tmp = getValue();
174                 if (tmp == 25)
175                         detectStatus = true;
176                 else // Getting 24 here
177                         detectStatus = false;
178
179                 return detectStatus;
180         }
181
182         public long getTimestampOfLastReading() {
183
184                 Date tmp = null;
185                 try {
186                         gettingLatestDataMutex.acquire();
187                         tmp = (Date)timestampOfLastDetecting.clone();
188
189                 } catch (Exception e) {
190                         e.printStackTrace();
191                 }
192                 gettingLatestDataMutex.release();
193                 long retLong = tmp.getTime();
194
195                 return retLong;
196         }
197
198         public void newMessageAvailable(IoTZigbeeMessage _zm) {
199
200                 //made by changwoo
201                 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
202                         IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
203                         if(message.getSuccessOrFail()){
204                                 //do something!
205
206                                 try {
207                                         gettingLatestDataMutex.acquire();
208                                         detectedValue = message.getStatus();
209                                         timestampOfLastDetecting = new Date();
210                                 } catch (Exception e) {
211                                         e.printStackTrace();
212                                 }
213                                 gettingLatestDataMutex.release();
214                                 try {
215                                         for (SmartthingsSensorCallback cb : callbackList) {
216                                                 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
217                                         }
218                                 } catch (Exception e) {
219                                         e.printStackTrace();
220                                 }
221                         }//if
222                 
223                 //made by changwoo
224                 }//if
225                 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
226                         IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
227                         if (message.getSuccessOrFail()) {
228                                 didWriteAttrb.set(true);
229                         }//if
230                 }//else if
231         }
232
233         public void registerCallback(SmartthingsSensorCallback _callbackTo) {
234                 callbackList.add(_callbackTo);
235         }
236 }