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.OcHeaderOption;
32 import org.iotivity.base.OcPlatform;
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;
39 import java.util.LinkedList;
40 import java.util.List;
42 import base.iotivity.org.examples.message.IMessageLogger;
47 * Creates a device resource and performs action based on client requests
49 public class DeviceResource extends Resource implements IMessageLogger {
50 private Context mContext;
52 private static String TAG = "DeviceResource: ";
57 * @param context to enable sending of broadcast messages to be displayed on the user screen
59 DeviceResource(Context context) {
62 // eventHandler for register deviceResource
63 OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
65 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
66 // this is where the main logic of DeviceResource is handled
67 return entityHandler(ocResourceRequest);
72 logMessage(TAG + "RegisterDeviceResource " + StringConstants.DEVICE_URI + " : " +
73 StringConstants.RESOURCE_TYPENAME + " : " + StringConstants.RESOURCE_INTERFACE);
74 mResourceHandle = OcPlatform.registerResource(StringConstants.DEVICE_URI,
75 StringConstants.RESOURCE_TYPENAME, StringConstants.RESOURCE_INTERFACE,
76 eh, EnumSet.of(ResourceProperty.DISCOVERABLE));
77 } catch (OcException e) {
78 logMessage(TAG + "registerResource error: " + e.getMessage());
79 Log.e(TAG, e.getMessage());
84 * update current state of device
86 * @return device representation
88 private void updateRepresentationValues() {
90 mRepresentation.setValue(StringConstants.DEVICE_NAME,
91 "Intel Powered 2 door, 1 light refrigerator");
92 } catch (OcException e) {
93 Log.e(TAG, e.getMessage());
98 * unregister the resource
100 private void deleteDeviceResource() {
102 OcPlatform.unregisterResource(mResourceHandle);
103 logMessage(TAG + "Unregister DeviceResource successful");
104 } catch (OcException e) {
105 logMessage(TAG + e.getMessage());
106 Log.e(TAG, e.getMessage());
111 * this is the main method which handles different incoming requests appropriately.
113 * @param request OcResourceRequest from the client
114 * @return EntityHandlerResult depending on whether the request was handled successfully or not
116 private EntityHandlerResult entityHandler(OcResourceRequest request) {
117 EntityHandlerResult result = EntityHandlerResult.ERROR;
118 if (null != request) {
119 List<OcHeaderOption> headerOptions = request.getHeaderOptions();
120 String clientAPIVersion = "";
121 String clientToken = "";
123 // search for header options map and look for API version and client token
124 for (OcHeaderOption headerOption : headerOptions) {
125 int optionId = headerOption.getOptionId();
126 if (StringConstants.API_VERSION_KEY == optionId) {
127 clientAPIVersion = headerOption.getOptionData();
128 logMessage(TAG + " Client API Version: " + clientAPIVersion);
129 } else if (StringConstants.CLIENT_VERSION_KEY == optionId) {
130 clientToken = headerOption.getOptionData();
131 logMessage(TAG + " Client Token: " + clientToken);
135 if (clientAPIVersion.equals(StringConstants.API_VERSION) &&
136 clientToken.equals(StringConstants.CLIENT_TOKEN)) {
137 List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();
138 OcHeaderOption apiVersion = new OcHeaderOption(StringConstants.API_VERSION_KEY,
139 StringConstants.API_VERSION);
140 serverHeaderOptions.add(apiVersion);
142 if (request.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
143 OcResourceResponse response = new OcResourceResponse();
144 response.setRequestHandle(request.getRequestHandle());
145 response.setResourceHandle(request.getResourceHandle());
146 response.setHeaderOptions(serverHeaderOptions);
148 switch (request.getRequestType()) {
150 response.setErrorCode(StringConstants.OK);
151 response.setResponseResult(EntityHandlerResult.OK);
152 updateRepresentationValues();
153 response.setResourceRepresentation(mRepresentation);
154 OcPlatform.sendResponse(response);
157 deleteDeviceResource();
158 response.setErrorCode(StringConstants.OK);
159 response.setResponseResult(EntityHandlerResult.OK);
162 result = EntityHandlerResult.OK;
164 } catch (OcException e) {
165 logMessage(TAG + e.getMessage());
166 Log.e(TAG, e.getMessage());
174 public void logMessage(String msg) {
176 if (StringConstants.ENABLE_PRINTING) {
181 public void logMsg(final String text) {
182 Intent intent = new Intent(StringConstants.INTENT);
183 intent.putExtra("message", text);
184 mContext.sendBroadcast(intent);