1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file EntityHandlerWemo.java
23 package oic.plugin.wemo;
25 import java.util.ArrayList;
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;
36 import android.util.Log;
38 import com.belkin.wemo.localsdk.WeMoDevice;
40 public class EntityHandlerWemo implements OcPlatform.EntityHandler {
42 final private static String TAG = "EntityHandlerWeMo";
44 public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
45 Log.e(TAG, "Entity Handler callback");
47 // OcResourceResponse response = new OcResourceResponse();
49 if (resourcerequest != null
50 && resourcerequest.getResourceUri().equals("/a/wemo")) {
51 RequestType requestType = resourcerequest.getRequestType();
52 EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
53 .getRequestHandlerFlagSet();
55 if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
57 Log.e(TAG, "requestFlag : Init");
59 if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
61 OcResourceResponse response = new OcResourceResponse();
62 OcRepresentation representation = new OcRepresentation();
63 response.setRequestHandle(resourcerequest
65 response.setResourceHandle(resourcerequest
66 .getResourceHandle());
67 switch (requestType) {
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";
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)) {
91 .getResourceRepresentation()
92 .getValueString("power")
94 Activator.mySmartPlug.m_power = "on";
95 newState = WeMoDevice.WEMO_DEVICE_ON;
96 } else if (resourcerequest
97 .getResourceRepresentation()
98 .getValueString("power")
100 Activator.mySmartPlug.m_power = "off";
101 newState = WeMoDevice.WEMO_DEVICE_OFF;
103 Activator.mWeMoSDKContext.setDeviceState(
104 newState, wemoDevice.getUDN());
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);
121 OcPlatform.sendResponse(response);
122 } catch (OcException e) {
123 // TODO Auto-generated catch block
125 return EntityHandlerResult.ERROR;
128 if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
130 Log.e(TAG, "requestFlag : Observer");
132 return EntityHandlerResult.OK;
134 return EntityHandlerResult.ERROR;