2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples.fridgeserver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.util.Log;
29 import org.iotivity.base.EntityHandlerResult;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
33 import org.iotivity.base.OcResourceRequest;
34 import org.iotivity.base.OcResourceResponse;
35 import org.iotivity.base.RequestHandlerFlag;
36 import org.iotivity.base.ResourceProperty;
38 import java.util.EnumSet;
43 * Creates a door resource and performs actions based on the client requests
45 public class DoorResource extends Resource implements OcPlatform.EntityHandler {
46 public static final String DOOR_URI = "/door/";
47 public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
48 public static final String DOOR_STATE_KEY = "state";
49 public static final String DOOR_SIDE_KEY = "side";
50 private boolean mDoorState;
56 * @param side side of the door
57 * @param context to enable sending of broadcast messages to be displayed on the user screen
59 DoorResource(String side, Context context) {
62 registerDoorResource();
65 private void registerDoorResource() {
66 String resourceURI = DOOR_URI + mSide;
67 logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
69 mResourceHandle = OcPlatform.registerResource(resourceURI,
71 OcPlatform.DEFAULT_INTERFACE,
73 EnumSet.of(ResourceProperty.DISCOVERABLE));
74 } catch (OcException e) {
75 logMessage(TAG + "Failed to register DoorResource");
76 Log.e(TAG, e.getMessage());
81 * this is the main method which handles different incoming requests appropriately.
83 * @param ocResourceRequest OcResourceRequest from the client
84 * @return EntityHandlerResult indicates whether the request was handled successfully or not
87 public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
88 EntityHandlerResult result = EntityHandlerResult.ERROR;
89 if (null != ocResourceRequest) {
91 if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
92 OcResourceResponse response = new OcResourceResponse();
93 response.setRequestHandle(ocResourceRequest.getRequestHandle());
94 response.setResourceHandle(ocResourceRequest.getResourceHandle());
96 switch (ocResourceRequest.getRequestType()) {
98 response.setErrorCode(SUCCESS);
99 updateRepresentationValues();
100 response.setResourceRepresentation(mRepresentation);
101 response.setResponseResult(EntityHandlerResult.OK);
102 OcPlatform.sendResponse(response);
105 response.setErrorCode(SUCCESS);
106 put(ocResourceRequest.getResourceRepresentation());
107 updateRepresentationValues();
108 response.setResourceRepresentation(mRepresentation);
109 response.setResponseResult(EntityHandlerResult.OK);
110 OcPlatform.sendResponse(response);
113 response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
114 response.setErrorCode(204);
115 OcPlatform.sendResponse(response);
118 result = EntityHandlerResult.OK;
120 } catch (OcException e) {
121 logMessage("Error in handleEntity of DoorResource");
122 Log.e(TAG, e.getMessage());
123 return EntityHandlerResult.ERROR;
126 logMessage("-----------------------------------------------------");
131 * helper function to update the current value of the door resource
133 private void updateRepresentationValues() {
135 mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
136 mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
137 logMessage(TAG + "door state is " + ((mDoorState == true) ? "open" : "close") +
138 " and door side is " + mSide);
139 } catch (OcException e) {
140 Log.e(TAG, e.getMessage());
145 * update the value of doorResource, depending on if door is open/ closed
147 * @param representation new state of a door
149 private void put(OcRepresentation representation) {
151 mDoorState = representation.getValue(DOOR_STATE_KEY);
152 } catch (OcException e) {
153 Log.e(TAG, e.getMessage());
155 // Note, we won't let the user change the door side!
158 //******************************************************************************
159 // End of the OIC specific code
160 //******************************************************************************
161 private Context mContext;
162 private static String TAG = "DoorResource: ";
164 public void logMessage(String msg) {
165 Intent intent = new Intent(FridgeServer.INTENT);
166 intent.putExtra(FridgeServer.MESSAGE, msg);
167 mContext.sendBroadcast(intent);