Skip to content

Commit

Permalink
Oh3.2 (#37)
Browse files Browse the repository at this point in the history
* Added update mode

* Fix reconnect issue #34

* Space between data in one area increased to 128

* Version update
  • Loading branch information
docbender authored Dec 23, 2021
1 parent 25f81a7 commit 9604f33
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 249 deletions.
2 changes: 1 addition & 1 deletion org.openhab.binding.simatic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.2.0</version>
</parent>

<artifactId>org.openhab.binding.simatic</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions org.openhab.binding.simatic/src/main/history/dependencies.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.6.0" name="org.openhab.binding.simatic-3.1.0-SNAPSHOT">
<features xmlns="http://karaf.apache.org/xmlns/features/v1.6.0" name="org.openhab.binding.simatic-3.2.0">
<feature version="0.0.0">
<feature>openhab-runtime-base</feature>
<feature>wrap</feature>
<bundle>mvn:org.openhab.addons.bundles/org.openhab.binding.simatic/3.1.0-SNAPSHOT</bundle>
<bundle>mvn:org.openhab.addons.bundles/org.openhab.binding.simatic/3.2.0</bundle>
<bundle>wrap:mvn:org.lastnpe.eea/eea-all/2.2.1</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.simatic.internal.config;

import org.openhab.binding.simatic.internal.simatic.SimaticUpdateMode;

/**
* The {@link SimaticBridgeConfiguration} class contains fields mapping thing configuration parameters.
*
Expand Down Expand Up @@ -53,4 +55,19 @@ public class SimaticBridgeConfiguration {
* Device poll rate
*/
public int pollRate = 1000;

/**
* Value update mode (OC,PL)
*/
public String updateMode = "OnChange";

/**
* Get Value Update Mode
*
* @return Return Update mode
*/
public SimaticUpdateMode getUpdateMode() {
return SimaticUpdateMode.valueOf(updateMode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openhab.binding.simatic.internal.simatic.SimaticGenericDevice;
import org.openhab.binding.simatic.internal.simatic.SimaticTCP;
import org.openhab.binding.simatic.internal.simatic.SimaticTCP200;
import org.openhab.binding.simatic.internal.simatic.SimaticUpdateMode;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
Expand Down Expand Up @@ -96,38 +97,35 @@ public void initialize() {

config = getConfigAs(SimaticBridgeConfiguration.class);

logger.debug("{} - Bridge configuration: Host/IP={},Rack={},Slot={},Comm={},Is200={},Charset={},PollRate={}",
logger.debug(
"{} - Bridge configuration: Host/IP={},Rack={},Slot={},Comm={},Is200={},Charset={},PollRate={},Mode={}",
getThing().getLabel(), config.address, config.rack, config.slot, config.communicationType,
config.isS7200, config.charset, config.pollRate);
config.isS7200, config.charset, config.pollRate, config.updateMode);

// configuration validation
boolean valid = true;

if (config.address == null || config.address.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No Host/IP address");
valid = false;
return;
}

if (config.rack < 0 || config.rack > 2) {
if (valid && (config.rack < 0 || config.rack > 2)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Invalid Rack number. Valid is 0-2.");
valid = false;
return;
}

if (config.slot < 0 || config.slot > 15) {
if (valid && (config.slot < 0 || config.slot > 15)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Invalid Slot number. Valid is 0-15.");
valid = false;
return;
}

if (config.communicationType == null || !(config.communicationType.equals("S7")
|| config.communicationType.equals("PG") || config.communicationType.equals("OP"))) {
if (valid && (config.communicationType == null || !(config.communicationType.equals("S7")
|| config.communicationType.equals("PG") || config.communicationType.equals("OP")))) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Invalid communication type.");
valid = false;
return;
}

if (config.pollRate <= 0) {
Expand All @@ -136,6 +134,19 @@ public void initialize() {
getThing().getLabel());
}

if (valid && (config.updateMode == null || !SimaticUpdateMode.validate(config.updateMode))) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Invalid value update mode.");
valid = false;
}

if (!valid) {
// updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
logger.error("{} - Bridge configuration is invalid. Host/IP={},Rack={},Slot={},Comm={},Is200={},Mode={}",
getThing().getLabel(), config.address, config.rack, config.slot, config.communicationType,
config.isS7200, config.updateMode);
return;
}

Charset charset;
if (config.charset == null || config.charset.isBlank()) {
charset = Charset.defaultCharset();
Expand All @@ -149,19 +160,13 @@ public void initialize() {

logger.info("{} - Current charset {}", getThing().getLabel(), charset.name());

if (!valid) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
logger.error("{} - Bridge configuration is invalid. Host/IP={},Rack={},Slot={},Comm={},Is200={}",
getThing().getLabel(), config.address, config.rack, config.slot, config.communicationType,
config.isS7200);
}

// S7-200 PLC
if (config.isS7200) {
connection = new SimaticTCP200(config.address, config.rack, config.slot, config.pollRate, charset);
connection = new SimaticTCP200(config.address, config.rack, config.slot, config.pollRate, charset,
SimaticUpdateMode.fromString(config.updateMode));
} else {
connection = new SimaticTCP(config.address, config.rack, config.slot, config.communicationType,
config.pollRate, charset);
config.pollRate, charset, SimaticUpdateMode.fromString(config.updateMode));
}

// react on connection changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.binding.simatic.internal.handler;

import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.simatic.internal.simatic.SimaticChannel;
import org.openhab.binding.simatic.internal.simatic.SimaticGenericDevice;
import org.openhab.binding.simatic.internal.simatic.SimaticUpdateMode;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -71,8 +73,8 @@ public void initialize() {
}

final SimaticChannel chConfig = channel.getConfiguration().as(SimaticChannel.class);
chConfig.channelId = channelUID;
chConfig.channelType = channelTypeUID;
chConfig.setChannelId(channelUID);
chConfig.setChannelType(channelTypeUID);

if (!chConfig.init(this)) {
errors++;
Expand Down Expand Up @@ -218,6 +220,9 @@ public void setError(String message) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
}

/**
* Clear thing error if necessary
*/
public void clearError() {
// no error
if (getThing().getStatus() == ThingStatus.ONLINE) {
Expand All @@ -229,4 +234,30 @@ public void clearError() {
updateStatus(ThingStatus.ONLINE);
}
}

/**
* Get configured code page
*
* @return
*/
public Charset getCharset() {
if (connection != null) {
return connection.getCharset();
}

return Charset.defaultCharset();
}

/**
* Get configured update mode
*
* @return
*/
public SimaticUpdateMode getUpdateMode() {
if (connection != null) {
return connection.getUpdateMode();
}

return SimaticUpdateMode.OnChange;
}
}
Loading

0 comments on commit 9604f33

Please sign in to comment.