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.jablotron.internal.handler;
15 import static org.openhab.binding.jablotron.JablotronBindingConstants.*;
17 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.jablotron.internal.model.JablotronControlResponse;
22 import org.openhab.binding.jablotron.internal.model.JablotronDataUpdateResponse;
23 import org.openhab.binding.jablotron.internal.model.JablotronServiceDetailSegment;
24 import org.openhab.core.cache.ExpiringCache;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.openhab.core.types.State;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link JablotronOasisHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Ondrej Pecta - Initial contribution
42 public class JablotronOasisHandler extends JablotronAlarmHandler {
44 private final Logger logger = LoggerFactory.getLogger(JablotronOasisHandler.class);
46 public JablotronOasisHandler(Thing thing, String alarmName) {
47 super(thing, alarmName);
48 dataCache = new ExpiringCache<>(CACHE_TIMEOUT_MS, this::sendGetStatusRequest);
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 if (RefreshType.REFRESH.equals(command)) {
54 logger.debug("refreshing channel: {}", channelUID.getId());
55 updateChannel(channelUID.getId());
57 switch (channelUID.getId()) {
59 if (command instanceof StringType) {
60 scheduler.execute(() -> {
61 sendCommand(command.toString());
65 case CHANNEL_STATUS_PGX:
66 if (command instanceof OnOffType) {
67 scheduler.execute(() -> {
68 controlSection("PGM_1", command.equals(OnOffType.ON) ? "set" : "unset");
72 case CHANNEL_STATUS_PGY:
73 if (command instanceof OnOffType) {
74 scheduler.execute(() -> {
75 controlSection("PGM_2", command.equals(OnOffType.ON) ? "set" : "unset");
83 private void updateChannel(String channel) {
84 ExpiringCache<JablotronDataUpdateResponse> localDataCache = dataCache;
85 if (localDataCache != null) {
87 case CHANNEL_STATUS_A:
88 updateSegmentStatus("STATE_1", localDataCache.getValue());
90 case CHANNEL_STATUS_B:
91 updateSegmentStatus("STATE_2", localDataCache.getValue());
93 case CHANNEL_STATUS_ABC:
94 updateSegmentStatus("STATE_3", localDataCache.getValue());
96 case CHANNEL_STATUS_PGX:
97 updateSegmentStatus("PGM_1", localDataCache.getValue());
99 case CHANNEL_STATUS_PGY:
100 updateSegmentStatus("PGM_2", localDataCache.getValue());
102 case CHANNEL_LAST_CHECK_TIME:
106 updateEventChannel(channel);
112 protected void updateSegmentStatus(JablotronServiceDetailSegment segment) {
113 logger.debug("Segment id: {} and status: {}", segment.getSegmentId(), segment.getSegmentState());
114 State newState = OnOffType.from(!"unset".equals(segment.getSegmentState()));
115 switch (segment.getSegmentId()) {
117 updateState(CHANNEL_STATUS_A, newState);
120 updateState(CHANNEL_STATUS_B, newState);
123 updateState(CHANNEL_STATUS_ABC, newState);
126 updateState(CHANNEL_STATUS_PGX, newState);
129 updateState(CHANNEL_STATUS_PGY, newState);
132 logger.debug("Unknown segment received: {} with state: {}", segment.getSegmentId(),
133 segment.getSegmentState());
137 public synchronized void controlSection(String section, String status) {
138 logger.debug("Controlling section: {} with status: {}", section, status);
139 JablotronControlResponse response = sendUserCode(section, section.toLowerCase(), status, "");
142 if (response == null) {
143 logger.debug("null response/status received during the control of section: {}", section);
147 public synchronized void sendCommand(String code) {
148 JablotronControlResponse response = sendUserCode(code);
149 scheduler.schedule(this::updateAlarmStatus, 1, TimeUnit.SECONDS);
151 if (response == null) {
152 logger.debug("null response/status received during sending a code");
156 private synchronized @Nullable JablotronControlResponse sendUserCode(String code) {
157 return sendUserCode("sections", "button_1", "partialSet", code);