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.jablotron.internal.handler;
15 import static org.openhab.binding.jablotron.JablotronBindingConstants.*;
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.time.format.DateTimeFormatter;
21 import java.util.List;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.jablotron.internal.config.JablotronDeviceConfig;
28 import org.openhab.binding.jablotron.internal.model.*;
29 import org.openhab.core.cache.ExpiringCache;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.thing.*;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.State;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.Gson;
41 * The {@link JablotronAlarmHandler} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Ondrej Pecta - Initial contribution
47 public abstract class JablotronAlarmHandler extends BaseThingHandler {
49 private final Logger logger = LoggerFactory.getLogger(getClass());
51 protected final Gson gson = new Gson();
53 protected JablotronDeviceConfig thingConfig = new JablotronDeviceConfig();
55 private String lastWarningTime = "";
57 protected String alarmName = "";
59 private boolean inService = false;
61 protected @Nullable ScheduledFuture<?> future = null;
63 protected @Nullable ExpiringCache<JablotronDataUpdateResponse> dataCache;
64 protected ExpiringCache<List<JablotronHistoryDataEvent>> eventCache;
66 public JablotronAlarmHandler(Thing thing, String alarmName) {
68 this.alarmName = alarmName;
69 eventCache = new ExpiringCache<>(CACHE_TIMEOUT_MS, this::sendGetEventHistory);
73 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
74 super.bridgeStatusChanged(bridgeStatusInfo);
75 if (ThingStatus.UNINITIALIZED == bridgeStatusInfo.getStatus()) {
81 public void dispose() {
87 public void initialize() {
88 thingConfig = getConfigAs(JablotronDeviceConfig.class);
89 future = scheduler.scheduleWithFixedDelay(this::updateAlarmStatus, 1, thingConfig.getRefresh(),
91 updateStatus(ThingStatus.ONLINE);
94 public boolean isInService() {
98 public String getAlarmName() {
102 protected abstract void updateSegmentStatus(JablotronServiceDetailSegment segment);
104 protected void updateSegmentStatus(String segmentName, @Nullable JablotronDataUpdateResponse dataUpdate) {
105 if (dataUpdate == null || !dataUpdate.isStatus()) {
108 List<JablotronServiceData> serviceData = dataUpdate.getData().getServiceData();
109 for (JablotronServiceData data : serviceData) {
110 if (!thingConfig.getServiceId().equals(data.getServiceId())) {
113 List<JablotronService> services = data.getData();
114 for (JablotronService service : services) {
115 JablotronServiceDetail detail = service.getData();
116 for (JablotronServiceDetailSegment segment : detail.getSegments()) {
117 if (segmentName.toUpperCase().equals(segment.getSegmentId())) {
118 updateSegmentStatus(segment);
125 private void cleanup() {
126 logger.debug("doing cleanup...");
127 ScheduledFuture<?> localFuture = future;
128 if (localFuture != null) {
129 localFuture.cancel(true);
133 protected State getCheckTime() {
134 ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
135 return new DateTimeType(zdt);
138 protected synchronized @Nullable JablotronDataUpdateResponse sendGetStatusRequest() {
139 JablotronBridgeHandler handler = getBridgeHandler();
140 if (handler != null) {
141 return handler.sendGetStatusRequest(getThing());
146 protected synchronized boolean updateAlarmStatus() {
147 JablotronDataUpdateResponse dataUpdate = sendGetStatusRequest();
148 if (dataUpdate == null) {
152 if (dataUpdate.isStatus()) {
153 updateState(CHANNEL_LAST_CHECK_TIME, getCheckTime());
154 List<JablotronServiceData> serviceData = dataUpdate.getData().getServiceData();
155 for (JablotronServiceData data : serviceData) {
156 if (!thingConfig.getServiceId().equals(data.getServiceId())) {
159 List<JablotronService> services = data.getData();
160 for (JablotronService service : services) {
161 JablotronServiceDetail detail = service.getData();
162 for (JablotronServiceDetailSegment segment : detail.getSegments()) {
163 updateSegmentStatus(segment);
169 logger.debug("Error during alarm status update: {}", dataUpdate.getErrorMessage());
172 List<JablotronHistoryDataEvent> events = sendGetEventHistory();
173 if (events != null && events.size() > 0) {
174 JablotronHistoryDataEvent event = events.get(0);
175 updateLastEvent(event);
181 protected @Nullable List<JablotronHistoryDataEvent> sendGetEventHistory() {
182 return sendGetEventHistory(alarmName);
185 private @Nullable List<JablotronHistoryDataEvent> sendGetEventHistory(String alarm) {
186 JablotronBridgeHandler handler = getBridgeHandler();
187 if (handler != null) {
188 return handler.sendGetEventHistory(getThing(), alarm);
193 protected void updateLastEvent(JablotronHistoryDataEvent event) {
194 updateState(CHANNEL_LAST_EVENT_TIME, new DateTimeType(getZonedDateTime(event.getDate())));
195 updateState(CHANNEL_LAST_EVENT, new StringType(event.getEventText()));
196 updateState(CHANNEL_LAST_EVENT_CLASS, new StringType(event.getIconType()));
197 updateState(CHANNEL_LAST_EVENT_INVOKER, new StringType(event.getInvokerName()));
199 // oasis does not have sections
200 if (getThing().getChannel(CHANNEL_LAST_EVENT_SECTION) != null) {
201 updateState(CHANNEL_LAST_EVENT_SECTION, new StringType(event.getSectionName()));
205 protected void updateEventChannel(String channel) {
206 List<JablotronHistoryDataEvent> events = eventCache.getValue();
207 if (events != null && events.size() > 0) {
208 JablotronHistoryDataEvent event = events.get(0);
210 case CHANNEL_LAST_EVENT_TIME:
211 updateState(CHANNEL_LAST_EVENT_TIME, new DateTimeType(getZonedDateTime(event.getDate())));
213 case CHANNEL_LAST_EVENT:
214 updateState(CHANNEL_LAST_EVENT, new StringType(event.getEventText()));
216 case CHANNEL_LAST_EVENT_CLASS:
217 updateState(CHANNEL_LAST_EVENT_CLASS, new StringType(event.getIconType()));
219 case CHANNEL_LAST_EVENT_INVOKER:
220 updateState(CHANNEL_LAST_EVENT_INVOKER, new StringType(event.getInvokerName()));
222 case CHANNEL_LAST_EVENT_SECTION:
223 updateState(CHANNEL_LAST_EVENT_SECTION, new StringType(event.getSectionName()));
229 public ZonedDateTime getZonedDateTime(String date) {
230 return ZonedDateTime.parse(date.substring(0, 22) + ":" + date.substring(22, 24),
231 DateTimeFormatter.ISO_DATE_TIME);
234 protected @Nullable JablotronControlResponse sendUserCode(String section, String key, String status, String code) {
235 JablotronBridgeHandler handler = getBridgeHandler();
236 if (handler != null) {
237 return handler.sendUserCode(getThing(), section, key, status, code);
242 protected @Nullable JablotronBridgeHandler getBridgeHandler() {
243 Bridge br = getBridge();
244 if (br != null && br.getHandler() != null) {
245 return (JablotronBridgeHandler) br.getHandler();
250 public void setStatus(ThingStatus status, ThingStatusDetail detail, String message) {
251 updateStatus(status, detail, message);
254 public void triggerAlarm(JablotronDiscoveredService service) {
255 if (!service.getWarningTime().equals(lastWarningTime)) {
256 logger.debug("Service id: {} is triggering an alarm: {}", thing.getUID().getId(), service.getWarning());
257 lastWarningTime = service.getWarningTime();
258 triggerChannel(CHANNEL_ALARM, service.getWarning());
262 public void setInService(boolean inService) {
263 this.inService = inService;