]> git.basschouten.com Git - openhab-addons.git/blob
00a3c0046be2c462996ef78a20e33e14891c59a8
[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.sleepiq.internal.handler;
14
15 import static org.openhab.binding.sleepiq.internal.SleepIQBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19
20 import org.openhab.binding.sleepiq.api.SleepIQ;
21 import org.openhab.binding.sleepiq.api.model.Bed;
22 import org.openhab.binding.sleepiq.api.model.BedSideStatus;
23 import org.openhab.binding.sleepiq.api.model.BedStatus;
24 import org.openhab.binding.sleepiq.internal.config.SleepIQBedConfiguration;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingStatusInfo;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandler;
36 import org.openhab.core.thing.binding.BridgeHandler;
37 import org.openhab.core.thing.binding.ThingHandler;
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  * The {@link SleepIQDualBedHandler} is responsible for handling channel state updates from the cloud service.
45  *
46  * @author Gregory Moyer - Initial contribution
47  */
48 public class SleepIQDualBedHandler extends BaseThingHandler implements BedStatusListener {
49     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections.singleton(THING_TYPE_DUAL_BED);
50
51     private final Logger logger = LoggerFactory.getLogger(SleepIQDualBedHandler.class);
52
53     private volatile String bedId;
54
55     public SleepIQDualBedHandler(final Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void initialize() {
61         logger.debug("Verifying SleepIQ cloud/bridge configuration");
62
63         Bridge bridge = getBridge();
64         if (bridge == null) {
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66                     "No cloud service bridge has been configured");
67             return;
68         }
69
70         ThingHandler handler = bridge.getHandler();
71         if (!(handler instanceof SleepIQCloudHandler)) {
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Incorrect bridge thing found");
73             return;
74         }
75
76         logger.debug("Reading SleepIQ bed binding configuration");
77         bedId = getConfigAs(SleepIQBedConfiguration.class).bedId;
78
79         logger.debug("Registering SleepIQ bed status listener");
80         SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) handler;
81         cloudHandler.registerBedStatusListener(this);
82
83         if (ThingStatus.ONLINE != bridge.getStatus()) {
84             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
85         } else {
86             updateProperties();
87             updateStatus(ThingStatus.ONLINE);
88         }
89     }
90
91     @Override
92     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
93         super.bridgeStatusChanged(bridgeStatusInfo);
94
95         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
96             updateProperties();
97         }
98     }
99
100     private void updateProperties() {
101         logger.debug("Updating SleepIQ bed properties for bed {}", bedId);
102
103         SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) getBridge().getHandler();
104         Bed bed = cloudHandler.getBed(bedId);
105         if (bed == null) {
106             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bed found with ID " + bedId);
107             return;
108         }
109
110         updateProperties(cloudHandler.updateProperties(bed, editProperties()));
111     }
112
113     @Override
114     public void dispose() {
115         logger.debug("Disposing bed handler for bed {}", bedId);
116
117         Bridge bridge = getBridge();
118         if (bridge != null) {
119             BridgeHandler bridgeHandler = bridge.getHandler();
120             if (bridgeHandler instanceof SleepIQCloudHandler) {
121                 ((SleepIQCloudHandler) bridgeHandler).unregisterBedStatusListener(this);
122             }
123         }
124
125         bedId = null;
126     }
127
128     @Override
129     public void onBedStateChanged(final SleepIQ cloud, final BedStatus status) {
130         if (!status.getBedId().equals(bedId)) {
131             return;
132         }
133
134         logger.debug("Updating left side status for bed {}", bedId);
135         BedSideStatus left = status.getLeftSide();
136         updateState(CHANNEL_LEFT_IN_BED, left.isInBed() ? OnOffType.ON : OnOffType.OFF);
137         updateState(CHANNEL_LEFT_SLEEP_NUMBER, new DecimalType(left.getSleepNumber()));
138         updateState(CHANNEL_LEFT_PRESSURE, new DecimalType(left.getPressure()));
139         updateState(CHANNEL_LEFT_LAST_LINK, new StringType(left.getLastLink().toString()));
140         updateState(CHANNEL_LEFT_ALERT_ID, new DecimalType(left.getAlertId()));
141         updateState(CHANNEL_LEFT_ALERT_DETAILED_MESSAGE, new StringType(left.getAlertDetailedMessage()));
142
143         logger.debug("Updating right side status for bed {}", bedId);
144         BedSideStatus right = status.getRightSide();
145         updateState(CHANNEL_RIGHT_IN_BED, right.isInBed() ? OnOffType.ON : OnOffType.OFF);
146         updateState(CHANNEL_RIGHT_SLEEP_NUMBER, new DecimalType(right.getSleepNumber()));
147         updateState(CHANNEL_RIGHT_PRESSURE, new DecimalType(right.getPressure()));
148         updateState(CHANNEL_RIGHT_LAST_LINK, new StringType(right.getLastLink().toString()));
149         updateState(CHANNEL_RIGHT_ALERT_ID, new DecimalType(right.getAlertId()));
150         updateState(CHANNEL_RIGHT_ALERT_DETAILED_MESSAGE, new StringType(right.getAlertDetailedMessage()));
151     }
152
153     @Override
154     public void handleCommand(final ChannelUID channelUID, final Command command) {
155         // all channels are read-only
156
157         if (command == RefreshType.REFRESH) {
158             SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) getBridge().getHandler();
159             cloudHandler.refreshBedStatus();
160         }
161     }
162 }