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;
40 import base.iotivity.org.examples.message.IMessageLogger;
45 * Creates a door resource and performs action based on client requests
47 public class DoorResource extends Resource implements IMessageLogger {
48 private Context mContext;
50 private static String TAG = "DoorResource: ";
51 private String mSide = StringConstants.LEFT;
52 private boolean mOpen;
53 private String resourceURI;
58 * @param side left or right side of the door
59 * @param context to enable sending of broadcast messages to be displayed on the user screen
61 DoorResource(String side, Context context) {
65 resourceURI = StringConstants.DOOR_URI + mSide;
67 // eventHandler for register doorResource
68 OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
70 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
71 // this is where the main logic of DoorResource is handled
72 return entityHandler(ocResourceRequest);
76 logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " +
77 StringConstants.RESOURCE_TYPEDOOR + " : " + StringConstants.RESOURCE_INTERFACE);
78 mResourceHandle = OcPlatform.registerResource(resourceURI,
79 StringConstants.RESOURCE_TYPEDOOR, StringConstants.RESOURCE_INTERFACE,
80 eh, EnumSet.of(ResourceProperty.DISCOVERABLE));
81 } catch (OcException e) {
82 logMessage(TAG + "DoorResource registerResource error: " + e.getMessage());
83 Log.e(TAG, e.getMessage());
88 * updates the current value of the door resource
90 * @return door representation
92 private void updateRepresentationValues() {
94 mRepresentation.setValue(StringConstants.SIDE, mSide);
95 mRepresentation.setValue(StringConstants.OPEN, mOpen);
96 mRepresentation.setValue(StringConstants.DEVICE_NAME,
97 "Intel Powered 2 door, 1 light refrigerator");
98 } catch (OcException e) {
99 Log.e(TAG, e.getMessage());
104 * update the OPEN value of doorResource (door is open/ closed)
106 * @param representation get current state of door
108 private void put(OcRepresentation representation) {
110 mOpen = representation.getValue(StringConstants.OPEN);
111 } catch (OcException e) {
112 Log.e(TAG, e.getMessage());
114 // Note, we won't let the user change the door side!
118 * this is the main method which handles different incoming requests appropriately.
120 * @param request OcResourceRequest from the client
121 * @return EntityHandlerResult depending on whether the request was handled successfully or not
123 private EntityHandlerResult entityHandler(OcResourceRequest request) {
124 EntityHandlerResult result = EntityHandlerResult.ERROR;
125 if (null != request) {
127 if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
128 OcResourceResponse response = new OcResourceResponse();
129 response.setRequestHandle(request.getRequestHandle());
130 response.setResourceHandle(request.getResourceHandle());
132 switch (request.getRequestType()) {
134 response.setErrorCode(StringConstants.OK);
135 updateRepresentationValues();
136 response.setResourceRepresentation(mRepresentation);
137 response.setResponseResult(EntityHandlerResult.OK);
138 OcPlatform.sendResponse(response);
141 response.setErrorCode(StringConstants.OK);
142 put(request.getResourceRepresentation());
143 updateRepresentationValues();
144 response.setResourceRepresentation(mRepresentation);
145 response.setResponseResult(EntityHandlerResult.OK);
146 OcPlatform.sendResponse(response);
149 response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
150 response.setErrorCode(204);
151 OcPlatform.sendResponse(response);
154 result = EntityHandlerResult.OK;
156 } catch (OcException e) {
157 logMessage(TAG + e.getMessage());
158 Log.e(TAG, e.getMessage());
159 return EntityHandlerResult.ERROR;
166 public void logMessage(String msg) {
168 if (StringConstants.ENABLE_PRINTING) {
173 public void logMsg(final String text) {
174 Intent intent = new Intent(StringConstants.INTENT);
175 intent.putExtra(StringConstants.MESSAGE, text);
176 mContext.sendBroadcast(intent);