Added countdown timer. Currently counts seconds from n to 0 with various events and callbacks

This commit is contained in:
Heath Volmer
2017-08-18 16:55:54 -06:00
parent 25122abed1
commit 4cda13f004
16 changed files with 194 additions and 13 deletions

View File

@@ -175,6 +175,7 @@
<Compile Include="SmartObjects\SmartObjectHelper.cs" />
<Compile Include="SmartObjects\SmartObjectHelperBase.cs" />
<Compile Include="Routing\TieLine.cs" />
<Compile Include="Timers\CountdownTimer.cs" />
<Compile Include="Touchpanels\MOVED LargeTouchpanelControllerBase.cs" />
<Compile Include="Touchpanels\TriListExtensions.cs" />
<Compile Include="Touchpanels\MOVED UIControllers\DevicePageControllerBase.cs" />

View File

@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using PepperDash.Core;
namespace PepperDash.Essentials.Core
{
public class SecondsCountdownTimer: IKeyed
{
public string Key { get; private set; }
public BoolFeedback IsRunningFeedback { get; private set; }
bool _IsRunning;
public IntFeedback PercentFeedback { get; private set; }
public StringFeedback TimeRemainingFeedback { get; private set; }
public bool CountsDown { get; set; }
public int SecondsToCount { get; set; }
public Action CompletionAction { get; set; }
public Action CancelAction { get; set; }
CTimer SecondTimer;
public DateTime StartTime { get; private set; }
public DateTime FinishTime { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="key"></param>
public SecondsCountdownTimer(string key)
{
Key = key;
IsRunningFeedback = new BoolFeedback(() => _IsRunning);
TimeRemainingFeedback = new StringFeedback(() =>
{
// Need to handle up and down here.
if (StartTime == null || FinishTime == null)
return "";
var timeSpan = FinishTime - DateTime.Now;
return Math.Round(timeSpan.TotalSeconds).ToString();
});
PercentFeedback = new IntFeedback(() =>
{
if (StartTime == null || FinishTime == null)
return 0;
double percent = (FinishTime - DateTime.Now).TotalSeconds
/ (FinishTime - StartTime).TotalSeconds
* 100;
return (int)percent;
});
}
/// <summary>
///
/// </summary>
public void Start()
{
if (_IsRunning)
return;
StartTime = DateTime.Now;
FinishTime = StartTime + TimeSpan.FromSeconds(SecondsToCount);
if (SecondTimer != null)
SecondTimer.Stop();
SecondTimer = new CTimer(SecondElapsedTimerCallback, null, 0, 1000);
_IsRunning = true;
IsRunningFeedback.FireUpdate();
}
public void Cancel()
{
CleanUp();
var a = CancelAction;
if (a != null)
a();
}
public void Reset()
{
_IsRunning = false;
Start();
}
public void Finish()
{
CleanUp();
var a = CompletionAction;
if (a != null)
a();
}
void CleanUp()
{
if (SecondTimer != null)
SecondTimer.Stop();
_IsRunning = false;
IsRunningFeedback.FireUpdate();
}
void SecondElapsedTimerCallback(object o)
{
PercentFeedback.FireUpdate();
TimeRemainingFeedback.FireUpdate();
if (DateTime.Now >= FinishTime)
Finish();
}
}
}

View File

@@ -162,8 +162,6 @@ namespace PepperDash.Essentials.Core
return true;
}
// Dialog is busy
//Debug.Console(2, "Modal is already visible");
return false;
}

View File

