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