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