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 light resource and performs actions based on the client requests
45 public class LightResource extends Resource implements OcPlatform.EntityHandler {
46 public static final String LIGHT_STATUS_KEY = "light";
47 public static final String LIGHT_URI = "/light";
48 public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
49 private boolean mIsLightOn = false;
54 * @param context to enable sending of broadcast messages to be displayed on the user screen
56 LightResource(Context context) {
58 registerLightResource();
61 private void registerLightResource() {
63 logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
64 mResourceHandle = OcPlatform.registerResource(LIGHT_URI,
66 OcPlatform.DEFAULT_INTERFACE,
68 EnumSet.of(ResourceProperty.DISCOVERABLE));
69 } catch (OcException e) {
70 logMessage(TAG + "Failed to register LightResource");
71 Log.e(TAG, e.getMessage());
73 logMessage("-----------------------------------------------------");
77 * this is the main method which handles different incoming requests appropriately.
79 * @param ocResourceRequest OcResourceRequest from the client
80 * @return EntityHandlerResult indicates whether the request was handled successfully or not
83 public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
84 EntityHandlerResult result = EntityHandlerResult.ERROR;
85 if (null != ocResourceRequest) {
87 if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
88 OcResourceResponse response = new OcResourceResponse();
89 response.setRequestHandle(ocResourceRequest.getRequestHandle());
90 response.setResourceHandle(ocResourceRequest.getResourceHandle());
92 switch (ocResourceRequest.getRequestType()) {
94 response.setErrorCode(SUCCESS);
95 updateRepresentationValues();
96 response.setResourceRepresentation(mRepresentation);
97 response.setResponseResult(EntityHandlerResult.OK);
98 OcPlatform.sendResponse(response);
99 result = EntityHandlerResult.OK;
102 response.setErrorCode(SUCCESS);
103 put(ocResourceRequest.getResourceRepresentation());
104 updateRepresentationValues();
105 response.setResourceRepresentation(mRepresentation);
106 response.setResponseResult(EntityHandlerResult.OK);
107 OcPlatform.sendResponse(response);
108 result = EntityHandlerResult.OK;
112 } catch (OcException e) {
113 logMessage("Error in handleEntity of LightResource");
114 Log.e(TAG, e.getMessage());
115 return EntityHandlerResult.ERROR;
118 logMessage("-----------------------------------------------------");
123 * updates the current state of the light (on/ off)
125 * @return light is on or off
127 private void updateRepresentationValues() {
129 mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
130 } catch (OcException e) {
131 Log.e(TAG, e.getMessage());
136 * update the value of mIsOn from the representation
138 * @param representation get current state of light
140 private void put(OcRepresentation representation) {
142 mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
143 } catch (OcException e) {
144 Log.e(TAG, e.getMessage());
148 //******************************************************************************
149 // End of the OIC specific code
150 //******************************************************************************
151 private Context mContext;
152 private static String TAG = "LightResource: ";
154 public void logMessage(String msg) {
155 Intent intent = new Intent(FridgeServer.INTENT);
156 intent.putExtra(FridgeServer.MESSAGE, msg);
157 mContext.sendBroadcast(intent);