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;
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.OcResourceHandle;
34 import org.iotivity.base.OcResourceRequest;
35 import org.iotivity.base.OcResourceResponse;
36 import org.iotivity.base.RequestHandlerFlag;
37 import org.iotivity.base.ResourceProperty;
39 import java.util.EnumSet;
44 * DoorResource is a sample OIC server resource created by the refrigerator.
46 public class DoorResource extends Resource implements OcPlatform.EntityHandler {
47 DoorResource(String side, Context context) {
51 registerDoorResource();
54 private void registerDoorResource() {
55 String resourceURI = DOOR_URI + mSide;
56 logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
58 mResourceHandle = OcPlatform.registerResource(
61 OcPlatform.DEFAULT_INTERFACE,
63 EnumSet.of(ResourceProperty.DISCOVERABLE));
64 } catch (OcException e) {
65 logMessage(TAG + "Failed to register DoorResource");
66 Log.e(TAG, e.getMessage());
68 logMessage("-----------------------------------------------------");
72 * sample implementation of eventHandler for doorResource - this can be implemented in many
75 * @param ocResourceRequest OcResourceRequest from the client
76 * @return EntityHandlerResult indicates whether the request was handled successfully or not
79 public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
80 EntityHandlerResult result = EntityHandlerResult.ERROR;
81 if (null != ocResourceRequest) {
83 if (ocResourceRequest.getRequestHandlerFlagSet().contains(
84 RequestHandlerFlag.REQUEST)) {
85 OcResourceResponse response = new OcResourceResponse();
86 response.setRequestHandle(ocResourceRequest.getRequestHandle());
87 response.setResourceHandle(ocResourceRequest.getResourceHandle());
89 switch (ocResourceRequest.getRequestType()) {
91 response.setErrorCode(Resource.SUCCESS);
92 updateRepresentationValues();
93 response.setResourceRepresentation(mRepresentation);
94 response.setResponseResult(EntityHandlerResult.OK);
95 OcPlatform.sendResponse(response);
98 response.setErrorCode(Resource.SUCCESS);
99 put(ocResourceRequest.getResourceRepresentation());
100 updateRepresentationValues();
101 response.setResourceRepresentation(mRepresentation);
102 response.setResponseResult(EntityHandlerResult.OK);
103 OcPlatform.sendResponse(response);
106 response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
107 response.setErrorCode(204);
108 OcPlatform.sendResponse(response);
111 result = EntityHandlerResult.OK;
113 } catch (OcException e) {
114 logMessage("Error in handleEntity of DoorResource");
115 Log.e(TAG, e.getMessage());
116 return EntityHandlerResult.ERROR;
119 logMessage("-----------------------------------------------------");
123 public OcResourceHandle getHandle() {
124 return mResourceHandle;
128 * helper function to update the current value of the door resource
130 private void updateRepresentationValues() {
132 mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
133 mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
134 logMessage(TAG + "door state is " + ((mDoorState == true) ? "open" : "close") +
135 " and door side is " + mSide);
136 } catch (OcException e) {
137 Log.e(TAG, e.getMessage());
142 * update the value of doorResource, depending on if door is open/ closed
144 * @param representation new state of a door
146 private void put(OcRepresentation representation) {
148 mDoorState = representation.getValue(DOOR_STATE_KEY);
149 } catch (OcException e) {
150 Log.e(TAG, e.getMessage());
152 // Note, we won't let the user change the door side!
155 //******************************************************************************
156 // End of the OIC specific code
157 //******************************************************************************
158 private Context mContext;
159 private String mSide;
160 private boolean mDoorState;
161 public static final String DOOR_URI = "/door/";
162 public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
163 private static String TAG = "DoorResource: ";
164 public static final String DOOR_STATE_KEY = "state";
165 public static final String DOOR_SIDE_KEY = "side";
167 private void logMessage(String msg) {
168 Intent intent = new Intent(Resource.INTENT);
169 intent.putExtra(Resource.MESSAGE, msg);
170 mContext.sendBroadcast(intent);