]> git.basschouten.com Git - openhab-addons.git/blob
02e83b9c0ae4b1a4ebde84afdd876a39e7906549
[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.PhoneManager;
22 import org.openhab.binding.freeboxos.internal.api.rest.PhoneManager.Config;
23 import org.openhab.binding.freeboxos.internal.api.rest.PhoneManager.Status;
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 FxsHandler} is responsible for handling everything associated to the landline associated with the
33  * Freebox Server.
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
38 public class FxsHandler extends ApiConsumerHandler {
39     private final Logger logger = LoggerFactory.getLogger(FxsHandler.class);
40
41     public FxsHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     void initializeProperties(Map<String, String> properties) throws FreeboxException {
47         getManager(PhoneManager.class).getStatus(getClientId())
48                 .ifPresent(status -> properties.put(Thing.PROPERTY_VENDOR, status.vendor()));
49     }
50
51     @Override
52     protected void internalPoll() throws FreeboxException {
53         logger.debug("Polling landline status...");
54
55         Config config = getManager(PhoneManager.class).getConfig();
56         updateConfigChannels(config);
57
58         getManager(PhoneManager.class).getStatus(getClientId()).ifPresent(this::updateStatusChannels);
59     }
60
61     protected void updateConfigChannels(Config config) {
62         updateChannelString(TELEPHONY_SERVICE, config.network());
63     }
64
65     protected void updateStatusChannels(Status status) {
66         updateChannelOnOff(ONHOOK, status.onHook());
67         updateChannelOnOff(RINGING, status.isRinging());
68         updateChannelString(HARDWARE_STATUS, status.hardwareDefect() ? "KO" : "OK");
69         updateStatus(ThingStatus.ONLINE);
70     }
71
72     @Override
73     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
74         if (RINGING.equals(channelId) && command instanceof OnOffType) {
75             getManager(PhoneManager.class).ringFxs(TRUE_COMMANDS.contains(command));
76             return true;
77         }
78         return super.internalHandleCommand(channelId, command);
79     }
80 }