]> git.basschouten.com Git - openhab-addons.git/blob
03d3e9b4878c07870567363172b14b424129e731
[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.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.time.ZonedDateTime;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.freeboxos.internal.action.CallActions;
27 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
28 import org.openhab.binding.freeboxos.internal.api.rest.CallManager;
29 import org.openhab.binding.freeboxos.internal.api.rest.CallManager.Call;
30 import org.openhab.binding.freeboxos.internal.api.rest.CallManager.Type;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Channel;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.openhab.core.types.UnDefType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link CallHandler} is responsible for handling everything associated to the phone calls received on the box line
42  *
43  * @author GaĆ«l L'hopital - Initial contribution
44  */
45 @NonNullByDefault
46 public class CallHandler extends ApiConsumerHandler {
47     private final Logger logger = LoggerFactory.getLogger(CallHandler.class);
48     private final Map<Type, ZonedDateTime> lastCalls = new HashMap<>(Type.values().length);
49
50     public CallHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     void initializeProperties(Map<String, String> properties) throws FreeboxException {
56         // nothing to do here
57     }
58
59     @Override
60     protected void internalPoll() throws FreeboxException {
61         logger.debug("Polling phone calls ...");
62
63         lastCalls.clear();
64
65         List<Call> entries = getManager(CallManager.class).getCallEntries();
66         Arrays.stream(Type.values()).forEach(callType -> entries.stream().filter(call -> call.type().equals(callType))
67                 .reduce((first, second) -> second).ifPresent(this::updateCallChannels));
68
69         // Clear incoming call if the youngest is not an incoming call
70         lastCalls.entrySet().stream().sorted(Map.Entry.comparingByValue()).reduce((first, second) -> second)
71                 .map(entry -> entry.getKey()).filter(type -> !Type.INCOMING.equals(type)).ifPresent(type -> {
72                     String groupName = Type.INCOMING.name().toLowerCase();
73                     getThing().getChannelsOfGroup(groupName).stream().map(Channel::getUID).filter(uid -> isLinked(uid))
74                             .forEach(uid -> updateState(uid, UnDefType.NULL));
75                 });
76
77         updateStatus(ThingStatus.ONLINE);
78     }
79
80     private void updateCallChannels(Call call) {
81         Type lastType = call.type();
82         lastCalls.put(lastType, call.datetime());
83         String group = lastType.name().toLowerCase();
84         String phoneNumber = call.number();
85
86         updateChannelString(group, NUMBER, phoneNumber);
87         updateChannelString(group, NAME, call.name());
88         updateChannelDateTimeState(group, TIMESTAMP, call.datetime());
89
90         // Do not consider duration for Missed & incoming calls
91         if (lastType == Type.ACCEPTED || lastType == Type.OUTGOING) {
92             updateChannelQuantity(group, DURATION, call.duration(), Units.SECOND);
93         }
94     }
95
96     public void emptyQueue() {
97         try {
98             getManager(CallManager.class).emptyQueue();
99         } catch (FreeboxException e) {
100             logger.warn("Error clearing call logs: {}", e.getMessage());
101         }
102     }
103
104     @Override
105     public Collection<Class<? extends ThingHandlerService>> getServices() {
106         return Set.of(CallActions.class);
107     }
108 }