1 //package iotcode.DoorlockSensor;
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 DoorlockSensor implements IoTZigbeeCallback, SmartthingsSensor {
26 private final int TIMEOUT_FOR_RESEND_MSEC = 900;
28 private IoTZigbee zigConnection = null;
30 private int status = 0;
31 private boolean didClose; // make sure that the clean up was done correctly
32 //private boolean detectStatus = false;
34 //private int detectedValue = 0;
35 private Date timestampOfLastDetecting = null;
37 private AtomicBoolean didAlreadyClose = new AtomicBoolean(true);
38 private AtomicBoolean didAlreadyInit = new AtomicBoolean(false);
39 private AtomicBoolean didWriteAttrb = new AtomicBoolean(false);
40 //private AtomicBoolean didMatchDscr = new AtomicBoolean(false);
41 private AtomicBoolean didBind = new AtomicBoolean(false);
42 private AtomicBoolean didDoorLockConfigureReporting = new AtomicBoolean(false); //made by Jiawei
43 static Semaphore gettingLatestDataMutex = new Semaphore(1);
45 private List < SmartthingsSensorCallback > callbackList = new CopyOnWriteArrayList < SmartthingsSensorCallback > ();
47 private int sensorId = 0;
49 @config private IoTSet<IoTDeviceAddress> DoorlockSensorUdpAddress; //
50 @config private IoTSet<IoTZigbeeAddress> DoorlockSensorZigbeeAddress;//
52 public DoorlockSensor(IoTSet<IoTDeviceAddress> dSet, IoTSet<IoTZigbeeAddress> zigSet) {
53 DoorlockSensorUdpAddress = dSet;
54 DoorlockSensorZigbeeAddress = zigSet;
57 public DoorlockSensor() {
62 if (didAlreadyInit.compareAndSet(false, true) == false) {
63 return; // already init
66 didAlreadyClose.set(false);
69 Iterator itrUdp = DoorlockSensorUdpAddress.iterator();
70 Iterator itrZig = DoorlockSensorZigbeeAddress.iterator();
72 zigConnection = new IoTZigbee((IoTDeviceAddress)itrUdp.next(), (IoTZigbeeAddress)itrZig.next());
75 System.out.println("DEBUG: Allocate iterators to print out addresses!");
76 Iterator itrDebugUdp = DoorlockSensorUdpAddress.iterator();
77 IoTDeviceAddress iotaddDebug = (IoTDeviceAddress)itrDebugUdp.next();
78 System.out.println("IP address: " + iotaddDebug.getCompleteAddress());
79 System.out.println("Source port: " + iotaddDebug.getSourcePortNumber());
80 System.out.println("Destination port: " + iotaddDebug.getDestinationPortNumber());
82 Iterator itrDebugZig = DoorlockSensorZigbeeAddress.iterator();
83 IoTZigbeeAddress iotzbaddDebug = (IoTZigbeeAddress)itrDebugZig.next();
84 System.out.println("Zigbee address: " + iotzbaddDebug.getAddress());
86 zigConnection.registerCallback(this);
87 System.out.println("Register callback!");
89 System.out.println("Initialized!");
96 // System.out.println("BroadcastingRouteRecordRequest ");
97 // zigConnection.sendBroadcastingRouteRecordRequest(0x0001);
100 System.out.println("Sending Management Permit Joining Request");
101 // for(int z=0; z<3; z++){
102 zigConnection.sendManagementPermitJoiningRequest(0x0002, 0x0036, 0x00);
107 while(!didBind.get()){
108 System.out.println("Sending Bind Request");
109 zigConnection.sendBindRequest(0x0003, 0x0101, 0x02);
113 while(!didDoorLockConfigureReporting.get()){
114 System.out.println("Sending Door Lock: Configure Reporting");
115 zigConnection.sendConfigureReportingCommand(0x0004, 0x0101, 0x0104, 0x01, 0x02, 0x0000, 0x30, 0x0000, 0x100E, null);
120 Scanner in = new Scanner(System.in);
121 System.out.println("\nUnlock door: 0");
122 System.out.println("Lock door: 1");
123 System.out.println("Read status: 2 (or anything else)");
124 String str = in.next();
125 if(str.equals("1")) {
126 System.out.println("the doorlock sensor is locking");
127 zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 0);
129 }else if(str.equals("0")){
130 System.out.println("the doorlock sensor is unlocking");
131 zigConnection.sendLockOrUnlockDoorRequest(0x0005, 0x0101, 0x0104, 0x02, 1);
134 System.out.println("Let's see the doorlock sensor's status currently");
135 zigConnection.sendReadDoorStatusRequest(0x0005, 0x0101, 0x0104, 0x02, 0x10, 0x00, 0x0000);
141 } catch (Exception e) {
147 private void sleep(int multipleTime){
152 Thread.sleep(TIMEOUT_FOR_RESEND_MSEC*multipleTime);
153 } catch(Exception e){
159 public int getStatus() {
163 gettingLatestDataMutex.acquire();
166 } catch (Exception e) {
169 gettingLatestDataMutex.release();
174 public void close() {
176 if (didAlreadyClose.compareAndSet(false, true) == false) {
177 return; // already init
180 didAlreadyInit.set(false);
184 zigConnection.close();
185 } catch (Exception e) {
190 public void Finalize() {
196 public void setId(int id) {
209 public long getTimestampOfLastReading() {
213 gettingLatestDataMutex.acquire();
214 tmp = (Date)timestampOfLastDetecting.clone();
216 } catch (Exception e) {
219 gettingLatestDataMutex.release();
220 long retLong = tmp.getTime();
225 public void newMessageAvailable(IoTZigbeeMessage _zm) {
228 if (_zm instanceof IoTZigbeeMessageZdoBindResponse) {
229 IoTZigbeeMessageZdoBindResponse message = (IoTZigbeeMessageZdoBindResponse)_zm;
230 if (message.getSucceeded()) {
234 else if (_zm instanceof IoTZigbeeMessageZclConfigureReportingResponse){
235 IoTZigbeeMessageZclConfigureReportingResponse message = (IoTZigbeeMessageZclConfigureReportingResponse)_zm;
236 if (message.getAllSuccess()) {
237 didDoorLockConfigureReporting.set(true);
240 else if (_zm instanceof IoTZigbeeMessageZclReadAttributesResponse) {
241 IoTZigbeeMessageZclReadAttributesResponse message = (IoTZigbeeMessageZclReadAttributesResponse)_zm;
242 List <IoTZigbeeMessageZclReadAttributesResponse.Attribute> attrList = message.getAttributes();
244 if (attrList.size() == 1) {
245 if(attrList.get(0).getAttributeId() == 0) {
246 byte[] data = attrList.get(0).getData();
250 gettingLatestDataMutex.acquire();
252 timestampOfLastDetecting = new Date();
253 } catch (Exception e) {
256 gettingLatestDataMutex.release();
259 for (SmartthingsSensorCallback cb : callbackList) {
260 cb.newReadingAvailable(this);
262 } catch (Exception e) {
270 //public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
271 public void registerCallback(SmartthingsSensorCallback _callbackTo) {
272 callbackList.add(_callbackTo);