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