1 // Standard Java Packages
3 import java.util.concurrent.atomic.AtomicBoolean;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.Semaphore;
8 //import iotchecker.qual.*;
9 //import iotcode.annotation.*;
12 import iotruntime.slave.*;
13 import iotcode.interfaces.*;
14 import iotruntime.zigbee.*;
16 /** Class Smartthings sensor driver for Smartthings sensor devices.
18 * @author Changwoo Lee, Rahmadi Trimananda <rtrimana @ uci.edu>
22 public class MultipurposeSensor implements IoTZigbeeCallback, SmartthingsSensor {
24 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
26 private IoTZigbee zigConnection = null;
27 private boolean didClose; // make sure that the clean up was done correctly
28 private boolean detectStatus = false;
30 private int detectedValue = 0;
31 private Date timestampOfLastDetecting = null;
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);
39 private List < SmartthingsSensorCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorCallback > ();
41 private int sensorId = 0;
43 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
44 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
46 public MultipurposeSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
48 devZigbeeAddress = zigSet;
53 if (didAlreadyInit.compareAndSet(false, true) == false) {
54 return; // already init
57 didAlreadyClose.set(false);
60 Iterator itrUdp = devUdpAddress.iterator();
61 Iterator itrZig = devZigbeeAddress.iterator();
63 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
66 System.out.println("DEBUG: Allocate iterators to print out addresses!");
67 Iterator itrDebugUdp = devUdpAddress.iterator();
68 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
69 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
70 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
71 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
73 Iterator itrDebugZig = devZigbeeAddress.iterator();
74 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
75 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
77 zigConnection.registerCallback(this);
78 System.out.println("Register callback!");
80 System.out.println("Initialized!");
84 System.out.println("Sending Management Permit Joining Request");
85 for(int z=0; z<3; z++){
86 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
91 while (!didWriteAttrb.get()) {
92 System.out.println("Sending Write Attribute Request");
93 zigConnection.sendWriteAttributesCommand(0x0002, 0x0500, 0x0104, 0x01);
98 System.out.println("Sending Enrollment Reponse");
99 zigConnection.sendEnrollmentResponse(0x0003, 0x0500, 0x0104, 0x01);
102 } catch (Exception e) {
108 private void sleep(int multipleTime){
113 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
114 } catch(Exception e){
119 public void close() {
121 if (didAlreadyClose.compareAndSet(false, true) == false) {
122 return; // already init
125 didAlreadyInit.set(false);
129 zigConnection.close();
130 } catch (Exception e) {
135 public void Finalize() {
141 public void setId(int id) {
153 public int getValue() {
157 gettingLatestDataMutex.acquire();
160 } catch (Exception e) {
163 gettingLatestDataMutex.release();
168 // MultipurposeSensor:
169 // - 24 = close = false
170 // - 25 = open = true
171 public boolean isActiveValue() {
173 int tmp = getValue();
176 else // Getting 24 here
177 detectStatus = false;
182 public long getTimestampOfLastReading() {
186 gettingLatestDataMutex.acquire();
187 tmp = (Date)timestampOfLastDetecting.clone();
189 } catch (Exception e) {
192 gettingLatestDataMutex.release();
193 long retLong = tmp.getTime();
198 public void newMessageAvailable(IoTZigbeeMessage _zm) {
201 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
202 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
203 if(message.getSuccessOrFail()){
206 gettingLatestDataMutex.acquire();
207 detectedValue = message.getStatus();
208 timestampOfLastDetecting = new Date();
209 } catch (Exception e) {
212 gettingLatestDataMutex.release();
214 for (SmartthingsSensorCallback cb : callbackList) {
215 cb.newReadingAvailable(this.getId(), this.getValue(), this.isActiveValue());
217 } catch (Exception e) {
224 else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
225 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
226 if (message.getSuccessOrFail()) {
227 didWriteAttrb.set(true);
232 public void registerCallback(SmartthingsSensorCallback _callbackTo) {
233 callbackList.add(_callbackTo);