]> git.basschouten.com Git - openhab-addons.git/blob
ea2bb3858b06278ba50e5c71f497c3eece25f1aa
[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.generacmobilelink.internal.handler;
14
15 import java.time.LocalDate;
16 import java.time.ZoneId;
17 import java.time.format.DateTimeFormatter;
18 import java.time.format.DateTimeParseException;
19
20 import javax.measure.quantity.Time;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.generacmobilelink.internal.dto.GeneratorStatusDTO;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link GeneracMobileLinkGeneratorHandler} is responsible for updating a generator things's channels
42  *
43  * @author Dan Cunningham - Initial contribution
44  */
45 @NonNullByDefault
46 public class GeneracMobileLinkGeneratorHandler extends BaseThingHandler {
47     private final Logger logger = LoggerFactory.getLogger(GeneracMobileLinkGeneratorHandler.class);
48     private @Nullable GeneratorStatusDTO status;
49
50     public GeneracMobileLinkGeneratorHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void handleCommand(ChannelUID channelUID, Command command) {
56         if (command instanceof RefreshType) {
57             updateState();
58         }
59     }
60
61     @Override
62     public void initialize() {
63         updateStatus(ThingStatus.UNKNOWN);
64     }
65
66     protected void updateGeneratorStatus(GeneratorStatusDTO status) {
67         this.status = status;
68         updateStatus(ThingStatus.ONLINE);
69         updateState();
70     }
71
72     protected void updateState() {
73         final GeneratorStatusDTO localStatus = status;
74         if (localStatus != null) {
75             updateState("connected", OnOffType.from(localStatus.connected));
76             updateState("greenLight", OnOffType.from(localStatus.greenLightLit));
77             updateState("yellowLight", OnOffType.from(localStatus.yellowLightLit));
78             updateState("redLight", OnOffType.from(localStatus.redLightLit));
79             updateState("blueLight", OnOffType.from(localStatus.blueLightLit));
80             try {
81                 // API returns a format like 12/20/2020
82                 updateState("statusDate",
83                         new DateTimeType(LocalDate
84                                 .parse(localStatus.generatorStatusDate, DateTimeFormatter.ofPattern("MM/dd/yyyy"))
85                                 .atStartOfDay(ZoneId.systemDefault())));
86             } catch (IllegalArgumentException | DateTimeParseException e) {
87                 logger.debug("Could not parse statusDate", e);
88             }
89             updateState("status", new StringType(localStatus.generatorStatus));
90             updateState("currentAlarmDescription", new StringType(localStatus.currentAlarmDescription));
91             updateState("runHours", new QuantityType<Time>(localStatus.runHours, Units.HOUR));
92             updateState("exerciseHours", new QuantityType<Time>(localStatus.exerciseHours, Units.HOUR));
93             updateState("fuelType", new DecimalType(localStatus.fuelType));
94             updateState("fuelLevel", QuantityType.valueOf(localStatus.fuelLevel, Units.PERCENT));
95             updateState("batteryVoltage", new StringType(localStatus.batteryVoltage));
96             updateState("serviceStatus", OnOffType.from(localStatus.generatorServiceStatus));
97         }
98     }
99 }