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.generacmobilelink.internal.handler;
15 import java.time.LocalDate;
16 import java.time.ZoneId;
17 import java.time.format.DateTimeFormatter;
18 import java.time.format.DateTimeParseException;
20 import javax.measure.quantity.Time;
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;
41 * The {@link GeneracMobileLinkGeneratorHandler} is responsible for updating a generator things's channels
43 * @author Dan Cunningham - Initial contribution
46 public class GeneracMobileLinkGeneratorHandler extends BaseThingHandler {
47 private final Logger logger = LoggerFactory.getLogger(GeneracMobileLinkGeneratorHandler.class);
48 private @Nullable GeneratorStatusDTO status;
50 public GeneracMobileLinkGeneratorHandler(Thing thing) {
55 public void handleCommand(ChannelUID channelUID, Command command) {
56 if (command instanceof RefreshType) {
62 public void initialize() {
63 updateStatus(ThingStatus.UNKNOWN);
66 protected void updateGeneratorStatus(GeneratorStatusDTO status) {
68 updateStatus(ThingStatus.ONLINE);
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));
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);
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));