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.gpstracker.internal.message;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
22 import org.openhab.binding.gpstracker.internal.message.dto.TransitionMessage;
25 * Handler for notification messages between trackers.
27 * @author Gabor Bicskei - Initial contribution
30 public class NotificationHandler {
32 * Location notifications need to be sent to the own tracker. Only the last location is saved for each tracker
35 private Map<String, LocationMessage> locationNotifications = new HashMap<>();
38 * TransitionMessage notification to send out to the own tracker. Notifications are saved in order they were
41 private Map<String, List<TransitionMessage>> transitionNotifications = new HashMap<>();
44 * Handling notification sent by other trackers.
46 * @param msg Notification message.
48 public void handleNotification(LocationMessage msg) {
50 String trackerId = msg.getTrackerId();
51 if (msg instanceof TransitionMessage message) {
52 List<TransitionMessage> transitionMessages = transitionNotifications.computeIfAbsent(trackerId,
53 k -> new ArrayList<>());
54 if (transitionMessages != null) {
55 transitionMessages.add(message);
58 locationNotifications.put(trackerId, msg);
64 * Collect all notifications about friend trackers.
66 * @return List of notification messages from friend trackers need to sent out
68 public List<LocationMessage> getNotifications() {
69 List<LocationMessage> ret;
71 ret = new ArrayList<>(locationNotifications.values());
72 transitionNotifications.values().forEach(ret::addAll);
73 locationNotifications.clear();
74 transitionNotifications.clear();