]> git.basschouten.com Git - openhab-addons.git/blob
84914f403c2b839b5740e7e139df4d7fc9397b17
[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 static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_CALL;
16
17 import java.time.ZonedDateTime;
18 import java.util.ArrayList;
19 import java.util.Comparator;
20 import java.util.List;
21
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;
26
27 /**
28  * The {@link CallManager} is the Java class used to handle api requests related to calls
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  */
32 @NonNullByDefault
33 public class CallManager extends RestManager {
34     private static final String LOG_SUB_PATH = "log/";
35     private static final String DELETE_ACTION = "delete_all";
36
37     private static class Calls extends Response<Call> {
38     }
39
40     public enum Type {
41         ACCEPTED,
42         MISSED,
43         OUTGOING,
44         INCOMING,
45         UNKNOWN
46     }
47
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.
52             String name) {
53
54         public @Nullable String name() {
55             return name.equals(number) ? null : name;
56         }
57     }
58
59     public CallManager(FreeboxOsSession session) throws FreeboxException {
60         super(session, LoginManager.Permission.CALLS, session.getUriBuilder().path(THING_CALL));
61     }
62
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));
72         }
73         return callList;
74     }
75
76     public void emptyQueue() throws FreeboxException {
77         post(LOG_SUB_PATH, DELETE_ACTION);
78     }
79 }