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.satel.internal.handler;
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.LinkedList;
20 import java.util.Optional;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.satel.internal.command.ClearTroublesCommand;
27 import org.openhab.binding.satel.internal.command.IntegraStatusCommand;
28 import org.openhab.binding.satel.internal.command.SatelCommand;
29 import org.openhab.binding.satel.internal.command.SetClockCommand;
30 import org.openhab.binding.satel.internal.event.IntegraStatusEvent;
31 import org.openhab.binding.satel.internal.event.NewStatesEvent;
32 import org.openhab.binding.satel.internal.types.StateType;
33 import org.openhab.core.library.types.DateTimeType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.State;
41 import org.openhab.core.types.UnDefType;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link SatelSystemHandler} is responsible for handling commands, which are
47 * sent to one of the system channels.
49 * @author Krzysztof Goworek - Initial contribution
52 public class SatelSystemHandler extends SatelStateThingHandler {
54 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SYSTEM);
56 private static final Set<String> STATUS_CHANNELS = Stream
57 .of(CHANNEL_DATE_TIME, CHANNEL_SERVICE_MODE, CHANNEL_TROUBLES, CHANNEL_TROUBLES_MEMORY,
58 CHANNEL_ACU100_PRESENT, CHANNEL_INTRX_PRESENT, CHANNEL_GRADE23_SET)
59 .collect(Collectors.toSet());
61 private final Logger logger = LoggerFactory.getLogger(SatelSystemHandler.class);
63 public SatelSystemHandler(Thing thing) {
68 public void handleCommand(ChannelUID channelUID, Command command) {
69 if (CHANNEL_USER_CODE.equals(channelUID.getId()) && command instanceof StringType) {
70 withBridgeHandlerPresent(bridgeHandler -> {
71 bridgeHandler.setUserCode(command.toFullString());
74 super.handleCommand(channelUID, command);
79 public void incomingEvent(IntegraStatusEvent event) {
80 logger.trace("Handling incoming event: {}", event);
81 if (getThingConfig().isCommandOnly()) {
84 updateState(CHANNEL_DATE_TIME,
85 event.getIntegraTime().map(dt -> (State) new DateTimeType(dt.atZone(getBridgeHandler().getZoneId())))
86 .orElse(UnDefType.UNDEF));
87 updateSwitch(CHANNEL_SERVICE_MODE, event.inServiceMode());
88 updateSwitch(CHANNEL_TROUBLES, event.troublesPresent());
89 updateSwitch(CHANNEL_TROUBLES_MEMORY, event.troublesMemory());
90 updateSwitch(CHANNEL_ACU100_PRESENT, event.isAcu100Present());
91 updateSwitch(CHANNEL_INTRX_PRESENT, event.isIntRxPresent());
92 updateSwitch(CHANNEL_GRADE23_SET, event.isGrade23Set());
96 protected StateType getStateType(String channelId) {
97 return StateType.NONE;
101 protected Optional<SatelCommand> convertCommand(ChannelUID channel, Command command) {
102 final SatelBridgeHandler bridgeHandler = getBridgeHandler();
103 switch (channel.getId()) {
104 case CHANNEL_TROUBLES:
105 case CHANNEL_TROUBLES_MEMORY:
106 if (command == OnOffType.ON) {
107 return Optional.empty();
109 return Optional.of(new ClearTroublesCommand(bridgeHandler.getUserCode()));
111 case CHANNEL_DATE_TIME:
112 DateTimeType dateTime = null;
113 if (command instanceof StringType) {
114 dateTime = DateTimeType.valueOf(command.toString());
115 } else if (command instanceof DateTimeType) {
116 dateTime = (DateTimeType) command;
118 if (dateTime != null) {
119 return Optional.of(new SetClockCommand(dateTime.getZonedDateTime()
120 .withZoneSameInstant(bridgeHandler.getZoneId()).toLocalDateTime(),
121 bridgeHandler.getUserCode()));
125 // do nothing for other types of status
129 return Optional.empty();
133 protected Collection<SatelCommand> getRefreshCommands(NewStatesEvent event) {
134 Collection<SatelCommand> result = new LinkedList<>();
135 boolean anyStatusChannelLinked = getThing().getChannels().stream()
136 .filter(channel -> STATUS_CHANNELS.contains(channel.getUID().getId()))
137 .anyMatch(channel -> isLinked(channel.getUID().getId()));
138 boolean needRefresh = anyStatusChannelLinked
139 && (requiresRefresh() || isLinked(CHANNEL_DATE_TIME) || event.isNew(IntegraStatusCommand.COMMAND_CODE));
141 result.add(new IntegraStatusCommand());