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.solaredge.internal.handler;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicReference;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.openhab.binding.solaredge.internal.AtomicReferenceTrait;
24 import org.openhab.binding.solaredge.internal.config.SolarEdgeConfiguration;
25 import org.openhab.binding.solaredge.internal.connector.WebInterface;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link SolarEdgeBaseHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Alexander Friese - initial contribution
45 public abstract class SolarEdgeBaseHandler extends BaseThingHandler implements SolarEdgeHandler, AtomicReferenceTrait {
46 private final Logger logger = LoggerFactory.getLogger(SolarEdgeBaseHandler.class);
48 private final long LIVE_POLLING_INITIAL_DELAY = 1;
49 private final long AGGREGATE_POLLING_INITIAL_DELAY = 2;
52 * Interface object for querying the Solaredge web interface
54 private WebInterface webInterface;
57 * Schedule for polling live data
59 private final AtomicReference<@Nullable Future<?>> liveDataPollingJobReference;
62 * Schedule for polling aggregate data
64 private final AtomicReference<@Nullable Future<?>> aggregateDataPollingJobReference;
66 public SolarEdgeBaseHandler(Thing thing, HttpClient httpClient) {
68 this.webInterface = new WebInterface(scheduler, this, httpClient);
69 this.liveDataPollingJobReference = new AtomicReference<>(null);
70 this.aggregateDataPollingJobReference = new AtomicReference<>(null);
74 public void handleCommand(ChannelUID channelUID, Command command) {
75 logger.debug("command for {}: {}", channelUID, command);
76 // write access is not supported.
80 public void initialize() {
81 logger.debug("About to initialize SolarEdge");
82 SolarEdgeConfiguration config = getConfiguration();
83 logger.debug("Solaredge initialized with configuration: {}", config);
87 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "waiting for web api login");
93 private void startPolling() {
94 updateJobReference(liveDataPollingJobReference,
95 scheduler.scheduleWithFixedDelay(new SolarEdgeLiveDataPolling(this), LIVE_POLLING_INITIAL_DELAY,
96 getConfiguration().getLiveDataPollingInterval(), TimeUnit.MINUTES));
98 updateJobReference(aggregateDataPollingJobReference,
99 scheduler.scheduleWithFixedDelay(new SolarEdgeAggregateDataPolling(this),
100 AGGREGATE_POLLING_INITIAL_DELAY, getConfiguration().getAggregateDataPollingInterval(),
105 * Disposes the bridge.
108 public void dispose() {
109 logger.debug("Handler disposed.");
111 cancelJobReference(liveDataPollingJobReference);
112 cancelJobReference(aggregateDataPollingJobReference);
114 webInterface.dispose();
118 public WebInterface getWebInterface() {
123 * will update all channels provided in the map
126 public void updateChannelStatus(Map<Channel, State> values) {
127 logger.debug("Handling channel update.");
129 for (Channel channel : values.keySet()) {
130 if (getChannels().contains(channel)) {
131 State value = values.get(channel);
133 logger.debug("Channel is to be updated: {}: {}", channel.getUID().getAsString(), value);
134 updateState(channel.getUID(), value);
136 logger.debug("Value is null or not provided by solaredge (channel: {})",
137 channel.getUID().getAsString());
138 updateState(channel.getUID(), UnDefType.UNDEF);
141 logger.debug("Could not identify channel: {} for model {}", channel.getUID().getAsString(),
142 getThing().getThingTypeUID().getAsString());
148 public void setStatusInfo(ThingStatus status, ThingStatusDetail statusDetail, String description) {
149 super.updateStatus(status, statusDetail, description);
153 public SolarEdgeConfiguration getConfiguration() {
154 return this.getConfigAs(SolarEdgeConfiguration.class);