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.goecharger.internal.handler;
15 import static org.openhab.binding.goecharger.internal.GoEChargerBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.goecharger.internal.GoEChargerConfiguration;
29 import org.openhab.binding.goecharger.internal.api.GoEStatusResponseBaseDTO;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.State;
39 import org.openhab.core.types.UnDefType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.gson.Gson;
44 import com.google.gson.JsonSyntaxException;
47 * The {@link GoEChargerBaseHandler} is responsible for handling commands, which are
48 * sent to one of the channels.
50 * @author Samuel Brucksch - Initial contribution
51 * @author Reinhard Plaim - Adapt to use API version 2
54 public abstract class GoEChargerBaseHandler extends BaseThingHandler {
56 private final Logger logger = LoggerFactory.getLogger(GoEChargerBaseHandler.class);
58 protected @Nullable GoEChargerConfiguration config;
60 protected List<String> allChannels = new ArrayList<>();
62 protected final Gson gson = new Gson();
64 private @Nullable ScheduledFuture<?> refreshJob;
66 protected final HttpClient httpClient;
68 public GoEChargerBaseHandler(Thing thing, HttpClient httpClient) {
70 this.httpClient = httpClient;
73 protected State getValue(String channelId, GoEStatusResponseBaseDTO goeResponseBase) {
76 if (goeResponseBase.maxCurrent == null) {
77 return UnDefType.UNDEF;
79 return new QuantityType<>(goeResponseBase.maxCurrent, Units.AMPERE);
81 if (goeResponseBase.cableEncoding == null) {
82 return UnDefType.UNDEF;
84 return new QuantityType<>(goeResponseBase.cableEncoding, Units.AMPERE);
86 if (goeResponseBase.firmware == null) {
87 return UnDefType.UNDEF;
89 return new StringType(goeResponseBase.firmware);
91 return UnDefType.UNDEF;
95 public void handleCommand(ChannelUID channelUID, Command command) {
99 public void initialize() {
100 config = getConfigAs(GoEChargerConfiguration.class);
101 allChannels = getThing().getChannels().stream().map(channel -> channel.getUID().getId())
102 .collect(Collectors.toList());
104 logger.debug("Number of channels found: {}", allChannels.size());
106 updateStatus(ThingStatus.UNKNOWN);
108 startAutomaticRefresh();
109 logger.debug("Finished initializing!");
113 protected GoEStatusResponseBaseDTO getGoEData()
114 throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException {
118 protected void updateChannelsAndStatus(@Nullable GoEStatusResponseBaseDTO goeResponse, @Nullable String message) {
121 private void refresh() {
122 synchronized (this) {
123 // Request new GoE data
125 GoEStatusResponseBaseDTO goeResponse = getGoEData();
126 updateChannelsAndStatus(goeResponse, null);
127 } catch (InterruptedException ie) {
128 Thread.currentThread().interrupt();
129 updateChannelsAndStatus(null, ie.getMessage());
130 } catch (TimeoutException | ExecutionException | JsonSyntaxException e) {
131 updateChannelsAndStatus(null, e.getMessage());
136 private void startAutomaticRefresh() {
137 synchronized (this) {
138 if (refreshJob == null || refreshJob.isCancelled()) {
139 GoEChargerConfiguration config = getConfigAs(GoEChargerConfiguration.class);
140 int delay = config.refreshInterval.intValue();
141 logger.debug("Running refresh job with delay {} s", delay);
142 refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, delay, TimeUnit.SECONDS);
148 public void dispose() {
149 logger.debug("Disposing the Go-eCharger handler.");
151 final ScheduledFuture<?> refreshJob = this.refreshJob;
152 if (refreshJob != null && !refreshJob.isCancelled()) {
153 refreshJob.cancel(true);
154 this.refreshJob = null;