]> git.basschouten.com Git - openhab-addons.git/blob
2f1ab7b06cf621502ae293328cc3d14aa88600b2
[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.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.binding.freeboxos.internal.api.rest.WebSocketManager;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link VmHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author GaĆ«l L'hopital - Initial contribution
35  */
36 @NonNullByDefault
37 public class VmHandler extends HostHandler {
38     private final Logger logger = LoggerFactory.getLogger(VmHandler.class);
39
40     // We start in pull mode and switch to push after a first update
41     private boolean pushSubscribed = false;
42
43     public VmHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void dispose() {
49         try {
50             getManager(WebSocketManager.class).unregisterVm(getClientId());
51         } catch (FreeboxException e) {
52             logger.warn("Error unregistering VM from the websocket: {}", e.getMessage());
53         }
54         super.dispose();
55     }
56
57     @Override
58     protected void internalPoll() throws FreeboxException {
59         if (pushSubscribed) {
60             return;
61         }
62         super.internalPoll();
63
64         logger.debug("Polling Virtual machine status");
65         VirtualMachine vm = getManager(VmManager.class).getDevice(getClientId());
66         updateVmChannels(vm);
67         getManager(WebSocketManager.class).registerVm(vm.id(), this);
68         pushSubscribed = true;
69     }
70
71     public void updateVmChannels(VirtualMachine vm) {
72         boolean running = Status.RUNNING.equals(vm.status());
73         updateChannelOnOff(VM_STATUS, STATUS, running);
74         updateChannelOnOff(CONNECTIVITY, REACHABLE, running);
75         updateStatus(running ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
76     }
77
78     @Override
79     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
80         if (STATUS.equals(channelId) && command instanceof OnOffType) {
81             getManager(VmManager.class).power(getClientId(), OnOffType.ON.equals(command));
82             return true;
83         }
84         return super.internalHandleCommand(channelId, command);
85     }
86 }