1 package example.binarylight;
2
3 import org.fourthline.cling.UpnpService;
4 import org.fourthline.cling.UpnpServiceImpl;
5 import org.fourthline.cling.binding.*;
6 import org.fourthline.cling.binding.annotations.*;
7 import org.fourthline.cling.model.*;
8 import org.fourthline.cling.model.meta.*;
9 import org.fourthline.cling.model.types.*;
10
11 import java.io.IOException;
12
13 public class BinaryLightServer implements Runnable {
14
15 public static void main(String[] args) throws Exception {
16
17 Thread serverThread = new Thread(new BinaryLightServer());
18 serverThread.setDaemon(false);
19 serverThread.start();
20 }
21
22 public void run() {
23 try {
24
25 final UpnpService upnpService = new UpnpServiceImpl();
26
27 Runtime.getRuntime().addShutdownHook(new Thread() {
28 @Override
29 public void run() {
30 upnpService.shutdown();
31 }
32 });
33
34
35 upnpService.getRegistry().addDevice(
36 createDevice()
37 );
38
39 } catch (Exception ex) {
40 System.err.println("Exception occured: " + ex);
41 ex.printStackTrace(System.err);
42 System.exit(1);
43 }
44 }
45
46
47 LocalDevice createDevice()
48 throws ValidationException, LocalServiceBindingException, IOException {
49
50 DeviceIdentity identity =
51 new DeviceIdentity(
52 UDN.uniqueSystemIdentifier("Demo Binary Light")
53 );
54
55 DeviceType type =
56 new UDADeviceType("BinaryLight", 1);
57
58 DeviceDetails details =
59 new DeviceDetails(
60 "Friendly Binary Light",
61 new ManufacturerDetails("ACME"),
62 new ModelDetails(
63 "BinLight2000",
64 "A demo light with on/off switch.",
65 "v1"
66 )
67 );
68
69 Icon icon =
70 new Icon(
71 "image/png", 48, 48, 8,
72 getClass().getResource("icon.png")
73 );
74
75 LocalService<SwitchPower> switchPowerService =
76 new AnnotationLocalServiceBinder().read(SwitchPower.class);
77
78 switchPowerService.setManager(
79 new DefaultServiceManager(switchPowerService, SwitchPower.class)
80 );
81
82 return new LocalDevice(identity, type, details, icon, switchPowerService);
83
84
85
86
87
88
89
90
91 }
92
93 }