332abba18d06fb718fe2119f32808d82dfb8fcd6
[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 /**
41  * DoorResource
42  * <p/>
43  * Creates a door resource and performs actions based on the client requests
44  */
45 public class DoorResource extends Resource implements OcPlatform.EntityHandler {
46     public static final String DOOR_URI = "/door/";
47     public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
48     public static final String DOOR_STATE_KEY = "state";
49     public static final String DOOR_SIDE_KEY = "side";
50     private boolean mDoorState;
51     private String mSide;
52
53     /**
54      * Constructor
55      *
56      * @param side    side of the door
57      * @param context to enable sending of broadcast messages to be displayed on the user screen
58      */
59     DoorResource(String side, Context context) {
60         mContext = context;
61         mSide = side;
62         registerDoorResource();
63     }
64
65     private void registerDoorResource() {
66         String resourceURI = DOOR_URI + mSide;
67         logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
68         try {
69             mResourceHandle = OcPlatform.registerResource(resourceURI,
70                     RESOURCE_TYPEDOOR,
71                     OcPlatform.DEFAULT_INTERFACE,
72                     this,
73                     EnumSet.of(ResourceProperty.DISCOVERABLE));
74         } catch (OcException e) {
75             logMessage(TAG + "Failed to register DoorResource");
76             Log.e(TAG, e.getMessage());
77         }
78     }
79
80     /**
81      * this is the main method which handles different incoming requests appropriately.
82      *
83      * @param ocResourceRequest OcResourceRequest from the client
84      * @return EntityHandlerResult indicates whether the request was handled successfully or not
85      */
86     @Override
87     public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
88         EntityHandlerResult result = EntityHandlerResult.ERROR;
89         if (null != ocResourceRequest) {
90             try {
91                 if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
92                     OcResourceResponse response = new OcResourceResponse();
93                     response.setRequestHandle(ocResourceRequest.getRequestHandle());
94                     response.setResourceHandle(ocResourceRequest.getResourceHandle());
95
96                     switch (ocResourceRequest.getRequestType()) {
97                         case GET:
98                             response.setErrorCode(SUCCESS);
99                             updateRepresentationValues();
100                             response.setResourceRepresentation(mRepresentation);
101                             response.setResponseResult(EntityHandlerResult.OK);
102                             OcPlatform.sendResponse(response);
103                             break;
104                         case PUT:
105                             response.setErrorCode(SUCCESS);
106                             put(ocResourceRequest.getResourceRepresentation());
107                             updateRepresentationValues();
108                             response.setResourceRepresentation(mRepresentation);
109                             response.setResponseResult(EntityHandlerResult.OK);
110                             OcPlatform.sendResponse(response);
111                             break;
112                         case DELETE:
113                             response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
114                             response.setErrorCode(204);
115                             OcPlatform.sendResponse(response);
116                             break;
117                     }
118                     result = EntityHandlerResult.OK;
119                 }
120             } catch (OcException e) {
121                 logMessage("Error in handleEntity of DoorResource");
122                 Log.e(TAG, e.getMessage());
123                 return EntityHandlerResult.ERROR;
124             }
125         }
126         logMessage("-----------------------------------------------------");
127         return result;
128     }
129
130     /**
131      * helper function to update the current value of the door resource
132      */
133     private void updateRepresentationValues() {
134         try {
135             mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
136             mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
137             logMessage(TAG + "door state is  " + ((mDoorState == true) ? "open" : "close") +
138                     " and door side is " + mSide);
139         } catch (OcException e) {
140             Log.e(TAG, e.getMessage());
141         }
142     }
143
144     /**
145      * update the value of doorResource, depending on if door is open/ closed
146      *
147      * @param representation new state of a door
148      */
149     private void put(OcRepresentation representation) {
150         try {
151             mDoorState = representation.getValue(DOOR_STATE_KEY);
152         } catch (OcException e) {
153             Log.e(TAG, e.getMessage());
154         }
155         // Note, we won't let the user change the door side!
156     }
157
158     //******************************************************************************
159     // End of the OIC specific code
160     //******************************************************************************
161     private Context mContext;
162     private static String TAG = "DoorResource: ";
163
164     public void logMessage(String msg) {
165         Intent intent = new Intent(FridgeServer.INTENT);
166         intent.putExtra(FridgeServer.MESSAGE, msg);
167         mContext.sendBroadcast(intent);
168     }
169 }