2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plclogo.internal.handler;
15 import static org.openhab.binding.plclogo.internal.PLCLogoBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
22 import java.util.concurrent.atomic.AtomicReference;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.plclogo.internal.PLCLogoClient;
27 import org.openhab.binding.plclogo.internal.config.PLCDigitalConfiguration;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.OpenClosedType;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.binding.builder.ChannelBuilder;
39 import org.openhab.core.thing.binding.builder.ThingBuilder;
40 import org.openhab.core.thing.type.ChannelTypeUID;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
43 import org.openhab.core.types.State;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 import Moka7.S7Client;
51 * The {@link PLCDigitalHandler} is responsible for handling commands, which are
52 * sent to one of the channels.
54 * @author Alexander Falkenstern - Initial contribution
57 public class PLCDigitalHandler extends PLCCommonHandler {
59 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DIGITAL);
61 private final Logger logger = LoggerFactory.getLogger(PLCDigitalHandler.class);
62 private AtomicReference<PLCDigitalConfiguration> config = new AtomicReference<>();
64 private static final Map<String, @Nullable Integer> LOGO_BLOCKS_0BA7;
66 Map<String, @Nullable Integer> buffer = new HashMap<>();
67 buffer.put(I_DIGITAL, 24); // 24 digital inputs
68 buffer.put(Q_DIGITAL, 16); // 16 digital outputs
69 buffer.put(M_DIGITAL, 27); // 27 digital markers
70 LOGO_BLOCKS_0BA7 = Collections.unmodifiableMap(buffer);
73 private static final Map<String, @Nullable Integer> LOGO_BLOCKS_0BA8;
75 Map<String, @Nullable Integer> buffer = new HashMap<>();
76 buffer.put(I_DIGITAL, 24); // 24 digital inputs
77 buffer.put(Q_DIGITAL, 20); // 20 digital outputs
78 buffer.put(M_DIGITAL, 64); // 64 digital markers
79 buffer.put(NI_DIGITAL, 64); // 64 network inputs
80 buffer.put(NQ_DIGITAL, 64); // 64 network outputs
81 LOGO_BLOCKS_0BA8 = Collections.unmodifiableMap(buffer);
84 private static final Map<String, @Nullable Map<String, @Nullable Integer>> LOGO_BLOCK_NUMBER;
86 Map<String, @Nullable Map<String, @Nullable Integer>> buffer = new HashMap<>();
87 buffer.put(LOGO_0BA7, LOGO_BLOCKS_0BA7);
88 buffer.put(LOGO_0BA8, LOGO_BLOCKS_0BA8);
89 LOGO_BLOCK_NUMBER = Collections.unmodifiableMap(buffer);
95 public PLCDigitalHandler(Thing thing) {
100 public void handleCommand(ChannelUID channelUID, Command command) {
101 if (!isThingOnline()) {
105 Channel channel = getThing().getChannel(channelUID.getId());
106 String name = getBlockFromChannel(channel);
107 if (!isValid(name) || (channel == null)) {
108 logger.debug("Can not update channel {}, block {}.", channelUID, name);
112 int bit = getBit(name);
113 int address = getAddress(name);
114 PLCLogoClient client = getLogoClient();
115 if ((address != INVALID) && (bit != INVALID) && (client != null)) {
116 if (command instanceof RefreshType) {
117 int base = getBase(name);
118 byte[] buffer = new byte[getBufferLength()];
119 int result = client.readDBArea(1, base, buffer.length, S7Client.S7WLByte, buffer);
121 updateChannel(channel, S7.GetBitAt(buffer, address - base, bit));
123 logger.debug("Can not read data from LOGO!: {}.", S7Client.ErrorText(result));
125 } else if ((command instanceof OpenClosedType) || (command instanceof OnOffType)) {
126 byte[] buffer = new byte[1];
127 String type = channel.getAcceptedItemType();
128 if (DIGITAL_INPUT_ITEM.equalsIgnoreCase(type)) {
129 S7.SetBitAt(buffer, 0, 0, ((OpenClosedType) command) == OpenClosedType.CLOSED);
130 } else if (DIGITAL_OUTPUT_ITEM.equalsIgnoreCase(type)) {
131 S7.SetBitAt(buffer, 0, 0, ((OnOffType) command) == OnOffType.ON);
133 logger.debug("Channel {} will not accept {} items.", channelUID, type);
135 int result = client.writeDBArea(1, 8 * address + bit, buffer.length, S7Client.S7WLBit, buffer);
137 logger.debug("Can not write data to LOGO!: {}.", S7Client.ErrorText(result));
140 logger.debug("Channel {} received not supported command {}.", channelUID, command);
143 logger.info("Invalid channel {} or client {} found.", channelUID, client);
148 public void setData(final byte[] data) {
149 if (!isThingOnline()) {
153 if (data.length != getBufferLength()) {
154 logger.info("Received and configured data sizes does not match.");
158 List<Channel> channels = thing.getChannels();
159 if (channels.size() != getNumberOfChannels()) {
160 logger.info("Received and configured channel sizes does not match.");
164 Boolean force = config.get().isUpdateForced();
165 for (Channel channel : channels) {
166 ChannelUID channelUID = channel.getUID();
167 String name = getBlockFromChannel(channel);
169 int bit = getBit(name);
170 int address = getAddress(name);
171 if ((address != INVALID) && (bit != INVALID)) {
172 DecimalType state = (DecimalType) getOldValue(name);
173 boolean value = S7.GetBitAt(data, address - getBase(name), bit);
174 if ((state == null) || ((value ? 1 : 0) != state.intValue()) || force) {
175 updateChannel(channel, value);
177 if (logger.isTraceEnabled()) {
178 int buffer = (data[address - getBase(name)] & 0xFF) + 0x100;
179 logger.trace("Channel {} received [{}].", channelUID, Integer.toBinaryString(buffer).substring(1));
182 logger.info("Invalid channel {} found.", channelUID);
188 protected void updateState(ChannelUID channelUID, State state) {
189 super.updateState(channelUID, state);
190 DecimalType value = state.as(DecimalType.class);
191 if (state instanceof OpenClosedType) {
192 OpenClosedType type = (OpenClosedType) state;
193 value = new DecimalType(type == OpenClosedType.CLOSED ? 1 : 0);
196 Channel channel = thing.getChannel(channelUID.getId());
197 setOldValue(getBlockFromChannel(channel), value);
201 protected void updateConfiguration(Configuration configuration) {
202 super.updateConfiguration(configuration);
203 config.set(getConfigAs(PLCDigitalConfiguration.class));
207 protected boolean isValid(final String name) {
208 if (2 <= name.length() && (name.length() <= 4)) {
209 String kind = getBlockKind();
210 if (Character.isDigit(name.charAt(1)) || Character.isDigit(name.charAt(2))) {
211 boolean valid = I_DIGITAL.equalsIgnoreCase(kind) || NI_DIGITAL.equalsIgnoreCase(kind);
212 valid = valid || Q_DIGITAL.equalsIgnoreCase(kind) || NQ_DIGITAL.equalsIgnoreCase(kind);
213 return name.startsWith(kind) && (valid || M_DIGITAL.equalsIgnoreCase(kind));
220 protected String getBlockKind() {
221 return config.get().getBlockKind();
225 protected int getNumberOfChannels() {
226 String kind = getBlockKind();
227 String family = getLogoFamily();
228 logger.debug("Get block number of {} LOGO! for {} blocks.", family, kind);
230 Map<?, @Nullable Integer> blocks = LOGO_BLOCK_NUMBER.get(family);
231 Integer number = (blocks != null) ? blocks.get(kind) : null;
232 return (number != null) ? number.intValue() : 0;
236 protected int getAddress(final String name) {
237 int address = super.getAddress(name);
238 if (address != INVALID) {
239 address = getBase(name) + (address - 1) / 8;
241 logger.info("Wrong configurated LOGO! block {} found.", name);
247 protected void doInitialization() {
248 Thing thing = getThing();
249 logger.debug("Initialize LOGO! digital input blocks handler.");
251 config.set(getConfigAs(PLCDigitalConfiguration.class));
253 super.doInitialization();
254 if (ThingStatus.OFFLINE != thing.getStatus()) {
255 String kind = getBlockKind();
256 String type = config.get().getChannelType();
257 String text = DIGITAL_INPUT_ITEM.equalsIgnoreCase(type) ? "input" : "output";
259 ThingBuilder tBuilder = editThing();
261 String label = thing.getLabel();
263 Bridge bridge = getBridge();
264 label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
265 label += (": digital " + text + "s");
267 tBuilder.withLabel(label);
269 for (int i = 0; i < getNumberOfChannels(); i++) {
270 String name = kind + String.valueOf(i + 1);
271 ChannelUID uid = new ChannelUID(thing.getUID(), name);
272 ChannelBuilder cBuilder = ChannelBuilder.create(uid, type);
273 cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
274 cBuilder.withLabel(name);
275 cBuilder.withDescription("Digital " + text + " block " + name);
276 cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
277 tBuilder.withChannel(cBuilder.build());
278 setOldValue(name, null);
281 updateThing(tBuilder.build());
282 updateStatus(ThingStatus.ONLINE);
287 * Calculate bit within address for block with given name.
289 * @param name Name of the LOGO! block
290 * @return Calculated bit
292 private int getBit(final String name) {
295 logger.debug("Get bit of {} LOGO! for block {} .", getLogoFamily(), name);
297 if (isValid(name) && (getAddress(name) != INVALID)) {
298 if (Character.isDigit(name.charAt(1))) {
299 bit = Integer.parseInt(name.substring(1));
300 } else if (Character.isDigit(name.charAt(2))) {
301 bit = Integer.parseInt(name.substring(2));
305 logger.info("Wrong configurated LOGO! block {} found.", name);
311 private void updateChannel(final Channel channel, boolean value) {
312 ChannelUID channelUID = channel.getUID();
313 String type = channel.getAcceptedItemType();
314 if (DIGITAL_INPUT_ITEM.equalsIgnoreCase(type)) {
315 updateState(channelUID, value ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
316 logger.debug("Channel {} accepting {} was set to {}.", channelUID, type, value);
317 } else if (DIGITAL_OUTPUT_ITEM.equalsIgnoreCase(type)) {
318 updateState(channelUID, value ? OnOffType.ON : OnOffType.OFF);
319 logger.debug("Channel {} accepting {} was set to {}.", channelUID, type, value);
321 logger.debug("Channel {} will not accept {} items.", channelUID, type);