Refactor and fix (#17394)

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2024-09-18 20:31:47 +02:00 committed by GitHub
parent 5a3a0c0ea2
commit 8d552bf3ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 35 additions and 21 deletions

View File

@ -14,6 +14,8 @@ package org.openhab.binding.smaenergymeter.internal;
import static org.openhab.binding.smaenergymeter.internal.SMAEnergyMeterBindingConstants.*; import static org.openhab.binding.smaenergymeter.internal.SMAEnergyMeterBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.smaenergymeter.internal.handler.SMAEnergyMeterHandler; import org.openhab.binding.smaenergymeter.internal.handler.SMAEnergyMeterHandler;
import org.openhab.binding.smaenergymeter.internal.packet.PacketListenerRegistry; import org.openhab.binding.smaenergymeter.internal.packet.PacketListenerRegistry;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -31,6 +33,7 @@ import org.osgi.service.component.annotations.Reference;
* *
* @author Osman Basha - Initial contribution * @author Osman Basha - Initial contribution
*/ */
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.smaenergymeter") @Component(service = ThingHandlerFactory.class, configurationPid = "binding.smaenergymeter")
public class SMAEnergyMeterHandlerFactory extends BaseThingHandlerFactory { public class SMAEnergyMeterHandlerFactory extends BaseThingHandlerFactory {
@ -47,7 +50,7 @@ public class SMAEnergyMeterHandlerFactory extends BaseThingHandlerFactory {
} }
@Override @Override
protected ThingHandler createHandler(Thing thing) { protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE_ENERGY_METER)) { if (thingTypeUID.equals(THING_TYPE_ENERGY_METER)) {

View File

@ -79,7 +79,12 @@ public class SMAEnergyMeterDiscoveryService extends AbstractDiscoveryService imp
return; return;
} }
packetListener.addPayloadHandler(this); try {
packetListener.addPayloadHandler(this);
} catch (IOException e) {
logger.warn("Could not start background discovery, unable to add PayloadHandler", e);
return;
}
this.packetListener = packetListener; this.packetListener = packetListener;
} }

View File

