[epsonprojector] several small improvements and formatting changes (#13207)

* several small improvements and formatting changes

Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
This commit is contained in:
mlobstein
2022-08-03 09:30:42 -05:00
committed by GitHub
parent ae418a4246
commit 22f7dfe309
6 changed files with 59 additions and 33 deletions

View File

@@ -45,5 +45,4 @@ public class EpsonProjectorBindingConstants {
// Config properties
public static final String THING_PROPERTY_HOST = "host";
public static final String THING_PROPERTY_PORT = "port";
public static final String THING_PROPERTY_MAC = "macAddress";
}

View File

@@ -15,7 +15,6 @@ package org.openhab.binding.epsonprojector.internal.discovery;
import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.*;
import java.io.IOException;
import java.net.SocketException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
@@ -26,9 +25,14 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.net.NetworkAddressService;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@@ -36,7 +40,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link EpsonProjectoreDiscoveryService} class implements a service
* The {@link EpsonProjectorDiscoveryService} class implements a service
* for discovering Epson projectors using the AMX Device Discovery protocol.
*
* @author Mark Hilbush - Initial contribution
@@ -53,13 +57,21 @@ public class EpsonProjectorDiscoveryService extends AbstractDiscoveryService {
public static final int BACKGROUND_DISCOVERY_DELAY_TIMEOUT_SEC = 10;
private NetworkAddressService networkAddressService;
private final TranslationProvider translationProvider;
private final LocaleProvider localeProvider;
private final @Nullable Bundle bundle;
private boolean terminate = false;
@Activate
public EpsonProjectorDiscoveryService(@Reference NetworkAddressService networkAddressService) {
public EpsonProjectorDiscoveryService(@Reference NetworkAddressService networkAddressService,
@Reference TranslationProvider translationProvider, @Reference LocaleProvider localeProvider) {
super(SUPPORTED_THING_TYPES_UIDS, 0, BACKGROUND_DISCOVERY_ENABLED);
this.networkAddressService = networkAddressService;
this.translationProvider = translationProvider;
this.localeProvider = localeProvider;
this.bundle = FrameworkUtil.getBundle(EpsonProjectorDiscoveryService.class);
epsonDiscoveryJob = null;
terminate = false;
}
@@ -105,9 +117,6 @@ public class EpsonProjectorDiscoveryService extends AbstractDiscoveryService {
try {
String ip = networkAddressService.getPrimaryIpv4HostAddress();
epsonMulticastListener = new MulticastListener((ip != null ? ip : local));
} catch (SocketException se) {
logger.debug("Discovery job got Socket exception creating multicast socket: {}", se.getMessage());
return;
} catch (IOException ioe) {
logger.debug("Discovery job got IO exception creating multicast socket: {}", ioe.getMessage());
return;
@@ -120,7 +129,7 @@ public class EpsonProjectorDiscoveryService extends AbstractDiscoveryService {
if (thingProperties != null) {
// The MulticastListener found a projector, add it as new thing
String uid = (String) thingProperties.get(THING_PROPERTY_MAC);
String uid = (String) thingProperties.get(Thing.PROPERTY_MAC_ADDRESS);
String ipAddress = (String) thingProperties.get(THING_PROPERTY_HOST);
if (uid != null) {
@@ -128,9 +137,12 @@ public class EpsonProjectorDiscoveryService extends AbstractDiscoveryService {
ThingUID thingUid = new ThingUID(THING_TYPE_PROJECTOR_TCP, uid);
logger.trace("Creating epson projector discovery result for: {}, IP={}", uid, ipAddress);
thingDiscovered(DiscoveryResultBuilder.create(thingUid).withProperties(thingProperties)
.withLabel("Epson Projector " + uid).withRepresentationProperty(THING_PROPERTY_MAC)
.build());
thingDiscovered(
DiscoveryResultBuilder.create(thingUid).withProperties(thingProperties)
.withLabel(translationProvider.getText(bundle,
"thing-type.epsonprojector.discovery.label", "Epson Projector",
localeProvider.getLocale(), uid))
.withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build());
}
}
} catch (IOException ioe) {

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.epsonprojector.internal.discovery;
import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.DEFAULT_PORT;
import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.THING_PROPERTY_HOST;
import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.THING_PROPERTY_MAC;
import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.THING_PROPERTY_PORT;
import java.io.IOException;
@@ -31,6 +30,7 @@ import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.Thing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,9 +47,9 @@ public class MulticastListener {
private MulticastSocket socket;
// Epson projector devices announce themselves on a multicast port
private static final String EPSON_MULTICAST_GROUP = "239.255.250.250";
private static final int EPSON_MULTICAST_PORT = 9131;
// Epson projector devices announce themselves on the AMX DDD multicast port
private static final String AMX_MULTICAST_GROUP = "239.255.250.250";
private static final int AMX_MULTICAST_PORT = 9131;
// How long to wait in milliseconds for a discovery beacon
public static final int DEFAULT_SOCKET_TIMEOUT_SEC = 3000;
@@ -61,12 +61,12 @@ public class MulticastListener {
InetAddress ifAddress = InetAddress.getByName(ipv4Address);
logger.debug("Discovery job using address {} on network interface {}", ifAddress.getHostAddress(),
NetworkInterface.getByInetAddress(ifAddress).getName());
socket = new MulticastSocket(EPSON_MULTICAST_PORT);
socket = new MulticastSocket(AMX_MULTICAST_PORT);
socket.setInterface(ifAddress);
socket.setSoTimeout(DEFAULT_SOCKET_TIMEOUT_SEC);
InetAddress mcastAddress = InetAddress.getByName(EPSON_MULTICAST_GROUP);
InetAddress mcastAddress = InetAddress.getByName(AMX_MULTICAST_GROUP);
socket.joinGroup(mcastAddress);
logger.debug("Multicast listener joined multicast group {}:{}", EPSON_MULTICAST_GROUP, EPSON_MULTICAST_PORT);
logger.debug("Multicast listener joined multicast group {}:{}", AMX_MULTICAST_GROUP, AMX_MULTICAST_PORT);
}
public void shutdown() {
@@ -120,7 +120,7 @@ public class MulticastListener {
if (keyValue.length == 2 && keyValue[0].contains("UUID") && !keyValue[1].isEmpty()) {
Map<String, Object> properties = new HashMap<>();
properties.put(THING_PROPERTY_MAC, keyValue[1]);
properties.put(Thing.PROPERTY_MAC_ADDRESS, keyValue[1]);
properties.put(THING_PROPERTY_HOST, packet.getAddress().getHostAddress());
properties.put(THING_PROPERTY_PORT, DEFAULT_PORT);
return properties;

View File

@@ -426,13 +426,14 @@ public class EpsonProjectorHandler extends BaseThingHandler {
}
private void closeConnection() {
EpsonProjectorDevice remoteController = device.get();
try {
logger.debug("Closing connection to device '{}'", this.thing.getUID());
remoteController.disconnect();
updateStatus(ThingStatus.OFFLINE);
} catch (EpsonProjectorException e) {
logger.debug("Error occurred when closing connection to device '{}'", this.thing.getUID(), e);
if (device.isPresent()) {
try {
logger.debug("Closing connection to device '{}'", this.thing.getUID());
device.get().disconnect();
updateStatus(ThingStatus.OFFLINE);
} catch (EpsonProjectorException e) {
logger.debug("Error occurred when closing connection to device '{}'", this.thing.getUID(), e);
}
}
}
}

View File

@@ -5,6 +5,7 @@ binding.epsonprojector.description = This binding is compatible with Epson proje
# thing types
thing-type.epsonprojector.discovery.label = Epson Projector {0}
thing-type.epsonprojector.projector-serial.label = Epson Projector - Serial
thing-type.epsonprojector.projector-serial.description = An Epson projector which supports the ESC/VP21 protocol via a serial port connection
thing-type.epsonprojector.projector-tcp.label = Epson Projector - TCP/IP