]> git.basschouten.com Git - openhab-addons.git/blob
a2efed2342bb3c1a34e45f9c413185e5604f462e
[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.tplinksmarthome.internal.device;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.tplinksmarthome.internal.Commands;
25 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType;
26 import org.openhab.binding.tplinksmarthome.internal.model.Realtime;
27 import org.openhab.binding.tplinksmarthome.internal.model.Sysinfo.Outlet;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * TP-Link Smart Home device with multiple sockets, like the HS107 and HS300.
37  *
38  * @author Hilbrand Bouwkamp - Initial contribution
39  */
40 @NonNullByDefault
41 public class PowerStripDevice extends EnergySwitchDevice {
42
43     private final Logger logger = LoggerFactory.getLogger(PowerStripDevice.class);
44
45     private final List<@Nullable Realtime> realTimeCacheList;
46     private final List<@Nullable String> childIds;
47
48     public PowerStripDevice(final TPLinkSmartHomeThingType type) {
49         final int nrOfSockets = type.getSockets();
50
51         realTimeCacheList = new ArrayList<>(nrOfSockets);
52         childIds = new ArrayList<>(Collections.nCopies(nrOfSockets, ""));
53     }
54
55     @Override
56     public String getUpdateCommand() {
57         return Commands.getSysinfo();
58     }
59
60     @Override
61     public void refreshedDeviceState(@Nullable final DeviceState deviceState) {
62         if (deviceState != null) {
63             for (int i = 0; i < childIds.size(); i++) {
64                 childIds.set(i, deviceState.getSysinfo().getChildren().get(i).getId());
65                 realTimeCacheList.add(i, refreshCache(i));
66             }
67         }
68     }
69
70     @Override
71     protected State getOnOffState(final DeviceState deviceState) {
72         // Global On/Off state is determined by the combined state of all sockets. If 1 socket is on, the state is on.
73         return OnOffType
74                 .from(deviceState.getSysinfo().getChildren().stream().anyMatch(e -> OnOffType.ON.equals(e.getState())));
75     }
76
77     @Override
78     public State updateChannel(final ChannelUID channelUid, final DeviceState deviceState) {
79         final int idx = channelToIndex(channelUid);
80
81         if (idx >= 0 && idx < childIds.size()) {
82             final Outlet outlet = deviceState.getSysinfo().getChildren().get(idx);
83             final String baseChannel = channelUid.getIdWithoutGroup();
84
85             if (CHANNEL_SWITCH.equals(baseChannel)) {
86                 return outlet.getState();
87             } else if (CHANNELS_ENERGY.contains(baseChannel)) {
88                 final Realtime realTime = realTimeCacheList.get(idx);
89
90                 return realTime == null ? UnDefType.UNDEF : updateEnergyChannel(baseChannel, realTime);
91             }
92         } else {
93             if (idx >= 0) {
94                 logger.debug("For channel update the index '{}' could be mapped to a channel. passed channel: {}", idx,
95                         channelUid);
96             }
97         }
98         return super.updateChannel(channelUid, deviceState);
99     }
100
101     @Override
102     protected @Nullable String getChildId(final ChannelUID channelUid) {
103         final int idx = channelToIndex(channelUid);
104
105         return idx >= 0 && idx < childIds.size() ? childIds.get(idx) : null;
106     }
107
108     private int channelToIndex(final ChannelUID channelUid) {
109         final String groupId = channelUid.getGroupId();
110
111         return (groupId != null && groupId.startsWith(CHANNEL_OUTLET_GROUP_PREFIX)
112                 ? Integer.parseInt(groupId.substring(CHANNEL_OUTLET_GROUP_PREFIX.length()))
113                 : 0) - 1;
114     }
115
116     private @Nullable Realtime refreshCache(final int idx) {
117         try {
118             final String childId = childIds.get(idx);
119
120             return childId == null ? null
121                     : commands.getRealtimeResponse(connection.sendCommand(Commands.getRealtimeWithContext(childId)));
122         } catch (final IOException e) {
123             return null;
124         } catch (final RuntimeException e) {
125             logger.debug(
126                     "Obtaining realtime data for channel-'{}' unexpectedly crashed. If this keeps happening please report: ",
127                     idx, e);
128             return null;
129         }
130     }
131 }