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