]> git.basschouten.com Git - openhab-addons.git/blob
3d315b4e670a60bcb2830f674669e19b737eb3f2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.jablotron.internal.handler;
14
15 import static org.openhab.binding.jablotron.JablotronBindingConstants.*;
16
17 import java.util.concurrent.TimeUnit;
18
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;
34
35 /**
36  * The {@link JablotronOasisHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Ondrej Pecta - Initial contribution
40  */
41 @NonNullByDefault
42 public class JablotronOasisHandler extends JablotronAlarmHandler {
43
44     private final Logger logger = LoggerFactory.getLogger(JablotronOasisHandler.class);
45
46     public JablotronOasisHandler(Thing thing, String alarmName) {
47         super(thing, alarmName);
48         dataCache = new ExpiringCache<>(CACHE_TIMEOUT_MS, this::sendGetStatusRequest);
49     }
50
51     @Override
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());
56         } else {
57             switch (channelUID.getId()) {
58                 case CHANNEL_COMMAND:
59                     if (command instanceof StringType) {
60                         scheduler.execute(() -> {
61                             sendCommand(command.toString());
62                         });
63                     }
64                     break;
65                 case CHANNEL_STATUS_PGX:
66                     if (command instanceof OnOffType) {
67                         scheduler.execute(() -> {
68                             controlSection("PGM_1", command.equals(OnOffType.ON) ? "set" : "unset");
69                         });
70                     }
71                     break;
72                 case CHANNEL_STATUS_PGY:
73                     if (command instanceof OnOffType) {
74                         scheduler.execute(() -> {
75                             controlSection("PGM_2", command.equals(OnOffType.ON) ? "set" : "unset");
76                         });
77                     }
78                     break;
79             }
80         }
81     }
82
83     private void updateChannel(String channel) {
84         ExpiringCache<JablotronDataUpdateResponse> localDataCache = dataCache;
85         if (localDataCache != null) {
86             switch (channel) {
87                 case CHANNEL_STATUS_A:
88                     updateSegmentStatus("STATE_1", localDataCache.getValue());
89                     break;
90                 case CHANNEL_STATUS_B:
91                     updateSegmentStatus("STATE_2", localDataCache.getValue());
92                     break;
93                 case CHANNEL_STATUS_ABC:
94                     updateSegmentStatus("STATE_3", localDataCache.getValue());
95                     break;
96                 case CHANNEL_STATUS_PGX:
97                     updateSegmentStatus("PGM_1", localDataCache.getValue());
98                     break;
99                 case CHANNEL_STATUS_PGY:
100                     updateSegmentStatus("PGM_2", localDataCache.getValue());
101                     break;
102                 case CHANNEL_LAST_CHECK_TIME:
103                     // not updating
104                     break;
105                 default:
106                     updateEventChannel(channel);
107             }
108         }
109     }
110
111     @Override
112     protected void updateSegmentStatus(JablotronServiceDetailSegment segment) {
113         logger.debug("Segment id: {} and status: {}", segment.getSegmentId(), segment.getSegmentState());
114         State newState = "unset".equals(segment.getSegmentState()) ? OnOffType.OFF : OnOffType.ON;
115         switch (segment.getSegmentId()) {
116             case "STATE_1":
117                 updateState(CHANNEL_STATUS_A, newState);
118                 break;
119             case "STATE_2":
120                 updateState(CHANNEL_STATUS_B, newState);
121                 break;
122             case "STATE_3":
123                 updateState(CHANNEL_STATUS_ABC, newState);
124                 break;
125             case "PGM_1":
126                 updateState(CHANNEL_STATUS_PGX, newState);
127                 break;
128             case "PGM_2":
129                 updateState(CHANNEL_STATUS_PGY, newState);
130                 break;
131             default:
132                 logger.debug("Unknown segment received: {} with state: {}", segment.getSegmentId(),
133                         segment.getSegmentState());
134         }
135     }
136
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, "");
140
141         updateAlarmStatus();
142         if (response == null) {
143             logger.debug("null response/status received during the control of section: {}", section);
144         }
145     }
146
147     public synchronized void sendCommand(String code) {
148         JablotronControlResponse response = sendUserCode(code);
149         scheduler.schedule(this::updateAlarmStatus, 1, TimeUnit.SECONDS);
150
151         if (response == null) {
152             logger.debug("null response/status received during sending a code");
153         }
154     }
155
156     private synchronized @Nullable JablotronControlResponse sendUserCode(String code) {
157         return sendUserCode("sections", "button_1", "partialSet", code);
158     }
159 }