Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / Android / plugin.wemo / src / oic / plugin / wemo / EntityHandlerWemo.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file EntityHandlerWemo.java
22
23 package oic.plugin.wemo;
24
25 import java.util.ArrayList;
26 import java.util.EnumSet;
27
28 import org.iotivity.base.EntityHandlerResult;
29 import org.iotivity.base.OcException;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcRepresentation;
32 import org.iotivity.base.OcResourceRequest;
33 import org.iotivity.base.OcResourceResponse;
34 import org.iotivity.base.RequestHandlerFlag;
35 import org.iotivity.base.RequestType;
36
37 import android.util.Log;
38
39 import com.belkin.wemo.localsdk.WeMoDevice;
40
41 public class EntityHandlerWemo implements OcPlatform.EntityHandler {
42
43     final private static String TAG = "EntityHandlerWeMo";
44
45     public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
46         Log.e(TAG, "Entity Handler callback");
47
48         // OcResourceResponse response = new OcResourceResponse();
49
50         if (resourcerequest != null
51                 && resourcerequest.getResourceUri().equals("/a/wemo")) {
52             RequestType requestType = resourcerequest.getRequestType();
53             EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
54                     .getRequestHandlerFlagSet();
55
56             if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
57             {
58                 Log.e(TAG, "requestFlag : Init");
59             }
60             if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
61             {
62                 OcResourceResponse response = new OcResourceResponse();
63                 OcRepresentation representation = new OcRepresentation();
64                 response.setRequestHandle(resourcerequest
65                         .getRequestHandle());
66                 response.setResourceHandle(resourcerequest
67                         .getResourceHandle());
68                 switch (requestType) {
69                     case GET:
70                         ArrayList<String> udns = Activator.mWeMoSDKContext
71                                 .getListOfWeMoDevicesOnLAN();
72                         for (String udn : udns) {
73                             WeMoDevice wemoDevice = Activator.mWeMoSDKContext
74                                     .getWeMoDeviceByUDN(udn);
75                             if (wemoDevice.getState() == "0") {
76                                 Activator.mySmartPlug.m_power = "off";
77                             } else if (wemoDevice.getState() == "1") {
78                                 Activator.mySmartPlug.m_power = "on";
79                             }
80                         }
81                         break;
82                     case PUT:
83                         ArrayList<String> uudns = Activator.mWeMoSDKContext
84                                 .getListOfWeMoDevicesOnLAN();
85                         for (String udn : uudns) {
86                             WeMoDevice wemoDevice = Activator.mWeMoSDKContext
87                                     .getWeMoDeviceByUDN(udn);
88                             String type = wemoDevice.getType();
89                             if (type.equals(WeMoDevice.SWITCH)) {
90                                 String newState = "";
91                                 try {
92                                     if (resourcerequest
93                                             .getResourceRepresentation()
94                                             .getValue("power")
95                                             .equals("on")) {
96                                         Activator.mySmartPlug.m_power = "on";
97                                         newState = WeMoDevice.WEMO_DEVICE_ON;
98                                     } else if (resourcerequest
99                                                 .getResourceRepresentation()
100                                                 .getValue("power")
101                                                 .equals("off")) {
102                                             Activator.mySmartPlug.m_power = "off";
103                                             newState = WeMoDevice.WEMO_DEVICE_OFF;
104                                         }
105                                     } catch (OcException e) {
106                                         // TODO Auto-generated catch block
107                                         Log.e(TAG, e.getMessage());
108                                     }
109
110                                 Activator.mWeMoSDKContext.setDeviceState(
111                                         newState, wemoDevice.getUDN());
112                             }
113                         }
114                         break;
115                     case POST:
116                         break;
117                 }
118                 response.setErrorCode(200);
119                 // representation.setUri("/a/wemo");
120                 try {
121                     representation.setValue("name",
122                             Activator.mySmartPlug.m_name);
123                     representation.setValue("power",
124                             Activator.mySmartPlug.m_power);
125                     representation.setValue("brightness", 0);
126                     representation.setValue("color", 0);
127                 } catch (OcException e) {
128                     // TODO Auto-generated catch block
129                     Log.e(TAG, e.getMessage());
130                 }
131                 
132                 response.setResourceRepresentation(representation);
133                 try {
134                     OcPlatform.sendResponse(response);
135                 } catch (OcException e) {
136                     // TODO Auto-generated catch block
137                     e.printStackTrace();
138                     return EntityHandlerResult.ERROR;
139                 }
140             }
141             if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
142             {
143                 Log.e(TAG, "requestFlag : Observer");
144             }
145             return EntityHandlerResult.OK;
146         }
147         return EntityHandlerResult.ERROR;
148     }
149 }