@ -16,6 +16,7 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
/** /**
@ -26,11 +27,12 @@ import org.openhab.core.library.types.DecimalType;
* @author Łukasz Dywicki - Extracted multicast group handling to * @author Łukasz Dywicki - Extracted multicast group handling to
* {@link org.openhab.binding.smaenergymeter.internal.packet.PacketListener}. * {@link org.openhab.binding.smaenergymeter.internal.packet.PacketListener}.
*/ */
@NonNullByDefault
public class EnergyMeter { public class EnergyMeter {
private static final byte[] E_METER_PROTOCOL_ID = new byte[] { 0x60, 0x69 }; private static final byte[] E_METER_PROTOCOL_ID = new byte[] { 0x60, 0x69 };
private String serialNumber; private String serialNumber = "";
private final FieldDTO powerIn; private final FieldDTO powerIn;
private final FieldDTO energyIn; private final FieldDTO energyIn;
private final FieldDTO powerOut; private final FieldDTO powerOut;

View File

@ -15,8 +15,10 @@ package org.openhab.binding.smaenergymeter.internal.handler;
import static org.openhab.binding.smaenergymeter.internal.SMAEnergyMeterBindingConstants.*; import static org.openhab.binding.smaenergymeter.internal.SMAEnergyMeterBindingConstants.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.smaenergymeter.internal.configuration.EnergyMeterConfig; import org.openhab.binding.smaenergymeter.internal.configuration.EnergyMeterConfig;
import org.openhab.binding.smaenergymeter.internal.packet.FilteringPayloadHandler; import org.openhab.binding.smaenergymeter.internal.packet.FilteringPayloadHandler;
@ -40,13 +42,14 @@ import org.slf4j.LoggerFactory;
* *
* @author Osman Basha - Initial contribution * @author Osman Basha - Initial contribution
*/ */
@NonNullByDefault
public class SMAEnergyMeterHandler extends BaseThingHandler implements PayloadHandler { public class SMAEnergyMeterHandler extends BaseThingHandler implements PayloadHandler {
private final Logger logger = LoggerFactory.getLogger(SMAEnergyMeterHandler.class); private final Logger logger = LoggerFactory.getLogger(SMAEnergyMeterHandler.class);
private final PacketListenerRegistry listenerRegistry; private final PacketListenerRegistry listenerRegistry;
private @Nullable PacketListener listener; private @Nullable PacketListener listener;
private @Nullable PayloadHandler handler; private @Nullable PayloadHandler handler;
private String serialNumber; private String serialNumber = "";
public SMAEnergyMeterHandler(Thing thing, PacketListenerRegistry listenerRegistry) { public SMAEnergyMeterHandler(Thing thing, PacketListenerRegistry listenerRegistry) {
super(thing); super(thing);
@ -73,8 +76,8 @@ public class SMAEnergyMeterHandler extends BaseThingHandler implements PayloadHa
EnergyMeterConfig config = getConfigAs(EnergyMeterConfig.class); EnergyMeterConfig config = getConfigAs(EnergyMeterConfig.class);
try { try {
serialNumber = config.getSerialNumber(); serialNumber = Objects.requireNonNullElse(config.getSerialNumber(), "");
if (serialNumber == null) { if (serialNumber.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"Meter serial number missing"); "Meter serial number missing");
return; return;

View File

@ -64,7 +64,9 @@ public class DefaultPacketListenerRegistry implements PacketListenerRegistry {
logger.warn("Multicast socket {} failed to terminate", entry.getKey(), e); logger.warn("Multicast socket {} failed to terminate", entry.getKey(), e);
} }
} }
scheduler.shutdownNow(); if (!scheduler.isShutdown()) {
scheduler.shutdownNow();
}
} }
public ScheduledFuture<?> addTask(ReceivingTask runnable) { public ScheduledFuture<?> addTask(ReceivingTask runnable) {

View File

@ -16,6 +16,7 @@ import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -55,7 +56,7 @@ public class PacketListener {
this.port = port; this.port = port;
} }
public void addPayloadHandler(PayloadHandler handler) { public void addPayloadHandler(PayloadHandler handler) throws IOException {
if (handlers.isEmpty()) { if (handlers.isEmpty()) {
open(); open();
} }
@ -75,22 +76,19 @@ public class PacketListener {
return socket != null && socket.isConnected(); return socket != null && socket.isConnected();
} }
private void open() { private void open() throws IOException {
if (isOpen()) { if (isOpen()) {
// no need to bind socket second time // no need to bind socket second time
return; return;
} }
try { MulticastSocket socket = new MulticastSocket(port);
MulticastSocket socket = new MulticastSocket(port); socket.setSoTimeout(5000);
socket.setSoTimeout(5000); InetAddress mcastGroupAddress = InetAddress.getByName(multicastGroup);
InetAddress address = InetAddress.getByName(multicastGroup); InetSocketAddress socketAddress = new InetSocketAddress(mcastGroupAddress, port);
socket.joinGroup(address); socket.joinGroup(socketAddress, null);
future = registry.addTask(new ReceivingTask(socket, multicastGroup + ":" + port, handlers)); future = registry.addTask(new ReceivingTask(socket, multicastGroup + ":" + port, handlers));
this.socket = socket; this.socket = socket;
} catch (IOException e) {
throw new RuntimeException("Could not open socket", e);
}
} }
void close() throws IOException { void close() throws IOException {
@ -100,10 +98,11 @@ public class PacketListener {
this.future = null; this.future = null;
} }
InetAddress address = InetAddress.getByName(multicastGroup); InetAddress mcastGroupAddress = InetAddress.getByName(multicastGroup);
InetSocketAddress socketAddress = new InetSocketAddress(mcastGroupAddress, port);
MulticastSocket socket = this.socket; MulticastSocket socket = this.socket;
if (socket != null) { if (socket != null) {
socket.leaveGroup(address); socket.leaveGroup(socketAddress, null);
socket.close(); socket.close();
this.socket = null; this.socket = null;
} }