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> multipurposeSensorUdpAddress;
46 @config private IoTSet<IoTZigbeeAddress> multipurposeSensorZigbeeAddress;
48 //public MultipurposeSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
49 //multipurposeSensorUdpAddress = dSet;
50 //multipurposeSensorZigbeeAddress = zigSet;
52 public MultipurposeSensor() {
57 if (didAlreadyInit.compareAndSet(false, true) == false) {
58 return; // already init
60 didAlreadyClose.set(false);
63 Iterator itrUdp = multipurposeSensorUdpAddress.iterator();
64 Iterator itrZig = multipurposeSensorZigbeeAddress.iterator();
66 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
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());
76 Iterator itrDebugZig = multipurposeSensorZigbeeAddress.iterator();
77 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
78 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
80 zigConnection.registerCallback(this);
81 System.out.println("Register callback!");
83 System.out.println("Initialized!");
87 System.out.println("Sending Management Permit Joining Request");
88 for(int z=0; z<3; z++){
89 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
94 while (!didWriteAttrb.get()) {
95 System.out.println("Sending Write Attribute Request");
96 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
101 System.out.println("Sending Enrollment Reponse");
102 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
105 } catch (Exception e) {
111 private void sleep(int multipleTime){
116 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
117 } catch(Exception e){
122 public void close() {
124 if (didAlreadyClose.compareAndSet(false, true) == false) {
125 return; // already init
128 didAlreadyInit.set(false);
132 zigConnection.close();
133 } catch (Exception e) {
138 public void Finalize() {
144 public void setId(int id) {
156 public int getValue() {
160 gettingLatestDataMutex.acquire();
163 } catch (Exception e) {
166 gettingLatestDataMutex.release();
171 // MultipurposeSensor:
172 // - 24 = close = false
173 // - 25 = open = true
174 public boolean isActiveValue() {
176 int tmp = getValue();
179 else // Getting 24 here
180 detectStatus = false;
185 public long getTimestampOfLastReading() {
189 gettingLatestDataMutex.acquire();
190 tmp = (Date)timestampOfLastDetecting.clone();
192 } catch (Exception e) {
195 gettingLatestDataMutex.release();
196 long retLong = tmp.getTime();
201 public void newMessageAvailable(IoTZigbeeMessage _zm) {
204 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
205 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
206 if(message.getSuccessOrFail()){
209 gettingLatestDataMutex.acquire();
210 detectedValue = message.getStatus();
211 timestampOfLastDetecting = new Date();
212 } catch (Exception e) {
215 gettingLatestDataMutex.release();
217 for (SmartthingsSensorSmartCallback cb : callbackList) {
218 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
220 } catch (Exception e) {
227 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
228 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
229 if (message.getSuccessOrFail()) {
230 didWriteAttrb.set(true);
235 public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
236 callbackList.add(_callbackTo);