]> git.basschouten.com Git - openhab-addons.git/blob
850e2ad99c4c30dafc61da7abe6f35f0e2c2e539
[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.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 TempSensorHandler} defines some methods that are used to interface
49  * with an OmniLink Temperature Sensor. This by extension also defines the
50  * Temperature 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 TempSensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
58     private final Logger logger = LoggerFactory.getLogger(TempSensorHandler.class);
59     private final int thingID = getThingNumber();
60     public @Nullable String number;
61
62     public TempSensorHandler(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             updateTempSensorProperties(bridgeHandler);
72         } else {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
74                     "Received null bridge while initializing Temperature Sensor!");
75         }
76     }
77
78     private void updateTempSensorProperties(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         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
103         Optional<TemperatureFormat> temperatureFormat = Optional.empty();
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         if (bridgeHandler != null) {
116             temperatureFormat = bridgeHandler.getTemperatureFormat();
117             if (!temperatureFormat.isPresent()) {
118                 logger.warn("Receieved null temperature format!");
119                 return;
120             }
121         } else {
122             logger.warn("Could not connect to Bridge, failed to get temperature format!");
123             return;
124         }
125
126         switch (channelUID.getId()) {
127             case CHANNEL_AUX_LOW_SETPOINT:
128                 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_HEAT_LOW_POINT.getNumber(),
129                         temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).intValue()),
130                         thingID);
131                 break;
132             case CHANNEL_AUX_HIGH_SETPOINT:
133                 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_COOL_HIGH_POINT.getNumber(),
134                         temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).intValue()),
135                         thingID);
136                 break;
137             default:
138                 logger.warn("Unknown channel for Temperature Sensor thing: {}", channelUID);
139         }
140     }
141
142     @Override
143     public void updateChannels(ExtendedAuxSensorStatus status) {
144         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
145         if (bridgeHandler != null) {
146             Optional<TemperatureFormat> temperatureFormat = bridgeHandler.getTemperatureFormat();
147             if (temperatureFormat.isPresent()) {
148                 updateState(CHANNEL_AUX_TEMP, new QuantityType<>(
149                         temperatureFormat.get().omniToFormat(status.getTemperature()),
150                         temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
151                 updateState(CHANNEL_AUX_LOW_SETPOINT, new QuantityType<>(
152                         temperatureFormat.get().omniToFormat(status.getCoolSetpoint()),
153                         temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
154                 updateState(CHANNEL_AUX_HIGH_SETPOINT, new QuantityType<>(
155                         temperatureFormat.get().omniToFormat(status.getHeatSetpoint()),
156                         temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
157             } else {
158                 logger.warn("Receieved null temperature format, could not update Temperature Sensor channels!");
159             }
160         } else {
161             logger.debug("Received null bridge while updating Temperature Sensor channels!");
162         }
163     }
164
165     @Override
166     protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
167         try {
168             final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
169             if (bridgeHandler != null) {
170                 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
171                         thingID, true);
172                 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
173             } else {
174                 logger.debug("Received null bridge while updating Temperature Sensor status!");
175                 return Optional.empty();
176             }
177         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
178             logger.debug("Received exception while refreshing Temperature Sensor status: {}", e.getMessage());
179             return Optional.empty();
180         }
181     }
182 }