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