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;
45 * LightResource is a sample OIC server resource created by the refrigerator.
47 public class LightResource extends Resource implements OcPlatform.EntityHandler {
48 LightResource(Context context) {
51 registerLightResource();
54 private void registerLightResource() {
56 logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
57 mResourceHandle = OcPlatform.registerResource(
60 OcPlatform.DEFAULT_INTERFACE,
62 EnumSet.of(ResourceProperty.DISCOVERABLE));
63 } catch (OcException e) {
64 logMessage(TAG + "Failed to register LightResource");
65 Log.e(TAG, e.getMessage());
67 logMessage("-----------------------------------------------------");
71 * sample implementation of eventHandler for lightResource - this can be implemented in many
74 * @param ocResourceRequest OcResourceRequest from the client
75 * @return EntityHandlerResult indicates whether the request was handled successfully or not
78 public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
79 EntityHandlerResult result = EntityHandlerResult.ERROR;
80 if (null != ocResourceRequest) {
82 if (ocResourceRequest.getRequestHandlerFlagSet().contains(
83 RequestHandlerFlag.REQUEST)) {
84 OcResourceResponse response = new OcResourceResponse();
85 response.setRequestHandle(ocResourceRequest.getRequestHandle());
86 response.setResourceHandle(ocResourceRequest.getResourceHandle());
87 switch (ocResourceRequest.getRequestType()) {
89 response.setErrorCode(Resource.SUCCESS);
90 updateRepresentationValues();
91 response.setResourceRepresentation(mRepresentation);
92 response.setResponseResult(EntityHandlerResult.OK);
93 OcPlatform.sendResponse(response);
94 result = EntityHandlerResult.OK;
97 response.setErrorCode(Resource.SUCCESS);
98 put(ocResourceRequest.getResourceRepresentation());
99 updateRepresentationValues();
100 response.setResourceRepresentation(mRepresentation);
101 response.setResponseResult(EntityHandlerResult.OK);
102 OcPlatform.sendResponse(response);
103 result = EntityHandlerResult.OK;
107 } catch (OcException e) {
108 logMessage("Error in handleEntity");
109 Log.e(TAG, e.getMessage());
110 return EntityHandlerResult.ERROR;
113 logMessage("-----------------------------------------------------");
117 public OcResourceHandle getHandle() {
118 return mResourceHandle;
122 * update the value of light (ON/ OFF) from the representation
124 * @param representation new state of light
126 private void put(OcRepresentation representation) {
128 mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
129 } catch (OcException e) {
130 Log.e(TAG, e.getMessage());
135 * helper function to update the current state of the light
137 private void updateRepresentationValues() {
139 mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
140 } catch (OcException e) {
141 Log.e(TAG, e.getMessage());
145 //******************************************************************************
146 // End of the OIC specific code
147 //******************************************************************************
148 private Context mContext;
149 private boolean mIsLightOn = false;
151 private static String TAG = "LightResource: ";
152 public static final String LIGHT_URI = "/light";
153 public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
154 public static final String LIGHT_STATUS_KEY = "light";
156 private void logMessage(String msg) {
157 Intent intent = new Intent(Resource.INTENT);
158 intent.putExtra(Resource.MESSAGE, msg);
159 mContext.sendBroadcast(intent);