Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / DoorResource.java
old mode 100644 (file)
new mode 100755 (executable)
index 438022b..332abba
-/*\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.OcPlatform;\r
-import org.iotivity.base.OcRepresentation;\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
-\r
-import base.iotivity.org.examples.message.IMessageLogger;\r
-\r
-/**\r
- * DoorResource\r
- * <p/>\r
- * Creates a door resource and performs action based on client requests\r
- */\r
-public class DoorResource extends Resource implements IMessageLogger {\r
-    private Context mContext;\r
-\r
-    private static String TAG = "DoorResource: ";\r
-    private String mSide = StringConstants.LEFT;\r
-    private boolean mOpen;\r
-    private String resourceURI;\r
-\r
-    /**\r
-     * Constructor\r
-     *\r
-     * @param side    left or right side of the door\r
-     * @param context to enable sending of broadcast messages to be displayed on the user screen\r
-     */\r
-    DoorResource(String side, Context context) {\r
-        mContext = context;\r
-        mSide = side;\r
-\r
-        resourceURI = StringConstants.DOOR_URI + mSide;\r
-\r
-        // eventHandler for register doorResource\r
-        OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {\r
-            @Override\r
-            public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {\r
-                // this is where the main logic of DoorResource is handled\r
-                return entityHandler(ocResourceRequest);\r
-            }\r
-        };\r
-        try {\r
-            logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " +\r
-                    StringConstants.RESOURCE_TYPEDOOR + " : " + StringConstants.RESOURCE_INTERFACE);\r
-            mResourceHandle = OcPlatform.registerResource(resourceURI,\r
-                    StringConstants.RESOURCE_TYPEDOOR, StringConstants.RESOURCE_INTERFACE,\r
-                    eh, EnumSet.of(ResourceProperty.DISCOVERABLE));\r
-        } catch (OcException e) {\r
-            logMessage(TAG + "DoorResource registerResource error: " + e.getMessage());\r
-            Log.e(TAG, e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * updates the current value of the door resource\r
-     *\r
-     * @return door representation\r
-     */\r
-    private void updateRepresentationValues() {\r
-        try {\r
-            mRepresentation.setValue(StringConstants.SIDE, mSide);\r
-            mRepresentation.setValue(StringConstants.OPEN, mOpen);\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
-     * update the OPEN value of doorResource (door is open/ closed)\r
-     *\r
-     * @param representation get current state of door\r
-     */\r
-    private void put(OcRepresentation representation) {\r
-        try {\r
-            mOpen = representation.getValue(StringConstants.OPEN);\r
-        } catch (OcException e) {\r
-            Log.e(TAG, e.getMessage());\r
-        }\r
-        // Note, we won't let the user change the door side!\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
-            try {\r
-                if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {\r
-                    OcResourceResponse response = new OcResourceResponse();\r
-                    response.setRequestHandle(request.getRequestHandle());\r
-                    response.setResourceHandle(request.getResourceHandle());\r
-\r
-                    switch (request.getRequestType()) {\r
-                        case GET:\r
-                            response.setErrorCode(StringConstants.OK);\r
-                            updateRepresentationValues();\r
-                            response.setResourceRepresentation(mRepresentation);\r
-                            response.setResponseResult(EntityHandlerResult.OK);\r
-                            OcPlatform.sendResponse(response);\r
-                            break;\r
-                        case PUT:\r
-                            response.setErrorCode(StringConstants.OK);\r
-                            put(request.getResourceRepresentation());\r
-                            updateRepresentationValues();\r
-                            response.setResourceRepresentation(mRepresentation);\r
-                            response.setResponseResult(EntityHandlerResult.OK);\r
-                            OcPlatform.sendResponse(response);\r
-                            break;\r
-                        case DELETE:\r
-                            response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);\r
-                            response.setErrorCode(204);\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
-                return EntityHandlerResult.ERROR;\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(StringConstants.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.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+
+/**
+ * DoorResource
+ * <p/>
+ * Creates a door resource and performs actions based on the client requests
+ */
+public class DoorResource extends Resource implements OcPlatform.EntityHandler {
+    public static final String DOOR_URI = "/door/";
+    public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
+    public static final String DOOR_STATE_KEY = "state";
+    public static final String DOOR_SIDE_KEY = "side";
+    private boolean mDoorState;
+    private String mSide;
+
+    /**
+     * Constructor
+     *
+     * @param side    side of the door
+     * @param context to enable sending of broadcast messages to be displayed on the user screen
+     */
+    DoorResource(String side, Context context) {
+        mContext = context;
+        mSide = side;
+        registerDoorResource();
+    }
+
+    private void registerDoorResource() {
+        String resourceURI = DOOR_URI + mSide;
+        logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
+        try {
+            mResourceHandle = OcPlatform.registerResource(resourceURI,
+                    RESOURCE_TYPEDOOR,
+                    OcPlatform.DEFAULT_INTERFACE,
+                    this,
+                    EnumSet.of(ResourceProperty.DISCOVERABLE));
+        } catch (OcException e) {
+            logMessage(TAG + "Failed to register DoorResource");
+            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) {
+            try {
+                if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+                    OcResourceResponse response = new OcResourceResponse();
+                    response.setRequestHandle(ocResourceRequest.getRequestHandle());
+                    response.setResourceHandle(ocResourceRequest.getResourceHandle());
+
+                    switch (ocResourceRequest.getRequestType()) {
+                        case GET:
+                            response.setErrorCode(SUCCESS);
+                            updateRepresentationValues();
+                            response.setResourceRepresentation(mRepresentation);
+                            response.setResponseResult(EntityHandlerResult.OK);
+                            OcPlatform.sendResponse(response);
+                            break;
+                        case PUT:
+                            response.setErrorCode(SUCCESS);
+                            put(ocResourceRequest.getResourceRepresentation());
+                            updateRepresentationValues();
+                            response.setResourceRepresentation(mRepresentation);
+                            response.setResponseResult(EntityHandlerResult.OK);
+                            OcPlatform.sendResponse(response);
+                            break;
+                        case DELETE:
+                            response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
+                            response.setErrorCode(204);
+                            OcPlatform.sendResponse(response);
+                            break;
+                    }
+                    result = EntityHandlerResult.OK;
+                }
+            } catch (OcException e) {
+                logMessage("Error in handleEntity of DoorResource");
+                Log.e(TAG, e.getMessage());
+                return EntityHandlerResult.ERROR;
+            }
+        }
+        logMessage("-----------------------------------------------------");
+        return result;
+    }
+
+    /**
+     * helper function to update the current value of the door resource
+     */
+    private void updateRepresentationValues() {
+        try {
+            mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
+            mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
+            logMessage(TAG + "door state is  " + ((mDoorState == true) ? "open" : "close") +
+                    " and door side is " + mSide);
+        } catch (OcException e) {
+            Log.e(TAG, e.getMessage());
+        }
+    }
+
+    /**
+     * update the value of doorResource, depending on if door is open/ closed
+     *
+     * @param representation new state of a door
+     */
+    private void put(OcRepresentation representation) {
+        try {
+            mDoorState = representation.getValue(DOOR_STATE_KEY);
+        } catch (OcException e) {
+            Log.e(TAG, e.getMessage());
+        }
+        // Note, we won't let the user change the door side!
+    }
+
+    //******************************************************************************
+    // End of the OIC specific code
+    //******************************************************************************
+    private Context mContext;
+    private static String TAG = "DoorResource: ";
+
+    public void logMessage(String msg) {
+        Intent intent = new Intent(FridgeServer.INTENT);
+        intent.putExtra(FridgeServer.MESSAGE, msg);
+        mContext.sendBroadcast(intent);
+    }
+}
\ No newline at end of file