]> git.basschouten.com Git - openhab-addons.git/blob
b22355df203a2488e902d2b0a0b297252b3b7217
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
19 import org.openhab.binding.freeboxos.internal.api.rest.VmManager;
20 import org.openhab.binding.freeboxos.internal.api.rest.VmManager.Status;
21 import org.openhab.binding.freeboxos.internal.api.rest.VmManager.VirtualMachine;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link VmHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  */
35 @NonNullByDefault
36 public class VmHandler extends HostHandler {
37     private final Logger logger = LoggerFactory.getLogger(VmHandler.class);
38
39     public VmHandler(Thing thing) {
40         super(thing);
41     }
42
43     @Override
44     protected void internalPoll() throws FreeboxException {
45         super.internalPoll();
46
47         if (!pushSubscribed) {
48             logger.debug("Polling Virtual machine status");
49             VirtualMachine vm = getManager(VmManager.class).getDevice(getClientId());
50             updateVmChannels(vm);
51         }
52     }
53
54     @Override
55     protected void internalForcePoll() throws FreeboxException {
56         super.internalForcePoll();
57
58         logger.debug("Polling Virtual machine status");
59         VirtualMachine vm = getManager(VmManager.class).getDevice(getClientId());
60         updateVmChannels(vm);
61     }
62
63     public void updateVmChannels(VirtualMachine vm) {
64         boolean running = Status.RUNNING.equals(vm.status());
65         updateChannelOnOff(VM_STATUS, STATUS, running);
66         updateChannelOnOff(CONNECTIVITY, REACHABLE, running);
67         updateStatus(running ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
68     }
69
70     @Override
71     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
72         if (STATUS.equals(channelId) && command instanceof OnOffType) {
73             getManager(VmManager.class).power(getClientId(), OnOffType.ON.equals(command));
74             return true;
75         }
76         return super.internalHandleCommand(channelId, command);
77     }
78 }