]> git.basschouten.com Git - openhab-addons.git/blob
23b027dd5619948025f809e61e174372dd0fecbb
[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.enphase.internal.handler;
14
15 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.enphase.internal.MessageTranslator;
20 import org.openhab.binding.enphase.internal.dto.InventoryJsonDTO.DeviceDTO;
21 import org.openhab.core.library.types.OpenClosedType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  * The {@link EnphaseInverterHandler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Hilbrand Bouwkamp - Initial contribution
33  */
34 @NonNullByDefault
35 public class EnphaseRelayHandler extends EnphaseDeviceHandler {
36
37     public EnphaseRelayHandler(final Thing thing, MessageTranslator messageTranslator) {
38         super(thing, messageTranslator);
39     }
40
41     @Override
42     public void handleCommand(final ChannelUID channelUID, final Command command) {
43         if (command instanceof RefreshType) {
44             final String channelId = channelUID.getId();
45
46             switch (channelId) {
47                 case RELAY_CHANNEL_RELAY:
48                     refreshRelayChannel(lastKnownDeviceState);
49                     break;
50                 case RELAY_CHANNEL_LINE_1_CONNECTED:
51                     refreshLine1Connect(lastKnownDeviceState);
52                     break;
53                 case RELAY_CHANNEL_LINE_2_CONNECTED:
54                     refreshLine2Connect(lastKnownDeviceState);
55                     break;
56                 case RELAY_CHANNEL_LINE_3_CONNECTED:
57                     refreshLine3Connect(lastKnownDeviceState);
58                     break;
59                 default:
60                     super.handleCommandRefresh(channelId);
61                     break;
62             }
63         }
64     }
65
66     private void refreshRelayChannel(@Nullable final DeviceDTO deviceDTO) {
67         updateState(RELAY_CHANNEL_RELAY, deviceDTO == null ? UnDefType.UNDEF
68                 : (RELAY_STATUS_CLOSED.equals(deviceDTO.relay) ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
69     }
70
71     private void refreshLine1Connect(@Nullable final DeviceDTO deviceDTO) {
72         updateState(RELAY_CHANNEL_LINE_1_CONNECTED, deviceDTO == null ? UnDefType.UNDEF
73                 : (deviceDTO.line1Connected ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
74     }
75
76     private void refreshLine2Connect(@Nullable final DeviceDTO deviceDTO) {
77         updateState(RELAY_CHANNEL_LINE_2_CONNECTED, deviceDTO == null ? UnDefType.UNDEF
78                 : (deviceDTO.line2Connected ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
79     }
80
81     private void refreshLine3Connect(@Nullable final DeviceDTO deviceDTO) {
82         updateState(RELAY_CHANNEL_LINE_3_CONNECTED, deviceDTO == null ? UnDefType.UNDEF
83                 : (deviceDTO.line3Connected ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
84     }
85
86     @Override
87     public void refreshDeviceState(@Nullable final DeviceDTO deviceDTO) {
88         refreshRelayChannel(deviceDTO);
89         refreshLine1Connect(deviceDTO);
90         refreshLine2Connect(deviceDTO);
91         refreshLine3Connect(deviceDTO);
92         super.refreshDeviceState(deviceDTO);
93     }
94 }