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