]> git.basschouten.com Git - openhab-addons.git/blob
a0751d68b7fa3fde45103d13c0828174bac06e04
[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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20
21 import javax.measure.quantity.Dimensionless;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
26 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.digitaldan.jomnilinkII.Message;
39 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
40 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
41 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
42 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
43 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
44 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
45
46 /**
47  * The {@link HumiditySensorHandler} defines some methods that are used to
48  * interface with an OmniLink Humidity Sensor. This by extension also defines
49  * the Humidity Sensor thing that openHAB will be able to pick up and interface
50  * with.
51  *
52  * @author Craig Hamilton - Initial contribution
53  * @author Ethan Dye - openHAB3 rewrite
54  */
55 @NonNullByDefault
56 public class HumiditySensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
57     private final Logger logger = LoggerFactory.getLogger(HumiditySensorHandler.class);
58     private final int thingID = getThingNumber();
59     public @Nullable String number;
60
61     public HumiditySensorHandler(Thing thing) {
62         super(thing);
63     }
64
65     @Override
66     public void initialize() {
67         super.initialize();
68         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
69         if (bridgeHandler != null) {
70             updateHumiditySensorProperties(bridgeHandler);
71         } else {
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
73                     "Received null bridge while initializing Humidity Sensor!");
74         }
75     }
76
77     private void updateHumiditySensorProperties(OmnilinkBridgeHandler bridgeHandler) {
78         final List<AreaProperties> areas = getAreaProperties();
79         if (areas != null) {
80             for (AreaProperties areaProperties : areas) {
81                 int areaFilter = bitFilterForArea(areaProperties);
82
83                 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
84                         .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
85                         .areaFilter(areaFilter).build();
86
87                 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
88                     Map<String, String> properties = editProperties();
89                     properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
90                     properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
91                     updateProperties(properties);
92                 }
93             }
94         }
95     }
96
97     @Override
98     @SuppressWarnings("unchecked")
99     public void handleCommand(ChannelUID channelUID, Command command) {
100         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
101
102         if (command instanceof RefreshType) {
103             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
104                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
105             return;
106         }
107
108         if (!(command instanceof QuantityType)) {
109             logger.debug("Invalid command: {}, must be QuantityType", command);
110             return;
111         }
112
113         switch (channelUID.getId()) {
114             case CHANNEL_AUX_LOW_SETPOINT:
115                 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_HEAT_LOW_POINT.getNumber(),
116                         TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
117                         thingID);
118                 break;
119             case CHANNEL_AUX_HIGH_SETPOINT:
120                 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_COOL_HIGH_POINT.getNumber(),
121                         TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
122                         thingID);
123                 break;
124             default:
125                 logger.warn("Unknown channel for Humdity Sensor thing: {}", channelUID);
126         }
127     }
128
129     @Override
130     public void updateChannels(ExtendedAuxSensorStatus status) {
131         logger.debug("updateChannels called for Humidity Sensor status: {}", status);
132         updateState(CHANNEL_AUX_HUMIDITY,
133                 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getTemperature()), Units.PERCENT));
134         updateState(CHANNEL_AUX_LOW_SETPOINT,
135                 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getHeatSetpoint()), Units.PERCENT));
136         updateState(CHANNEL_AUX_HIGH_SETPOINT,
137                 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getCoolSetpoint()), Units.PERCENT));
138     }
139
140     @Override
141     protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
142         try {
143             final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
144             if (bridgeHandler != null) {
145                 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
146                         thingID, true);
147                 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
148             } else {
149                 logger.debug("Received null bridge while updating Humidity Sensor status!");
150                 return Optional.empty();
151             }
152         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
153             logger.debug("Received exception while refreshing Humidity Sensor status: {}", e.getMessage());
154             return Optional.empty();
155         }
156     }
157 }