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;
45 * Creates a device resource and performs action based on client requests
47 public class DeviceResource extends Resource implements OcPlatform.EntityHandler {
48 public static final String DEVICE_URI = "/device";
49 public static final String RESOURCE_TYPENAME = "intel.fridge";
50 public static final String API_VERSION = "v.1.0";
51 public static final String CLIENT_TOKEN = "21ae43gf";
52 public static final String DEVICE_NAME = "device_name";
53 private static String TAG = "DeviceResource: ";
54 public static final int SUCCESS = 200;
55 public static final int API_VERSION_KEY = 2048;
56 public static final int CLIENT_VERSION_KEY = 3000;
58 private Context mContext;
63 * @param context to enable sending of broadcast messages to be displayed on the user screen
65 DeviceResource(Context context) {
67 registerDeviceResource();
70 private void registerDeviceResource() {
72 logMessage("RegisterDeviceResource " + DEVICE_URI + " : " + RESOURCE_TYPENAME);
73 mResourceHandle = OcPlatform.registerResource(
76 OcPlatform.DEFAULT_INTERFACE,
78 EnumSet.of(ResourceProperty.DISCOVERABLE));
79 } catch (OcException e) {
80 logMessage(TAG + "Failed to register DeviceResource");
81 Log.e(TAG, e.getMessage());
86 * this is the main method which handles different incoming requests appropriately.
88 * @param ocResourceRequest OcResourceRequest from the client
89 * @return EntityHandlerResult indicates whether the request was handled successfully or not
92 public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
93 EntityHandlerResult result = EntityHandlerResult.ERROR;
94 if (null != ocResourceRequest) {
95 List<OcHeaderOption> headerOptions = ocResourceRequest.getHeaderOptions();
96 String clientAPIVersion = "";
97 String clientToken = "";
98 // search for header options map and look for API version and client token
99 for (OcHeaderOption headerOption : headerOptions) {
100 int optionId = headerOption.getOptionId();
101 if (API_VERSION_KEY == optionId) {
102 clientAPIVersion = headerOption.getOptionData();
103 logMessage(TAG + " Client API Version: " + clientAPIVersion);
104 } else if (CLIENT_VERSION_KEY == optionId) {
105 clientToken = headerOption.getOptionData();
106 logMessage(TAG + " Client Token: " + clientToken);
109 if (clientAPIVersion.equals(API_VERSION) &&
110 clientToken.equals(CLIENT_TOKEN)) {
111 List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();
112 OcHeaderOption apiVersion = new OcHeaderOption(API_VERSION_KEY,
114 serverHeaderOptions.add(apiVersion);
116 if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
117 OcResourceResponse response = new OcResourceResponse();
118 response.setRequestHandle(ocResourceRequest.getRequestHandle());
119 response.setResourceHandle(ocResourceRequest.getResourceHandle());
120 response.setHeaderOptions(serverHeaderOptions);
122 switch (ocResourceRequest.getRequestType()) {
124 response.setErrorCode(SUCCESS);
125 response.setResponseResult(EntityHandlerResult.OK);
126 updateRepresentationValues();
127 response.setResourceRepresentation(mRepresentation);
128 OcPlatform.sendResponse(response);
131 result = EntityHandlerResult.OK;
133 } catch (OcException e) {
134 logMessage("Error in handleEntity of DeviceResource");
135 Log.e(TAG, e.getMessage());
139 logMessage("-----------------------------------------------------");
144 * update state of device
146 * @return device representation
148 private void updateRepresentationValues() {
150 mRepresentation.setValue(DEVICE_NAME,
151 "Intel Powered 3 door, 1 light refrigerator");
152 } catch (OcException e) {
153 Log.e(TAG, e.getMessage());
157 //******************************************************************************
158 // End of the OIC specific code
159 //******************************************************************************
160 public void logMessage(String msg) {
161 Intent intent = new Intent(FridgeServer.INTENT);
162 intent.putExtra("message", msg);
163 mContext.sendBroadcast(intent);