Adjusting files in ZigbeeTest; preparing things for 4th benchmark, i.e. generating...
[iot2.git] / benchmarks / drivers / MotionSensor / MotionSensor.java
1 package iotcode.MotionSensor;
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 MotionSensor 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 didAlreadyClose = new AtomicBoolean(true);
36         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
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 MotionSensor(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
87                         System.out.println("Sending Management Permit Joining Request");
88                         for(int z=0; z<3; z++){
89                                 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
90                                 sleep(0);
91                         }
92                         
93                         //made by changwoo
94                         while (!didWriteAttrb.get()) {
95                                 System.out.println("Sending Write Attribute Request");
96                                 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
97                                 sleep(0);
98                         }
99
100                         //made by changwoo
101                         System.out.println("Sending Enrollment Reponse");
102                         zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
103                         sleep(0);
104
105                 } catch (Exception e) {
106                         e.printStackTrace();
107                 }
108         }
109
110         //made by changwoo
111         private void sleep(int multipleTime){
112                 if(multipleTime<=0){
113                         multipleTime=1;
114                 }
115                 try{
116                         Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
117                 } catch(Exception e){
118                         e.printStackTrace();
119                 }
120         }
121
122         public void close() {
123
124                 if (didAlreadyClose.compareAndSet(false, true) == false) {
125                         return; // already init
126                 }
127
128                 didAlreadyInit.set(false);
129
130
131                 try {
132                         zigConnection.close();
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                 }
136         }
137
138
139         public void Finalize() {
140                 if (!didClose) {
141                         close();
142                 }
143         }
144
145         public void setId(int id) {
146
147                 sensorId = id;
148
149         }
150
151         public int getId() {
152
153                 return sensorId;
154
155         }
156
157         public int getValue() {
158
159                 int tmp = 0;
160                 try {
161                         gettingLatestDataMutex.acquire();
162                         tmp = detectedValue;
163
164                 } catch (Exception e) {
165                         e.printStackTrace();
166                 }
167                 gettingLatestDataMutex.release();
168
169                 return tmp;
170         }
171
172         // MotionSensor: 
173         // - 24 = no motion = false
174         // - 26 = motion = true
175         // After getting 26, if there is no motion for ~12 seconds then we get back 24
176         public boolean isActiveValue() {
177
178                 int tmp = getValue();
179                 if (tmp == 26)
180                         detectStatus = true;
181                 else // Getting 24 here
182                         detectStatus = false;
183
184                 return detectStatus;
185         }
186
187         public long getTimestampOfLastReading() {
188
189                 Date tmp = null;
190                 try {
191                         gettingLatestDataMutex.acquire();
192                         tmp = (Date)timestampOfLastDetecting.clone();
193
194                 } catch (Exception e) {
195                         e.printStackTrace();
196                 }
197                 gettingLatestDataMutex.release();
198                 long retLong = tmp.getTime();
199
200                 return retLong;
201         }
202
203         public void newMessageAvailable(IoTZigbeeMessage _zm) {
204
205                 //made by changwoo
206                 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
207                         IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
208                         if(message.getSuccessOrFail()){
209                                 //do something!
210
211                                 try {
212                                         gettingLatestDataMutex.acquire();
213                                         detectedValue = message.getStatus();
214                                         timestampOfLastDetecting = new Date();
215                                 } catch (Exception e) {
216                                         e.printStackTrace();
217                                 }
218                                 gettingLatestDataMutex.release();
219                                 try {
220                                         for (SmartthingsSensorSmartCallback cb : callbackList) {
221                                                 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
222                                         }
223                                 } catch (Exception e) {
224                                         e.printStackTrace();
225                                 }
226                         }//if
227                 
228                 //made by changwoo
229                 } else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
230                         IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
231                         if (message.getSuccessOrFail()) {
232                                 didWriteAttrb.set(true);
233                         }
234                 }
235         }
236
237         public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
238                 callbackList.add(_callbackTo);
239         }
240 }