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;
21 * Handler for notification messages between trackers.
23 * @author Gabor Bicskei - Initial contribution
25 public class NotificationHandler {
27 * Location notifications need to be sent to the own tracker. Only the last location is saved for each tracker
30 private Map<String, LocationMessage> locationNotifications = new HashMap<>();
33 * TransitionMessage notification to send out to the own tracker. Notifications are saved in order they were
36 private Map<String, List<TransitionMessage>> transitionNotifications = new HashMap<>();
39 * Handling notification sent by other trackers.
41 * @param msg Notification message.
43 public void handleNotification(LocationMessage msg) {
45 String trackerId = msg.getTrackerId();
46 if (msg instanceof TransitionMessage) {
47 List<TransitionMessage> transitionMessages = transitionNotifications.computeIfAbsent(trackerId,
48 k -> new ArrayList<>());
49 transitionMessages.add((TransitionMessage) msg);
51 locationNotifications.put(trackerId, msg);
57 * Collect all notifications about friend trackers.
59 * @return List of notification messages from friend trackers need to sent out
61 public List<LocationMessage> getNotifications() {
62 List<LocationMessage> ret;
64 ret = new ArrayList<>(locationNotifications.values());
65 transitionNotifications.values().forEach(ret::addAll);
66 locationNotifications.clear();
67 transitionNotifications.clear();