]> git.basschouten.com Git - openhab-addons.git/blob
d7fb8c814bc5e86dcc36a25d70f5d19eac2f8d06
[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.satel.internal.handler;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.LinkedList;
20 import java.util.Optional;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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;
44
45 /**
46  * The {@link SatelSystemHandler} is responsible for handling commands, which are
47  * sent to one of the system channels.
48  *
49  * @author Krzysztof Goworek - Initial contribution
50  */
51 @NonNullByDefault
52 public class SatelSystemHandler extends SatelStateThingHandler {
53
54     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SYSTEM);
55
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());
60
61     private final Logger logger = LoggerFactory.getLogger(SatelSystemHandler.class);
62
63     public SatelSystemHandler(Thing thing) {
64         super(thing);
65     }
66
67     @Override
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());
72             });
73         } else {
74             super.handleCommand(channelUID, command);
75         }
76     }
77
78     @Override
79     public void incomingEvent(IntegraStatusEvent event) {
80         logger.trace("Handling incoming event: {}", event);
81         if (getThingConfig().isCommandOnly()) {
82             return;
83         }
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());
93     }
94
95     @Override
96     protected StateType getStateType(String channelId) {
97         return StateType.NONE;
98     }
99
100     @Override
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();
108                 } else {
109                     return Optional.of(new ClearTroublesCommand(bridgeHandler.getUserCode()));
110                 }
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;
117                 }
118                 if (dateTime != null) {
119                     return Optional.of(new SetClockCommand(dateTime.getZonedDateTime()
120                             .withZoneSameInstant(bridgeHandler.getZoneId()).toLocalDateTime(),
121                             bridgeHandler.getUserCode()));
122                 }
123                 break;
124             default:
125                 // do nothing for other types of status
126                 break;
127         }
128
129         return Optional.empty();
130     }
131
132     @Override
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));
140         if (needRefresh) {
141             result.add(new IntegraStatusCommand());
142         }
143
144         return result;
145     }
146 }