X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=android%2Fexamples%2Ffridgeserver%2Fsrc%2Fmain%2Fjava%2Forg%2Fiotivity%2Fbase%2Fexamples%2Ffridgeserver%2FDoorResource.java;h=332abba18d06fb718fe2119f32808d82dfb8fcd6;hb=17c68b2fd1e74586f85e552eeab4e32dc121f8a0;hp=438022b4cfc7ca950253b603049e7ba4378b94fb;hpb=8c01dff2c5bc5496f7dc1632c498943ec6ecb015;p=platform%2Fupstream%2Fiotivity.git diff --git a/android/examples/fridgeserver/src/main/java/org/iotivity/base/examples/fridgeserver/DoorResource.java b/android/examples/fridgeserver/src/main/java/org/iotivity/base/examples/fridgeserver/DoorResource.java old mode 100644 new mode 100755 index 438022b..332abba --- a/android/examples/fridgeserver/src/main/java/org/iotivity/base/examples/fridgeserver/DoorResource.java +++ b/android/examples/fridgeserver/src/main/java/org/iotivity/base/examples/fridgeserver/DoorResource.java @@ -1,178 +1,169 @@ -/* - * //****************************************************************** - * // - * // 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; - -import base.iotivity.org.examples.message.IMessageLogger; - -/** - * DoorResource - *

- * Creates a door resource and performs action based on client requests - */ -public class DoorResource extends Resource implements IMessageLogger { - private Context mContext; - - private static String TAG = "DoorResource: "; - private String mSide = StringConstants.LEFT; - private boolean mOpen; - private String resourceURI; - - /** - * Constructor - * - * @param side left or right 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; - - resourceURI = StringConstants.DOOR_URI + mSide; - - // eventHandler for register doorResource - OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() { - @Override - public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) { - // this is where the main logic of DoorResource is handled - return entityHandler(ocResourceRequest); - } - }; - try { - logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + - StringConstants.RESOURCE_TYPEDOOR + " : " + StringConstants.RESOURCE_INTERFACE); - mResourceHandle = OcPlatform.registerResource(resourceURI, - StringConstants.RESOURCE_TYPEDOOR, StringConstants.RESOURCE_INTERFACE, - eh, EnumSet.of(ResourceProperty.DISCOVERABLE)); - } catch (OcException e) { - logMessage(TAG + "DoorResource registerResource error: " + e.getMessage()); - Log.e(TAG, e.getMessage()); - } - } - - /** - * updates the current value of the door resource - * - * @return door representation - */ - private void updateRepresentationValues() { - try { - mRepresentation.setValue(StringConstants.SIDE, mSide); - mRepresentation.setValue(StringConstants.OPEN, mOpen); - mRepresentation.setValue(StringConstants.DEVICE_NAME, - "Intel Powered 2 door, 1 light refrigerator"); - } catch (OcException e) { - Log.e(TAG, e.getMessage()); - } - } - - /** - * update the OPEN value of doorResource (door is open/ closed) - * - * @param representation get current state of door - */ - private void put(OcRepresentation representation) { - try { - mOpen = representation.getValue(StringConstants.OPEN); - } catch (OcException e) { - Log.e(TAG, e.getMessage()); - } - // Note, we won't let the user change the door side! - } - - /** - * 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) { - try { - if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) { - OcResourceResponse response = new OcResourceResponse(); - response.setRequestHandle(request.getRequestHandle()); - response.setResourceHandle(request.getResourceHandle()); - - switch (request.getRequestType()) { - case GET: - response.setErrorCode(StringConstants.OK); - updateRepresentationValues(); - response.setResourceRepresentation(mRepresentation); - response.setResponseResult(EntityHandlerResult.OK); - OcPlatform.sendResponse(response); - break; - case PUT: - response.setErrorCode(StringConstants.OK); - put(request.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(TAG + e.getMessage()); - Log.e(TAG, e.getMessage()); - return EntityHandlerResult.ERROR; - } - } - 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(StringConstants.MESSAGE, text); - mContext.sendBroadcast(intent); - } -} +/* + * //****************************************************************** + * // + * // 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 + *

+ * 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