]> git.basschouten.com Git - openhab-addons.git/blob
f52f29135f0cea87cad91cdd64bf48adcce4cea9
[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.solarmax.internal;
14
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
18 import javax.measure.Unit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.solarmax.internal.connector.SolarMaxCommandKey;
23 import org.openhab.binding.solarmax.internal.connector.SolarMaxConnector;
24 import org.openhab.binding.solarmax.internal.connector.SolarMaxData;
25 import org.openhab.binding.solarmax.internal.connector.SolarMaxException;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link SolarMaxHandler} is responsible for handling commands, which are
41  * sent to one of the channels.
42  *
43  * @author Jamie Townsend - Initial contribution
44  */
45 @NonNullByDefault
46 public class SolarMaxHandler extends BaseThingHandler {
47
48     private final Logger logger = LoggerFactory.getLogger(SolarMaxHandler.class);
49
50     private SolarMaxConfiguration config = getConfigAs(SolarMaxConfiguration.class);
51
52     @Nullable
53     private ScheduledFuture<?> pollingJob;
54
55     public SolarMaxHandler(final Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void handleCommand(final ChannelUID channelUID, final Command command) {
61         // Read only
62     }
63
64     @Override
65     public void initialize() {
66         config = getConfigAs(SolarMaxConfiguration.class);
67
68         configurePolling(); // Setup the scheduler
69     }
70
71     /**
72      * This is called to start the refresh job and also to reset that refresh job when a config change is done.
73      */
74     private void configurePolling() {
75         logger.debug("Polling data from {} at {}:{} every {} seconds ", getThing().getUID(), this.config.host,
76                 this.config.portNumber, this.config.refreshInterval);
77         if (this.config.refreshInterval > 0) {
78             if (pollingJob == null || pollingJob.isCancelled()) {
79                 pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 0, this.config.refreshInterval,
80                         TimeUnit.SECONDS);
81             }
82         }
83     }
84
85     @Override
86     public void dispose() {
87         if (pollingJob != null && !pollingJob.isCancelled()) {
88             pollingJob.cancel(true);
89         }
90         pollingJob = null;
91     }
92
93     /**
94      * Polling event used to get data from the SolarMax device
95      */
96     private Runnable pollingRunnable = () -> {
97         updateValuesFromDevice();
98     };
99
100     private synchronized void updateValuesFromDevice() {
101         logger.debug("Updating data from {} at {}:{} ", getThing().getUID(), this.config.host, this.config.portNumber);
102         // get the data from the SolarMax device
103         try {
104             SolarMaxData solarMaxData = SolarMaxConnector.getAllValuesFromSolarMax(config.host, config.portNumber);
105
106             if (solarMaxData.wasCommunicationSuccessful()) {
107                 updateStatus(ThingStatus.ONLINE);
108                 updateProperties(solarMaxData);
109                 updateChannels(solarMaxData);
110                 return;
111             }
112         } catch (SolarMaxException e) {
113             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
114                     "Communication error with the device: " + e.getMessage());
115         }
116     }
117
118     /*
119      * Update the channels
120      */
121     private void updateChannels(SolarMaxData solarMaxData) {
122         logger.debug("Updating all channels");
123         for (SolarMaxChannel solarMaxChannel : SolarMaxChannel.values()) {
124             String channelId = solarMaxChannel.getChannelId();
125             Channel channel = getThing().getChannel(channelId);
126
127             if (channelId.equals(SolarMaxChannel.CHANNEL_LAST_UPDATED.getChannelId())) {
128                 // CHANNEL_LAST_UPDATED shows when the device was last read and does not come from the device, so handle
129                 // it specially
130                 State state = solarMaxData.getDataDateTime();
131                 logger.debug("Update channel state: {} - {}", channelId, state);
132                 updateState(channel.getUID(), state);
133
134             } else {
135                 // must be somthing to collect from the device, so...
136                 if (solarMaxData.has(SolarMaxCommandKey.valueOf(channelId))) {
137                     if (channel == null) {
138                         logger.error("No channel found with id: {}", channelId);
139                     }
140                     State state = convertValueToState(solarMaxData.get(SolarMaxCommandKey.valueOf(channelId)),
141                             solarMaxChannel.getUnit());
142
143                     if (channel != null && state != null) {
144                         logger.debug("Update channel state: {} - {}", channelId, state);
145                         updateState(channel.getUID(), state);
146                     } else {
147                         logger.debug("Error refreshing channel {}: {}", getThing().getUID(), channelId);
148                     }
149                 }
150             }
151         }
152     }
153
154     private @Nullable State convertValueToState(Number value, @Nullable Unit<?> unit) {
155         if (unit == null) {
156             return new DecimalType(value.floatValue());
157         }
158         return new QuantityType<>(value, unit);
159     }
160
161     /*
162      * Update the properties
163      */
164     private void updateProperties(SolarMaxData solarMaxData) {
165         logger.debug("Updating properties");
166         for (SolarMaxProperty solarMaxProperty : SolarMaxProperty.values()) {
167             String propertyId = solarMaxProperty.getPropertyId();
168             Number valNumber = solarMaxData.get(SolarMaxCommandKey.valueOf(propertyId));
169             if (valNumber == null) {
170                 logger.debug("Null value returned for value of {}: {}", getThing().getUID(), propertyId);
171                 continue;
172             }
173
174             // deal with properties
175             if (propertyId.equals(SolarMaxProperty.PROPERTY_BUILD_NUMBER.getPropertyId())
176                     || propertyId.equals(SolarMaxProperty.PROPERTY_SOFTWARE_VERSION.getPropertyId())) {
177                 updateProperty(solarMaxProperty.getPropertyId(), valNumber.toString());
178                 continue;
179             }
180         }
181     }
182 }