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