blob: 46676bfa2ca3d54ca5f5ed3615298619fb08ef1f [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Francesco Furfarid1072b52006-05-03 07:44:48 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid1072b52006-05-03 07:44:48 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Francesco Furfarid1072b52006-05-03 07:44:48 +000018 */
19
20package org.apache.felix.upnp.sample.binaryLight;
21
Francesco Furfari63e117d2006-10-10 11:54:58 +000022import java.net.InetAddress;
23import java.net.UnknownHostException;
Francesco Furfarid1072b52006-05-03 07:44:48 +000024import java.util.Dictionary;
25import java.util.Properties;
26
27import org.osgi.framework.BundleContext;
28import org.osgi.service.upnp.UPnPDevice;
29import org.osgi.service.upnp.UPnPIcon;
30import org.osgi.service.upnp.UPnPService;
31
32/*
Karl Paulsd312acc2007-06-18 20:38:33 +000033* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarid1072b52006-05-03 07:44:48 +000034*/
35
36public class LightDevice implements UPnPDevice {
37
38 final private String DEVICE_ID = "uuid:Felix-BinaryLight";
39 private BundleContext context;
40 private LightModel model;
41 private LightUI ui;
42 private PowerSwitchService powerSwitch;
43 private UPnPService[] services;
44 private Dictionary dictionary;
45 private UPnPEventNotifier notifier;
46
47 public LightDevice(BundleContext context) {
48 this.context=context;
49 model = new LightModel();
50 ui = new LightUI(model);
51 powerSwitch = new PowerSwitchService(model);
52 services = new UPnPService[]{powerSwitch};
53 setupDeviceProperties();
54 buildEventNotifyer();
55 }
56
Francesco Furfari63e117d2006-10-10 11:54:58 +000057 public LightModel getModel(){
58 return model;
59 }
Francesco Furfarid1072b52006-05-03 07:44:48 +000060 /**
61 *
62 */
63 private void buildEventNotifyer() {
64 notifier = new UPnPEventNotifier(context,DEVICE_ID,powerSwitch,model);
65 }
66
67 private void setupDeviceProperties(){
68 dictionary = new Properties();
69 dictionary.put(UPnPDevice.UPNP_EXPORT,"");
70 dictionary.put(
71 org.osgi.service
72 .device.Constants.DEVICE_CATEGORY,
73 new String[]{UPnPDevice.DEVICE_CATEGORY}
74 );
75 //dictionary.put(UPnPDevice.DEVICE_CATEGORY,new String[]{UPnPDevice.DEVICE_CATEGORY});
76 dictionary.put(UPnPDevice.FRIENDLY_NAME,"Felix OSGi-UPnP BinaryLight");
77 dictionary.put(UPnPDevice.MANUFACTURER,"Apache Software Foundation");
78 dictionary.put(UPnPDevice.MANUFACTURER_URL,"http://incubator.apache.org/felix/");
79 dictionary.put(UPnPDevice.MODEL_DESCRIPTION,"A BinaryLight device to test OSGi to UPnP service export");
80 dictionary.put(UPnPDevice.MODEL_NAME,"Lucciola");
81 dictionary.put(UPnPDevice.MODEL_NUMBER,"1.0");
82 dictionary.put(UPnPDevice.MODEL_URL,"http://incubator.apache.org/felix/lucciola");
Francesco Furfari63e117d2006-10-10 11:54:58 +000083 String port = context.getProperty("org.osgi.service.http.port");
84 InetAddress inet;
85 try {
86 inet = InetAddress.getLocalHost();
87 String hostname = inet.getHostName();
88 //String hostname = inet.getHostAddress();
89 dictionary.put(UPnPDevice.PRESENTATION_URL,"http://"+hostname + ":"+port+"/upnp/binaryLight/");
90 } catch (UnknownHostException e) {
91 System.out.println("Warning: enable to cacth localhost name");
92 }
Francesco Furfarid1072b52006-05-03 07:44:48 +000093 dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");
94 dictionary.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:BinaryLight:1");
95 dictionary.put(UPnPDevice.UDN,DEVICE_ID);
96 //dictionary.put(UPnPDevice.ID,dictionary.get(UPnPDevice.UDN));
97 dictionary.put(UPnPDevice.UPC,"1213456789");
98 }
99
100
101 /* (non-Javadoc)
102 * @see org.osgi.service.upnp.UPnPDevice#getService(java.lang.String)
103 */
104 public UPnPService getService(String serviceId) {
105 if (serviceId.equals(powerSwitch.getId())) return powerSwitch;
106 return null;
107 }
108
109 /* (non-Javadoc)
110 * @see org.osgi.service.upnp.UPnPDevice#getServices()
111 */
112 public UPnPService[] getServices() {
113 return services;
114 }
115
116 /* (non-Javadoc)
117 * @see org.osgi.service.upnp.UPnPDevice#getIcons(java.lang.String)
118 */
119 public UPnPIcon[] getIcons(String locale) {
120 UPnPIcon icon = new LightIcon();
121 return new UPnPIcon[]{icon} ;
122 }
123
124 /* (non-Javadoc)
125 * @see org.osgi.service.upnp.UPnPDevice#getDescriptions(java.lang.String)
126 */
127 public Dictionary getDescriptions(String locale) {
128 return dictionary;
129 }
130
131 /**
132 *
133 */
134 public void close() {
135 ui.dispose();
136 notifier.destroy();
137 }
138
139}