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 static org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants.*;
17 import java.util.Arrays;
19 import javax.measure.quantity.Dimensionless;
20 import javax.measure.quantity.ElectricPotential;
21 import javax.measure.quantity.Time;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.generacmobilelink.internal.dto.Apparatus;
26 import org.openhab.binding.generacmobilelink.internal.dto.ApparatusDetail;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
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.RefreshType;
39 import org.openhab.core.types.UnDefType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link GeneracMobileLinkGeneratorHandler} is responsible for updating a generator things's channels
46 * @author Dan Cunningham - Initial contribution
49 public class GeneracMobileLinkGeneratorHandler extends BaseThingHandler {
50 private final Logger logger = LoggerFactory.getLogger(GeneracMobileLinkGeneratorHandler.class);
52 private @Nullable Apparatus apparatus;
53 private @Nullable ApparatusDetail apparatusDetail;
55 public GeneracMobileLinkGeneratorHandler(Thing thing) {
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 if (command instanceof RefreshType) {
67 public void initialize() {
68 updateStatus(ThingStatus.UNKNOWN);
71 protected void updateGeneratorStatus(Apparatus apparatus, ApparatusDetail apparatusDetail) {
72 this.apparatus = apparatus;
73 this.apparatusDetail = apparatusDetail;
74 updateStatus(ThingStatus.ONLINE);
78 private void updateState() {
79 Apparatus apparatus = this.apparatus;
80 ApparatusDetail apparatusDetail = this.apparatusDetail;
81 if (apparatus == null || apparatusDetail == null) {
84 updateState(CHANNEL_HERO_IMAGE_URL, new StringType(apparatusDetail.heroImageUrl));
85 updateState(CHANNEL_STATUS_LABEL, new StringType(apparatusDetail.statusLabel));
86 updateState(CHANNEL_STATUS_TEXT, new StringType(apparatusDetail.statusText));
87 updateState(CHANNEL_ACTIVATION_DATE, new DateTimeType(apparatusDetail.activationDate));
88 updateState(CHANNEL_DEVICE_SSID, new StringType(apparatusDetail.deviceSsid));
89 updateState(CHANNEL_STATUS, new DecimalType(apparatusDetail.apparatusStatus));
90 updateState(CHANNEL_IS_CONNECTED, OnOffType.from(apparatusDetail.isConnected));
91 updateState(CHANNEL_IS_CONNECTING, OnOffType.from(apparatusDetail.isConnecting));
92 updateState(CHANNEL_SHOW_WARNING, OnOffType.from(apparatusDetail.showWarning));
93 updateState(CHANNEL_HAS_MAINTENANCE_ALERT, OnOffType.from(apparatusDetail.hasMaintenanceAlert));
94 updateState(CHANNEL_LAST_SEEN, new DateTimeType(apparatusDetail.lastSeen));
95 updateState(CHANNEL_CONNECTION_TIME, new DateTimeType(apparatusDetail.connectionTimestamp));
96 Arrays.stream(apparatusDetail.properties).filter(p -> p.type == 70).findFirst().ifPresent(p -> {
98 updateState(CHANNEL_RUN_HOURS, new QuantityType<Time>(Integer.parseInt(p.value), Units.HOUR));
99 } catch (NumberFormatException e) {
100 logger.debug("Could not parse runHours {}", p.value);
101 updateState(CHANNEL_RUN_HOURS, UnDefType.UNDEF);
104 Arrays.stream(apparatusDetail.properties).filter(p -> p.type == 69).findFirst().ifPresent(p -> {
106 updateState(CHANNEL_BATTERY_VOLTAGE,
107 new QuantityType<ElectricPotential>(Float.parseFloat(p.value), Units.VOLT));
108 } catch (NumberFormatException e) {
109 logger.debug("Could not parse batteryVoltage {}", p.value);
110 updateState(CHANNEL_BATTERY_VOLTAGE, UnDefType.UNDEF);
113 Arrays.stream(apparatusDetail.properties).filter(p -> p.type == 31).findFirst().ifPresent(p -> {
115 updateState(CHANNEL_HOURS_OF_PROTECTION, new QuantityType<Time>(Float.parseFloat(p.value), Units.HOUR));
116 } catch (NumberFormatException e) {
117 logger.debug("Could not parse hoursOfProtection {}", p.value);
118 updateState(CHANNEL_HOURS_OF_PROTECTION, UnDefType.UNDEF);
121 apparatus.properties.stream().filter(p -> p.type == 3).findFirst().ifPresent(p -> {
123 if (p.value.signalStrength != null) {
124 updateState(CHANNEL_SIGNAL_STRENGH, new QuantityType<Dimensionless>(
125 Integer.parseInt(p.value.signalStrength.replaceAll("%", "")), Units.PERCENT));
127 } catch (NumberFormatException e) {
128 logger.debug("Could not parse signalStrength {}", p.value.signalStrength);
129 updateState(CHANNEL_SIGNAL_STRENGH, UnDefType.UNDEF);