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