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