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.folding.internal.handler;
15 import java.io.IOException;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.library.types.StringType;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.binding.BaseThingHandler;
23 import org.openhab.core.types.Command;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Thing handler representing a Folding slot.
30 * If control of each slot (CPU, GPUs, etc.) is desired, the user can add
31 * Slot handlers. The Slot handler exposes the status of a slot, and allows
32 * users to start / stop folding.
34 * @author Marius Bjørnstad - Initial contribution
36 public class SlotHandler extends BaseThingHandler implements SlotUpdateListener {
38 private Logger logger = LoggerFactory.getLogger(SlotHandler.class);
40 public SlotHandler(Thing thing) {
45 public void initialize() {
46 getBridgeHandler().registerSlot(myId(), this);
49 private FoldingClientHandler getBridgeHandler() {
50 return (FoldingClientHandler) super.getBridge().getHandler();
53 private String myId() {
54 return (String) getThing().getConfiguration().get("id");
58 public void handleCommand(ChannelUID channelUID, Command command) {
60 if ("run".equals(channelUID.getId())) {
61 if (command == OnOffType.ON) {
62 getBridgeHandler().sendCommand("unpause " + myId());
63 } else if (command == OnOffType.OFF) {
64 getBridgeHandler().sendCommand("pause " + myId());
66 } else if ("finish".equals(channelUID.getId())) {
67 if (command == OnOffType.ON) {
68 getBridgeHandler().sendCommand("finish " + myId());
69 } else if (command == OnOffType.OFF) {
70 getBridgeHandler().sendCommand("unpause " + myId());
73 getBridgeHandler().refresh();
74 getBridgeHandler().delayedRefresh();
75 } catch (IOException e) {
76 logger.debug("Input/output error while handing command to Folding slot", e);
81 public void refreshed(SlotInfo si) {
82 updateStatus(ThingStatus.ONLINE);
83 updateState(getThing().getChannel("status").getUID(), new StringType(si.status));
84 boolean finishing = "FINISHING".equals(si.status);
85 boolean run = finishing || "READY".equals(si.status) || "RUNNING".equals(si.status);
86 updateState(getThing().getChannel("finish").getUID(), OnOffType.from(finishing));
87 updateState(getThing().getChannel("run").getUID(), OnOffType.from(run));
88 updateState(getThing().getChannel("description").getUID(), new StringType(si.description));