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 light resource and performs action based on client requests
47 public class LightResource extends Resource implements IMessageLogger {
48 private Context mContext;
50 private static String TAG = "LightResource: ";
52 private boolean mIsOn = false;
57 * @param context to enable sending of broadcast messages to be displayed on the user screen
59 LightResource(Context context) {
61 //eventHandler for register lightResource
62 OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
64 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
65 // this is where the main logic of LightResource is handled
66 return entityHandler(ocResourceRequest);
70 logMessage(TAG + "RegisterLightResource " + StringConstants.LIGHT_URI + " : " +
71 StringConstants.RESOURCE_TYPELIGHT + " : " + StringConstants.RESOURCE_INTERFACE);
72 mResourceHandle = OcPlatform.registerResource(StringConstants.LIGHT_URI,
73 StringConstants.RESOURCE_TYPELIGHT, StringConstants.RESOURCE_INTERFACE,
74 eh, EnumSet.of(ResourceProperty.DISCOVERABLE));
75 } catch (OcException e) {
76 logMessage(TAG + "LightResource registerResource error: " + e.getMessage());
77 Log.e(TAG, e.getMessage());
82 * updates the current state of the light (on/ off)
84 * @return light is on or off
86 private void updateRepresentationValues() {
88 mRepresentation.setValue(StringConstants.ON, mIsOn);
89 } catch (OcException e) {
90 Log.e(TAG, e.getMessage());
95 * update the value of mIsOn from the representation
97 * @param representation get current state of light
99 private void put(OcRepresentation representation) {
101 mIsOn = representation.getValue(StringConstants.ON);
102 } catch (OcException e) {
103 Log.e(TAG, e.getMessage());
108 * this is the main method which handles different incoming requests appropriately.
110 * @param request OcResourceRequest from the client
111 * @return EntityHandlerResult depending on whether the request was handled successfully or not
113 private EntityHandlerResult entityHandler(OcResourceRequest request) {
114 EntityHandlerResult result = EntityHandlerResult.ERROR;
115 if (null != request) {
117 if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
118 OcResourceResponse response = new OcResourceResponse();
119 response.setRequestHandle(request.getRequestHandle());
120 response.setResourceHandle(request.getResourceHandle());
122 switch (request.getRequestType()) {
124 response.setErrorCode(StringConstants.OK);
125 updateRepresentationValues();
126 response.setResourceRepresentation(mRepresentation);
127 response.setResponseResult(EntityHandlerResult.OK);
128 OcPlatform.sendResponse(response);
129 result = EntityHandlerResult.OK;
132 response.setErrorCode(StringConstants.OK);
133 put(request.getResourceRepresentation());
134 updateRepresentationValues();
135 response.setResourceRepresentation(mRepresentation);
136 response.setResponseResult(EntityHandlerResult.OK);
137 OcPlatform.sendResponse(response);
138 result = EntityHandlerResult.OK;
142 } catch (OcException e) {
143 logMessage(TAG + e.getMessage());
144 Log.e(TAG, e.getMessage());
145 return EntityHandlerResult.ERROR;
152 public void logMessage(String msg) {
154 if (StringConstants.ENABLE_PRINTING) {
159 public void logMsg(final String text) {
160 Intent intent = new Intent(StringConstants.INTENT);
161 intent.putExtra(StringConstants.MESSAGE, text);
162 mContext.sendBroadcast(intent);