]> git.basschouten.com Git - openhab-addons.git/blob
83a31ad088273864f3c067a6d680a1993e582dca
[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.folding.internal.handler;
14
15 import java.io.IOException;
16
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;
26
27 /**
28  * Thing handler representing a Folding slot.
29  *
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.
33  *
34  * @author Marius Bjørnstad - Initial contribution
35  */
36 public class SlotHandler extends BaseThingHandler implements SlotUpdateListener {
37
38     private Logger logger = LoggerFactory.getLogger(SlotHandler.class);
39
40     public SlotHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void initialize() {
46         getBridgeHandler().registerSlot(myId(), this);
47     }
48
49     private FoldingClientHandler getBridgeHandler() {
50         return (FoldingClientHandler) super.getBridge().getHandler();
51     }
52
53     private String myId() {
54         return (String) getThing().getConfiguration().get("id");
55     }
56
57     @Override
58     public void handleCommand(ChannelUID channelUID, Command command) {
59         try {
60             if (channelUID.getId().equals("run")) {
61                 if (command == OnOffType.ON) {
62                     getBridgeHandler().sendCommand("unpause " + myId());
63                 } else if (command == OnOffType.OFF) {
64                     getBridgeHandler().sendCommand("pause " + myId());
65                 }
66             } else if (channelUID.getId().equals("finish")) {
67                 if (command == OnOffType.ON) {
68                     getBridgeHandler().sendCommand("finish " + myId());
69                 } else if (command == OnOffType.OFF) {
70                     getBridgeHandler().sendCommand("unpause " + myId());
71                 }
72             }
73             getBridgeHandler().refresh();
74             getBridgeHandler().delayedRefresh();
75         } catch (IOException e) {
76             logger.debug("Input/output error while handing command to Folding slot", e);
77         }
78     }
79
80     @Override
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(), finishing ? OnOffType.ON : OnOffType.OFF);
87         updateState(getThing().getChannel("run").getUID(), run ? OnOffType.ON : OnOffType.OFF);
88         updateState(getThing().getChannel("description").getUID(), new StringType(si.description));
89     }
90 }