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