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;
26 import java.util.EnumSet;
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;
37 import android.util.Log;
39 import com.belkin.wemo.localsdk.WeMoDevice;
41 public class EntityHandlerWemo implements OcPlatform.EntityHandler {
43 final private static String TAG = "EntityHandlerWeMo";
45 public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
46 Log.e(TAG, "Entity Handler callback");
48 // OcResourceResponse response = new OcResourceResponse();
50 if (resourcerequest != null
51 && resourcerequest.getResourceUri().equals("/a/wemo")) {
52 RequestType requestType = resourcerequest.getRequestType();
53 EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
54 .getRequestHandlerFlagSet();
56 if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
58 Log.e(TAG, "requestFlag : Init");
60 if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
62 OcResourceResponse response = new OcResourceResponse();
63 OcRepresentation representation = new OcRepresentation();
64 response.setRequestHandle(resourcerequest
66 response.setResourceHandle(resourcerequest
67 .getResourceHandle());
68 switch (requestType) {
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";
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)) {
93 .getResourceRepresentation()
96 Activator.mySmartPlug.m_power = "on";
97 newState = WeMoDevice.WEMO_DEVICE_ON;
98 } else if (resourcerequest
99 .getResourceRepresentation()
102 Activator.mySmartPlug.m_power = "off";
103 newState = WeMoDevice.WEMO_DEVICE_OFF;
105 } catch (OcException e) {
106 // TODO Auto-generated catch block
107 Log.e(TAG, e.getMessage());
110 Activator.mWeMoSDKContext.setDeviceState(
111 newState, wemoDevice.getUDN());
118 response.setErrorCode(200);
119 // representation.setUri("/a/wemo");
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());
132 response.setResourceRepresentation(representation);
134 OcPlatform.sendResponse(response);
135 } catch (OcException e) {
136 // TODO Auto-generated catch block
138 return EntityHandlerResult.ERROR;
141 if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
143 Log.e(TAG, "requestFlag : Observer");
145 return EntityHandlerResult.OK;
147 return EntityHandlerResult.ERROR;