2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.magentatv.internal.network;
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.*;
16 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.*;
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;
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;
31 * The {@link MagentaTVPoweroffListener} implements a UPnP listener to detect
32 * power-off of the receiver
34 * @author Markus Michels - Initial contribution
37 public class MagentaTVPoweroffListener extends Thread {
38 private final Logger logger = LoggerFactory.getLogger(MagentaTVPoweroffListener.class);
40 private final MagentaTVHandlerFactory handlerFactory;
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";
46 protected final MulticastSocket socket;
47 protected @Nullable NetworkInterface networkInterface;
48 protected byte[] buf = new byte[256];
50 public MagentaTVPoweroffListener(MagentaTVHandlerFactory handlerFactory,
51 @Nullable NetworkInterface networkInterface) throws IOException {
52 setName("OH-Binding-magentatv-upnp-listener");
55 this.handlerFactory = handlerFactory;
56 this.networkInterface = networkInterface;
57 socket = new MulticastSocket(UPNP_PORT);
63 logger.debug("Listening to SSDP shutdown messages");
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.
76 logger.debug("SSDP listener started");
77 socket.setReceiveBufferSize(1024);
78 socket.setReuseAddress(true);
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);
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());
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);
103 } catch (RuntimeException e) {
104 logger.debug("Unable to process SSDP message: {}", message);
107 } catch (IOException | RuntimeException e) {
108 logger.debug("Poweroff listener failure: {}", e.getMessage());
114 public boolean isStarted() {
115 return socket.isBound();
119 * Make sure the socket gets closed
121 public void close() {
123 logger.debug("No longer listening to SSDP messages");
124 if (!socket.isClosed()) {
131 * Make sure the socket gets closed
133 public void dispose() {
134 logger.debug("SSDP listener terminated");