use of Moka7.S7Client in project openhab1-addons by openhab.
the class PLCLogoBinding method updated.
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
Boolean configured = false;
if (config != null) {
String refreshIntervalString = Objects.toString(config.get("refresh"), null);
if (StringUtils.isNotBlank(refreshIntervalString)) {
refreshInterval = Long.parseLong(refreshIntervalString);
}
if (controllers == null) {
controllers = new HashMap<String, PLCLogoConfig>();
}
Enumeration<String> keys = config.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
// don't want to process here ...
if ("service.pid".equals(key)) {
continue;
}
Matcher matcher = EXTRACT_CONFIG_PATTERN.matcher(key);
if (!matcher.matches()) {
continue;
}
matcher.reset();
matcher.find();
String controllerName = matcher.group(1);
PLCLogoConfig deviceConfig = controllers.get(controllerName);
if (deviceConfig == null) {
deviceConfig = new PLCLogoConfig();
controllers.put(controllerName, deviceConfig);
logger.info("Create new config for {}", controllerName);
}
if (matcher.group(2).equals("host")) {
String ip = config.get(key).toString();
deviceConfig.setIP(ip);
logger.info("Set host of {}: {}", controllerName, ip);
configured = true;
}
if (matcher.group(2).equals("remoteTSAP")) {
String tsap = config.get(key).toString();
deviceConfig.setRemoteTSAP(Integer.decode(tsap));
logger.info("Set remote TSAP for {}: {}", controllerName, tsap);
}
if (matcher.group(2).equals("localTSAP")) {
String tsap = config.get(key).toString();
deviceConfig.setLocalTSAP(Integer.decode(tsap));
logger.info("Set local TSAP for {}: {}", controllerName, tsap);
}
if (matcher.group(2).equals("model")) {
PLCLogoModel model = null;
String modelName = config.get(key).toString();
if (modelName.equalsIgnoreCase("0BA7")) {
model = PLCLogoModel.LOGO_MODEL_0BA7;
} else if (modelName.equalsIgnoreCase("0BA8")) {
model = PLCLogoModel.LOGO_MODEL_0BA8;
} else {
logger.info("Found unknown model for {}: {}", controllerName, modelName);
}
if (model != null) {
deviceConfig.setModel(model);
logger.info("Set model for {}: {}", controllerName, modelName);
}
}
}
// while
Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, PLCLogoConfig> thisEntry = entries.next();
String controllerName = thisEntry.getKey();
PLCLogoConfig deviceConfig = thisEntry.getValue();
S7Client LogoS7Client = deviceConfig.getS7Client();
if (LogoS7Client == null) {
LogoS7Client = new Moka7.S7Client();
} else {
LogoS7Client.Disconnect();
}
LogoS7Client.SetConnectionParams(deviceConfig.getlogoIP(), deviceConfig.getlocalTSAP(), deviceConfig.getremoteTSAP());
logger.info("About to connect to {}", controllerName);
if ((LogoS7Client.Connect() == 0) && LogoS7Client.Connected) {
logger.info("Connected to PLC LOGO! device {}", controllerName);
} else {
logger.error("Could not connect to PLC LOGO! device {} : {}", controllerName, S7Client.ErrorText(LogoS7Client.LastError));
throw new ConfigurationException("Could not connect to PLC LOGO! device ", controllerName + " " + deviceConfig.getlogoIP());
}
deviceConfig.setS7Client(LogoS7Client);
}
setProperlyConfigured(configured);
} else {
logger.info("No configuration for PLCLogoBinding");
}
}
use of Moka7.S7Client in project openhab1-addons by openhab.
the class PLCLogoBinding method deactivate.
@Override
public void deactivate() {
for (PLCLogoBindingProvider provider : providers) {
provider.removeBindingChangeListener(this);
}
providers.clear();
Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, PLCLogoConfig> thisEntry = entries.next();
PLCLogoConfig logoConfig = thisEntry.getValue();
S7Client LogoS7Client = logoConfig.getS7Client();
if (LogoS7Client != null) {
LogoS7Client.Disconnect();
}
}
controllers.clear();
}
use of Moka7.S7Client in project openhab1-addons by openhab.
the class PLCLogoBinding method execute.
@Override
protected void execute() {
if (!bindingsExist()) {
logger.debug("There is no existing plclogo binding configuration => refresh cycle aborted!");
return;
}
Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, PLCLogoConfig> thisEntry = entries.next();
String controller = thisEntry.getKey();
PLCLogoConfig logoConfig = thisEntry.getValue();
S7Client LogoS7Client = logoConfig.getS7Client();
if (LogoS7Client == null) {
logger.debug("No S7client for {} found", controller);
} else {
lock.lock();
int result = ReadLogoDBArea(LogoS7Client, logoConfig.getMemorySize());
lock.unlock();
if (result != 0) {
logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
return;
}
// Now have the LOGO! memory (note: not suitable for S7) - more efficient than multiple reads (test
// shows <14mS to read all)
// iterate through bindings to see what has changed - this approach assumes a small number (< 100)of
// bindings
// otherwise might see what has changed in memory and map to binding
}
for (PLCLogoBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
if (config.getController().equals(controller)) {
// it is for our currently selected controller
PLCLogoMemoryConfig rd = config.getRD();
int address = -1;
try {
address = rd.getAddress(logoConfig.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid address for block {} on {}", rd.getBlockName(), controller);
continue;
}
int currentValue;
if (rd.isDigital()) {
int bit = -1;
try {
bit = rd.getBit(logoConfig.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid bit for block {} on {}", rd.getBlockName(), controller);
continue;
}
currentValue = S7.GetBitAt(data, address, bit) ? 1 : 0;
} else {
/*
* After the data transfer from a LOGO! Base Module to LOGO!Soft Comfort,
* you can view only analog values within the range of -32768 to 32767 on LOGO!Soft Comfort.
* If an analog value exceeds the value range,
* then only the nearest upper limit (32767) or lower limit (-32768) can be displayed.
*/
currentValue = S7.GetShortAt(data, address);
}
if (config.isSet()) {
if (currentValue == config.getLastValue()) {
continue;
}
int delta = Math.abs(config.getLastValue() - currentValue);
if (!rd.isDigital() && (delta < config.getAnalogDelta())) {
continue;
}
}
boolean isValid = false;
Item item = provider.getItem(itemName);
switch(rd.getKind()) {
case I:
case NI:
{
isValid = item instanceof ContactItem;
break;
}
case Q:
case NQ:
{
isValid = item instanceof SwitchItem;
break;
}
case M:
case VB:
case VW:
{
isValid = item instanceof ContactItem || item instanceof SwitchItem;
break;
}
default:
{
break;
}
}
if (item instanceof NumberItem || isValid) {
eventPublisher.postUpdate(itemName, createState(item, currentValue));
config.setLastValue(currentValue);
} else {
String block = rd.getBlockName();
logger.warn("Block {} is incompatible with item {} on {}", block, item.getName(), controller);
}
}
}
}
}
}
use of Moka7.S7Client in project openhab1-addons by openhab.
the class PLCLogoBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// the code being executed when a command was sent on the openHAB
// event bus goes here. This method is only called if one of the
// BindingProviders provide a binding for the given 'itemName'.
// Note itemname is the item name not the controller name/instance!
//
super.internalReceiveCommand(itemName, command);
logger.debug("internalReceiveCommand() is called!");
for (PLCLogoBindingProvider provider : providers) {
if (!provider.providesBindingFor(itemName)) {
continue;
}
PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
if (!controllers.containsKey(config.getController())) {
logger.warn("Invalid write requested for controller {}", config.getController());
continue;
}
PLCLogoConfig controller = controllers.get(config.getController());
PLCLogoMemoryConfig wr = config.getWR();
int address = -1;
try {
address = wr.getAddress(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid address for block {} on {}", wr.getBlockName(), controller);
continue;
}
int bit = -1;
try {
bit = wr.getBit(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid bit for block {} on {}", wr.getBlockName(), controller);
continue;
}
if (!wr.isInRange(controller.getModel())) {
logger.warn("Invalid write request for block {} at address {}", wr.getBlockName(), address);
continue;
}
// Send command to the LOGO! controller memory
S7Client LogoS7Client = controller.getS7Client();
if (LogoS7Client == null) {
logger.debug("No S7client for controller {} found", config.getController());
continue;
}
final byte[] buffer = new byte[2];
int size = wr.isDigital() ? 1 : 2;
lock.lock();
int result = LogoS7Client.ReadArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Read word from logo memory: at {} {} bytes, result = {}", address, size, result);
if (result == 0) {
Item item = config.getItem();
if (item instanceof NumberItem && !wr.isDigital()) {
if (command instanceof DecimalType) {
int oldValue = S7.GetShortAt(buffer, 0);
int newValue = ((DecimalType) command).intValue();
S7.SetWordAt(buffer, 0, newValue);
logger.debug("Changed word at {} from {} to {}", address, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} two bytes: [{}, {}]", address, buffer[0], buffer[1]);
}
} else if (item instanceof SwitchItem && wr.isDigital()) {
if (command instanceof OnOffType) {
boolean oldValue = S7.GetBitAt(buffer, 0, bit) ? true : false;
boolean newValue = command == OnOffType.ON ? true : false;
S7.SetBitAt(buffer, 0, bit, newValue);
logger.debug("Changed bit {}.{} from {} to {}", address, bit, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} one byte: [{}]", address, buffer[0]);
}
}
// If nothing was written and read was ok, nothing will happen here
if (result != 0) {
logger.warn("Failed to write memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
} else {
logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
lock.unlock();
}
}
Aggregations