Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / DeviceResource.java
index fc33258..9868f32 100644 (file)
-/*\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;
+
+import base.iotivity.org.examples.message.IMessageLogger;
+
+/**
+ * DeviceResource
+ * <p/>
+ * Creates a device resource and performs action based on client requests
+ */
+public class DeviceResource extends Resource implements IMessageLogger {
+    private Context mContext;
+
+    private static String TAG = "DeviceResource: ";
+
+    /**
+     * constructor
+     *
+     * @param context to enable sending of broadcast messages to be displayed on the user screen
+     */
+    DeviceResource(Context context) {
+        mContext = context;
+
+        // eventHandler for register deviceResource
+        OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
+            @Override
+            public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+                // this is where the main logic of DeviceResource is handled
+                return entityHandler(ocResourceRequest);
+            }
+        };
+
+        try {
+            logMessage(TAG + "RegisterDeviceResource " + StringConstants.DEVICE_URI + " : " +
+                    StringConstants.RESOURCE_TYPENAME + " : " + StringConstants.RESOURCE_INTERFACE);
+            mResourceHandle = OcPlatform.registerResource(StringConstants.DEVICE_URI,
+                    StringConstants.RESOURCE_TYPENAME, StringConstants.RESOURCE_INTERFACE,
+                    eh, EnumSet.of(ResourceProperty.DISCOVERABLE));
+        } catch (OcException e) {
+            logMessage(TAG + "registerResource error: " + e.getMessage());
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    /**
+     * update current state of device
+     *
+     * @return device representation
+     */
+    private void updateRepresentationValues() {
+        try {
+            mRepresentation.setValue(StringConstants.DEVICE_NAME,
+                    "Intel Powered 2 door, 1 light refrigerator");
+        } catch (OcException e) {
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    /**
+     * unregister the resource
+     */
+    private void deleteDeviceResource() {
+        try {
+            OcPlatform.unregisterResource(mResourceHandle);
+            logMessage(TAG + "Unregister DeviceResource successful");
+        } catch (OcException e) {
+            logMessage(TAG + e.getMessage());
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    /**
+     * this is the main method which handles different incoming requests appropriately.
+     *
+     * @param request OcResourceRequest from the client
+     * @return EntityHandlerResult depending on whether the request was handled successfully or not
+     */
+    private EntityHandlerResult entityHandler(OcResourceRequest request) {
+        EntityHandlerResult result = EntityHandlerResult.ERROR;
+        if (null != request) {
+            List<OcHeaderOption> headerOptions = request.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 (StringConstants.API_VERSION_KEY == optionId) {
+                    clientAPIVersion = headerOption.getOptionData();
+                    logMessage(TAG + " Client API Version: " + clientAPIVersion);
+                } else if (StringConstants.CLIENT_VERSION_KEY == optionId) {
+                    clientToken = headerOption.getOptionData();
+                    logMessage(TAG + " Client Token: " + clientToken);
+                }
+            }
+
+            if (clientAPIVersion.equals(StringConstants.API_VERSION) &&
+                    clientToken.equals(StringConstants.CLIENT_TOKEN)) {
+                List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();
+                OcHeaderOption apiVersion = new OcHeaderOption(StringConstants.API_VERSION_KEY,
+                        StringConstants.API_VERSION);
+                serverHeaderOptions.add(apiVersion);
+                try {
+                    if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+                        OcResourceResponse response = new OcResourceResponse();
+                        response.setRequestHandle(request.getRequestHandle());
+                        response.setResourceHandle(request.getResourceHandle());
+                        response.setHeaderOptions(serverHeaderOptions);
+
+                        switch (request.getRequestType()) {
+                            case GET:
+                                response.setErrorCode(StringConstants.OK);
+                                response.setResponseResult(EntityHandlerResult.OK);
+                                updateRepresentationValues();
+                                response.setResourceRepresentation(mRepresentation);
+                                OcPlatform.sendResponse(response);
+                                break;
+                            case DELETE:
+                                deleteDeviceResource();
+                                response.setErrorCode(StringConstants.OK);
+                                response.setResponseResult(EntityHandlerResult.OK);
+                                break;
+                        }
+                        result = EntityHandlerResult.OK;
+                    }
+                } catch (OcException e) {
+                    logMessage(TAG + e.getMessage());
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public void logMessage(String msg) {
+        logMsg(msg);
+        if (StringConstants.ENABLE_PRINTING) {
+            Log.i(TAG, msg);
+        }
+    }
+
+    public void logMsg(final String text) {
+        Intent intent = new Intent(StringConstants.INTENT);
+        intent.putExtra("message", text);
+        mContext.sendBroadcast(intent);
+    }
+}