]> git.basschouten.com Git - openhab-addons.git/blob
75a0d4c9b39818581b463ff416c22b3b546d2aef
[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.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enphase.internal.EnphaseBindingConstants;
22 import org.openhab.binding.enphase.internal.MessageTranslator;
23 import org.openhab.binding.enphase.internal.dto.InventoryJsonDTO.DeviceDTO;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.types.UnDefType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Generic base Thing handler for different Enphase devices.
36  *
37  * @author Hilbrand Bouwkamp - Initial contribution
38  */
39 @NonNullByDefault
40 abstract class EnphaseDeviceHandler extends BaseThingHandler {
41     protected final Logger logger = LoggerFactory.getLogger(getClass());
42
43     protected @Nullable DeviceDTO lastKnownDeviceState;
44
45     private final MessageTranslator messageTranslator;
46     private String serialNumber = "";
47
48     public EnphaseDeviceHandler(final Thing thing, MessageTranslator messageTranslator) {
49         super(thing);
50         this.messageTranslator = messageTranslator;
51     }
52
53     /**
54      * @return the serialNumber
55      */
56     public String getSerialNumber() {
57         return serialNumber;
58     }
59
60     protected void handleCommandRefresh(final String channelId) {
61         switch (channelId) {
62             case DEVICE_CHANNEL_STATUS:
63                 refreshStatus(lastKnownDeviceState);
64                 break;
65             case DEVICE_CHANNEL_PRODUCING:
66                 refreshProducing(lastKnownDeviceState);
67                 break;
68             case DEVICE_CHANNEL_COMMUNICATING:
69                 refreshCommunicating(lastKnownDeviceState);
70                 break;
71             case DEVICE_CHANNEL_PROVISIONED:
72                 refreshProvisioned(lastKnownDeviceState);
73                 break;
74             case DEVICE_CHANNEL_OPERATING:
75                 refreshOperating(lastKnownDeviceState);
76                 break;
77         }
78     }
79
80     private void refreshStatus(final @Nullable DeviceDTO deviceDTO) {
81         updateState(DEVICE_CHANNEL_STATUS, deviceDTO == null ? UnDefType.UNDEF
82                 : new StringType(messageTranslator.translate((deviceDTO.getDeviceStatus()))));
83     }
84
85     private void refreshProducing(final @Nullable DeviceDTO deviceDTO) {
86         updateState(DEVICE_CHANNEL_PRODUCING,
87                 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.producing));
88     }
89
90     private void refreshCommunicating(final @Nullable DeviceDTO deviceDTO) {
91         updateState(DEVICE_CHANNEL_COMMUNICATING,
92                 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.communicating));
93     }
94
95     private void refreshProvisioned(final @Nullable DeviceDTO deviceDTO) {
96         updateState(DEVICE_CHANNEL_PROVISIONED,
97                 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.provisioned));
98     }
99
100     private void refreshOperating(final @Nullable DeviceDTO deviceDTO) {
101         updateState(DEVICE_CHANNEL_OPERATING,
102                 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.operating));
103     }
104
105     public void refreshDeviceState(final @Nullable DeviceDTO deviceDTO) {
106         refreshStatus(deviceDTO);
107         refreshProducing(deviceDTO);
108         refreshCommunicating(deviceDTO);
109         refreshProvisioned(deviceDTO);
110         refreshOperating(deviceDTO);
111         refreshProperties(deviceDTO);
112         refreshDeviceStatus(deviceDTO != null);
113     }
114
115     public void refreshDeviceStatus(final boolean hasData) {
116         if (isInitialized()) {
117             if (hasData) {
118                 if (getThing().getStatus() != ThingStatus.ONLINE) {
119                     updateStatus(ThingStatus.ONLINE);
120                 }
121             } else {
122                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
123                         messageTranslator.translate(ERROR_NODATA));
124             }
125         }
126     }
127
128     private void refreshProperties(@Nullable final DeviceDTO deviceDTO) {
129         if (deviceDTO != null) {
130             final Map<String, String> properties = editProperties();
131
132             properties.put(DEVICE_PROPERTY_PART_NUMBER, deviceDTO.partNumber);
133             updateProperties(properties);
134         }
135     }
136
137     @Override
138     public void initialize() {
139         serialNumber = (String) getConfig().get(EnphaseBindingConstants.CONFIG_SERIAL_NUMBER);
140         if (!EnphaseBindingConstants.isValidSerial(serialNumber)) {
141             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Serial Number is not valid");
142         } else {
143             updateStatus(ThingStatus.UNKNOWN);
144         }
145     }
146 }