Преглед на файлове

IBridgeService Interface is under developing and testing on BridgeServerActivity.

Trishia преди 4 години
родител
ревизия
3cfce5a165

+ 8 - 6
app/src/main/aidl/org/iotivity/bridge/homeserver/IBridgeService.aidl

@@ -2,11 +2,9 @@ package org.iotivity.bridge.homeserver;
 
 import org.iotivity.bridge.homeserver.IBridgeServiceCallback;
 import org.iotivity.bridge.homeserver.IBridgeActivityCallback;
-/**
- * Example of defining an interface for calling on to a remote service
- * (running in another process).
- */
+
 interface IBridgeService {
+
     /**
      * Often you want to allow a service to call back to its clients.
      * This shows how to do so, by registering a callback interface with
@@ -38,14 +36,18 @@ interface IBridgeService {
 
     /**
      * retrieve all on-line device list in BridgeService
-     * @return devices list data with json format as String
+     * @return devices list data with json array format as String
+     * [{"name":"Light Switch","di":"15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","piid":"2b32e152-3756-4fb6-b3f2-d8db7aafe39f","icv":"ocf.2.0.0","dmv":"ocf.res.1.3.0,ocf.sh.1.3.0"},
+     * {"name":"Television","di":"4e7bf95f-1658-4a43-7f78-44522db5255f","piid":"2cd8e585-ef21-4fd1-7958-6ca7e54adcce","icv":"ocf.2.0.0","dmv":"ocf.res.1.3.0, ocf.sh.1.3.0"}]
      */
     String retrieveAllDevices();
 
     /**
      * retrieve all resources of the deivce in BridgeService
      * @param device_uuid : unique identity of ocf device
-     * @return resource list data with json format as String
+     * @return resource list data with json array format as String
+     * [{"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri”:”/light","rt":"[oic.r.switch.binary]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE"},
+     * {"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri":"/dimming","rt":"[oic.r.light.dimming]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE"}]
      */
     String retrieveAllResources(in String device_uuid) ;
 

+ 9 - 0
app/src/main/aidl/org/iotivity/bridge/homeserver/IBridgeServiceCallback.aidl

@@ -8,6 +8,15 @@ package org.iotivity.bridge.homeserver;
 
 interface IBridgeServiceCallback {
 
+    /*
+    * This is notifying async result for requests such as GET, OBSERVE, POST/PUT
+    * @param result_code 0-OK, 1-FAIL, 2-ERROR
+    * @param result_data json format as string (eg.
+    * {"id":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a/light",
+    * "res_type":"OBSERVE",
+    * "res_data":{"n":"Light Switch","value":false}
+    })
+    */
     void onResultReceived(int result_code, String result_data);
 
 }

+ 5 - 1
app/src/main/java/org/iotivity/bridge/homeserver/AlarmReceiver.java

@@ -4,12 +4,16 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
+import android.util.Log;
 
-public class AlarmReceiver  extends BroadcastReceiver {
+public class AlarmReceiver extends BroadcastReceiver {
+
+    final static String TAG = AlarmReceiver.class.getName();
 
     @Override
     public void onReceive(Context context, Intent intent) {
 
+        Log.w(TAG, "AlarmReceiver onReceive() called");
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             Intent in = new Intent(context, BridgeService.class);
             context.startForegroundService(in);

+ 2 - 1
app/src/main/java/org/iotivity/bridge/homeserver/BridgeServerActivity.java

@@ -283,7 +283,8 @@ public class BridgeServerActivity extends AppCompatActivity {
 
         @Override
         public void onResultReceived(int result_code, String result_data) throws RemoteException {
-            // TODO : received some notification from BridgeService
+            msg("result_code="+result_code + ", result_data="+result_data );
+            printLine();
         }
     };
 

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

@@ -1,5 +1,6 @@
 package org.iotivity.bridge.homeserver;
 
+import android.annotation.SuppressLint;
 import android.app.AlarmManager;
 import android.app.IntentService;
 import android.app.Notification;
@@ -57,12 +58,27 @@ public class BridgeService extends IntentService {
 
     private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
 
-    // IRemoteService MSG TYPEs
-    public static final int MSG_REPORT = 1 ;
+    // Message Handler MSG TYPEs
+    public static final int MSG_RESULT = 1 ;
     public static final int MSG_STRING = 2;
     public static final int MSG_LINE = 3;
     public static final int MSG_DEVICE_DISCOVERED = 4 ;
 
+    // IBridgeService RESULT TYPEs
+    public enum ResultType {
+        OK,
+        FAIL,
+        ERROR
+    }
+
+    // IBridgeService RESULT RESPONSE TYPEs
+    public enum ResponseType{
+        GET,
+        OBSERVE,
+        OBSERVE_STOP,
+        POST,
+        PUT
+    }
 
     public BridgeService() {
         super(BridgeService.class.getName());
@@ -176,22 +192,22 @@ public class BridgeService extends IntentService {
 
         @Override
         public boolean requestObserveResource(String device_uuid, String resource_uri) throws RemoteException {
-            return false;
+            return doObserveResource(device_uuid, resource_uri) ;
         }
 
         @Override
         public boolean requestStopObserveResource(String device_uuid, String resource_uri) throws RemoteException {
-            return false;
+            return stopObserveResource(device_uuid, resource_uri);
         }
 
         @Override
         public boolean requestObserveDeviceSpecificResources(String device_uuid) throws RemoteException {
-            return false;
+            return doObserveDeviceSpecificResources(device_uuid);
         }
 
         @Override
         public boolean requestStopObserveDeviceAllResources(String device_uuid) throws RemoteException {
-            return false;
+            return stopObserveDeviceSpecificResources(device_uuid);
         }
 
         public void killCallbacks() {
@@ -202,6 +218,7 @@ public class BridgeService extends IntentService {
         public void broadcastResultToClients(int result_code, String result_data) {
             // Broadcast to all clients the new value.
             final int count = sCallbacks.beginBroadcast();
+            Log.i(TAG, "ServiceClientCallback count=" + count);
             for (int index=0; index<count; index++) {
                 try {
                     sCallbacks.getBroadcastItem(index).onResultReceived(result_code, result_data);
@@ -266,7 +283,12 @@ public class BridgeService extends IntentService {
         mHandler.sendEmptyMessage(MSG_LINE);
     }
 
+    public void broadcastResult(int result_code, String result_data) {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_RESULT,result_code, 0, result_data));
+    }
+
     private final Handler mHandler = new Handler() {
+        @SuppressLint("HandlerLeak")
         @Override public void handleMessage(Message msg) {
             switch (msg.what) {
                 case MSG_LINE: {
@@ -278,6 +300,9 @@ public class BridgeService extends IntentService {
                 case MSG_DEVICE_DISCOVERED: {
                     rBinder.broadcastDeviceDiscoveredToClients();
                 } break ;
+                case MSG_RESULT: {
+                    rBinder.broadcastResultToClients(msg.arg1, (String)msg.obj);
+                } break ;
                 default:
                     super.handleMessage(msg);
             }
@@ -332,7 +357,7 @@ public class BridgeService extends IntentService {
 
         Log.d(TAG, "onTaskRemoved()");
 
-        this.restartServiceByAlarmManager();
+        //this.restartServiceByAlarmManager();
     }
 
     @Override
@@ -410,7 +435,7 @@ public class BridgeService extends IntentService {
 
 
             }
-        }, 10, 60, TimeUnit.SECONDS); // 60sec interval after 10sec delay
+        }, 10, 90, TimeUnit.SECONDS); //90 sec interval after 10sec delay
 
         /*executorService.scheduleAtFixedRate(new Runnable() {
             @Override
@@ -419,7 +444,7 @@ public class BridgeService extends IntentService {
                 bridgeServer.loadPairwisedDeviceUUIDs();
             }
         }, 30, 60, TimeUnit.SECONDS); // 30sec interval after 30sec delay
-*/
+        */
         /*executorService.schedule(new Runnable() {
             @Override
             public void run() {
@@ -448,7 +473,7 @@ public class BridgeService extends IntentService {
         return false ;
     }
 
-    public OcRemoteDevice findPairwisedRemoteDevice (String device_uuid) {
+    public OcRemoteDevice findPairwisedRemoteDevice(String device_uuid) {
         if(bridgeServer!=null) {
             return bridgeServer.getPairwisedDevice(device_uuid);
         }
@@ -579,7 +604,7 @@ public class BridgeService extends IntentService {
         return false ;
     }
 
-    public boolean registerObserverOnDeviceSpecificResources(String device_uuid) {
+    public boolean doObserveDeviceSpecificResources(String device_uuid) {
 
         OcRemoteDevice remoteDevice = findPairwisedRemoteDevice(device_uuid);
         if (remoteDevice == null) return false ;
@@ -600,7 +625,7 @@ public class BridgeService extends IntentService {
         else return false ;
     }
 
-    public boolean registerObserver(String device_uuid, String resource_uri) {
+    public boolean doObserveResource(String device_uuid, String resource_uri) {
 
         OcRemoteDevice remoteDevice = findPairwisedRemoteDevice(device_uuid);
         if (remoteDevice == null) return false ;
@@ -615,7 +640,7 @@ public class BridgeService extends IntentService {
         return false ;
     }
 
-    public boolean unregisterObserverOnDeviceSpecificResources(String device_uuid) {
+    public boolean stopObserveDeviceSpecificResources(String device_uuid) {
 
         OcRemoteDevice remoteDevice = findPairwisedRemoteDevice(device_uuid);
         if (remoteDevice == null) return false ;
@@ -632,7 +657,7 @@ public class BridgeService extends IntentService {
         else return false ;
     }
 
-    public boolean unregisterObserver(String device_uuid, String resource_uri) {
+    public boolean stopObserveResource(String device_uuid, String resource_uri) {
 
         OcRemoteDevice remoteDevice = findPairwisedRemoteDevice(device_uuid);
         if (remoteDevice == null) return false ;

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

@@ -35,7 +35,9 @@ public class DeviceDiscoveryHandler implements OcDeviceDiscoveryHandler {
             /* check if Bridge has the discovered device's pairwised credential */
             if(service.hasPairwisedDeviceUUIDs(remoteDevice.getDeviceId())) {
                 service.addPairwisedDevice(remoteDevice);
-
+                Log.d(TAG, "getAllResources :" + service.getAllResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                Log.d(TAG, "ObserveAll :" + service.doObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                Log.d(TAG, "StopObserveAll :" + service.stopObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
             }
 
             /* check if the device is accessible via coaps get /oic/d or not

+ 25 - 4
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/ObserveResponseHandler.java

@@ -1,12 +1,18 @@
 package org.iotivity.bridge.homeserver.handler.client;
 
+import android.util.Log;
 import android.util.Pair;
 
 import org.iotivity.OCClientResponse;
+import org.iotivity.OCRep;
+import org.iotivity.OCRepresentation;
 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.json.JSONException;
+import org.json.JSONObject;
 
 import java.util.ArrayList;
 
@@ -25,13 +31,28 @@ public class ObserveResponseHandler implements OCResponseHandler {
     @Override
     public void handler(OCClientResponse response) {
 
-        service.msg("OBSERVER "+resource.getUri()+" Response Handler: Code = "+response.getCode().toString());
+        Log.d(TAG, "OBSERVE "+resource.getUri()+" Response Handler: Code = "+response.getCode().toString());
         if(response.getCode() != OCStatus.OC_STATUS_OK) {
-
-            service.printLine();
             return;
         }
 
-        final ArrayList<Pair<String, Object>> props = ResponseParsingHelper.parseClientResponse(response, service);
+        OCRepresentation rep = response.getPayload();
+        String json = OCRep.toJSON(rep, false); // boolean : prettyPrint or not
+        Log.d(TAG, "OBSERVE "+resource.getUri()+" Response : " + json);
+
+        try {
+            JSONObject props = new JSONObject(json);
+            JSONObject result = new JSONObject();
+            result.put("id", resource.getAnchor() + resource.getUri());
+            result.put("di", Utils.getDIFromAnchor(resource.getAnchor()));
+            result.put("uri", resource.getUri());
+            result.put("res_type", BridgeService.ResponseType.OBSERVE.name());
+            result.put("res_data", props);
+
+            service.broadcastResult(BridgeService.ResultType.OK.ordinal(), result.toString());
+
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
     }
 }

+ 17 - 0
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/ResponseParsingHelper.java

@@ -8,6 +8,9 @@ import org.iotivity.OCRepresentation;
 import org.iotivity.bridge.homeserver.BridgeServerActivity;
 import org.iotivity.bridge.homeserver.BridgeService;
 import org.iotivity.bridge.homeserver.utils.Utils;
+import org.iotivity.oc.OcRepresentation;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -168,4 +171,18 @@ public class ResponseParsingHelper {
 
         return props ;
     }
+
+    static public String parseClientResponse(OCClientResponse response, BridgeService service, String data) {
+
+        try {
+            OCRepresentation rep = response.getPayload();
+            OCRep.toJSON(rep, true);
+
+
+        }catch (Exception ex) {
+
+        }
+
+        return null ;
+    }
 }

+ 8 - 0
app/src/main/java/org/iotivity/bridge/homeserver/utils/Utils.java

@@ -355,4 +355,12 @@ public class Utils {
         sb.append("]");
         return sb.toString();
     }
+
+    public static String getDIFromAnchor(String anchor){
+
+        if(anchor.contains("ocf:")) {
+            return anchor.substring(anchor.lastIndexOf("/")+1);
+        }
+        return null;
+    }
 }