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 Activator.java
23 package oic.plugin.wemo;
25 import java.util.ArrayList;
26 import java.util.EnumSet;
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.ObserveType;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcResource;
33 import org.iotivity.base.OcResourceHandle;
34 import org.iotivity.base.PlatformConfig;
35 import org.iotivity.base.QualityOfService;
36 import org.iotivity.base.ResourceProperty;
37 import org.iotivity.base.ServiceType;
38 import org.osgi.framework.BundleActivator;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceReference;
42 import android.app.Activity;
43 import android.content.Context;
44 import android.os.Bundle;
45 import android.util.Log;
47 import com.belkin.wemo.localsdk.WeMoDevice;
48 import com.belkin.wemo.localsdk.WeMoSDKContext;
50 public class Activator extends Activity implements BundleActivator,
51 WeMoSDKContext.NotificationListener {
52 final private static String TAG = "SimpleClient";
54 private static BundleContext context;
55 private Context AppContext;
56 public static WeMoSDKContext mWeMoSDKContext = null;
57 WeMoDevice wemoDevice;
59 static class SmartPlug {
60 public String m_power;
69 static int OBSERVE_TYPE_TO_USE = ObserveType.OBSERVE
72 public static SmartPlug mySmartPlug;
73 public static OcResource curResource;
74 public static OcResourceHandle resourceHandle;
76 static Activity mActivity;
78 static BundleContext getContext() {
84 protected void onCreate(Bundle savedInstanceState) {
85 super.onCreate(savedInstanceState);
89 public void start(BundleContext bundleContext) throws Exception {
93 context = bundleContext;
96 * Android Application Context
98 ServiceReference<Context> ref = context
99 .getServiceReference(Context.class);
100 this.AppContext = ((Context) context.getService(ref));
105 this.mWeMoSDKContext = new WeMoSDKContext(this.AppContext);
106 this.mWeMoSDKContext.addNotificationListener(this);
109 mySmartPlug = new SmartPlug();
112 public void stop(BundleContext bundleContext) throws Exception {
113 OcPlatform.unregisterResource(resourceHandle);
115 mWeMoSDKContext = null;
120 public void onNotify(final String event, final String udn) {
121 runOnUiThread(new Runnable() {
123 Activator.this.wemoDevice = Activator.this.mWeMoSDKContext
124 .getWeMoDeviceByUDN(udn);
125 if (event.equals("refresh")) {
126 ArrayList<String> udns = Activator.this.mWeMoSDKContext
127 .getListOfWeMoDevicesOnLAN();
128 ArrayList<WeMoDevice> wemoDevices = new ArrayList();
129 for (String udn : udns) {
130 WeMoDevice listDevice = Activator.this.mWeMoSDKContext
131 .getWeMoDeviceByUDN(udn);
132 if ((listDevice != null) && (listDevice.isAvailable())) {
133 wemoDevices.add(listDevice);
134 Log.i("WeMoPlugin", "WeMo Decive Name: "
135 + listDevice.getFriendlyName());
136 Log.i("WeMoPlugin", "WeMo Decive UDN: "
137 + listDevice.getUDN());
138 Log.i("WeMoPlugin", "WeMo Decive Type: "
139 + listDevice.getType());
140 Log.i("WeMoPlugin", "WeMo Decive Serial #: "
141 + listDevice.getSerialNumber());
144 } else if (event.equals(WeMoSDKContext.ADD_DEVICE)) {
145 ArrayList<String> udns = Activator.this.mWeMoSDKContext
146 .getListOfWeMoDevicesOnLAN();
147 ArrayList<WeMoDevice> wemoDevices = new ArrayList();
148 for (String udn : udns) {
149 WeMoDevice listDevice = Activator.this.mWeMoSDKContext
150 .getWeMoDeviceByUDN(udn);
151 if ((listDevice != null) && (listDevice.isAvailable())) {
152 PlatformConfig cfg = new PlatformConfig(
155 ModeType.CLIENT_SERVER, "0.0.0.0", 0,
156 QualityOfService.LOW);
158 OcPlatform.Configure(cfg);
160 EntityHandlerWemo entitycb = new EntityHandlerWemo();
162 resourceHandle = OcPlatform
168 EnumSet.of(ResourceProperty.DISCOVERABLE));
169 } catch (OcException e) {
179 private void refresh() {
180 this.mWeMoSDKContext.refreshListOfWeMoDevicesOnLAN();