using System; using System.Collections.Generic; using Crestron.SimplSharp; using Newtonsoft.Json.Linq; using PepperDash.Core; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Handler for press/hold/release messages /// public static class PressAndHoldHandler { private const long ButtonHeartbeatInterval = 1000; private static readonly Dictionary _pushedActions = new Dictionary(); private static readonly Dictionary>> _pushedActionHandlers; static PressAndHoldHandler() { _pushedActionHandlers = new Dictionary>> { {"pressed", AddTimer }, {"held", ResetTimer }, {"released", StopTimer } }; } private static void AddTimer(string deviceKey, Action action) { Debug.LogDebug("Attempting to add timer for {deviceKey}", deviceKey); if (_pushedActions.TryGetValue(deviceKey, out CTimer cancelTimer)) { Debug.LogDebug("Timer for {deviceKey} already exists", deviceKey); return; } Debug.LogDebug("Adding timer for {deviceKey} with due time {dueTime}", deviceKey, ButtonHeartbeatInterval); action(true); cancelTimer = new CTimer(o => { Debug.LogDebug("Timer expired for {deviceKey}", deviceKey); action(false); _pushedActions.Remove(deviceKey); }, ButtonHeartbeatInterval); _pushedActions.Add(deviceKey, cancelTimer); } private static void ResetTimer(string deviceKey, Action action) { Debug.LogDebug("Attempting to reset timer for {deviceKey}", deviceKey); if (!_pushedActions.TryGetValue(deviceKey, out CTimer cancelTimer)) { Debug.LogDebug("Timer for {deviceKey} not found", deviceKey); return; } Debug.LogDebug("Resetting timer for {deviceKey} with due time {dueTime}", deviceKey, ButtonHeartbeatInterval); cancelTimer.Reset(ButtonHeartbeatInterval); } private static void StopTimer(string deviceKey, Action action) { Debug.LogDebug("Attempting to stop timer for {deviceKey}", deviceKey); if (!_pushedActions.TryGetValue(deviceKey, out CTimer cancelTimer)) { Debug.LogDebug("Timer for {deviceKey} not found", deviceKey); return; } Debug.LogDebug("Stopping timer for {deviceKey} with due time {dueTime}", deviceKey, ButtonHeartbeatInterval); action(false); cancelTimer.Stop(); _pushedActions.Remove(deviceKey); } public static Action> GetPressAndHoldHandler(string value) { Debug.LogDebug("Getting press and hold handler for {value}", value); if (!_pushedActionHandlers.TryGetValue(value, out Action> handler)) { Debug.LogDebug("Press and hold handler for {value} not found", value); return null; } Debug.LogDebug("Got handler for {value}", value); return handler; } /// /// HandlePressAndHold method /// public static void HandlePressAndHold(string deviceKey, JToken content, Action action) { var msg = content.ToObject>(); Debug.LogDebug("Handling press and hold message of {type} for {deviceKey}", msg.Value, deviceKey); var timerHandler = GetPressAndHoldHandler(msg.Value); if (timerHandler == null) { return; } timerHandler(deviceKey, action); } } }