Parcourir la source

LightControlService Bindig issue was solved.

Trishia il y a 4 ans
Parent
commit
c34773a3ab
1 fichiers modifiés avec 38 ajouts et 34 suppressions
  1. 38 34
      app/src/main/java/org/iotivity/bridge/homeserver/BridgeService.java

+ 38 - 34
app/src/main/java/org/iotivity/bridge/homeserver/BridgeService.java

@@ -131,7 +131,7 @@ public class BridgeService extends IntentService {
         if (IBridgeService.class.getName().equals(intent.getAction())) {
         if (IBridgeService.class.getName().equals(intent.getAction())) {
             return rBinder; // return Remote Binder
             return rBinder; // return Remote Binder
         } else if (ILightControlService.class.getName().equals(intent.getAction())) {
         } else if (ILightControlService.class.getName().equals(intent.getAction())) {
-            return lightBinder; // return Remote Binder
+            return null ;//lightBinder; // return Remote Binder
         }
         }
         return lBinder; // return Local Binder
         return lBinder; // return Local Binder
     }
     }
@@ -304,7 +304,7 @@ public class BridgeService extends IntentService {
     private LightControlServiceConnection lightConn = new LightControlServiceConnection();
     private LightControlServiceConnection lightConn = new LightControlServiceConnection();
     public void bindLightControlService() {
     public void bindLightControlService() {
 
 
-        Log.d(TAG, "SecheduledTaskInit : bindLightControlService() called.");
+        Log.d(TAG, "ScheduledTaskInit : bindLightControlService() called.");
 
 
         // Light Control Service Remote Bind
         // Light Control Service Remote Bind
         Intent lIntent = new Intent();
         Intent lIntent = new Intent();
@@ -341,26 +341,23 @@ public class BridgeService extends IntentService {
         public void onServiceConnected(ComponentName name, IBinder service) {
         public void onServiceConnected(ComponentName name, IBinder service) {
 
 
             Log.d(TAG, "onServiceConnected ComponentName=" + name + ", IBinder=" + service.toString());
             Log.d(TAG, "onServiceConnected ComponentName=" + name + ", IBinder=" + service.toString());
-            if (service instanceof ILightControlService.Stub) {
+            try {
+                lightControlBinder = ILightControlService.Stub.asInterface(service);
+                lightControlBinder.registerLightControlServiceCallback(lightCallback);
 
 
-                try {
-                    lightControlBinder = ILightControlService.Stub.asInterface(service);
-                    lightControlBinder.registerLightControlServiceCallback(lightCallback);
-
-                    // Discover All Light Control Devices
-                    discoverAllLightControlDevices();
-
-                } catch (RemoteException 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.
-                }
+                // Discover All Light Control Devices
+                discoverAllLightControlDevices();
 
 
-                msg( "Light Control Remote Service Connected");
-                printLine();
+            } catch (RemoteException 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.
             }
             }
+
+            msg( "Light Control Remote Service Connected");
+            printLine();
         }
         }
 
 
         @Override
         @Override
@@ -417,7 +414,7 @@ public class BridgeService extends IntentService {
     /**
     /**
      * The ILightControlService is defined through IDL
      * The ILightControlService is defined through IDL
      */
      */
-    private final ILightControlServiceImpl lightBinder = new ILightControlServiceImpl() ;
+    /*private final ILightControlServiceImpl lightBinder = new ILightControlServiceImpl() ;
     class ILightControlServiceImpl extends ILightControlService.Stub {
     class ILightControlServiceImpl extends ILightControlService.Stub {
 
 
         final RemoteCallbackList<ILightControlServiceCallback> callbacks
         final RemoteCallbackList<ILightControlServiceCallback> callbacks
@@ -531,7 +528,7 @@ public class BridgeService extends IntentService {
             }
             }
             callbacks.finishBroadcast();
             callbacks.finishBroadcast();
         }
         }
-    };
+    };*/
 
 
     public String doGetLightControlResource(long device_index, String device_id, String resource_uri) {
     public String doGetLightControlResource(long device_index, String device_id, String resource_uri) {
         try {
         try {
@@ -539,7 +536,8 @@ public class BridgeService extends IntentService {
             String device_sn = OCUuidUtil.uuidToString(piid); // same with vod_id(Virtual Device Id) of VirtualOCFDevice
             String device_sn = OCUuidUtil.uuidToString(piid); // same with vod_id(Virtual Device Id) of VirtualOCFDevice
 
 
             // doGetProperties with Light Control Service
             // doGetProperties with Light Control Service
-            String properties = lightBinder.doGetResourceProperties(device_sn, resource_uri);
+            if(lightControlBinder==null) return null ;
+            String properties = lightControlBinder.doGetResourceProperties(device_sn, resource_uri);
             return properties ;
             return properties ;
         } catch (RemoteException e) {
         } catch (RemoteException e) {
             Log.e(TAG, "Light Control Service Remote Error : ", e);
             Log.e(TAG, "Light Control Service Remote Error : ", e);
@@ -549,8 +547,10 @@ public class BridgeService extends IntentService {
 
 
     public void discoverAllLightControlDevices() {
     public void discoverAllLightControlDevices() {
 
 
-        Log.d(TAG, "SecheduledTaskInit : doRetrieveLightControlAllDevices() called.");
         try {
         try {
+            if(lightControlBinder==null) return ;
+
+            Log.d(TAG, "ScheduledTaskInit : doRetrieveLightControlAllDevices() called.");
             String devices = lightControlBinder.retrieveAllDevices();
             String devices = lightControlBinder.retrieveAllDevices();
             Log.d(TAG, "Light Control retrieveAllDevices() : "+ devices);
             Log.d(TAG, "Light Control retrieveAllDevices() : "+ devices);
             if(devices != null) {
             if(devices != null) {
@@ -646,7 +646,7 @@ public class BridgeService extends IntentService {
                     }
                     }
                 }
                 }
             }
             }
-        } catch (RemoteException | JSONException ex) {
+        } catch (Exception 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
@@ -657,8 +657,9 @@ public class BridgeService extends IntentService {
 
 
     public void doGetLightControlResources() {
     public void doGetLightControlResources() {
 
 
-        Log.d(TAG, "SecheduledTaskInit : doGetLightControlResources() called.");
-        if(bridgeServer != null && lightBinder != null) {
+        if(bridgeServer != null && lightControlBinder != null) {
+            Log.d(TAG, "ScheduledTaskInit : doGetLightControlResources() called.");
+
             ArrayList<VirtualOCFDevice> vods = bridgeServer.getVirtualOCFDeviceList();
             ArrayList<VirtualOCFDevice> vods = bridgeServer.getVirtualOCFDeviceList();
             for (VirtualOCFDevice vod : vods) {
             for (VirtualOCFDevice vod : vods) {
                 if (vod.getEcoName().equals(LightControlServiceConnection.ECO_NAME) && vod.isDiscovered()
                 if (vod.getEcoName().equals(LightControlServiceConnection.ECO_NAME) && vod.isDiscovered()
@@ -668,7 +669,7 @@ public class BridgeService extends IntentService {
                         try {
                         try {
                             // doGetProperties with Light Control Service
                             // doGetProperties with Light Control Service
                             String prev_props = bridgeServer.getVirtualOCFDeviceProperties(device_sn, resource.getResourceUri());
                             String prev_props = bridgeServer.getVirtualOCFDeviceProperties(device_sn, resource.getResourceUri());
-                            String properies = lightBinder.doGetResourceProperties(device_sn, resource.getResourceUri());
+                            String properies = lightControlBinder.doGetResourceProperties(device_sn, resource.getResourceUri());
                             bridgeServer.addOrUpdateVirtualOCFDeviceProperties(device_sn, resource.getResourceUri(), properies);
                             bridgeServer.addOrUpdateVirtualOCFDeviceProperties(device_sn, resource.getResourceUri(), properies);
 
 
                             /*Log.d(TAG,"(1) prev_props : " +prev_props + ", cur_props : " +properies );
                             /*Log.d(TAG,"(1) prev_props : " +prev_props + ", cur_props : " +properies );
@@ -696,8 +697,10 @@ public class BridgeService extends IntentService {
             OCUuid piid = OCCoreRes.getDeviceInfo(device_index).getPiid();
             OCUuid piid = OCCoreRes.getDeviceInfo(device_index).getPiid();
             String device_sn = OCUuidUtil.uuidToString(piid); // same with vod_id(Virtual Device Id) of VirtualOCFDevice
             String device_sn = OCUuidUtil.uuidToString(piid); // same with vod_id(Virtual Device Id) of VirtualOCFDevice
 
 
-            Log.d(TAG, "doPostLightControlResource() called.");
-            lightBinder.doPostResourceProperties(device_sn, resource_uri, prop_data);
+            if(lightControlBinder!=null) {
+                Log.d(TAG, "doPostLightControlResource() called.");
+                lightControlBinder.doPostResourceProperties(device_sn, resource_uri, prop_data);
+            }
 
 
         } catch (RemoteException e) {
         } catch (RemoteException e) {
             Log.e(TAG, "Light Control Service Remote Error : ", e);
             Log.e(TAG, "Light Control Service Remote Error : ", e);
@@ -733,7 +736,7 @@ public class BridgeService extends IntentService {
                     rBinder.broadcastResultToClients(msg.arg1, (String)msg.obj);
                     rBinder.broadcastResultToClients(msg.arg1, (String)msg.obj);
                 } break ;
                 } break ;
                 case MSG_LIGHT_RESULT: {
                 case MSG_LIGHT_RESULT: {
-                    lightBinder.broadcastResultToClients(msg.arg1, (String)msg.obj);
+                    //lightBinder.broadcastResultToClients(msg.arg1, (String)msg.obj);
                 } break ;
                 } break ;
                 default:
                 default:
                     super.handleMessage(msg);
                     super.handleMessage(msg);
@@ -883,21 +886,22 @@ public class BridgeService extends IntentService {
          * executorService.schedule(Classname::someTask, delayInSeconds, TimeUnit.SECONDS);                 // once after a delay
          * executorService.schedule(Classname::someTask, delayInSeconds, TimeUnit.SECONDS);                 // once after a delay
          * executorService.scheduleAtFixedRate(Classname::someTask, 0, delayInSeconds, TimeUnit.SECONDS);   // at fixed time interval
          * executorService.scheduleAtFixedRate(Classname::someTask, 0, delayInSeconds, TimeUnit.SECONDS);   // at fixed time interval
          */
          */
-        if(executorLightControl != null && executorLightControl.isShutdown() == false ) { executorLightControl.shutdown();}
+        if(executorLightControl != null && executorLightControl.isShutdown() == false ) {
+            executorLightControl.shutdown();
+        }
         executorLightControl = Executors.newSingleThreadScheduledExecutor();
         executorLightControl = Executors.newSingleThreadScheduledExecutor();
         executorLightControl.scheduleAtFixedRate(new Runnable() {
         executorLightControl.scheduleAtFixedRate(new Runnable() {
             @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 ScheduledTask per 180sec was called.");
+                Log.d(TAG, "LightControl ScheduledTask per 30sec was called.");
                 if(lightControlBinder==null) {
                 if(lightControlBinder==null) {
                     bindLightControlService();
                     bindLightControlService();
                 }
                 }
                 discoverAllLightControlDevices();
                 discoverAllLightControlDevices();
                 doGetLightControlResources();
                 doGetLightControlResources();
             }
             }
-        }, 30, 180, TimeUnit.SECONDS); // 180sec interval after 30sec delay
-
+        }, 30, 30, TimeUnit.SECONDS); // 180sec interval after 30sec delay
     }
     }
 
 
     public void stopScheduledTaskForLightControl() {
     public void stopScheduledTaskForLightControl() {