]> git.basschouten.com Git - openhab-addons.git/blob
d5d58312075ac0cd3bc8b50d09c73b932e19bfb8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.boschshc.internal.devices.twinguard;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Temperature;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
22 import org.openhab.binding.boschshc.internal.devices.twinguard.dto.AirQualityLevelState;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonSyntaxException;
37
38 /**
39  * The {@link BoschSHCHandler} is responsible for handling commands for the TwinGuard handler.
40  *
41  * @author Stefan Kästle - Initial contribution
42  */
43 @NonNullByDefault
44 public class BoschTwinguardHandler extends BoschSHCHandler {
45
46     public BoschTwinguardHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void handleCommand(ChannelUID channelUID, Command command) {
52         Bridge bridge = this.getBridge();
53
54         if (bridge != null) {
55             logger.debug("Handle command for: {} - {}", channelUID.getThingUID(), command);
56
57             if (command instanceof RefreshType) {
58                 AirQualityLevelState state = this.getState("AirQualityLevel", AirQualityLevelState.class);
59                 if (state != null) {
60                     updateAirQualityState(state);
61                 }
62             }
63         } else {
64             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Bridge is NUL");
65         }
66     }
67
68     void updateAirQualityState(AirQualityLevelState state) {
69         updateState(CHANNEL_TEMPERATURE, new QuantityType<Temperature>(state.temperature, SIUnits.CELSIUS));
70         updateState(CHANNEL_TEMPERATURE_RATING, new StringType(state.temperatureRating));
71         updateState(CHANNEL_HUMIDITY, new QuantityType<Dimensionless>(state.humidity, Units.ONE));
72         updateState(CHANNEL_HUMIDITY_RATING, new StringType(state.humidityRating));
73         updateState(CHANNEL_PURITY, new QuantityType<Dimensionless>(state.purity, Units.ONE));
74         updateState(CHANNEL_AIR_DESCRIPTION, new StringType(state.description));
75         updateState(CHANNEL_PURITY_RATING, new StringType(state.purityRating));
76         updateState(CHANNEL_COMBINED_RATING, new StringType(state.combinedRating));
77     }
78
79     @Override
80     public void processUpdate(String id, JsonElement state) throws JsonSyntaxException {
81         logger.debug("Twinguard: received update: {} {}", id, state);
82
83         AirQualityLevelState parsed = GSON.fromJson(state, AirQualityLevelState.class);
84         if (parsed == null) {
85             logger.warn("Received unknown update in in-wall switch: {}", state);
86             return;
87         }
88
89         logger.debug("Parsed switch state of {}: {}", this.getBoschID(), parsed);
90         updateAirQualityState(parsed);
91     }
92 }