2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tplinksmarthome.internal.device;
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
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;
36 * TP-Link Smart Home device with multiple sockets, like the HS107 and HS300.
38 * @author Hilbrand Bouwkamp - Initial contribution
41 public class PowerStripDevice extends EnergySwitchDevice {
43 private final Logger logger = LoggerFactory.getLogger(PowerStripDevice.class);
45 private final List<@Nullable Realtime> realTimeCacheList;
46 private final List<@Nullable String> childIds;
48 public PowerStripDevice(final TPLinkSmartHomeThingType type) {
49 final int nrOfSockets = type.getSockets();
51 realTimeCacheList = new ArrayList<>(nrOfSockets);
52 childIds = new ArrayList<>(Collections.nCopies(nrOfSockets, ""));
56 public String getUpdateCommand() {
57 return Commands.getSysinfo();
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));
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.
74 .from(deviceState.getSysinfo().getChildren().stream().anyMatch(e -> OnOffType.ON.equals(e.getState())));
78 public State updateChannel(final ChannelUID channelUid, final DeviceState deviceState) {
79 final int idx = channelToIndex(channelUid);
81 if (idx >= 0 && idx < childIds.size()) {
82 final Outlet outlet = deviceState.getSysinfo().getChildren().get(idx);
83 final String baseChannel = channelUid.getIdWithoutGroup();
85 if (CHANNEL_SWITCH.equals(baseChannel)) {
86 return outlet.getState();
87 } else if (CHANNELS_ENERGY.contains(baseChannel)) {
88 final Realtime realTime = realTimeCacheList.get(idx);
90 return realTime == null ? UnDefType.UNDEF : updateEnergyChannel(baseChannel, realTime);
94 logger.debug("For channel update the index '{}' could be mapped to a channel. passed channel: {}", idx,
98 return super.updateChannel(channelUid, deviceState);
102 protected @Nullable String getChildId(final ChannelUID channelUid) {
103 final int idx = channelToIndex(channelUid);
105 return idx >= 0 && idx < childIds.size() ? childIds.get(idx) : null;
108 private int channelToIndex(final ChannelUID channelUid) {
109 final String groupId = channelUid.getGroupId();
111 return (groupId != null && groupId.startsWith(CHANNEL_OUTLET_GROUP_PREFIX)
112 ? Integer.parseInt(groupId.substring(CHANNEL_OUTLET_GROUP_PREFIX.length()))
116 private @Nullable Realtime refreshCache(final int idx) {
118 final String childId = childIds.get(idx);
120 return childId == null ? null
121 : commands.getRealtimeResponse(connection.sendCommand(Commands.getRealtimeWithContext(childId)));
122 } catch (final IOException e) {
124 } catch (final RuntimeException e) {
126 "Obtaining realtime data for channel-'{}' unexpectedly crashed. If this keeps happening please report: ",