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