From ef9c653ad7c5bef550b8b0f65d42d739544cc3c7 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 25 Feb 2024 22:16:01 -0500 Subject: [PATCH] `Device connected` notification only show once every half hour per device to avoid spam --- CHANGELOG.md | 4 +++ .../Services/LoungeService/LoungeApi.bs | 31 +++++++------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 800aaa4d..3a3d2df4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Small improvement in the context menu of the Playlist view - Videos can be paused and resumed using the `Ok` remote button +### Fixed + +- `Device connected` notification only show once every 30 minutes per device to avoid spam + ## [0.20.1] - 2024-02-23 ### Fixed diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs index 8215cf1c..46eedcac 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs @@ -13,6 +13,11 @@ import "pkg:/source/utils/UrlUtils.bs" const COMMAND_HANDLER_PREFIX = "_Handle_" +' Apparently devices can be connected and disconnected very frequently. +' We don't want to spam the user with notifications, so we'll only show a notification +' if the last notification was more than 30 minutes ago. +const CONNECT_NOTIFICATION_MIN_INTERVAL_SECONDS = 1800 + class LoungeApi public loungeServiceNode @@ -57,13 +62,13 @@ class LoungeApi commandHandlers[COMMAND_TYPE_DPAD_COMMAND] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_VOICE_COMMAND] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_ON_USER_ACTIVITY] = m.HandleIgnoreCommand + commandHandlers[COMMAND_TYPE_REMOTE_DISCONNECTED] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_NOW_PLAYING] = m.HandleGetPlayingNow commandHandlers[COMMAND_TYPE_SESSION_ID] = m.HandleSessionId commandHandlers[COMMAND_TYPE_G_SESSION_ID] = m.HandleGSessionId commandHandlers[COMMAND_TYPE_LOUNGE_STATUS] = m.HandleLoungeStatus commandHandlers[COMMAND_TYPE_REMOTE_CONNECTED] = m.HandleRemoteConnected - commandHandlers[COMMAND_TYPE_REMOTE_DISCONNECTED] = m.HandleRemoteDisconnected commandHandlers[COMMAND_TYPE_SET_VOLUME] = m.HandleSetVolume commandHandlers[COMMAND_TYPE_SET_PLAYLIST] = m.HandleSetPlaylist commandHandlers[COMMAND_TYPE_UPDATE_PLAYLIST] = m.HandleUpdatePlaylist @@ -340,7 +345,7 @@ class LoungeApi for each device in devices if m.devices.DoesExist(device.id) - device.lastConnectEventType = m.devices[device.id].lastConnectEventType + device.lastConnectNotificationTime = m.devices[device.id].lastConnectNotificationTime end if m.devices[device.id] = device end for @@ -362,11 +367,14 @@ class LoungeApi m.devices[id] = newDevice - if ValidString(newDevice.lastConnectEventType) = commandType + nowSeconds = TimeUtils.Now().AsSeconds() + lastConnectNotificationInterval = nowSeconds - ValidInt(newDevice.lastConnectNotificationTime) + if lastConnectNotificationInterval < CONNECT_NOTIFICATION_MIN_INTERVAL_SECONDS + LogInfo("Skipping connect notification. Last connect notification interval:", lastConnectNotificationInterval, "seconds") return end if - newDevice.lastConnectEventType = commandType + newDevice.lastConnectNotificationTime = nowSeconds userName = newDevice.user deviceName = newDevice.name @@ -375,21 +383,6 @@ class LoungeApi LoungeDeviceConnected.ShowNotifcation(m.notificationsNode, deviceName, userName, userAvatarUri) end function - function HandleRemoteDisconnected(commandId as integer, commandType as string, commandArgs as object, messagesToSend as object) as void - LogInfo(commandType, FormatJson(commandArgs)) - newDevice = commandArgs - id = newDevice.id - if StringUtils.IsNullOrEmpty(id) - return - end if - - if not m.devices.DoesExist(id) - return - end if - - m.devices[id].lastConnectEventType = commandType - end function - function HandleSetVolume(commandId as integer, commandType as string, commandArgs as object, messagesToSend as object) as void player = m.videoQueueNode.player if player = invalid