2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enphase.internal.handler;
15 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.*;
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;
35 * Generic base Thing handler for different Enphase devices.
37 * @author Hilbrand Bouwkamp - Initial contribution
40 abstract class EnphaseDeviceHandler extends BaseThingHandler {
41 protected final Logger logger = LoggerFactory.getLogger(getClass());
43 protected @Nullable DeviceDTO lastKnownDeviceState;
45 private final MessageTranslator messageTranslator;
46 private String serialNumber = "";
48 public EnphaseDeviceHandler(final Thing thing, MessageTranslator messageTranslator) {
50 this.messageTranslator = messageTranslator;
54 * @return the serialNumber
56 public String getSerialNumber() {
60 protected void handleCommandRefresh(final String channelId) {
62 case DEVICE_CHANNEL_STATUS:
63 refreshStatus(lastKnownDeviceState);
65 case DEVICE_CHANNEL_PRODUCING:
66 refreshProducing(lastKnownDeviceState);
68 case DEVICE_CHANNEL_COMMUNICATING:
69 refreshCommunicating(lastKnownDeviceState);
71 case DEVICE_CHANNEL_PROVISIONED:
72 refreshProvisioned(lastKnownDeviceState);
74 case DEVICE_CHANNEL_OPERATING:
75 refreshOperating(lastKnownDeviceState);
80 private void refreshStatus(final @Nullable DeviceDTO deviceDTO) {
81 updateState(DEVICE_CHANNEL_STATUS, deviceDTO == null ? UnDefType.UNDEF
82 : new StringType(messageTranslator.translate((deviceDTO.getDeviceStatus()))));
85 private void refreshProducing(final @Nullable DeviceDTO deviceDTO) {
86 updateState(DEVICE_CHANNEL_PRODUCING,
87 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.producing));
90 private void refreshCommunicating(final @Nullable DeviceDTO deviceDTO) {
91 updateState(DEVICE_CHANNEL_COMMUNICATING,
92 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.communicating));
95 private void refreshProvisioned(final @Nullable DeviceDTO deviceDTO) {
96 updateState(DEVICE_CHANNEL_PROVISIONED,
97 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.provisioned));
100 private void refreshOperating(final @Nullable DeviceDTO deviceDTO) {
101 updateState(DEVICE_CHANNEL_OPERATING,
102 deviceDTO == null ? UnDefType.UNDEF : OnOffType.from(deviceDTO.operating));
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);
115 public void refreshDeviceStatus(final boolean hasData) {
116 if (isInitialized()) {
118 if (getThing().getStatus() != ThingStatus.ONLINE) {
119 updateStatus(ThingStatus.ONLINE);
122 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
123 messageTranslator.translate(ERROR_NODATA));
128 private void refreshProperties(@Nullable final DeviceDTO deviceDTO) {
129 if (deviceDTO != null) {
130 final Map<String, String> properties = editProperties();
132 properties.put(DEVICE_PROPERTY_PART_NUMBER, deviceDTO.partNumber);
133 updateProperties(properties);
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");
143 updateStatus(ThingStatus.UNKNOWN);