Imported Upstream version 0.9.1
[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
27 import org.iotivity.base.EntityHandlerResult;
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcPlatform;
30 import org.iotivity.base.OcRepresentation;
31 import org.iotivity.base.OcResourceRequest;
32 import org.iotivity.base.OcResourceResponse;
33 import org.iotivity.base.RequestHandlerFlag;
34 import org.iotivity.base.RequestType;
35
36 import android.util.Log;
37
38 import com.belkin.wemo.localsdk.WeMoDevice;
39
40 public class EntityHandlerWemo implements OcPlatform.EntityHandler {
41
42     final private static String TAG = "EntityHandlerWeMo";
43
44     public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
45         Log.e(TAG, "Entity Handler callback");
46
47         // OcResourceResponse response = new OcResourceResponse();
48
49         if (resourcerequest != null
50                 && resourcerequest.getResourceUri().equals("/a/wemo")) {
51             RequestType requestType = resourcerequest.getRequestType();
52             EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
53                     .getRequestHandlerFlagSet();
54
55             if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
56             {
57                 Log.e(TAG, "requestFlag : Init");
58             }
59             if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
60             {
61                 OcResourceResponse response = new OcResourceResponse();
62                 OcRepresentation representation = new OcRepresentation();
63                 response.setRequestHandle(resourcerequest
64                         .getRequestHandle());
65                 response.setResourceHandle(resourcerequest
66                         .getResourceHandle());
67                 switch (requestType) {
68                     case GET:
69                         ArrayList<String> udns = Activator.mWeMoSDKContext
70                                 .getListOfWeMoDevicesOnLAN();
71                         for (String udn : udns) {
72                             WeMoDevice wemoDevice = Activator.mWeMoSDKContext
73                                     .getWeMoDeviceByUDN(udn);
74                             if (wemoDevice.getState() == "0") {
75                                 Activator.mySmartPlug.m_power = "off";
76                             } else if (wemoDevice.getState() == "1") {
77                                 Activator.mySmartPlug.m_power = "on";
78                             }
79                         }
80                         break;
81                     case PUT:
82                         ArrayList<String> uudns = Activator.mWeMoSDKContext
83                                 .getListOfWeMoDevicesOnLAN();
84                         for (String udn : uudns) {
85                             WeMoDevice wemoDevice = Activator.mWeMoSDKContext
86                                     .getWeMoDeviceByUDN(udn);
87                             String type = wemoDevice.getType();
88                             if (type.equals(WeMoDevice.SWITCH)) {
89                                 String newState = "";
90                                 if (resourcerequest
91                                         .getResourceRepresentation()
92                                         .getValueString("power")
93                                         .equals("on")) {
94                                     Activator.mySmartPlug.m_power = "on";
95                                     newState = WeMoDevice.WEMO_DEVICE_ON;
96                                 } else if (resourcerequest
97                                         .getResourceRepresentation()
98                                         .getValueString("power")
99                                         .equals("off")) {
100                                     Activator.mySmartPlug.m_power = "off";
101                                     newState = WeMoDevice.WEMO_DEVICE_OFF;
102                                 }
103                                 Activator.mWeMoSDKContext.setDeviceState(
104                                         newState, wemoDevice.getUDN());
105                             }
106                         }
107                         break;
108                     case POST:
109                         break;
110                 }
111                 response.setErrorCode(200);
112                 // representation.setUri("/a/wemo");
113                 representation.setValueString("name",
114                         Activator.mySmartPlug.m_name);
115                 representation.setValueString("power",
116                         Activator.mySmartPlug.m_power);
117                 representation.setValueInt("brightness", 0);
118                 representation.setValueInt("color", 0);
119                 response.setResourceRepresentation(representation);
120                 try {
121                     OcPlatform.sendResponse(response);
122                 } catch (OcException e) {
123                     // TODO Auto-generated catch block
124                     e.printStackTrace();
125                     return EntityHandlerResult.ERROR;
126                 }
127             }
128             if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
129             {
130                 Log.e(TAG, "requestFlag : Observer");
131             }
132             return EntityHandlerResult.OK;
133         }
134         return EntityHandlerResult.ERROR;
135     }
136 }