]> git.basschouten.com Git - openhab-addons.git/blob
a02fbf91a5c8bc6b2642610d28952dc7510d5356
[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 static org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants.*;
16
17 import java.util.Arrays;
18
19 import javax.measure.quantity.Dimensionless;
20 import javax.measure.quantity.ElectricPotential;
21 import javax.measure.quantity.Time;
22
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;
42
43 /**
44  * The {@link GeneracMobileLinkGeneratorHandler} is responsible for updating a generator things's channels
45  *
46  * @author Dan Cunningham - Initial contribution
47  */
48 @NonNullByDefault
49 public class GeneracMobileLinkGeneratorHandler extends BaseThingHandler {
50     private final Logger logger = LoggerFactory.getLogger(GeneracMobileLinkGeneratorHandler.class);
51
52     private @Nullable Apparatus apparatus;
53     private @Nullable ApparatusDetail apparatusDetail;
54
55     public GeneracMobileLinkGeneratorHandler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         if (command instanceof RefreshType) {
62             updateState();
63         }
64     }
65
66     @Override
67     public void initialize() {
68         updateStatus(ThingStatus.UNKNOWN);
69     }
70
71     protected void updateGeneratorStatus(Apparatus apparatus, ApparatusDetail apparatusDetail) {
72         this.apparatus = apparatus;
73         this.apparatusDetail = apparatusDetail;
74         updateStatus(ThingStatus.ONLINE);
75         updateState();
76     }
77
78     private void updateState() {
79         Apparatus apparatus = this.apparatus;
80         ApparatusDetail apparatusDetail = this.apparatusDetail;
81         if (apparatus == null || apparatusDetail == null) {
82             return;
83         }
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 -> {
97             try {
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);
102             }
103         });
104         Arrays.stream(apparatusDetail.properties).filter(p -> p.type == 69).findFirst().ifPresent(p -> {
105             try {
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);
111             }
112         });
113         Arrays.stream(apparatusDetail.properties).filter(p -> p.type == 31).findFirst().ifPresent(p -> {
114             try {
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);
119             }
120         });
121         apparatus.properties.stream().filter(p -> p.type == 3).findFirst().ifPresent(p -> {
122             try {
123                 if (p.value.signalStrength != null) {
124                     updateState(CHANNEL_SIGNAL_STRENGH, new QuantityType<Dimensionless>(
125                             Integer.parseInt(p.value.signalStrength.replaceAll("%", "")), Units.PERCENT));
126                 }
127             } catch (NumberFormatException e) {
128                 logger.debug("Could not parse signalStrength {}", p.value.signalStrength);
129                 updateState(CHANNEL_SIGNAL_STRENGH, UnDefType.UNDEF);
130             }
131         });
132     }
133 }