@@ -45,7 +45,6 @@ namespace PepperDash.Essentials.Devices.Displays
: base(key, name)
{
Communication = comm;
//Communication.TextReceived += new EventHandler<GenericCommMethodReceiveTextArgs>(Communication_TextReceived);
Communication.BytesReceived += new EventHandler<GenericCommMethodReceiveBytesArgs>(Communication_BytesReceived);
ID = id == null ? (byte)0x01 : Convert.ToByte(id, 16); // If id is null, set default value of 0x01, otherwise assign value passed in constructor
@@ -59,7 +58,6 @@ namespace PepperDash.Essentials.Devices.Displays
: base(key, name)
{
Communication = new GenericTcpIpClient(key + "-tcp", hostname, port, 5000);
//Communication.TextReceived += new EventHandler<GenericCommMethodReceiveTextArgs>(Communication_TextReceived);
ID = id == null ? (byte)0x01 : Convert.ToByte(id, 16); // If id is null, set default value of 0x01, otherwise assign value passed in constructor
Init();
}
@@ -186,7 +184,7 @@ namespace PepperDash.Essentials.Devices.Displays
// Good length, grab the message
var message = newBytes.Skip(4).Take(msgLen).ToArray();
Debug.Console(0, this, "Parsing input: {0}", ComTextHelper.GetEscapedText(message));
Debug.Console(2, this, "Parsing input: {0}", ComTextHelper.GetEscapedText(message));
// At this point, the ack/nak is the first byte
if (message[0] == 0x41)
@@ -194,7 +192,7 @@ namespace PepperDash.Essentials.Devices.Displays
switch (message[1]) // type byte
{
case 0x00: // General status
UpdatePowerFB(message[2]);
UpdatePowerFBWithSource(message[2], message[3]); // "power" can be misrepresented when the display sleeps
UpdateVolumeFB(message[3]);
UpdateMuteFb(message[4]);
UpdateInputFb(message[5]);
@@ -233,12 +231,23 @@ namespace PepperDash.Essentials.Devices.Displays
}
/// <summary>
/// Checks power feedback AND that source >0x10
///
/// </summary>
/// <param name="b"></param>
void UpdatePowerFB(byte b)
void UpdatePowerFBWithSource(byte pb, byte ib)
{
var newVal = b == 1;
var newVal = pb == 1 && ib > 0x10;
if (newVal != _PowerIsOn)
{
_PowerIsOn = newVal;
PowerIsOnFeedback.FireUpdate();
}
}
void UpdatePowerFB(byte pb)
{
var newVal = pb == 1;
if (newVal != _PowerIsOn)
{
_PowerIsOn = newVal;
@@ -419,10 +428,11 @@ namespace PepperDash.Essentials.Devices.Displays
}
public void InputGet()
{
{
SendBytes(new byte[] { 0xAA, 0x14, 0x00, 0x00, 0x00 });
}
/// <summary>
/// Executes a switch, turning on display if necessary.
/// </summary>

View File

@@ -71,7 +71,19 @@ namespace PepperDash.Essentials
DeviceManager.ActivateAll();
Debug.Console(0, "Essentials load complete\r" +
"-------------------------------------------------------------");
}
//********************************************************************
#warning Remove these test things:
var cdt = new SecondsCountdownTimer("timer") { SecondsToCount = 20 };
cdt.CompletionAction = () => Debug.Console(0, "TIMER DONE FOOLS!");
cdt.IsRunningFeedback.OutputChange += (o, a) => Debug.Console(0, "TIMER Running={0}", cdt.IsRunningFeedback);
cdt.PercentFeedback.OutputChange += (o, a) => Debug.Console(0, "TIMER {0}%", cdt.PercentFeedback);
cdt.TimeRemainingFeedback.OutputChange += (o,a) => Debug.Console(0, "TIMER time: {0}", cdt.TimeRemainingFeedback);
DeviceManager.AddDevice(cdt);
//********************************************************************
}
catch (Exception e)
{
Debug.Console(0, "FATAL INITIALIZE ERROR. System is in an inconsistent state:\r{0}", e);

View File

@@ -29,14 +29,34 @@ namespace PepperDash.Essentials
public BoolFeedback IsWarmingFeedback { get; private set; }
public BoolFeedback IsCoolingFeedback { get; private set; }
public BoolFeedback ShutdownPendingFeedback { get; private set; }
public BoolFeedback PowerOffPendingFeedback { get; private set; }
public IntFeedback PowerOffPendingTimerPercentFeedback { get; private set; }
public StringFeedback PowerOffPendingTimeStringFeedback { get; private set; }
bool _PowerOffPending;
public int PowerOffDelaySeconds { get; set; }
public BoolFeedback VacancyPowerDownFeedback { get; private set; }
protected abstract Func<bool> OnFeedbackFunc { get; }
public EssentialsRoomBase(string key, string name) : base(key, name)
{
OnFeedback = new BoolFeedback(OnFeedbackFunc);
PowerOffPendingFeedback = new BoolFeedback(() => _PowerOffPending);
}
/// <summary>
/// Triggers the shutdown timer
/// </summary>
public void StartShutdown()
{
if (!_PowerOffPending)
{
_PowerOffPending = true;
PowerOffPendingFeedback.FireUpdate();
}
}
}
}

View File

@@ -168,6 +168,8 @@ namespace PepperDash.Essentials
TriList.StringInput[UIStringJoin.StartActivityText].StringValue =
"Tap Share to begin";
TriList.BooleanInput[UIBoolJoin.LogoDefaultVisible].BoolValue = true;
}
/// <summary>

View File

@@ -319,19 +319,32 @@ namespace PepperDash.Essentials
/// 15061 Reveals the dual-display subpage
/// </summary>
public const uint DualDisplayPageVisible = 15061;
/// <summary>
/// 15062 Reveals the toggle switch for the sharing mode
/// </summary>
public const uint ToggleSharingModeVisible = 15062;
/// <summary>
/// 15063 Press for the toggle mode switch
/// </summary>
public const uint ToggleSharingModePress = 15063;
/// <summary>
/// 15064
/// </summary>
public const uint LogoDefaultVisible = 15064;
/// <summary>
/// 15065
/// </summary>
public const uint LogoUrlVisible = 15065;
/// <summary>
/// 15085 Visibility join for help subpage
/// </summary>
public const uint HelpPageVisible = 15085;
/// <summary>
/// 15086 Press for help header button
/// </summary>

View File

@@ -72,6 +72,11 @@ namespace PepperDash.Essentials
/// </summary>
public const uint HelpMessage = 3922;
/// <summary>
/// 3923
/// </summary>
public const uint LogoUrl = 3923;
/// <summary>
/// 3961 Name of source on display 1
/// </summary>

View File

@@ -1,3 +1,5 @@
devjson:1 {"deviceKey":"display-1-comMonitor","methodName":"PrintStatus"}
devjson:1 {"deviceKey":"display-1-com","methodName":"SimulateReceive", "params": ["\\x05\\x06taco\\xAA"]}
devjson:1 {"deviceKey":"display-1-com","methodName":"SimulateReceive", "params": ["\\x05\\x06taco\\xAA"]}
devjson:1 {"deviceKey":"timer","methodName":"Start" }