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