]> git.basschouten.com Git - openhab-addons.git/blob
ed774ad07926b18400c672dfd5828435098a5510
[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.api.rest;
14
15 import java.util.List;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
21 import org.openhab.binding.freeboxos.internal.api.Response;
22
23 /**
24  * The {@link PhoneManager} is the Java class used to handle api requests related to phone and calls
25  *
26  * @author GaĆ«l L'hopital - Initial contribution
27  */
28 @NonNullByDefault
29 public class PhoneManager extends ConfigurableRest<PhoneManager.Config, PhoneManager.ConfigResponse> {
30     private static final String DECT_PAGE_ACTION = "dect_page_%s";
31     private static final String FXS_RING_ACTION = "fxs_ring_%s";
32     private static final String PATH = "phone";
33
34     protected class ConfigResponse extends Response<Config> {
35     }
36
37     protected class StatusResponse extends Response<Status> {
38     }
39
40     private enum NetworkStatus {
41         WORKING,
42         UNKNOWN
43     }
44
45     public static record Config(NetworkStatus network, boolean dectEcoMode, String dectPin, int dectRingPattern,
46             boolean dectRegistration, boolean dectNemoMode, boolean dectEnabled, boolean dectRingOnOff) {
47     }
48
49     public enum Type {
50         FXS,
51         DECT,
52         UNKNOWN
53     }
54
55     public static record Status(int id, boolean isRinging, boolean onHook, boolean hardwareDefect, Type type,
56             @Nullable String vendor, int gainRx, int gainTx) {
57
58         public String vendor() {
59             String localVendor = vendor;
60             return localVendor != null ? localVendor : "Unknown";
61         }
62     }
63
64     public PhoneManager(FreeboxOsSession session) throws FreeboxException {
65         super(session, LoginManager.Permission.CALLS, ConfigResponse.class, session.getUriBuilder().path(PATH),
66                 CONFIG_PATH);
67     }
68
69     public List<Status> getPhoneStatuses() throws FreeboxException {
70         return get(StatusResponse.class, "");
71     }
72
73     public Optional<Status> getStatus(int id) throws FreeboxException {
74         return Optional.ofNullable(getSingle(StatusResponse.class, Integer.toString(id)));
75     }
76
77     public void ringFxs(boolean startIt) throws FreeboxException {
78         post(FXS_RING_ACTION.formatted(startIt ? "start" : "stop"));
79     }
80
81     public void ringDect(boolean startIt) throws FreeboxException {
82         post(DECT_PAGE_ACTION.formatted(startIt ? "start" : "stop"));
83     }
84
85     public void setGainRx(int clientId, int gain) throws FreeboxException {
86         Optional<Status> result = getStatus(clientId);
87         if (result.isPresent()) {
88             Status status = result.get();
89             Status newStatus = new Status(status.id, status.isRinging, status.onHook, status.hardwareDefect,
90                     status.type, status.vendor, gain, status.gainTx);
91             put(StatusResponse.class, newStatus, Integer.toString(clientId));
92         }
93     }
94
95     public void setGainTx(int clientId, int gain) throws FreeboxException {
96         Optional<Status> result = getStatus(clientId);
97         if (result.isPresent()) {
98             Status status = result.get();
99             Status newStatus = new Status(status.id, status.isRinging, status.onHook, status.hardwareDefect,
100                     status.type, status.vendor, status.gainRx, gain);
101             put(StatusResponse.class, newStatus, Integer.toString(clientId));
102         }
103     }
104
105     public void alternateRing(boolean status) throws FreeboxException {
106         Config config = getConfig();
107         Config newConfig = new Config(config.network, config.dectEcoMode, config.dectPin, config.dectRingPattern,
108                 config.dectRegistration, config.dectNemoMode, config.dectEnabled, status);
109         put(ConfigResponse.class, newConfig, CONFIG_PATH);
110     }
111
112     public boolean setStatus(boolean enabled) throws FreeboxException {
113         Config config = getConfig();
114         Config newConfig = new Config(config.network, config.dectEcoMode, config.dectPin, config.dectRingPattern,
115                 config.dectRegistration, config.dectNemoMode, enabled, config.dectRingOnOff);
116         return setConfig(newConfig).dectEnabled;
117     }
118 }