Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / DeviceResource.java
old mode 100644 (file)
new mode 100755 (executable)
index fc33258..6ce3c35
-/*\r
- * //******************************************************************\r
- * //\r
- * // Copyright 2015 Intel Corporation.\r
- * //\r
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
- * //\r
- * // Licensed under the Apache License, Version 2.0 (the "License");\r
- * // you may not use this file except in compliance with the License.\r
- * // You may obtain a copy of the License at\r
- * //\r
- * //      http://www.apache.org/licenses/LICENSE-2.0\r
- * //\r
- * // Unless required by applicable law or agreed to in writing, software\r
- * // distributed under the License is distributed on an "AS IS" BASIS,\r
- * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * // See the License for the specific language governing permissions and\r
- * // limitations under the License.\r
- * //\r
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
- */\r
-\r
-package org.iotivity.base.examples.fridgeserver;\r
-\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.util.Log;\r
-\r
-import org.iotivity.base.EntityHandlerResult;\r
-import org.iotivity.base.OcException;\r
-import org.iotivity.base.OcHeaderOption;\r
-import org.iotivity.base.OcPlatform;\r
-import org.iotivity.base.OcResourceRequest;\r
-import org.iotivity.base.OcResourceResponse;\r
-import org.iotivity.base.RequestHandlerFlag;\r
-import org.iotivity.base.ResourceProperty;\r
-\r
-import java.util.EnumSet;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-\r
-import base.iotivity.org.examples.message.IMessageLogger;\r
-\r
-/**\r
- * DeviceResource\r
- * <p/>\r
- * Creates a device resource and performs action based on client requests\r
- */\r
-public class DeviceResource extends Resource implements IMessageLogger {\r
-    private Context mContext;\r
-\r
-    private static String TAG = "DeviceResource: ";\r
-\r
-    /**\r
-     * constructor\r
-     *\r
-     * @param context to enable sending of broadcast messages to be displayed on the user screen\r
-     */\r
-    DeviceResource(Context context) {\r
-        mContext = context;\r
-\r
-        // eventHandler for register deviceResource\r
-        OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {\r
-            @Override\r
-            public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {\r
-                // this is where the main logic of DeviceResource is handled\r
-                return entityHandler(ocResourceRequest);\r
-            }\r
-        };\r
-\r
-        try {\r
-            logMessage(TAG + "RegisterDeviceResource " + StringConstants.DEVICE_URI + " : " +\r
-                    StringConstants.RESOURCE_TYPENAME + " : " + StringConstants.RESOURCE_INTERFACE);\r
-            mResourceHandle = OcPlatform.registerResource(StringConstants.DEVICE_URI,\r
-                    StringConstants.RESOURCE_TYPENAME, StringConstants.RESOURCE_INTERFACE,\r
-                    eh, EnumSet.of(ResourceProperty.DISCOVERABLE));\r
-        } catch (OcException e) {\r
-            logMessage(TAG + "registerResource error: " + e.getMessage());\r
-            Log.e(TAG, e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * update current state of device\r
-     *\r
-     * @return device representation\r
-     */\r
-    private void updateRepresentationValues() {\r
-        try {\r
-            mRepresentation.setValue(StringConstants.DEVICE_NAME,\r
-                    "Intel Powered 2 door, 1 light refrigerator");\r
-        } catch (OcException e) {\r
-            Log.e(TAG, e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * unregister the resource\r
-     */\r
-    private void deleteDeviceResource() {\r
-        try {\r
-            OcPlatform.unregisterResource(mResourceHandle);\r
-            logMessage(TAG + "Unregister DeviceResource successful");\r
-        } catch (OcException e) {\r
-            logMessage(TAG + e.getMessage());\r
-            Log.e(TAG, e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * this is the main method which handles different incoming requests appropriately.\r
-     *\r
-     * @param request OcResourceRequest from the client\r
-     * @return EntityHandlerResult depending on whether the request was handled successfully or not\r
-     */\r
-    private EntityHandlerResult entityHandler(OcResourceRequest request) {\r
-        EntityHandlerResult result = EntityHandlerResult.ERROR;\r
-        if (null != request) {\r
-            List<OcHeaderOption> headerOptions = request.getHeaderOptions();\r
-            String clientAPIVersion = "";\r
-            String clientToken = "";\r
-\r
-            // search for header options map and look for API version and client token\r
-            for (OcHeaderOption headerOption : headerOptions) {\r
-                int optionId = headerOption.getOptionId();\r
-                if (StringConstants.API_VERSION_KEY == optionId) {\r
-                    clientAPIVersion = headerOption.getOptionData();\r
-                    logMessage(TAG + " Client API Version: " + clientAPIVersion);\r
-                } else if (StringConstants.CLIENT_VERSION_KEY == optionId) {\r
-                    clientToken = headerOption.getOptionData();\r
-                    logMessage(TAG + " Client Token: " + clientToken);\r
-                }\r
-            }\r
-            if (clientAPIVersion.equals(StringConstants.API_VERSION) &&\r
-                    clientToken.equals(StringConstants.CLIENT_TOKEN)) {\r
-                List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();\r
-                OcHeaderOption apiVersion = new OcHeaderOption(StringConstants.API_VERSION_KEY,\r
-                        StringConstants.API_VERSION);\r
-                serverHeaderOptions.add(apiVersion);\r
-                try {\r
-                    if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {\r
-                        OcResourceResponse response = new OcResourceResponse();\r
-                        response.setRequestHandle(request.getRequestHandle());\r
-                        response.setResourceHandle(request.getResourceHandle());\r
-                        response.setHeaderOptions(serverHeaderOptions);\r
-\r
-                        switch (request.getRequestType()) {\r
-                            case GET:\r
-                                response.setErrorCode(StringConstants.OK);\r
-                                response.setResponseResult(EntityHandlerResult.OK);\r
-                                updateRepresentationValues();\r
-                                response.setResourceRepresentation(mRepresentation);\r
-                                OcPlatform.sendResponse(response);\r
-                                break;\r
-                            case DELETE:\r
-                                deleteDeviceResource();\r
-                                response.setErrorCode(StringConstants.OK);\r
-                                response.setResponseResult(EntityHandlerResult.OK);\r
-                                break;\r
-                            case POST:\r
-                                response.setResponseResult(EntityHandlerResult.ERROR);\r
-                                OcPlatform.sendResponse(response);\r
-                                break;\r
-                        }\r
-                        result = EntityHandlerResult.OK;\r
-                    }\r
-                } catch (OcException e) {\r
-                    logMessage(TAG + e.getMessage());\r
-                    Log.e(TAG, e.getMessage());\r
-                }\r
-            }\r
-        }\r
-        return result;\r
-    }\r
-\r
-    @Override\r
-    public void logMessage(String msg) {\r
-        logMsg(msg);\r
-        if (StringConstants.ENABLE_PRINTING) {\r
-            Log.i(TAG, msg);\r
-        }\r
-    }\r
-\r
-    public void logMsg(final String text) {\r
-        Intent intent = new Intent(StringConstants.INTENT);\r
-        intent.putExtra("message", text);\r
-        mContext.sendBroadcast(intent);\r
-    }\r
-}\r
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2015 Intel Corporation.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package org.iotivity.base.examples.fridgeserver;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcHeaderOption;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * DeviceResource
+ * <p/>
+ * Creates a device resource and performs action based on client requests
+ */
+public class DeviceResource extends Resource implements OcPlatform.EntityHandler {
+    public static final String DEVICE_URI = "/device";
+    public static final String RESOURCE_TYPENAME = "intel.fridge";
+    public static final String API_VERSION = "v.1.0";
+    public static final String CLIENT_TOKEN = "21ae43gf";
+    public static final String DEVICE_NAME = "device_name";
+    private static String TAG = "DeviceResource: ";
+    public static final int SUCCESS = 200;
+    public static final int API_VERSION_KEY = 2048;
+    public static final int CLIENT_VERSION_KEY = 3000;
+
+    private Context mContext;
+
+    /**
+     * constructor
+     *
+     * @param context to enable sending of broadcast messages to be displayed on the user screen
+     */
+    DeviceResource(Context context) {
+        mContext = context;
+        registerDeviceResource();
+    }
+
+    private void registerDeviceResource() {
+        try {
+            logMessage("RegisterDeviceResource " + DEVICE_URI + " : " + RESOURCE_TYPENAME);
+            mResourceHandle = OcPlatform.registerResource(
+                    DEVICE_URI,
+                    RESOURCE_TYPENAME,
+                    OcPlatform.DEFAULT_INTERFACE,
+                    this,
+                    EnumSet.of(ResourceProperty.DISCOVERABLE));
+        } catch (OcException e) {
+            logMessage(TAG + "Failed to register DeviceResource");
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    /**
+     * this is the main method which handles different incoming requests appropriately.
+     *
+     * @param ocResourceRequest OcResourceRequest from the client
+     * @return EntityHandlerResult indicates whether the request was handled successfully or not
+     */
+    @Override
+    public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+        EntityHandlerResult result = EntityHandlerResult.ERROR;
+        if (null != ocResourceRequest) {
+            List<OcHeaderOption> headerOptions = ocResourceRequest.getHeaderOptions();
+            String clientAPIVersion = "";
+            String clientToken = "";
+            // search for header options map and look for API version and client token
+            for (OcHeaderOption headerOption : headerOptions) {
+                int optionId = headerOption.getOptionId();
+                if (API_VERSION_KEY == optionId) {
+                    clientAPIVersion = headerOption.getOptionData();
+                    logMessage(TAG + " Client API Version: " + clientAPIVersion);
+                } else if (CLIENT_VERSION_KEY == optionId) {
+                    clientToken = headerOption.getOptionData();
+                    logMessage(TAG + " Client Token: " + clientToken);
+                }
+            }
+            if (clientAPIVersion.equals(API_VERSION) &&
+                    clientToken.equals(CLIENT_TOKEN)) {
+                List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();
+                OcHeaderOption apiVersion = new OcHeaderOption(API_VERSION_KEY,
+                        API_VERSION);
+                serverHeaderOptions.add(apiVersion);
+                try {
+                    if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+                        OcResourceResponse response = new OcResourceResponse();
+                        response.setRequestHandle(ocResourceRequest.getRequestHandle());
+                        response.setResourceHandle(ocResourceRequest.getResourceHandle());
+                        response.setHeaderOptions(serverHeaderOptions);
+
+                        switch (ocResourceRequest.getRequestType()) {
+                            case GET:
+                                response.setErrorCode(SUCCESS);
+                                response.setResponseResult(EntityHandlerResult.OK);
+                                updateRepresentationValues();
+                                response.setResourceRepresentation(mRepresentation);
+                                OcPlatform.sendResponse(response);
+                                break;
+                        }
+                        result = EntityHandlerResult.OK;
+                    }
+                } catch (OcException e) {
+                    logMessage("Error in handleEntity of DeviceResource");
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+        }
+        logMessage("-----------------------------------------------------");
+        return result;
+    }
+
+    /**
+     * update state of device
+     *
+     * @return device representation
+     */
+    private void updateRepresentationValues() {
+        try {
+            mRepresentation.setValue(DEVICE_NAME,
+                    "Intel Powered 3 door, 1 light refrigerator");
+        } catch (OcException e) {
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    //******************************************************************************
+    // End of the OIC specific code
+    //******************************************************************************
+    public void logMessage(String msg) {
+        Intent intent = new Intent(FridgeServer.INTENT);
+        intent.putExtra("message", msg);
+        mContext.sendBroadcast(intent);
+    }
+}