]> git.basschouten.com Git - openhab-addons.git/blob
58ea2ae9e6e76951966b9e597d7e5ef5a3a81039
[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.goecharger.internal.handler;
14
15 import static org.openhab.binding.goecharger.internal.GoEChargerBindingConstants.*;
16
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;
24
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;
42
43 import com.google.gson.Gson;
44 import com.google.gson.JsonSyntaxException;
45
46 /**
47  * The {@link GoEChargerBaseHandler} is responsible for handling commands, which are
48  * sent to one of the channels.
49  *
50  * @author Samuel Brucksch - Initial contribution
51  * @author Reinhard Plaim - Adapt to use API version 2
52  */
53 @NonNullByDefault
54 public abstract class GoEChargerBaseHandler extends BaseThingHandler {
55
56     private final Logger logger = LoggerFactory.getLogger(GoEChargerBaseHandler.class);
57
58     protected @Nullable GoEChargerConfiguration config;
59
60     protected List<String> allChannels = new ArrayList<>();
61
62     protected final Gson gson = new Gson();
63
64     private @Nullable ScheduledFuture<?> refreshJob;
65
66     protected final HttpClient httpClient;
67
68     public GoEChargerBaseHandler(Thing thing, HttpClient httpClient) {
69         super(thing);
70         this.httpClient = httpClient;
71     }
72
73     protected State getValue(String channelId, GoEStatusResponseBaseDTO goeResponseBase) {
74         switch (channelId) {
75             case MAX_CURRENT:
76                 if (goeResponseBase.maxCurrent == null) {
77                     return UnDefType.UNDEF;
78                 }
79                 return new QuantityType<>(goeResponseBase.maxCurrent, Units.AMPERE);
80             case CABLE_ENCODING:
81                 if (goeResponseBase.cableEncoding == null) {
82                     return UnDefType.UNDEF;
83                 }
84                 return new QuantityType<>(goeResponseBase.cableEncoding, Units.AMPERE);
85             case FIRMWARE:
86                 if (goeResponseBase.firmware == null) {
87                     return UnDefType.UNDEF;
88                 }
89                 return new StringType(goeResponseBase.firmware);
90         }
91         return UnDefType.UNDEF;
92     }
93
94     @Override
95     public void handleCommand(ChannelUID channelUID, Command command) {
96     }
97
98     @Override
99     public void initialize() {
100         config = getConfigAs(GoEChargerConfiguration.class);
101         allChannels = getThing().getChannels().stream().map(channel -> channel.getUID().getId())
102                 .collect(Collectors.toList());
103
104         logger.debug("Number of channels found: {}", allChannels.size());
105
106         updateStatus(ThingStatus.UNKNOWN);
107
108         startAutomaticRefresh();
109         logger.debug("Finished initializing!");
110     }
111
112     @Nullable
113     protected GoEStatusResponseBaseDTO getGoEData()
114             throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException {
115         return null;
116     }
117
118     protected void updateChannelsAndStatus(@Nullable GoEStatusResponseBaseDTO goeResponse, @Nullable String message) {
119     }
120
121     private void refresh() {
122         synchronized (this) {
123             // Request new GoE data
124             try {
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());
132             }
133         }
134     }
135
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);
143             }
144         }
145     }
146
147     @Override
148     public void dispose() {
149         logger.debug("Disposing the Go-eCharger handler.");
150
151         final ScheduledFuture<?> refreshJob = this.refreshJob;
152         if (refreshJob != null && !refreshJob.isCancelled()) {
153             refreshJob.cancel(true);
154             this.refreshJob = null;
155         }
156     }
157 }