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 MotionSensor 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 @config private IoTSet<IoTDeviceAddress> devUdpAddress;
42 @config private IoTSet<IoTZigbeeAddress> devZigbeeAddress;
44 public MotionSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
46 devZigbeeAddress = zigSet;
51 if (didAlreadyInit.compareAndSet(false, true) == false) {
52 return; // already init
55 didAlreadyClose.set(false);
58 Iterator itrUdp = devUdpAddress.iterator();
59 Iterator itrZig = devZigbeeAddress.iterator();
61 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
64 System.out.println("DEBUG: Allocate iterators to print out addresses!");
65 Iterator itrDebugUdp = devUdpAddress.iterator();
66 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
67 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
68 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
69 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
71 Iterator itrDebugZig = devZigbeeAddress.iterator();
72 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
73 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
75 zigConnection.registerCallback(this);
76 System.out.println("Register callback!");
78 System.out.println("Initialized!");
83 //System.out.println("Sending Management Permit Joining Request");
84 for(int z=0; z<3; z++){
85 zigConnection.sendManagementPermitJoiningRequest(0x0001, 0x0036, 0x00);
86 System.out.println("Sending Management Permit Joining Request");
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) {
136 public void Finalize() {
142 public int getValue() {
146 gettingLatestDataMutex.acquire();
149 } catch (Exception e) {
152 gettingLatestDataMutex.release();
158 // - 24 = no motion = false
159 // - 26 = motion = true
160 // After getting 26, if there is no motion for ~12 seconds then we get back 24
161 public boolean isActiveValue() {
163 int tmp = getValue();
166 else // Getting 24 here
167 detectStatus = false;
172 public long getTimestampOfLastReading() {
176 gettingLatestDataMutex.acquire();
177 tmp = (Date)timestampOfLastDetecting.clone();
179 } catch (Exception e) {
182 gettingLatestDataMutex.release();
183 long retLong = tmp.getTime();
188 public void newMessageAvailable(IoTZigbeeMessage _zm) {
191 if(_zm instanceof IoTZigbeeMessageZclZoneStatusChangeNotification){
192 IoTZigbeeMessageZclZoneStatusChangeNotification message = (IoTZigbeeMessageZclZoneStatusChangeNotification)_zm;
193 if(message.getSuccessOrFail()){
197 gettingLatestDataMutex.acquire();
198 detectedValue = message.getStatus();
199 timestampOfLastDetecting = new Date();
200 } catch (Exception e) {
203 gettingLatestDataMutex.release();
205 for (SmartthingsSensorCallback cb : callbackList) {
206 cb.newReadingAvailable(this.getValue(), this.isActiveValue());
208 } catch (Exception e) {
214 } else if (_zm instanceof IoTZigbeeMessageZclWriteAttributesResponse) {
215 IoTZigbeeMessageZclWriteAttributesResponse message = (IoTZigbeeMessageZclWriteAttributesResponse)_zm;
216 if (message.getSuccessOrFail()) {
217 didWriteAttrb.set(true);
222 public void registerCallback(SmartthingsSensorCallback _callbackTo) {
223 callbackList.add(_callbackTo);