]> git.basschouten.com Git - openhab-addons.git/blob
c004f1c544453bd3d50c67f0a847516935244929
[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.nobohub.internal;
14
15 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_COMPONENT_CURRENT_TEMPERATURE;
16 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_MODEL;
17 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_NAME;
18 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE;
19 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_ZONE;
20
21 import java.util.Map;
22
23 import javax.measure.quantity.Temperature;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.nobohub.internal.model.Component;
28 import org.openhab.binding.nobohub.internal.model.SerialNumber;
29 import org.openhab.binding.nobohub.internal.model.Zone;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.binding.BaseThingHandler;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Shows information about a Component in the Nobø Hub.
45  *
46  * @author Jørgen Austvik - Initial contribution
47  */
48 @NonNullByDefault
49 public class ComponentHandler extends BaseThingHandler {
50
51     private final Logger logger = LoggerFactory.getLogger(ComponentHandler.class);
52
53     private final NoboHubTranslationProvider messages;
54
55     protected @Nullable SerialNumber serialNumber;
56
57     public ComponentHandler(Thing thing, NoboHubTranslationProvider messages) {
58         super(thing);
59         this.messages = messages;
60     }
61
62     public void onUpdate(Component component) {
63         updateStatus(ThingStatus.ONLINE);
64
65         double temp = component.getTemperature();
66         if (!Double.isNaN(temp)) {
67             QuantityType<Temperature> currentTemperature = new QuantityType<>(temp, SIUnits.CELSIUS);
68             updateState(CHANNEL_COMPONENT_CURRENT_TEMPERATURE, currentTemperature);
69         }
70
71         Map<String, String> properties = editProperties();
72         properties.put(Thing.PROPERTY_SERIAL_NUMBER, component.getSerialNumber().toString());
73         properties.put(PROPERTY_NAME, component.getName());
74         properties.put(PROPERTY_MODEL, component.getSerialNumber().getComponentType());
75
76         String zoneName = getZoneName(component.getZoneId());
77         if (zoneName != null) {
78             properties.put(PROPERTY_ZONE, zoneName);
79         }
80
81         String tempForZoneName = getZoneName(component.getTemperatureSensorForZoneId());
82         if (tempForZoneName != null) {
83             properties.put(PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE, tempForZoneName);
84         }
85         updateProperties(properties);
86     }
87
88     private @Nullable String getZoneName(int zoneId) {
89         Bridge noboHub = getBridge();
90         if (null != noboHub) {
91             NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
92             if (hubHandler != null) {
93                 Zone zone = hubHandler.getZone(zoneId);
94                 if (null != zone) {
95                     return zone.getName();
96                 }
97             }
98         }
99
100         return null;
101     }
102
103     @Override
104     public void initialize() {
105         String serialNumberString = getConfigAs(ComponentConfiguration.class).serialNumber;
106         if (serialNumberString != null && !serialNumberString.isEmpty()) {
107             SerialNumber sn = new SerialNumber(serialNumberString);
108             if (!sn.isWellFormed()) {
109                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110                         "@text/message.component.illegal.serial [\"" + serialNumberString + "\"]");
111             } else {
112                 this.serialNumber = sn;
113                 updateStatus(ThingStatus.ONLINE);
114             }
115         } else {
116             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/message.missing.serial");
117         }
118     }
119
120     @Override
121     public void handleCommand(ChannelUID channelUID, Command command) {
122         if (command instanceof RefreshType) {
123             logger.debug("Refreshing channel {}", channelUID);
124             if (null != serialNumber) {
125                 Component component = getComponent();
126                 if (null == component) {
127                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
128                             messages.getText("message.component.notfound", serialNumber, channelUID));
129                 } else {
130                     onUpdate(component);
131                 }
132             } else {
133                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
134                         "@text/message.component.missing.id [\"" + channelUID + "\"]");
135             }
136
137             return;
138         }
139
140         logger.debug("This component is a read-only device and cannot handle commands.");
141     }
142
143     public @Nullable SerialNumber getSerialNumber() {
144         return serialNumber;
145     }
146
147     private @Nullable Component getComponent() {
148         Bridge noboHub = getBridge();
149         if (null != noboHub) {
150             NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
151             SerialNumber serialNumber = this.serialNumber;
152             if (null != serialNumber && null != hubHandler) {
153                 return hubHandler.getComponent(serialNumber);
154             }
155         }
156
157         return null;
158     }
159 }