2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.api.rest;
15 import java.util.List;
16 import java.util.Optional;
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;
24 * The {@link PhoneManager} is the Java class used to handle api requests related to phone and calls
26 * @author Gaƫl L'hopital - Initial contribution
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";
34 protected class ConfigResponse extends Response<Config> {
37 protected class StatusResponse extends Response<Status> {
40 private enum NetworkStatus {
45 public static record Config(NetworkStatus network, boolean dectEcoMode, String dectPin, int dectRingPattern,
46 boolean dectRegistration, boolean dectNemoMode, boolean dectEnabled, boolean dectRingOnOff) {
55 public static record Status(int id, boolean isRinging, boolean onHook, boolean hardwareDefect, Type type,
56 @Nullable String vendor, int gainRx, int gainTx) {
58 public String vendor() {
59 String localVendor = vendor;
60 return localVendor != null ? localVendor : "Unknown";
64 public PhoneManager(FreeboxOsSession session) throws FreeboxException {
65 super(session, LoginManager.Permission.CALLS, ConfigResponse.class, session.getUriBuilder().path(PATH),
69 public List<Status> getPhoneStatuses() throws FreeboxException {
70 return get(StatusResponse.class, "");
73 public Optional<Status> getStatus(int id) throws FreeboxException {
74 return Optional.ofNullable(getSingle(StatusResponse.class, Integer.toString(id)));
77 public void ringFxs(boolean startIt) throws FreeboxException {
78 post(FXS_RING_ACTION.formatted(startIt ? "start" : "stop"));
81 public void ringDect(boolean startIt) throws FreeboxException {
82 post(DECT_PAGE_ACTION.formatted(startIt ? "start" : "stop"));
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));
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));
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);
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;