소스 검색

Notifying changed resource to Observers for Bridged Client was added.

Trishia 4 년 전
부모
커밋
2a4a07be24

+ 4 - 1
app/src/main/java/org/iotivity/bridge/homeserver/BridgeServer.java

@@ -336,6 +336,8 @@ public class BridgeServer {
         OCMain.resourceSetRequestHandler(resource, OCMethod.OC_POST, new PostRequestHandler(service, device));
         OCMain.resourceSetRequestHandler(resource, OCMethod.OC_PUT, new PutRequestHandler(service, device));
         OCMain.addResource(resource);
+
+        device.addOCResource(resource);
     }
 
     public void addLightVirtualOCFServer(String virtual_di, String devicename, String econame) {
@@ -379,6 +381,7 @@ public class BridgeServer {
                 OCMain.resourceSetRequestHandler(res_light, OCMethod.OC_POST, new PostLightRequestHandler(service, light));
                 OCMain.resourceSetRequestHandler(res_light, OCMethod.OC_PUT, new PutLightRequestHandler(service, light));
                 OCMain.addResource(res_light);
+                vod.addOCResource(res_light);
 
                 // Add Dimming Resource
                 OCResource res_dimming = OCMain.newResource(devicename, "/dimming", (short) 1, (long) vod_index);
@@ -393,7 +396,7 @@ public class BridgeServer {
                 OCMain.resourceSetRequestHandler(res_dimming, OCMethod.OC_POST, new PostLightRequestHandler(service, light));
                 OCMain.resourceSetRequestHandler(res_dimming, OCMethod.OC_PUT, new PutLightRequestHandler(service, light));
                 OCMain.addResource(res_dimming);
-
+                vod.addOCResource(res_dimming);
 
                 // set immutable device id for Platform Independent Identifier (piid)
                 OCMain.setImmutableDeviceIdentifier(vod_index, OCUuidUtil.stringToUuid(virtual_di));

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

@@ -393,16 +393,20 @@ public class BridgeService extends IntentService {
                     String tid = obj.getString("tid");              // Transaction Id
                     String sn = obj.getString("sn");                // Serial Number (Virtual Device Id)
                     String uri = obj.getString("uri");              // Resource URI
-                    String res_type = obj.getString("res_type");    // GET, POST, PUT
+                    String res_type = obj.getString("res_type");    // GET, POST, PUT, OBSERVE
                     String res_data = obj.getString("res_data");    // Properties
 
                     // Update Properties of the resource from result
                     bridgeServer.addOrUpdateVirtualOCFDeviceProperties(sn, uri, res_data);
 
                     // TODO : need to check // notify to Observers for changed properties's value
-                    /*VirtualOCFDevice vod = bridgeServer.getVirtualOCFDevice(sn);
-                    OCResource ocResource = OCCoreRes.getResourceByUri(uri, vod.getVODIndex());
-                    OCMain.notifyObservers(ocResource);*/
+                    VirtualOCFDevice vod = bridgeServer.getVirtualOCFDevice(sn);
+                    if(vod != null) {
+                        OCResource ocResource = vod.getOCResource(uri);
+                        if(ocResource != null) {
+                            OCMain.notifyObservers(ocResource);
+                        }
+                    }
                 }
             } catch (JSONException e) {
                 Log.e(TAG, "Light Control Service Remote Error : ", e);
@@ -667,15 +671,11 @@ public class BridgeService extends IntentService {
                             bridgeServer.addOrUpdateVirtualOCFDeviceProperties(device_sn, resource.getResourceUri(), properies);
 
                             // TODO : need to check // notify to Observers for changed properties's value
-                            /*Log.d(TAG, "OCCoreRes.getResourceByUri() before : " + vod.getDeviceName() +
-                                    ", " +resource.getResourceUri()+", "+ vod.getVODIndex());
-                            //OCDeviceInfo info = OCCoreRes.getDeviceInfo(vod.getVODIndex());
-                            OCResource ocResource = OCCoreRes.getResourceByUri(resource.getResourceUri(), vod.getVODIndex());
-                            if(ocResource != null && ocResource.getNumObservers() > 0) {
-                                Log.d(TAG, "OCCoreRes.getResourceByUri() after : " + ocResource.getName() +
-                                        ", " +ocResource.getUri() +", "+ ocResource.getNumObservers());
+                            OCResource ocResource = vod.getOCResource(resource.getResourceUri());
+                            if(ocResource != null) {
                                 OCMain.notifyObservers(ocResource);
-                            }*/
+                            }
+
                         } catch (RemoteException e) {
                             Log.e(TAG, "Light Control Service Remote Error : ", e);
                         }

+ 33 - 0
app/src/main/java/org/iotivity/bridge/homeserver/model/VirtualOCFDevice.java

@@ -1,5 +1,6 @@
 package org.iotivity.bridge.homeserver.model;
 
+import org.iotivity.OCResource;
 import org.iotivity.oc.OcCborException;
 import org.iotivity.oc.OcRepresentation;
 
@@ -19,6 +20,8 @@ public class VirtualOCFDevice extends OCFDevice {
     private boolean discovered ;
     private boolean added_to_bridge ;
 
+    private ArrayList<OCResource> ocResources = new ArrayList<OCResource>() ;
+
     public VirtualOCFDevice(int vod_index, String name, String econame, String virtual_di, String type) {
         super(name, type);
 
@@ -74,4 +77,34 @@ public class VirtualOCFDevice extends OCFDevice {
 
         return sb.toString();
     }
+
+    public ArrayList<OCResource> getOCResources() {
+        return ocResources;
+    }
+
+    public OCResource getOCResource(String resource_uri) {
+        for(OCResource resource : ocResources) {
+            if (resource.getUri().equalsIgnoreCase(resource_uri)) {
+                return resource ;
+            }
+        }
+        return null ;
+    }
+
+    public void addOCResource(OCResource ocResource) {
+        ocResources.add(ocResource);
+    }
+
+    public void removeOCResource(int index) {
+
+        ocResources.remove(index);
+    }
+
+    public void removeOCResource(String uri) {
+        ArrayList<OCResource> resources1 = (ArrayList<OCResource>)ocResources.clone();
+        for(int loop = 0 ; loop < resources1.size() ; loop++) {
+            OCResource resource1 = resources1.get(loop);
+            if(resource1.getUri().equals(uri)) ocResources.remove(loop) ;
+        }
+    }
 }