]> git.basschouten.com Git - openhab-addons.git/blob
406eb3d0fee0025c6d3d5a03feaa1cf58cb8867b
[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 java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.enphase.internal.MessageTranslator;
24 import org.openhab.binding.enphase.internal.dto.InverterDTO;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * The {@link EnphaseInverterHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Hilbrand Bouwkamp - Initial contribution
40  */
41 @NonNullByDefault
42 public class EnphaseInverterHandler extends EnphaseDeviceHandler {
43
44     private @Nullable InverterDTO lastKnownState;
45
46     public EnphaseInverterHandler(final Thing thing, MessageTranslator messageTranslator) {
47         super(thing, messageTranslator);
48     }
49
50     @Override
51     public void handleCommand(final ChannelUID channelUID, final Command command) {
52         if (command instanceof RefreshType) {
53             final String channelId = channelUID.getId();
54
55             switch (channelId) {
56                 case INVERTER_CHANNEL_LAST_REPORT_WATTS:
57                     refreshLastReportWatts(lastKnownState);
58                     break;
59                 case INVERTER_CHANNEL_MAX_REPORT_WATTS:
60                     refreshMaxReportWatts(lastKnownState);
61                     break;
62                 case INVERTER_CHANNEL_LAST_REPORT_DATE:
63                     refreshLastReportDate(lastKnownState);
64                     break;
65                 default:
66                     super.handleCommandRefresh(channelId);
67                     break;
68             }
69         }
70     }
71
72     public void refreshInverterChannels(final @Nullable InverterDTO inverterDTO) {
73         refreshLastReportWatts(inverterDTO);
74         refreshMaxReportWatts(inverterDTO);
75         refreshLastReportDate(inverterDTO);
76         lastKnownState = inverterDTO;
77     }
78
79     private void refreshLastReportWatts(final @Nullable InverterDTO inverterDTO) {
80         updateState(INVERTER_CHANNEL_LAST_REPORT_WATTS,
81                 inverterDTO == null ? UnDefType.UNDEF : new QuantityType<>(inverterDTO.lastReportWatts, Units.WATT));
82     }
83
84     private void refreshMaxReportWatts(final @Nullable InverterDTO inverterDTO) {
85         updateState(INVERTER_CHANNEL_MAX_REPORT_WATTS,
86                 inverterDTO == null ? UnDefType.UNDEF : new QuantityType<>(inverterDTO.maxReportWatts, Units.WATT));
87     }
88
89     private void refreshLastReportDate(final @Nullable InverterDTO inverterDTO) {
90         final State state;
91
92         if (inverterDTO == null) {
93             state = UnDefType.UNDEF;
94         } else {
95             final Instant instant = Instant.ofEpochSecond(inverterDTO.lastReportDate);
96             final ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
97             logger.trace("[{}] Epoch time {}, zonedDateTime: {}", getThing().getUID(), inverterDTO.lastReportDate,
98                     zonedDateTime);
99             state = new DateTimeType(zonedDateTime);
100         }
101         updateState(INVERTER_CHANNEL_LAST_REPORT_DATE, state);
102     }
103 }