Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / LightResource.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.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
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
40 /**
41  * LightResource
42  * <p/>
43  * Creates a light resource and performs actions based on the client requests
44  */
45 public class LightResource extends Resource implements OcPlatform.EntityHandler {
46     public static final String LIGHT_STATUS_KEY = "light";
47     public static final String LIGHT_URI = "/light";
48     public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
49     private boolean mIsLightOn = false;
50
51     /**
52      * constructor
53      *
54      * @param context to enable sending of broadcast messages to be displayed on the user screen
55      */
56     LightResource(Context context) {
57         mContext = context;
58         registerLightResource();
59     }
60
61     private void registerLightResource() {
62         try {
63             logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
64             mResourceHandle = OcPlatform.registerResource(LIGHT_URI,
65                     RESOURCE_TYPELIGHT,
66                     OcPlatform.DEFAULT_INTERFACE,
67                     this,
68                     EnumSet.of(ResourceProperty.DISCOVERABLE));
69         } catch (OcException e) {
70             logMessage(TAG + "Failed to register LightResource");
71             Log.e(TAG, e.getMessage());
72         }
73         logMessage("-----------------------------------------------------");
74     }
75
76     /**
77      * this is the main method which handles different incoming requests appropriately.
78      *
79      * @param ocResourceRequest OcResourceRequest from the client
80      * @return EntityHandlerResult indicates whether the request was handled successfully or not
81      */
82     @Override
83     public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
84         EntityHandlerResult result = EntityHandlerResult.ERROR;
85         if (null != ocResourceRequest) {
86             try {
87                 if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
88                     OcResourceResponse response = new OcResourceResponse();
89                     response.setRequestHandle(ocResourceRequest.getRequestHandle());
90                     response.setResourceHandle(ocResourceRequest.getResourceHandle());
91
92                     switch (ocResourceRequest.getRequestType()) {
93                         case GET:
94                             response.setErrorCode(SUCCESS);
95                             updateRepresentationValues();
96                             response.setResourceRepresentation(mRepresentation);
97                             response.setResponseResult(EntityHandlerResult.OK);
98                             OcPlatform.sendResponse(response);
99                             result = EntityHandlerResult.OK;
100                             break;
101                         case PUT:
102                             response.setErrorCode(SUCCESS);
103                             put(ocResourceRequest.getResourceRepresentation());
104                             updateRepresentationValues();
105                             response.setResourceRepresentation(mRepresentation);
106                             response.setResponseResult(EntityHandlerResult.OK);
107                             OcPlatform.sendResponse(response);
108                             result = EntityHandlerResult.OK;
109                             break;
110                     }
111                 }
112             } catch (OcException e) {
113                 logMessage("Error in handleEntity of LightResource");
114                 Log.e(TAG, e.getMessage());
115                 return EntityHandlerResult.ERROR;
116             }
117         }
118         logMessage("-----------------------------------------------------");
119         return result;
120     }
121
122     /**
123      * updates the current state of the light (on/ off)
124      *
125      * @return light is on or off
126      */
127     private void updateRepresentationValues() {
128         try {
129             mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
130         } catch (OcException e) {
131             Log.e(TAG, e.getMessage());
132         }
133     }
134
135     /**
136      * update the value of mIsOn from the representation
137      *
138      * @param representation get current state of light
139      */
140     private void put(OcRepresentation representation) {
141         try {
142             mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
143         } catch (OcException e) {
144             Log.e(TAG, e.getMessage());
145         }
146     }
147
148     //******************************************************************************
149     // End of the OIC specific code
150     //******************************************************************************
151     private Context mContext;
152     private static String TAG = "LightResource: ";
153
154     public void logMessage(String msg) {
155         Intent intent = new Intent(FridgeServer.INTENT);
156         intent.putExtra(FridgeServer.MESSAGE, msg);
157         mContext.sendBroadcast(intent);
158     }
159 }