Imported Upstream version 0.9.2
[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 import base.iotivity.org.examples.message.IMessageLogger;
43
44 /**
45  * DeviceResource
46  * <p/>
47  * Creates a device resource and performs action based on client requests
48  */
49 public class DeviceResource extends Resource implements IMessageLogger {
50     private Context mContext;
51
52     private static String TAG = "DeviceResource: ";
53
54     /**
55      * constructor
56      *
57      * @param context to enable sending of broadcast messages to be displayed on the user screen
58      */
59     DeviceResource(Context context) {
60         mContext = context;
61
62         // eventHandler for register deviceResource
63         OcPlatform.EntityHandler eh = new OcPlatform.EntityHandler() {
64             @Override
65             public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
66                 // this is where the main logic of DeviceResource is handled
67                 return entityHandler(ocResourceRequest);
68             }
69         };
70
71         try {
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());
80         }
81     }
82
83     /**
84      * update current state of device
85      *
86      * @return device representation
87      */
88     private void updateRepresentationValues() {
89         try {
90             mRepresentation.setValue(StringConstants.DEVICE_NAME,
91                     "Intel Powered 2 door, 1 light refrigerator");
92         } catch (OcException e) {
93             Log.e(TAG, e.getMessage());
94         }
95     }
96
97     /**
98      * unregister the resource
99      */
100     private void deleteDeviceResource() {
101         try {
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());
107         }
108     }
109
110     /**
111      * this is the main method which handles different incoming requests appropriately.
112      *
113      * @param request OcResourceRequest from the client
114      * @return EntityHandlerResult depending on whether the request was handled successfully or not
115      */
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 = "";
122
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);
132                 }
133             }
134
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);
141                 try {
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);
147
148                         switch (request.getRequestType()) {
149                             case GET:
150                                 response.setErrorCode(StringConstants.OK);
151                                 response.setResponseResult(EntityHandlerResult.OK);
152                                 updateRepresentationValues();
153                                 response.setResourceRepresentation(mRepresentation);
154                                 OcPlatform.sendResponse(response);
155                                 break;
156                             case DELETE:
157                                 deleteDeviceResource();
158                                 response.setErrorCode(StringConstants.OK);
159                                 response.setResponseResult(EntityHandlerResult.OK);
160                                 break;
161                         }
162                         result = EntityHandlerResult.OK;
163                     }
164                 } catch (OcException e) {
165                     logMessage(TAG + e.getMessage());
166                     Log.e(TAG, e.getMessage());
167                 }
168             }
169         }
170         return result;
171     }
172
173     @Override
174     public void logMessage(String msg) {
175         logMsg(msg);
176         if (StringConstants.ENABLE_PRINTING) {
177             Log.i(TAG, msg);
178         }
179     }
180
181     public void logMsg(final String text) {
182         Intent intent = new Intent(StringConstants.INTENT);
183         intent.putExtra("message", text);
184         mContext.sendBroadcast(intent);
185     }
186 }