2 * Copyright (c) 2010-2023 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.oceanic.internal.handler;
15 import java.math.BigDecimal;
16 import java.util.List;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.oceanic.internal.OceanicBindingConstants.OceanicChannelSelector;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.TypeParser;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link OceanicThingHandler} is the abstract class responsible for handling commands, which are
39 * sent to one of the channels
41 * @author Karel Goderis - Initial contribution
44 public abstract class OceanicThingHandler extends BaseThingHandler {
46 public static final String INTERVAL = "interval";
47 public static final String BUFFER_SIZE = "buffer";
48 private final Logger logger = LoggerFactory.getLogger(OceanicThingHandler.class);
50 protected int bufferSize;
51 protected @Nullable ScheduledFuture<?> pollingJob;
52 protected static String lastLineReceived = "";
54 public OceanicThingHandler(Thing thing) {
58 private Runnable resetRunnable = () -> {
63 private Runnable pollingRunnable = () -> {
65 if (getThing().getStatus() == ThingStatus.ONLINE) {
66 for (Channel aChannel : getThing().getChannels()) {
67 for (OceanicChannelSelector selector : OceanicChannelSelector.values()) {
68 ChannelUID theChannelUID = new ChannelUID(getThing().getUID(), selector.toString());
69 if (aChannel.getUID().equals(theChannelUID)
70 && selector.getTypeValue() == OceanicChannelSelector.ValueSelectorType.GET) {
71 String response = requestResponse(selector.name());
72 if (response != null && !response.isEmpty()) {
73 if (selector.isProperty()) {
74 logger.debug("Updating the property '{}' with value '{}'", selector.toString(),
75 selector.convertValue(response));
76 Map<String, String> properties = editProperties();
77 properties.put(selector.toString(), selector.convertValue(response));
78 updateProperties(properties);
80 State value = createStateForType(selector, response);
82 updateState(theChannelUID, value);
86 logger.warn("Received an empty answer for '{}'", selector.name());
92 } catch (Exception e) {
93 logger.error("An exception occurred while polling the Oceanic Water Softener: '{}'", e.getMessage(), e);
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
95 scheduler.schedule(resetRunnable, 0, TimeUnit.SECONDS);
100 public void initialize() {
101 if (getConfig().get(BUFFER_SIZE) == null) {
104 bufferSize = ((BigDecimal) getConfig().get(BUFFER_SIZE)).intValue();
107 if (pollingJob == null || pollingJob.isCancelled()) {
108 pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 1,
109 ((BigDecimal) getConfig().get(INTERVAL)).intValue(), TimeUnit.SECONDS);
114 public void dispose() {
115 if (pollingJob != null && !pollingJob.isCancelled()) {
116 pollingJob.cancel(true);
122 public void handleCommand(ChannelUID channelUID, Command command) {
123 if (getThing().getStatus() == ThingStatus.ONLINE) {
124 if (!(command instanceof RefreshType)) {
125 String commandAsString = command.toString();
126 String channelID = channelUID.getId();
128 for (Channel aChannel : getThing().getChannels()) {
129 if (aChannel.getUID().equals(channelUID)) {
131 OceanicChannelSelector selector = OceanicChannelSelector.getValueSelector(channelID,
132 OceanicChannelSelector.ValueSelectorType.SET);
136 commandAsString = selector.name() + commandAsString;
139 commandAsString = selector.name();
142 String response = requestResponse(commandAsString);
143 if ("ERR".equals(response)) {
144 logger.error("An error occurred while setting '{}' to {}", selector.toString(),
147 } catch (IllegalArgumentException e) {
149 "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
150 channelID, command.toString());
159 @SuppressWarnings("unchecked")
160 private @Nullable State createStateForType(OceanicChannelSelector selector, String value) {
161 return TypeParser.parseState(List.of((Class<? extends State>) selector.getTypeClass()),
162 selector.convertValue(value));
165 protected abstract @Nullable String requestResponse(String commandAsString);