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 static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_CALL;
17 import java.time.ZonedDateTime;
18 import java.util.ArrayList;
19 import java.util.Comparator;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
25 import org.openhab.binding.freeboxos.internal.api.Response;
28 * The {@link CallManager} is the Java class used to handle api requests related to calls
30 * @author Gaƫl L'hopital - Initial contribution
33 public class CallManager extends RestManager {
34 private static final String LOG_SUB_PATH = "log/";
35 private static final String DELETE_ACTION = "delete_all";
37 private static class Calls extends Response<Call> {
40 public static enum Type {
48 public static record Call(Type type, //
49 ZonedDateTime datetime, // Call creation timestamp.
50 String number, // Calling or called number
51 int duration, // Call duration in seconds.
54 public @Nullable String name() {
55 return name.equals(number) ? null : name;
59 public CallManager(FreeboxOsSession session) throws FreeboxException {
60 super(session, LoginManager.Permission.CALLS, session.getUriBuilder().path(THING_CALL));
63 // Retrieves a sorted list of all call entries
64 public List<Call> getCallEntries() throws FreeboxException {
65 List<Call> callList = new ArrayList<>(
66 get(Calls.class, LOG_SUB_PATH).stream().sorted(Comparator.comparing(Call::datetime)).toList());
67 Call last = callList.get(callList.size() - 1);
68 // The INCOMING type call can only be set on the last call if its duration is 0;
69 if (last.type == Type.MISSED && last.duration == 0) {
70 callList.remove(callList.size() - 1);
71 callList.add(new Call(Type.INCOMING, last.datetime, last.number, 0, last.name));
76 public void emptyQueue() throws FreeboxException {
77 post(LOG_SUB_PATH, DELETE_ACTION);