|
@@ -308,6 +308,7 @@ public class BridgeService extends IntentService {
|
|
LightControlServiceConnection lightConn = new LightControlServiceConnection();
|
|
LightControlServiceConnection lightConn = new LightControlServiceConnection();
|
|
Intent lIntent = new Intent();
|
|
Intent lIntent = new Intent();
|
|
lIntent.setClassName("org.iotivity.bridge.homeserver", "org.iotivity.bridge.homeserver.BridgeService");
|
|
lIntent.setClassName("org.iotivity.bridge.homeserver", "org.iotivity.bridge.homeserver.BridgeService");
|
|
|
|
+ //lIntent.setClassName("com.icontrols.service.control", "com.iconstrols.service.control.LightControlService"); // TODO : need to check
|
|
lIntent.setAction(ILightControlService.class.getName());
|
|
lIntent.setAction(ILightControlService.class.getName());
|
|
bindService(lIntent, lightConn, Context.BIND_AUTO_CREATE);
|
|
bindService(lIntent, lightConn, Context.BIND_AUTO_CREATE);
|
|
}
|
|
}
|
|
@@ -315,6 +316,8 @@ public class BridgeService extends IntentService {
|
|
class LightControlServiceConnection implements ServiceConnection {
|
|
class LightControlServiceConnection implements ServiceConnection {
|
|
|
|
|
|
public static final String ECO_NAME = "rs-485" ;
|
|
public static final String ECO_NAME = "rs-485" ;
|
|
|
|
+ public static final String ONLINE = "on-line" ;
|
|
|
|
+ public static final String OFFLINE = "off-line" ;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
@@ -322,91 +325,14 @@ public class BridgeService extends IntentService {
|
|
Log.d(TAG, "onServiceConnected ComponentName=" + name + ", IBinder=" + service.toString());
|
|
Log.d(TAG, "onServiceConnected ComponentName=" + name + ", IBinder=" + service.toString());
|
|
if (service instanceof ILightControlService.Stub) {
|
|
if (service instanceof ILightControlService.Stub) {
|
|
|
|
|
|
- lightControlBinder = ILightControlService.Stub.asInterface(service);
|
|
|
|
-
|
|
|
|
try {
|
|
try {
|
|
|
|
+ lightControlBinder = ILightControlService.Stub.asInterface(service);
|
|
lightControlBinder.registerLightControlServiceCallback(lightCallback);
|
|
lightControlBinder.registerLightControlServiceCallback(lightCallback);
|
|
|
|
|
|
- String devices = lightControlBinder.retrieveAllDevices();
|
|
|
|
- Log.d(TAG, "Light Control retrieveAllDevices() : "+ devices);
|
|
|
|
- if(devices != null) {
|
|
|
|
- JSONArray array = new JSONArray(devices);
|
|
|
|
-
|
|
|
|
- for (int index = 0 ; index < array.length(); index++) {
|
|
|
|
- JSONObject devs = array.getJSONObject(index);
|
|
|
|
- Iterator<String> it_devs = devs.keys();
|
|
|
|
- String n = null, sn = null, rt = null, state = null ;
|
|
|
|
-
|
|
|
|
- while (it_devs.hasNext()) {
|
|
|
|
- String key = it_devs.next();
|
|
|
|
- Object value = devs.get(key);
|
|
|
|
- if (key.equals("n") && value instanceof String) {
|
|
|
|
- n = (String)value;
|
|
|
|
- Log.d(TAG, "Light Device : n = " + n);
|
|
|
|
- } else if (key.equals("sn") && value instanceof String) {
|
|
|
|
- sn = (String)value;
|
|
|
|
- Log.d(TAG, "Light Device : sn = " + sn);
|
|
|
|
- } else if (key.equals("rt") && value instanceof JSONArray) {
|
|
|
|
- Log.d(TAG, "Light Device : rt(org) = " + ((JSONArray)value).toString());
|
|
|
|
- rt = ((JSONArray)value).getString(0);
|
|
|
|
- Log.d(TAG, "Light Device : rt = " + rt);
|
|
|
|
- } else if (key.equals("state") && value instanceof String) {
|
|
|
|
- state = (String)value;
|
|
|
|
- Log.d(TAG, "Light Device : state = " + state);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // add device on ocf platform
|
|
|
|
- VirtualOCFDevice device = bridgeServer.addVirtualOCFDevice(n, sn, rt, ECO_NAME);
|
|
|
|
- if(device==null) continue;
|
|
|
|
-
|
|
|
|
- String resources = lightControlBinder.retrieveAllResources(sn);
|
|
|
|
- Log.d(TAG, "Light Control retrieveAllResources() : " + resources);
|
|
|
|
- if(resources!=null) {
|
|
|
|
- JSONArray resArr = new JSONArray(resources);
|
|
|
|
- for (int loop = 0 ; loop < resArr.length(); loop++) {
|
|
|
|
- JSONObject res = resArr.getJSONObject(loop);
|
|
|
|
- Iterator<String> rKeys = res.keys();
|
|
|
|
- String rName = null, rUri = null, rRT = null, rIF = null ;
|
|
|
|
-
|
|
|
|
- while (rKeys.hasNext()) {
|
|
|
|
- String rKey = rKeys.next();
|
|
|
|
- Object rValue = res.get(rKey);
|
|
|
|
- if (rKey.equals("n") && rValue instanceof String) {
|
|
|
|
- rName = (String)rValue;
|
|
|
|
- Log.d(TAG, "Light Resource : n = " + rName);
|
|
|
|
- } else if (rKey.equals("uri") && rValue instanceof String) {
|
|
|
|
- rUri = (String)rValue;
|
|
|
|
- Log.d(TAG, "Light Resource : uri = " + rUri);
|
|
|
|
- } else if (rKey.equals("rt") && rValue instanceof JSONArray) {
|
|
|
|
- Log.d(TAG, "Light Resource : rt(org) = " + ((JSONArray)rValue).toString());
|
|
|
|
- rRT = ((JSONArray)rValue).getString(0);
|
|
|
|
- Log.d(TAG, "Light Resource : rt = " + rRT);
|
|
|
|
- } else if (rKey.equals("if") && rValue instanceof String) {
|
|
|
|
- rIF = (String)rValue;
|
|
|
|
- Log.d(TAG, "Light Resource : if = " + rIF);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // add resource on the device
|
|
|
|
- bridgeServer.addVirtualDeviceResource(device, rName, rUri, rRT, rIF);
|
|
|
|
-
|
|
|
|
- // get resource properties
|
|
|
|
- String properites = lightControlBinder.doGetResourceProperties(sn, rUri);
|
|
|
|
- Log.d(TAG, "Light Control doGetResourceProperties("+sn+", "+rUri+") : " + properites);
|
|
|
|
-
|
|
|
|
- JSONObject props = new JSONObject(properites);
|
|
|
|
- Iterator<String> pKeys = props.keys();
|
|
|
|
- while(pKeys.hasNext()) {
|
|
|
|
- String pKey = pKeys.next();
|
|
|
|
- Object pValue = props.get(pKey);
|
|
|
|
- // update properties state
|
|
|
|
- device.addProperty(rUri, pKey, pValue);
|
|
|
|
- }
|
|
|
|
|
|
+ // Discover All Light Control Devices
|
|
|
|
+ discoverAllLightControlDevices();
|
|
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } catch (RemoteException | JSONException ex) {
|
|
|
|
|
|
+ } catch (RemoteException ex) {
|
|
Log.e(TAG, "Register Callback", ex);
|
|
Log.e(TAG, "Register Callback", ex);
|
|
// In this case the service has crashed before we could even
|
|
// In this case the service has crashed before we could even
|
|
// do anything with it; we can count on soon being
|
|
// do anything with it; we can count on soon being
|
|
@@ -599,6 +525,108 @@ public class BridgeService extends IntentService {
|
|
return null ;
|
|
return null ;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void discoverAllLightControlDevices() {
|
|
|
|
+
|
|
|
|
+ Log.d(TAG, "SecheduledTaskInit : doRetrieveLightControlAllDevices() called.");
|
|
|
|
+ try {
|
|
|
|
+ String devices = lightControlBinder.retrieveAllDevices();
|
|
|
|
+ Log.d(TAG, "Light Control retrieveAllDevices() : "+ devices);
|
|
|
|
+ if(devices != null) {
|
|
|
|
+ JSONArray array = new JSONArray(devices);
|
|
|
|
+
|
|
|
|
+ for (int index = 0 ; index < array.length(); index++) {
|
|
|
|
+ JSONObject devs = array.getJSONObject(index);
|
|
|
|
+ Iterator<String> it_devs = devs.keys();
|
|
|
|
+ String n = null, sn = null, rt = null, state = null ;
|
|
|
|
+
|
|
|
|
+ while (it_devs.hasNext()) {
|
|
|
|
+ String key = it_devs.next();
|
|
|
|
+ Object value = devs.get(key);
|
|
|
|
+ if (key.equals("n") && value instanceof String) {
|
|
|
|
+ n = (String)value;
|
|
|
|
+ Log.d(TAG, "Light Device : n = " + n);
|
|
|
|
+ } else if (key.equals("sn") && value instanceof String) {
|
|
|
|
+ sn = (String)value;
|
|
|
|
+ Log.d(TAG, "Light Device : sn = " + sn);
|
|
|
|
+ } else if (key.equals("rt") && value instanceof JSONArray) {
|
|
|
|
+ Log.d(TAG, "Light Device : rt(org) = " + ((JSONArray)value).toString());
|
|
|
|
+ rt = ((JSONArray)value).getString(0);
|
|
|
|
+ Log.d(TAG, "Light Device : rt = " + rt);
|
|
|
|
+ } else if (key.equals("state") && value instanceof String) {
|
|
|
|
+ state = (String)value;
|
|
|
|
+ Log.d(TAG, "Light Device : state = " + state);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ VirtualOCFDevice device = null ;
|
|
|
|
+ if(bridgeServer.existVirtualOCFDevice(sn)) { // exist Virtual OCF Device
|
|
|
|
+ if(LightControlServiceConnection.OFFLINE.equalsIgnoreCase(state)) {
|
|
|
|
+ device = bridgeServer.getVirtualOCFDevice(sn);
|
|
|
|
+ bridgeServer.disconnectVirtualDevice(device.getVODIndex()); // TODO : neet do check this logic properly
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ // add device on ocf platform
|
|
|
|
+ device = bridgeServer.addVirtualOCFDevice(n, sn, rt, LightControlServiceConnection.ECO_NAME);
|
|
|
|
+ if(device==null) continue;
|
|
|
|
+
|
|
|
|
+ String resources = lightControlBinder.retrieveAllResources(sn);
|
|
|
|
+ Log.d(TAG, "Light Control retrieveAllResources() : " + resources);
|
|
|
|
+ if(resources!=null) {
|
|
|
|
+ JSONArray resArr = new JSONArray(resources);
|
|
|
|
+ for (int loop = 0 ; loop < resArr.length(); loop++) {
|
|
|
|
+ JSONObject res = resArr.getJSONObject(loop);
|
|
|
|
+ Iterator<String> rKeys = res.keys();
|
|
|
|
+ String rName = null, rUri = null, rRT = null, rIF = null ;
|
|
|
|
+
|
|
|
|
+ while (rKeys.hasNext()) {
|
|
|
|
+ String rKey = rKeys.next();
|
|
|
|
+ Object rValue = res.get(rKey);
|
|
|
|
+ if (rKey.equals("n") && rValue instanceof String) {
|
|
|
|
+ rName = (String)rValue;
|
|
|
|
+ Log.d(TAG, "Light Resource : n = " + rName);
|
|
|
|
+ } else if (rKey.equals("uri") && rValue instanceof String) {
|
|
|
|
+ rUri = (String)rValue;
|
|
|
|
+ Log.d(TAG, "Light Resource : uri = " + rUri);
|
|
|
|
+ } else if (rKey.equals("rt") && rValue instanceof JSONArray) {
|
|
|
|
+ Log.d(TAG, "Light Resource : rt(org) = " + ((JSONArray)rValue).toString());
|
|
|
|
+ rRT = ((JSONArray)rValue).getString(0);
|
|
|
|
+ Log.d(TAG, "Light Resource : rt = " + rRT);
|
|
|
|
+ } else if (rKey.equals("if") && rValue instanceof String) {
|
|
|
|
+ rIF = (String)rValue;
|
|
|
|
+ Log.d(TAG, "Light Resource : if = " + rIF);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // add resource on the device
|
|
|
|
+ bridgeServer.addVirtualDeviceResource(device, rName, rUri, rRT, rIF);
|
|
|
|
+
|
|
|
|
+ // get resource properties
|
|
|
|
+ String properites = lightControlBinder.doGetResourceProperties(sn, rUri);
|
|
|
|
+ Log.d(TAG, "Light Control doGetResourceProperties("+sn+", "+rUri+") : " + properites);
|
|
|
|
+
|
|
|
|
+ JSONObject props = new JSONObject(properites);
|
|
|
|
+ Iterator<String> pKeys = props.keys();
|
|
|
|
+ while(pKeys.hasNext()) {
|
|
|
|
+ String pKey = pKeys.next();
|
|
|
|
+ Object pValue = props.get(pKey);
|
|
|
|
+ // update properties state
|
|
|
|
+ device.addProperty(rUri, pKey, pValue);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (RemoteException | JSONException ex) {
|
|
|
|
+ Log.e(TAG, "Register Callback", ex);
|
|
|
|
+ // In this case the service has crashed before we could even
|
|
|
|
+ // do anything with it; we can count on soon being
|
|
|
|
+ // disconnected (and then reconnected if it can be restarted)
|
|
|
|
+ // so there is no need to do anything here.
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public void doGetLightControlResources() {
|
|
public void doGetLightControlResources() {
|
|
|
|
|
|
Log.d(TAG, "SecheduledTaskInit : doGetLightControlResources() called.");
|
|
Log.d(TAG, "SecheduledTaskInit : doGetLightControlResources() called.");
|
|
@@ -811,7 +839,8 @@ public class BridgeService extends IntentService {
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
// Getting Properites of Light Control Service for resource monitoring
|
|
// Getting Properites of Light Control Service for resource monitoring
|
|
- Log.d(TAG, "LightControl Resources Retrieve ScheduledTask per 180sec was called.");
|
|
|
|
|
|
+ Log.d(TAG, "LightControl ScheduledTask per 180sec was called.");
|
|
|
|
+ discoverAllLightControlDevices();
|
|
doGetLightControlResources();
|
|
doGetLightControlResources();
|
|
}
|
|
}
|
|
}, 30, 180, TimeUnit.SECONDS); // 30sec interval after 30sec delay
|
|
}, 30, 180, TimeUnit.SECONDS); // 30sec interval after 30sec delay
|