Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / DeviceResource.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base.examples.fridgeserver;
24
25 import android.content.Context;
26 import android.content.Intent;
27 import android.util.Log;
28
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;
37
38 import java.util.EnumSet;
39 import java.util.LinkedList;
40 import java.util.List;
41
42 /**
43  * DeviceResource
44  * <p/>
45  * Creates a device resource and performs action based on client requests
46  */
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;
57
58     private Context mContext;
59
60     /**
61      * constructor
62      *
63      * @param context to enable sending of broadcast messages to be displayed on the user screen
64      */
65     DeviceResource(Context context) {
66         mContext = context;
67         registerDeviceResource();
68     }
69
70     private void registerDeviceResource() {
71         try {
72             logMessage("RegisterDeviceResource " + DEVICE_URI + " : " + RESOURCE_TYPENAME);
73             mResourceHandle = OcPlatform.registerResource(
74                     DEVICE_URI,
75                     RESOURCE_TYPENAME,
76                     OcPlatform.DEFAULT_INTERFACE,
77                     this,
78                     EnumSet.of(ResourceProperty.DISCOVERABLE));
79         } catch (OcException e) {
80             logMessage(TAG + "Failed to register DeviceResource");
81             Log.e(TAG, e.getMessage());
82         }
83     }
84
85     /**
86      * this is the main method which handles different incoming requests appropriately.
87      *
88      * @param ocResourceRequest OcResourceRequest from the client
89      * @return EntityHandlerResult indicates whether the request was handled successfully or not
90      */
91     @Override
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);
107                 }
108             }
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,
113                         API_VERSION);
114                 serverHeaderOptions.add(apiVersion);
115                 try {
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);
121
122                         switch (ocResourceRequest.getRequestType()) {
123                             case GET:
124                                 response.setErrorCode(SUCCESS);
125                                 response.setResponseResult(EntityHandlerResult.OK);
126                                 updateRepresentationValues();
127                                 response.setResourceRepresentation(mRepresentation);
128                                 OcPlatform.sendResponse(response);
129                                 break;
130                         }
131                         result = EntityHandlerResult.OK;
132                     }
133                 } catch (OcException e) {
134                     logMessage("Error in handleEntity of DeviceResource");
135                     Log.e(TAG, e.getMessage());
136                 }
137             }
138         }
139         logMessage("-----------------------------------------------------");
140         return result;
141     }
142
143     /**
144      * update state of device
145      *
146      * @return device representation
147      */
148     private void updateRepresentationValues() {
149         try {
150             mRepresentation.setValue(DEVICE_NAME,
151                     "Intel Powered 3 door, 1 light refrigerator");
152         } catch (OcException e) {
153             Log.e(TAG, e.getMessage());
154         }
155     }
156
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);
164     }
165 }