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.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
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;
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;
41 * The {@link CallHandler} is responsible for handling everything associated to the phone calls received on the box line
43 * @author Gaƫl L'hopital - Initial contribution
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);
50 public CallHandler(Thing thing) {
55 void initializeProperties(Map<String, String> properties) throws FreeboxException {
60 protected void internalPoll() throws FreeboxException {
61 logger.debug("Polling phone calls ...");
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));
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));
77 updateStatus(ThingStatus.ONLINE);
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();
86 updateChannelString(group, NUMBER, phoneNumber);
87 updateChannelString(group, NAME, call.name());
88 updateChannelDateTimeState(group, TIMESTAMP, call.datetime());
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);
96 public void emptyQueue() {
98 getManager(CallManager.class).emptyQueue();
99 } catch (FreeboxException e) {
100 logger.warn("Error clearing call logs: {}", e.getMessage());
105 public Collection<Class<? extends ThingHandlerService>> getServices() {
106 return Set.of(CallActions.class);