소스 검색

Post/Put Interface APIs and ResponseHandler were tested.

Trishia 4 년 전
부모
커밋
07fbe727ad

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

@@ -30,6 +30,7 @@ import org.iotivity.bridge.homeserver.handler.client.DeviceDiscoveryHandler;
 import org.iotivity.bridge.homeserver.handler.client.GetResponseHandler;
 import org.iotivity.bridge.homeserver.handler.client.ObserveResponseHandler;
 import org.iotivity.bridge.homeserver.handler.client.PostResponseHandler;
+import org.iotivity.bridge.homeserver.handler.client.PutResponseHandler;
 import org.iotivity.bridge.homeserver.model.Bridge;
 import org.iotivity.bridge.homeserver.utils.Utils;
 import org.iotivity.oc.OcCborEncoder;
@@ -565,8 +566,9 @@ public class BridgeService extends IntentService {
             OcRemoteResource resource = bridgeServer.getPairwisedResource(device_uuid, resource_uri);
             if(resource==null) return false ;
 
-            PostResponseHandler postLight = new PostResponseHandler(getService(), resource);
-            if (OcUtils.initPost(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, postLight, OCQos.LOW_QOS)) {
+            Log.d(TAG,"Post Request called.");
+            PostResponseHandler post = new PostResponseHandler(getService(), resource);
+            if (OcUtils.initPost(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, post, OCQos.LOW_QOS)) {
 
                 try {
                     JSONObject json = new JSONObject(prop_data);
@@ -577,20 +579,15 @@ public class BridgeService extends IntentService {
                         Object value = json.get(key);
 
                         if (value instanceof Boolean) {
-                            Log.d(TAG, "type = " + Boolean.class.toString());
-                            root.setBoolean(key, (Boolean) value);
+                            root.setBoolean(key, (Boolean)value);
                         } else if (value instanceof Integer) {
-                            Log.d(TAG, "type = " + Integer.class.toString());
-                            root.setLong(key, (Integer) value);
+                            root.setLong(key, (Integer)value);
                         } else if (value instanceof Long) {
-                            Log.d(TAG, "type = " + Long.class.toString());
-                            root.setLong(key, (Long) value);
+                            root.setLong(key, (Long)value);
                         } else if (value instanceof Double) {
-                            Log.d(TAG, "type = " + Double.class.toString());
-                            root.setDouble(key, (Double) value);
+                            root.setDouble(key, (Double)value);
                         } else if (value instanceof String) {
-                            Log.d(TAG, "type = " + String.class.toString());
-                            root.setTextString(key, (String) value);
+                            root.setTextString(key, (String)value);
                         }
                     }
                     root.done();
@@ -599,20 +596,67 @@ public class BridgeService extends IntentService {
                 }
 
                 if (OcUtils.doPost()) {
-                    msg("\tSent POST request");
-                    printLine();
+                    Log.d(TAG, "\tSent POST request");
                     return true ;
                 } else {
-                    msg("\tCould not send POST request");
-                    printLine();
+                    Log.d(TAG, "\tCould not send POST request");
                     return false ;
                 }
             } else {
-                msg("\tCould not init POST request");
-                printLine();
+                Log.d(TAG, "\tCould not init POST request");
                 return false ;
             }
+        }
+        return false ;
+    }
+
+    public boolean doPutDeviceResource(String device_uuid, String resource_uri, String prop_data)  {
+
+        if(bridgeServer!=null) {
+
+            OcRemoteResource resource = bridgeServer.getPairwisedResource(device_uuid, resource_uri);
+            if(resource==null) return false ;
+
+            Log.d(TAG,"Put Request called.");
+            PutResponseHandler put = new PutResponseHandler(getService(), resource);
+            if (OcUtils.initPut(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, put, OCQos.LOW_QOS)) {
 
+                try {
+                    JSONObject json = new JSONObject(prop_data);
+                    Iterator<String> it = json.keys();
+                    OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
+                    while (it.hasNext()) {
+                        String key = it.next();
+                        Object value = json.get(key);
+
+                        if (value instanceof Boolean) {
+                            root.setBoolean(key, (Boolean)value);
+                        } else if (value instanceof Integer) {
+                            root.setLong(key, (Integer)value);
+                        } else if (value instanceof Long) {
+                            root.setLong(key, (Long)value);
+                        } else if (value instanceof Double) {
+                            root.setDouble(key, (Double)value);
+                        } else if (value instanceof String) {
+                            root.setTextString(key, (String)value);
+                        }
+                    }
+                    root.done();
+                } catch (JSONException ex) {
+                    Log.d(TAG, "Json Parsing Error : ", ex);
+                }
+
+                if (OcUtils.doPut()) {
+                    Log.d(TAG, "\tSent PUT request");
+                    return true ;
+                } else {
+                    Log.d(TAG, "\tCould not send PUT request");
+                    return false ;
+                }
+            } else {
+                Log.d(TAG, "\tCould not init PUT request");
+                return false ;
+            }
         }
         return false ;
     }
@@ -622,10 +666,8 @@ public class BridgeService extends IntentService {
         OcRemoteDevice remoteDevice = findPairwisedRemoteDevice(device_uuid);
         if (remoteDevice == null) return false ;
 
-        /* TODO : Observe only device specfic resources until shutdown or until stop Observe.
-         * This may cause too many observing to the device
-         * To avoid this issue, I think Resource Observing list should be managed in BridgeService
-         * or stop Observing before Observing */
+        /* Observe only device specfic resources until shutdown or until stop Observe.
+         */
         int count = 0 ;
         for(OcRemoteResource resource : remoteDevice.getResources()) {
             if(Utils.isDeviceSpecificResource(resource.getTypes())) {

+ 3 - 3
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/DeviceDiscoveryHandler.java

@@ -36,10 +36,10 @@ public class DeviceDiscoveryHandler implements OcDeviceDiscoveryHandler {
             if(service.hasPairwisedDeviceUUIDs(remoteDevice.getDeviceId())) {
                 service.addPairwisedDevice(remoteDevice);
                 Log.d(TAG, "getAllResources : " + service.getDeviceResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
-                //Log.d(TAG, "getDeviceSpecificResources: " + service.doGetDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
 
-                Log.d(TAG, "StopObserveAll : " + service.stopObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
-                Log.d(TAG, "ObserveAll : " + service.doObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                //Log.d(TAG, "ObserveAll : " + service.doObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                //Log.d(TAG, "StopObserveAll : " + service.stopObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                Log.d(TAG, "getDeviceSpecificResources: " + service.doGetDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
             }
 
             /* check if the device is accessible via coaps get /oic/d or not

+ 6 - 0
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/GetResponseHandler.java

@@ -55,5 +55,11 @@ public class GetResponseHandler implements OCResponseHandler {
         } catch (JSONException ex) {
             Log.e(TAG, "JSON Error : ", ex);
         }
+
+        // for test
+        //service.doPostDeviceResource(Utils.getDIFromAnchor(resource.getAnchor()),
+        //        resource.getUri(), json);
+        //service.doPutDeviceResource(Utils.getDIFromAnchor(resource.getAnchor()),
+        //        resource.getUri(), json);
     }
 }

+ 1 - 1
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/Post2LightResponseHandler.java

@@ -47,7 +47,7 @@ public class Post2LightResponseHandler implements OCResponseHandler {
         activity.printLine();
 
         // Stop trigger register. It will be stop Observe after 30secs
-        StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(activity, resource);
+        StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(resource);
         OcUtils.setDelayedHandler(stopObserve, 10);
         activity.msg("Stop OBSERVE Trigger register");
         activity.printLine();

+ 13 - 0
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/PostResponseHandler.java

@@ -3,6 +3,7 @@ package org.iotivity.bridge.homeserver.handler.client;
 import android.util.Log;
 
 import org.iotivity.OCClientResponse;
+import org.iotivity.OCQos;
 import org.iotivity.OCRep;
 import org.iotivity.OCRepresentation;
 import org.iotivity.OCResponseHandler;
@@ -12,6 +13,7 @@ import org.iotivity.bridge.homeserver.BridgeService;
 import org.iotivity.bridge.homeserver.model.Light;
 import org.iotivity.bridge.homeserver.utils.Utils;
 import org.iotivity.oc.OcRemoteResource;
+import org.iotivity.oc.OcUtils;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -49,5 +51,16 @@ public class PostResponseHandler implements OCResponseHandler {
         } catch (JSONException ex) {
             Log.e(TAG, "JSON Error : ", ex);
         }
+
+        // Observe the response of Post Request
+        ObserveResponseHandler observerLight = new ObserveResponseHandler(service, resource);
+        OcUtils.doObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, observerLight, OCQos.LOW_QOS);
+
+        // Stop Observe right after Observe
+        // OcUtils.stopObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()));
+
+        // Stop trigger register. It will be stop Observe after some secs
+        //StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(resource);
+        //OcUtils.setDelayedHandler(stopObserve, 10);
     }
 }

+ 13 - 0
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/PutResponseHandler.java

@@ -3,11 +3,13 @@ package org.iotivity.bridge.homeserver.handler.client;
 import android.util.Log;
 
 import org.iotivity.OCClientResponse;
+import org.iotivity.OCQos;
 import org.iotivity.OCResponseHandler;
 import org.iotivity.OCStatus;
 import org.iotivity.bridge.homeserver.BridgeService;
 import org.iotivity.bridge.homeserver.utils.Utils;
 import org.iotivity.oc.OcRemoteResource;
+import org.iotivity.oc.OcUtils;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -45,5 +47,16 @@ public class PutResponseHandler implements OCResponseHandler {
         } catch (JSONException ex) {
             Log.e(TAG, "JSON Error : ", ex);
         }
+
+        // Observe the response of Post Request
+        ObserveResponseHandler observerLight = new ObserveResponseHandler(service, resource);
+        OcUtils.doObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, observerLight, OCQos.LOW_QOS);
+
+        // Stop Observe right after Observe
+        OcUtils.stopObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()));
+
+        // Stop trigger register. It will be stop Observe after some secs
+        //StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(resource);
+        //OcUtils.setDelayedHandler(stopObserve, 10);
     }
 }

+ 7 - 5
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/StopObserveTriggerHandler.java

@@ -1,8 +1,11 @@
 package org.iotivity.bridge.homeserver.handler.client;
 
+import android.util.Log;
+
 import org.iotivity.OCEventCallbackResult;
 import org.iotivity.OCTriggerHandler;
 import org.iotivity.bridge.homeserver.BridgeServerActivity;
+import org.iotivity.bridge.homeserver.BridgeService;
 import org.iotivity.bridge.homeserver.model.Light;
 import org.iotivity.bridge.homeserver.utils.Utils;
 import org.iotivity.oc.OcRemoteResource;
@@ -12,19 +15,18 @@ public class StopObserveTriggerHandler implements OCTriggerHandler {
 
     private static final String TAG = StopObserveTriggerHandler.class.getSimpleName();
 
-    private BridgeServerActivity activity;
     private OcRemoteResource resource ;
 
-    public StopObserveTriggerHandler(BridgeServerActivity activity, OcRemoteResource resource) {
-        this.activity = activity;
+    public StopObserveTriggerHandler(OcRemoteResource resource) {
         this.resource = resource ;
     }
 
     @Override
     public OCEventCallbackResult handler() {
-        activity.msg("Stopping OBSERVE");
-        activity.printLine();
+
+        Log.d(TAG, "Stop Observe by Trigger");
         OcUtils.stopObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()));
+
         return OCEventCallbackResult.OC_EVENT_DONE;
     }
 }

+ 1 - 1
app/src/main/java/org/iotivity/bridge/homeserver/handler/server/PostLightRequestHandler.java

@@ -28,7 +28,7 @@ public class PostLightRequestHandler implements OCRequestHandler {
     public void handler(OCRequest request, int interfaces) {
         Log.d(TAG, "inside Post Light Request Handler");
 
-        service.msg("Post "+request.getResource().getUri()+" Request :");
+        service.msg("Post "+request.getResource().getUri()+" Request Received.");
 
         OcRepresentation rep = new OcRepresentation(request.getRequestPayload());
         while (rep != null) {