2 * //******************************************************************
\r
4 * // Copyright 2015 Intel Corporation.
\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 * // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * // you may not use this file except in compliance with the License.
\r
10 * // You may obtain a copy of the License at
\r
12 * // http://www.apache.org/licenses/LICENSE-2.0
\r
14 * // Unless required by applicable law or agreed to in writing, software
\r
15 * // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * // See the License for the specific language governing permissions and
\r
18 * // limitations under the License.
\r
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
23 package org.iotivity.base.examples.fridgeserver;
\r
25 import android.content.Context;
\r
26 import android.content.Intent;
\r
27 import android.util.Log;
\r
29 import org.iotivity.base.EntityHandlerResult;
\r
30 import org.iotivity.base.OcException;
\r
31 import org.iotivity.base.OcPlatform;
\r
32 import org.iotivity.base.OcRepresentation;
\r
33 import org.iotivity.base.OcResourceRequest;
\r
34 import org.iotivity.base.OcResourceResponse;
\r
35 import org.iotivity.base.RequestHandlerFlag;
\r
36 import org.iotivity.base.ResourceProperty;
\r
38 import java.util.EnumSet;
\r
40 import base.iotivity.org.examples.message.IMessageLogger;
\r
45 * Creates a light resource and performs action based on client requests
\r
47 public class LightResource extends Resource implements IMessageLogger {
\r
48 private Context mContext;
\r
50 private static String TAG = "LightResource: ";
\r
52 private boolean mIsOn = false;
\r
57 * @param context to enable sending of broadcast messages to be displayed on the user screen
\r
59 LightResource(Context context) {
\r
61 //eventHandler for register lightResource
\r
62 OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
\r
64 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
\r
65 // this is where the main logic of LightResource is handled
\r
66 return entityHandler(ocResourceRequest);
\r
70 logMessage(TAG + "RegisterLightResource " + StringConstants.LIGHT_URI + " : " +
\r
71 StringConstants.RESOURCE_TYPELIGHT + " : " + StringConstants.RESOURCE_INTERFACE);
\r
72 mResourceHandle = OcPlatform.registerResource(StringConstants.LIGHT_URI,
\r
73 StringConstants.RESOURCE_TYPELIGHT, StringConstants.RESOURCE_INTERFACE,
\r
74 eh, EnumSet.of(ResourceProperty.DISCOVERABLE));
\r
75 } catch (OcException e) {
\r
76 logMessage(TAG + "LightResource registerResource error: " + e.getMessage());
\r
77 Log.e(TAG, e.getMessage());
\r
82 * updates the current state of the light (on/ off)
\r
84 * @return light is on or off
\r
86 private void updateRepresentationValues() {
\r
88 mRepresentation.setValue(StringConstants.ON, mIsOn);
\r
89 } catch (OcException e) {
\r
90 Log.e(TAG, e.getMessage());
\r
95 * update the value of mIsOn from the representation
\r
97 * @param representation get current state of light
\r
99 private void put(OcRepresentation representation) {
\r
101 mIsOn = representation.getValue(StringConstants.ON);
\r
102 } catch (OcException e) {
\r
103 Log.e(TAG, e.getMessage());
\r
108 * this is the main method which handles different incoming requests appropriately.
\r
110 * @param request OcResourceRequest from the client
\r
111 * @return EntityHandlerResult depending on whether the request was handled successfully or not
\r
113 private EntityHandlerResult entityHandler(OcResourceRequest request) {
\r
114 EntityHandlerResult result = EntityHandlerResult.ERROR;
\r
115 if (null != request) {
\r
117 if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
\r
118 OcResourceResponse response = new OcResourceResponse();
\r
119 response.setRequestHandle(request.getRequestHandle());
\r
120 response.setResourceHandle(request.getResourceHandle());
\r
122 switch (request.getRequestType()) {
\r
124 response.setErrorCode(StringConstants.OK);
\r
125 updateRepresentationValues();
\r
126 response.setResourceRepresentation(mRepresentation);
\r
127 response.setResponseResult(EntityHandlerResult.OK);
\r
128 OcPlatform.sendResponse(response);
\r
129 result = EntityHandlerResult.OK;
\r
132 response.setErrorCode(StringConstants.OK);
\r
133 put(request.getResourceRepresentation());
\r
134 updateRepresentationValues();
\r
135 response.setResourceRepresentation(mRepresentation);
\r
136 response.setResponseResult(EntityHandlerResult.OK);
\r
137 OcPlatform.sendResponse(response);
\r
138 result = EntityHandlerResult.OK;
\r
142 } catch (OcException e) {
\r
143 logMessage(TAG + e.getMessage());
\r
144 Log.e(TAG, e.getMessage());
\r
145 return EntityHandlerResult.ERROR;
\r
152 public void logMessage(String msg) {
\r
154 if (StringConstants.ENABLE_PRINTING) {
\r
159 public void logMsg(final String text) {
\r
160 Intent intent = new Intent(StringConstants.INTENT);
\r
161 intent.putExtra(StringConstants.MESSAGE, text);
\r
162 mContext.sendBroadcast(intent);
\r