5b91b4ef4922393d1d34a0f8f2340b8a4fe90b50
[iot2.git] / benchmarks / drivers / Java / MultipurposeSensor / MultipurposeSensor.java
1 package iotcode.MultipurposeSensor;
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 MultipurposeSensor 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> multipurposeSensorUdpAddress;
46         @config private IoTSet<IoTZigbeeAddress> multipurposeSensorZigbeeAddress;
47
48         //public MultipurposeSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
49                 //multipurposeSensorUdpAddress = dSet;
50                 //multipurposeSensorZigbeeAddress = zigSet;
51         //}
52         public MultipurposeSensor() {
53         }
54
55         public void init() {
56
57                 if (didAlreadyInit.compareAndSet(false, true) == false) {
58                         return; // already init
59                 }
60                 didAlreadyClose.set(false);
61
62                 try {
63                         Iterator itrUdp = multipurposeSensorUdpAddress.iterator();
64                         Iterator itrZig = multipurposeSensorZigbeeAddress.iterator();
65
66                         zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
67
68                         // DEBUG
69                         System.out.println("DEBUG: Allocate iterators to print out addresses!");
70                         Iterator itrDebugUdp = multipurposeSensorUdpAddress.iterator();
71                         IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
72                         System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
73                         System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
74                         System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
75
76                         Iterator itrDebugZig = multipurposeSensorZigbeeAddress.iterator();
77                         IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
78                         System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
79
80                         zigConnection.registerCallback(this);
81                         System.out.println("Register callback!");
82                         zigConnection.init();
83                         System.out.println("Initialized!");
84
85                         //made by changwoo
86                         sleep(10);
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         public void Finalize() {
139                 if (!didClose) {
140                         close();
141                 }
142         }
143
144         public void setId(int id) {
145
146                 sensorId = id;
147
148         }
149
150         public int getId() {
151
152                 return sensorId;
153
154         }
155
156         public int getValue() {
157
158                 int tmp = 0;
159                 try {
160                         gettingLatestDataMutex.acquire();
161                         tmp = detectedValue;
162
163                 } catch (Exception e) {
164                         e.printStackTrace();
165                 }
166                 gettingLatestDataMutex.release();
167
168                 return tmp;
169         }
170
171         // MultipurposeSensor: 
172         // - 24 = close = false
173         // - 25 = open = true
174         public boolean isActiveValue() {
175
176                 int tmp = getValue();
177                 if (tmp == 25)
178                         detectStatus = true;
179                 else // Getting 24 here
180                         detectStatus = false;
181
182                 return detectStatus;
183         }
184
185         public long getTimestampOfLastReading() {
186
187                 Date tmp = null;
188                 try {
189                         gettingLatestDataMutex.acquire();
190                         tmp = (Date)timestampOfLastDetecting.clone();
191
192                 } catch (Exception e) {
193                         e.printStackTrace();
194                 }
195                 gettingLatestDataMutex.release();
196                 long retLong = tmp.getTime();
197
198                 return retLong;
199         }
200
201         public void newMessageAvailable(IoTZigbeeMessage _zm) {
202
203                 //made by changwoo
204                 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
205                         IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
206                         if(message.getSuccessOrFail()){
207                                 //do something!
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 }