diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/config/simaticGenericConfiguration.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/config/simaticGenericConfiguration.java deleted file mode 100644 index a61aaa0..0000000 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/config/simaticGenericConfiguration.java +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) 2010-2020 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.simatic.internal.config; - -/** - * The {@link SimaticGenericConfiguration} class contains fields mapping thing configuration parameters. - * - * @author VitaTucek - Initial contribution - */ -public class SimaticGenericConfiguration { - -} diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticBridgeHandler.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticBridgeHandler.java index 40b3c8c..7b31b6f 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticBridgeHandler.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticBridgeHandler.java @@ -14,6 +14,7 @@ import java.util.ArrayList; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.simatic.internal.SimaticBindingConstants; @@ -59,6 +60,7 @@ public class SimaticBridgeHandler extends BaseBridgeHandler { * * @param bridge */ + @SuppressWarnings("null") public SimaticBridgeHandler(Bridge bridge) { super(bridge); @@ -149,6 +151,11 @@ public void initialize() { } }); + connection.onMetricsUpdated((requests, bytes) -> { + updateState(chRequests, new DecimalType(requests)); + updateState(chBytes, new DecimalType(bytes)); + }); + // temporarily status updateStatus(ThingStatus.UNKNOWN); @@ -188,7 +195,6 @@ public void handleCommand(ChannelUID channelUID, Command command) { // get cached values if (command instanceof RefreshType) { - // updateState(channelUID, value); } } @@ -213,7 +219,7 @@ public void updateConfig() { } } - ArrayList stateItems = new ArrayList(stateChannelCount); + var stateItems = new ArrayList<@NonNull SimaticChannel>(stateChannelCount); for (Thing th : getThing().getThings()) { var h = ((SimaticGenericHandler) th.getHandler()); @@ -228,12 +234,13 @@ public void updateConfig() { } if (connection != null) { - connection.setDataAreas(stateItems); - } + var c = connection; + c.setDataAreas(stateItems); - if (connection.isConnected()) { - updateState(chAreasCount, new DecimalType(connection.getReadAreas().size())); - updateState(chAreas, new StringType(connection.getReadAreas().toString())); + if (c.isConnected()) { + updateState(chAreasCount, new DecimalType(c.getReadAreas().size())); + updateState(chAreas, new StringType(c.getReadAreas().toString())); + } } updateState(chTagCount, new DecimalType(channelCount)); diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticGenericHandler.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticGenericHandler.java index 854249e..682bb0e 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticGenericHandler.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticGenericHandler.java @@ -17,7 +17,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.binding.simatic.internal.config.SimaticGenericConfiguration; import org.openhab.binding.simatic.internal.simatic.SimaticChannel; import org.openhab.binding.simatic.internal.simatic.SimaticGenericDevice; import org.openhab.core.thing.Channel; @@ -27,6 +26,7 @@ import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.binding.BaseThingHandler; +import org.openhab.core.thing.binding.BridgeHandler; import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; @@ -45,7 +45,6 @@ public class SimaticGenericHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(SimaticGenericHandler.class); - private @Nullable SimaticGenericConfiguration config; private @Nullable SimaticGenericDevice connection = null; private long errorSetTime = 0; @@ -57,8 +56,6 @@ public SimaticGenericHandler(Thing thing) { @Override public void initialize() { - config = getConfigAs(SimaticGenericConfiguration.class); - logger.debug("{} - initialize. Channels count={}", thing.getLabel(), thing.getChannels().size()); int errors = 0; @@ -89,14 +86,22 @@ public void initialize() { channels.put(channelUID, chConfig); } - // get connection and update status - bridgeStatusChanged(getBridge().getStatusInfo()); + var bridge = getBridge(); + + if (bridge == null) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED); + } else { + // get connection and update status + bridgeStatusChanged(bridge.getStatusInfo()); + } if (errors > 0) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Channel configuration error"); } - // - ((SimaticBridgeHandler) getBridge().getHandler()).updateConfig(); + BridgeHandler handler; + if (bridge != null && (handler = bridge.getHandler()) != null) { + ((SimaticBridgeHandler) handler).updateConfig(); + } } @Override diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticGenericDevice.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticGenericDevice.java index 9b2cef0..5b50db6 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticGenericDevice.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticGenericDevice.java @@ -10,6 +10,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Deque; @@ -22,6 +23,7 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.simatic.internal.SimaticBindingConstants; import org.openhab.binding.simatic.internal.simatic.SimaticPortState.PortStates; @@ -58,7 +60,7 @@ public class SimaticGenericDevice implements SimaticIDevice { public final int MAX_RESEND_COUNT = 2; /** item config */ - protected List stateItems; + protected List<@NonNull SimaticChannel> stateItems; /** flag that device is connected */ private boolean connected = false; @@ -116,7 +118,7 @@ public void dispose() { * Called at specified period */ protected void execute() { - if (!isConnected() && shouldReconnect()) { + if (shouldReconnect()) { reconnectWithDelaying(); } if (isConnected()) { @@ -126,7 +128,7 @@ protected void execute() { } @Override - public void setDataAreas(List stateItems) { + public void setDataAreas(@NonNull ArrayList<@NonNull SimaticChannel> stateItems) { this.stateItems = stateItems; // prepare data if device is connected (depends on PDU size) if (isConnected()) { @@ -186,7 +188,7 @@ protected void setConnected(boolean state) { * Reconnect device */ public boolean reconnect() { - logger.info("{}: Trying to reconnect", toString()); + logger.info("{} - Trying to reconnect", toString()); close(); return open(); @@ -196,8 +198,7 @@ public boolean reconnect() { * Reconnect device */ public void reconnectWithDelaying() { - - logger.debug("reconnectWithDelaying(): {}/{}/{}", rcTest, rcTestMax, RECONNECT_DELAY_MAX); + logger.debug("{} - reconnectWithDelaying(): {}/{}/{}", toString(), rcTest, rcTestMax, RECONNECT_DELAY_MAX); if (rcTest < rcTestMax) { rcTest++; @@ -399,7 +400,6 @@ public void readDataArea(SimaticReadDataArea area) throws SimaticReadException { * After item configuration is loaded this method prepare reading areas for this device */ public void prepareData() { - if (stateItems == null) { return; } diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticIDevice.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticIDevice.java index d698c67..9b0e820 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticIDevice.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticIDevice.java @@ -8,8 +8,9 @@ */ package org.openhab.binding.simatic.internal.simatic; -import java.util.List; +import java.util.ArrayList; +import org.eclipse.jdt.annotation.NonNull; import org.openhab.core.types.Command; /** @@ -60,7 +61,7 @@ public interface MetricsUpdated { * Set read write areas * */ - public void setDataAreas(List stateItems); + public void setDataAreas(@NonNull ArrayList<@NonNull SimaticChannel> stateItems); /** * Function return device string representation diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP.java index 209aa90..f1738c1 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP.java @@ -113,12 +113,9 @@ public Boolean open() { // open socket try { sock = new Socket(this.plcAddress, 102); - oStream = sock.getOutputStream(); iStream = sock.getInputStream(); - di = new PLCinterface(oStream, iStream, "IF1", 0, Nodave.PROTOCOL_ISOTCP); - dc = new TCPConnection(di, rack, slot, communicationType); if (dc.connectPLC() == 0) { @@ -132,17 +129,15 @@ public Boolean open() { setConnected(true); } else { logger.error("{} - cannot connect to PLC", this.toString()); - + tryReconnect.set(true); return false; } - } catch (IOException ex) { - logger.error("{} - cannot connect to PLC. {}", this.toString(), ex.getMessage()); - return false; } catch (Exception ex) { logger.error("{} - cannot connect to PLC. {}", this.toString(), ex.getMessage()); + tryReconnect.set(true); return false; } finally { - tryReconnect.set(true); + } return true; } @@ -193,13 +188,6 @@ public void close() { */ @Override protected boolean sendDataOut(SimaticWriteDataArea data) { - // TODO: If the connection is reset because of a network error, we can try to re-send the write instead of - // dropping it. -- AchilleGR - // TODO: Don't allow writing to addresses corresponding to items marked as read only -- AchilleGR - if (logger.isDebugEnabled()) { - logger.debug("{} - Sending data to device", this.toString()); - } - if (!isConnected()) { logger.debug("{} - Not connected. Sent discarted.", this.toString()); return false; diff --git a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP200.java b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP200.java index 327e570..21b201b 100644 --- a/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP200.java +++ b/org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP200.java @@ -8,7 +8,6 @@ */ package org.openhab.binding.simatic.internal.simatic; -import java.io.IOException; import java.net.Socket; import org.openhab.binding.simatic.internal.libnodave.Nodave; @@ -58,13 +57,9 @@ public Boolean open() { // open socket try { sock = new Socket(this.plcAddress, 102); - oStream = sock.getOutputStream(); - iStream = sock.getInputStream(); - di = new PLCinterface(oStream, iStream, "IF1", 0, Nodave.PROTOCOL_ISOTCP); - dc = new TCP243Connection(di, rack, slot); if (dc.connectPLC() == 0) { @@ -76,15 +71,15 @@ public Boolean open() { setConnected(true); } else { logger.error("{} - cannot connect to PLC", this.toString()); - + tryReconnect.set(true); return false; } - } catch (IOException ex) { + } catch (Exception ex) { logger.error("{} - cannot connect to PLC due: {}", this.toString(), ex.getMessage()); - + tryReconnect.set(true); return false; } finally { - tryReconnect.set(false); + } return true;