]> git.basschouten.com Git - openhab-addons.git/blob
19f66258ea5f35a502efde38bff4ff37026e744a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.magentatv.internal.network;
14
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.*;
16 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.*;
17
18 import java.io.IOException;
19 import java.net.DatagramPacket;
20 import java.net.InetAddress;
21 import java.net.MulticastSocket;
22 import java.net.NetworkInterface;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.magentatv.internal.MagentaTVHandlerFactory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link MagentaTVPoweroffListener} implements a UPnP listener to detect
32  * power-off of the receiver
33  *
34  * @author Markus Michels - Initial contribution
35  */
36 @NonNullByDefault
37 public class MagentaTVPoweroffListener extends Thread {
38     private final Logger logger = LoggerFactory.getLogger(MagentaTVPoweroffListener.class);
39
40     private final MagentaTVHandlerFactory handlerFactory;
41
42     public static final String UPNP_MULTICAST_ADDRESS = "239.255.255.250";
43     public static final int UPNP_PORT = 1900;
44     public static final String UPNP_BYEBYE_MESSAGE = "ssdp:byebye";
45
46     protected final MulticastSocket socket;
47     protected @Nullable NetworkInterface networkInterface;
48     protected byte[] buf = new byte[256];
49
50     public MagentaTVPoweroffListener(MagentaTVHandlerFactory handlerFactory,
51             @Nullable NetworkInterface networkInterface) throws IOException {
52         setName("OH-Binding-magentatv-upnp-listener");
53         setDaemon(true);
54
55         this.handlerFactory = handlerFactory;
56         this.networkInterface = networkInterface;
57         socket = new MulticastSocket(UPNP_PORT);
58     }
59
60     @Override
61     public void start() {
62         if (!isStarted()) {
63             logger.debug("Listening to SSDP shutdown messages");
64             super.start();
65         }
66     }
67
68     /**
69      * Listening thread. Receive SSDP multicast packets and filter for byebye If
70      * such a packet is received the handlerFactory is called, which then dispatches
71      * the event to the thing handler.
72      */
73     @Override
74     public void run() {
75         try {
76             logger.debug("SSDP listener started");
77             socket.setReceiveBufferSize(1024);
78             socket.setReuseAddress(true);
79
80             // Join the Multicast group on the selected network interface
81             socket.setNetworkInterface(networkInterface);
82             InetAddress group = InetAddress.getByName(UPNP_MULTICAST_ADDRESS);
83             socket.joinGroup(group);
84
85             // read the SSDP messages
86             while (!socket.isClosed()) {
87                 DatagramPacket packet = new DatagramPacket(buf, buf.length);
88                 socket.receive(packet);
89                 String message = new String(packet.getData(), 0, packet.getLength());
90                 try {
91                     String ipAddress = substringAfter(packet.getAddress().toString(), "/");
92                     if (message.contains("NTS: ")) {
93                         String ssdpMsg = substringBetween(message, "NTS: ", "\r");
94                         if (ssdpMsg != null) {
95                             if (message.contains(MR400_DEF_DESCRIPTION_URL)
96                                     || message.contains(MR401B_DEF_DESCRIPTION_URL)) {
97                                 if (ssdpMsg.contains(UPNP_BYEBYE_MESSAGE)) {
98                                     handlerFactory.onPowerOff(ipAddress);
99                                 }
100                             }
101                         }
102                     }
103                 } catch (RuntimeException e) {
104                     logger.debug("Unable to process SSDP message: {}", message);
105                 }
106             }
107         } catch (IOException | RuntimeException e) {
108             logger.debug("Poweroff listener failure: {}", e.getMessage());
109         } finally {
110             close();
111         }
112     }
113
114     public boolean isStarted() {
115         return socket.isBound();
116     }
117
118     /**
119      * Make sure the socket gets closed
120      */
121     public void close() {
122         if (isStarted()) {
123             logger.debug("No longer listening to SSDP messages");
124             if (!socket.isClosed()) {
125                 socket.close();
126             }
127         }
128     }
129
130     /**
131      * Make sure the socket gets closed
132      */
133     public void dispose() {
134         logger.debug("SSDP listener terminated");
135         close();
136     }
137 }