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