Adding changes and files for doorlock driver
[iot2.git] / benchmarks / other / ZigbeeTest / MotionSensor.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 MotionSensor 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 didAlreadyClose = new AtomicBoolean(true);
34         private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
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         public MotionSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
45                 devUdpAddress = dSet;
46                 devZigbeeAddress = zigSet;
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                         //made by changwoo
81                         sleep(10);
82
83                         //System.out.println("Sending Management Permit Joining Request");
84                         for(int z=0; z<3; z++){
85                                 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
86                                 System.out.println("Sending Management Permit Joining Request");
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
136         public void Finalize() {
137                 if (!didClose) {
138                         close();
139                 }
140         }
141
142         public int getValue() {
143
144                 int tmp = 0;
145                 try {
146                         gettingLatestDataMutex.acquire();
147                         tmp = detectedValue;
148
149                 } catch (Exception e) {
150                         e.printStackTrace();
151                 }
152                 gettingLatestDataMutex.release();
153
154                 return tmp;
155         }
156
157         // MotionSensor: 
158         // - 24 = no motion = false
159         // - 26 = motion = true
160         // After getting 26, if there is no motion for ~12 seconds then we get back 24
161         public boolean isActiveValue() {
162
163                 int tmp = getValue();
164                 if (tmp == 26)
165                         detectStatus = true;
166                 else // Getting 24 here
167                         detectStatus = false;
168
169                 return detectStatus;
170         }
171
172         public long getTimestampOfLastReading() {
173
174                 Date tmp = null;
175                 try {
176                         gettingLatestDataMutex.acquire();
177                         tmp = (Date)timestampOfLastDetecting.clone();
178
179                 } catch (Exception e) {
180                         e.printStackTrace();
181                 }
182                 gettingLatestDataMutex.release();
183                 long retLong = tmp.getTime();
184
185                 return retLong;
186         }
187
188         public void newMessageAvailable(IoTZigbeeMessage _zm) {
189
190                 //made by changwoo
191                 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
192                         IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
193                         if(message.getSuccessOrFail()){
194                                 //do something!
195
196                                 try {
197                                         gettingLatestDataMutex.acquire();
198                                         detectedValue = message.getStatus();
199                                         timestampOfLastDetecting = new Date();
200                                 } catch (Exception e) {
201                                         e.printStackTrace();
202                                 }
203                                 gettingLatestDataMutex.release();
204                                 try {
205                                         for (SmartthingsSensorCallback cb : callbackList) {
206                                                 cb.newReadingAvailable(this.getValue(), this.isActiveValue());
207                                         }
208                                 } catch (Exception e) {
209                                         e.printStackTrace();
210                                 }
211                         }//if
212                 
213                 //made by changwoo
214                 } else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
215                         IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
216                         if (message.getSuccessOrFail()) {
217                                 didWriteAttrb.set(true);
218                         }
219                 }
220         }
221
222         public void registerCallback(SmartthingsSensorCallback _callbackTo) {
223                 callbackList.add(_callbackTo);
224         }
225 }