1 package iotcode.MotionSensor;
3 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import iotrmi.Java.IoTRMIComm;
11 import iotrmi.Java.IoTRMICommClient;
12 import iotrmi.Java.IoTRMICommServer;
13 import iotrmi.Java.IoTRMIUtil;
15 import iotcode.interfaces.*;
17 public class SmartthingsSensor_Skeleton implements SmartthingsSensor {
19 private SmartthingsSensor mainObj;
20 private int objectId = 3;
21 // Communications and synchronizations
22 private IoTRMIComm rmiComm;
23 private AtomicBoolean didAlreadyInitWaitInvoke;
24 private AtomicBoolean methodReceived;
25 private byte[] methodBytes = null;
27 private static Integer[] object2Permission = { 3, 2, 5, 6, 1, 4, 0 };
28 private static List<Integer> set2Allowed;
31 public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, int _portSend, int _portRecv) throws Exception {
33 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
35 IoTRMIUtil.mapSkel.put(_mainObj, this);
36 IoTRMIUtil.mapSkelId.put(_mainObj, objectId);
37 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
38 methodReceived = new AtomicBoolean(false);
39 rmiComm.registerSkeleton(objectId, methodReceived);
40 Thread thread1 = new Thread() {
43 ___waitRequestInvokeMethod();
54 public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
58 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
59 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
60 methodReceived = new AtomicBoolean(false);
61 rmiComm.registerSkeleton(objectId, methodReceived);
64 public boolean didAlreadyInitWaitInvoke() {
65 return didAlreadyInitWaitInvoke.get();
72 public int getValue() {
73 return mainObj.getValue();
76 public boolean isActiveValue() {
77 return mainObj.isActiveValue();
80 public long getTimestampOfLastReading() {
81 return mainObj.getTimestampOfLastReading();
84 public void setId(int id) {
89 return mainObj.getId();
92 public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
93 mainObj.registerCallback(_callbackTo);
96 public void ___init() {
97 byte[] localMethodBytes = methodBytes;
98 rmiComm.setGetMethodBytes();
99 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { }, new Class<?>[] { }, localMethodBytes);
103 public void ___getValue() throws IOException {
104 byte[] localMethodBytes = methodBytes;
105 rmiComm.setGetMethodBytes();
106 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { }, new Class<?>[] { }, localMethodBytes);
107 Object retObj = getValue();
108 rmiComm.sendReturnObj(retObj, localMethodBytes);
111 public void ___isActiveValue() throws IOException {
112 byte[] localMethodBytes = methodBytes;
113 rmiComm.setGetMethodBytes();
114 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { }, new Class<?>[] { }, localMethodBytes);
115 Object retObj = isActiveValue();
116 rmiComm.sendReturnObj(retObj, localMethodBytes);
119 public void ___getTimestampOfLastReading() throws IOException {
120 byte[] localMethodBytes = methodBytes;
121 rmiComm.setGetMethodBytes();
122 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { }, new Class<?>[] { }, localMethodBytes);
123 Object retObj = getTimestampOfLastReading();
124 rmiComm.sendReturnObj(retObj, localMethodBytes);
127 public void ___setId() {
128 byte[] localMethodBytes = methodBytes;
129 rmiComm.setGetMethodBytes();
130 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
131 setId((int) paramObj[0]);
134 public void ___getId() throws IOException {
135 byte[] localMethodBytes = methodBytes;
136 rmiComm.setGetMethodBytes();
137 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { }, new Class<?>[] { }, localMethodBytes);
138 Object retObj = getId();
139 rmiComm.sendReturnObj(retObj, localMethodBytes);
142 public void ___registerCallback() {
143 byte[] localMethodBytes = methodBytes;
144 rmiComm.setGetMethodBytes();
145 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
147 int[] stubIdArray0 = (int[]) paramObj[0];
148 int objIdRecv0 = stubIdArray0[0];
149 SmartthingsSensorSmartCallback newStub0 = null;
150 if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
151 newStub0 = new SmartthingsSensorSmartCallback_Stub(rmiComm, objIdRecv0);
152 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
153 rmiComm.setObjectIdCounter(objIdRecv0);
154 rmiComm.decrementObjectIdCounter();
157 newStub0 = (SmartthingsSensorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
159 SmartthingsSensorSmartCallback stub0 = newStub0;
160 registerCallback(stub0);
161 } catch(Exception ex) {
162 ex.printStackTrace();
163 throw new Error("Exception from callback object instantiation!");
167 public void ___waitRequestInvokeMethod() throws IOException {
168 didAlreadyInitWaitInvoke.compareAndSet(false, true);
170 if (!methodReceived.get()) {
173 methodBytes = rmiComm.getMethodBytes();
174 methodReceived.set(false);
175 int _objectId = IoTRMIComm.getObjectId(methodBytes);
176 int methodId = IoTRMIComm.getMethodId(methodBytes);
177 if (_objectId == objectId) {
178 if (!set2Allowed.contains(methodId)) {
179 throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId);
192 catch (Exception ex) {
193 ex.printStackTrace();
204 catch (Exception ex) {
205 ex.printStackTrace();
216 catch (Exception ex) {
217 ex.printStackTrace();
226 ___getTimestampOfLastReading();
228 catch (Exception ex) {
229 ex.printStackTrace();
240 catch (Exception ex) {
241 ex.printStackTrace();
252 catch (Exception ex) {
253 ex.printStackTrace();
262 ___registerCallback();
264 catch (Exception ex) {
265 ex.printStackTrace();
271 throw new Error("Method Id " + methodId + " not recognized!");