]> git.basschouten.com Git - openhab-addons.git/blob
d0ac23d3c938a7ee86fe94f637f907b52b8430c0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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             case VOLTAGE_L1:
91                 if (goeResponseBase.energy == null) {
92                     return UnDefType.UNDEF;
93                 }
94                 return new QuantityType<>(goeResponseBase.energy[0], Units.VOLT);
95             case VOLTAGE_L2:
96                 if (goeResponseBase.energy == null) {
97                     return UnDefType.UNDEF;
98                 }
99                 return new QuantityType<>(goeResponseBase.energy[1], Units.VOLT);
100             case VOLTAGE_L3:
101                 if (goeResponseBase.energy == null) {
102                     return UnDefType.UNDEF;
103                 }
104                 return new QuantityType<>(goeResponseBase.energy[2], Units.VOLT);
105         }
106         return UnDefType.UNDEF;
107     }
108
109     @Override
110     public void handleCommand(ChannelUID channelUID, Command command) {
111     }
112
113     @Override
114     public void initialize() {
115         config = getConfigAs(GoEChargerConfiguration.class);
116         allChannels = getThing().getChannels().stream().map(channel -> channel.getUID().getId())
117                 .collect(Collectors.toList());
118
119         logger.debug("Number of channels found: {}", allChannels.size());
120
121         updateStatus(ThingStatus.UNKNOWN);
122
123         startAutomaticRefresh();
124         logger.debug("Finished initializing!");
125     }
126
127     @Nullable
128     protected GoEStatusResponseBaseDTO getGoEData()
129             throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException {
130         return null;
131     }
132
133     protected void updateChannelsAndStatus(@Nullable GoEStatusResponseBaseDTO goeResponse, @Nullable String message) {
134     }
135
136     private void refresh() {
137         synchronized (this) {
138             // Request new GoE data
139             try {
140                 GoEStatusResponseBaseDTO goeResponse = getGoEData();
141                 updateChannelsAndStatus(goeResponse, null);
142             } catch (InterruptedException ie) {
143                 Thread.currentThread().interrupt();
144                 updateChannelsAndStatus(null, ie.getMessage());
145             } catch (TimeoutException | ExecutionException | JsonSyntaxException e) {
146                 updateChannelsAndStatus(null, e.getMessage());
147             }
148         }
149     }
150
151     private void startAutomaticRefresh() {
152         synchronized (this) {
153             if (refreshJob == null || refreshJob.isCancelled()) {
154                 GoEChargerConfiguration config = getConfigAs(GoEChargerConfiguration.class);
155                 int delay = config.refreshInterval.intValue();
156                 logger.debug("Running refresh job with delay {} s", delay);
157                 refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, delay, TimeUnit.SECONDS);
158             }
159         }
160     }
161
162     @Override
163     public void dispose() {
164         logger.debug("Disposing the Go-eCharger handler.");
165
166         final ScheduledFuture<?> refreshJob = this.refreshJob;
167         if (refreshJob != null && !refreshJob.isCancelled()) {
168             refreshJob.cancel(true);
169             this.refreshJob = null;
170         }
171     }
172 }