Skip to content

Commit

Permalink
Reconnect fix
Browse files Browse the repository at this point in the history
  • Loading branch information
docbender committed Sep 22, 2020
1 parent 2194520 commit bf493bf
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 70 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class SimaticBridgeHandler extends BaseBridgeHandler {
*
* @param bridge
*/
@SuppressWarnings("null")
public SimaticBridgeHandler(Bridge bridge) {
super(bridge);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -188,7 +195,6 @@ public void handleCommand(ChannelUID channelUID, Command command) {

// get cached values
if (command instanceof RefreshType) {

// updateState(channelUID, value);
}
}
Expand All @@ -213,7 +219,7 @@ public void updateConfig() {
}
}

ArrayList<SimaticChannel> stateItems = new ArrayList<SimaticChannel>(stateChannelCount);
var stateItems = new ArrayList<@NonNull SimaticChannel>(stateChannelCount);

for (Thing th : getThing().getThings()) {
var h = ((SimaticGenericHandler) th.getHandler());
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class SimaticGenericDevice implements SimaticIDevice {
public final int MAX_RESEND_COUNT = 2;

/** item config */
protected List<SimaticChannel> stateItems;
protected List<@NonNull SimaticChannel> stateItems;

/** flag that device is connected */
private boolean connected = false;
Expand Down Expand Up @@ -116,7 +118,7 @@ public void dispose() {
* Called at specified period
*/
protected void execute() {
if (!isConnected() && shouldReconnect()) {
if (shouldReconnect()) {
reconnectWithDelaying();
}
if (isConnected()) {
Expand All @@ -126,7 +128,7 @@ protected void execute() {
}

@Override
public void setDataAreas(List<SimaticChannel> stateItems) {
public void setDataAreas(@NonNull ArrayList<@NonNull SimaticChannel> stateItems) {
this.stateItems = stateItems;
// prepare data if device is connected (depends on PDU size)
if (isConnected()) {
Expand Down Expand Up @@ -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();
Expand All @@ -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++;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface MetricsUpdated {
* Set read write areas
*
*/
public void setDataAreas(List<SimaticChannel> stateItems);
public void setDataAreas(@NonNull ArrayList<@NonNull SimaticChannel> stateItems);

/**
* Function return device string representation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit bf493bf

Please sign in to comment.