1 package iotcode.MultipurposeSensor;
3 // Standard Java Packages
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.concurrent.CopyOnWriteArrayList;
7 import java.util.concurrent.Semaphore;
10 //import iotchecker.qual.*;
11 import iotcode.annotation.*;
14 import iotruntime.slave.*;
15 import iotcode.interfaces.*;
16 import iotruntime.zigbee.*;
18 /** Class Smartthings sensor driver for Smartthings sensor devices.
20 * @author Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
24 public class MultipurposeSensor implements IoTZigbeeCallback, SmartthingsSensor {
26 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
28 private IoTZigbee zigConnection = null;
29 private boolean didClose; // make sure that the clean up was done correctly
30 private boolean detectStatus = false;
32 private int detectedValue = 0;
33 private Date timestampOfLastDetecting = null;
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);
41 private List < SmartthingsSensorSmartCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorSmartCallback > ();
43 private int sensorId = 0;
45 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
46 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
48 public MultipurposeSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
49 //devUdpAddress = dSet;
50 //devZigbeeAddress = zigSet;
55 if (didAlreadyInit.compareAndSet(false, true) == false) {
56 return; // already init
59 didAlreadyClose.set(false);
62 Iterator itrUdp = devUdpAddress.iterator();
63 Iterator itrZig = devZigbeeAddress.iterator();
65 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
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());
75 Iterator itrDebugZig = devZigbeeAddress.iterator();
76 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
77 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
79 zigConnection.registerCallback(this);
80 System.out.println("Register callback!");
82 System.out.println("Initialized!");
86 System.out.println("Sending Management Permit Joining Request");
87 for(int z=0; z<3; z++){
88 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
93 while (!didWriteAttrb.get()) {
94 System.out.println("Sending Write Attribute Request");
95 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
100 System.out.println("Sending Enrollment Reponse");
101 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
104 } catch (Exception e) {
110 private void sleep(int multipleTime){
115 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
116 } catch(Exception e){
121 public void close() {
123 if (didAlreadyClose.compareAndSet(false, true) == false) {
124 return; // already init
127 didAlreadyInit.set(false);
131 zigConnection.close();
132 } catch (Exception e) {
137 public void Finalize() {
143 public void setId(int id) {
155 public int getValue() {
159 gettingLatestDataMutex.acquire();
162 } catch (Exception e) {
165 gettingLatestDataMutex.release();
170 // MultipurposeSensor:
171 // - 24 = close = false
172 // - 25 = open = true
173 public boolean isActiveValue() {
175 int tmp = getValue();
178 else // Getting 24 here
179 detectStatus = false;
184 public long getTimestampOfLastReading() {
188 gettingLatestDataMutex.acquire();
189 tmp = (Date)timestampOfLastDetecting.clone();
191 } catch (Exception e) {
194 gettingLatestDataMutex.release();
195 long retLong = tmp.getTime();
200 public void newMessageAvailable(IoTZigbeeMessage _zm) {
203 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
204 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
205 if(message.getSuccessOrFail()){
208 gettingLatestDataMutex.acquire();
209 detectedValue = message.getStatus();
210 timestampOfLastDetecting = new Date();
211 } catch (Exception e) {
214 gettingLatestDataMutex.release();
216 for (SmartthingsSensorSmartCallback cb : callbackList) {
217 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
219 } catch (Exception e) {
226 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
227 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
228 if (message.getSuccessOrFail()) {
229 didWriteAttrb.set(true);
234 public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
235 callbackList.add(_callbackTo);