-/*\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
- * LightResource\r
- * <p/>\r
- * Creates a light resource and performs action based on client requests\r
- */\r
-public class LightResource extends Resource implements IMessageLogger {\r
- private Context mContext;\r
-\r
- private static String TAG = "LightResource: ";\r
-\r
- private boolean mIsOn = false;\r
-\r
- /**\r
- * constructor\r
- *\r
- * @param context to enable sending of broadcast messages to be displayed on the user screen\r
- */\r
- LightResource(Context context) {\r
- mContext = context;\r
- //eventHandler for register lightResource\r
- OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {\r
- @Override\r
- public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {\r
- // this is where the main logic of LightResource is handled\r
- return entityHandler(ocResourceRequest);\r
- }\r
- };\r
- try {\r
- logMessage(TAG + "RegisterLightResource " + StringConstants.LIGHT_URI + " : " +\r
- StringConstants.RESOURCE_TYPELIGHT + " : " + StringConstants.RESOURCE_INTERFACE);\r
- mResourceHandle = OcPlatform.registerResource(StringConstants.LIGHT_URI,\r
- StringConstants.RESOURCE_TYPELIGHT, StringConstants.RESOURCE_INTERFACE,\r
- eh, EnumSet.of(ResourceProperty.DISCOVERABLE));\r
- } catch (OcException e) {\r
- logMessage(TAG + "LightResource registerResource error: " + e.getMessage());\r
- Log.e(TAG, e.getMessage());\r
- }\r
- }\r
-\r
- /**\r
- * updates the current state of the light (on/ off)\r
- *\r
- * @return light is on or off\r
- */\r
- private void updateRepresentationValues() {\r
- try {\r
- mRepresentation.setValue(StringConstants.ON, mIsOn);\r
- } catch (OcException e) {\r
- Log.e(TAG, e.getMessage());\r
- }\r
- }\r
-\r
- /**\r
- * update the value of mIsOn from the representation\r
- *\r
- * @param representation get current state of light\r
- */\r
- private void put(OcRepresentation representation) {\r
- try {\r
- mIsOn = representation.getValue(StringConstants.ON);\r
- } catch (OcException e) {\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
- 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
- result = EntityHandlerResult.OK;\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
- result = EntityHandlerResult.OK;\r
- break;\r
- }\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;
+
+/**
+ * LightResource
+ * <p/>
+ * Creates a light resource and performs actions based on the client requests
+ */
+public class LightResource extends Resource implements OcPlatform.EntityHandler {
+ public static final String LIGHT_STATUS_KEY = "light";
+ public static final String LIGHT_URI = "/light";
+ public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
+ private boolean mIsLightOn = false;
+
+ /**
+ * constructor
+ *
+ * @param context to enable sending of broadcast messages to be displayed on the user screen
+ */
+ LightResource(Context context) {
+ mContext = context;
+ registerLightResource();
+ }
+
+ private void registerLightResource() {
+ try {
+ logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
+ mResourceHandle = OcPlatform.registerResource(LIGHT_URI,
+ RESOURCE_TYPELIGHT,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register LightResource");
+ Log.e(TAG, e.getMessage());
+ }
+ logMessage("-----------------------------------------------------");
+ }
+
+ /**
+ * 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);
+ result = EntityHandlerResult.OK;
+ break;
+ case PUT:
+ response.setErrorCode(SUCCESS);
+ put(ocResourceRequest.getResourceRepresentation());
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ result = EntityHandlerResult.OK;
+ break;
+ }
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity of LightResource");
+ Log.e(TAG, e.getMessage());
+ return EntityHandlerResult.ERROR;
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ /**
+ * updates the current state of the light (on/ off)
+ *
+ * @return light is on or off
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * update the value of mIsOn from the representation
+ *
+ * @param representation get current state of light
+ */
+ private void put(OcRepresentation representation) {
+ try {
+ mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ private Context mContext;
+ private static String TAG = "LightResource: ";
+
+ public void logMessage(String msg) {
+ Intent intent = new Intent(FridgeServer.INTENT);
+ intent.putExtra(FridgeServer.MESSAGE, msg);
+ mContext.sendBroadcast(intent);
+ }
+}