-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerAction.cs
85 lines (76 loc) · 2.37 KB
/
TimerAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using streamdeck_client_csharp;
using streamdeck_client_csharp.Events;
namespace TimerAndClock
{
[Action("com.tyren.timerandclock.timer")]
public class TimerAction : ActionBase
{
private StreamDeckConnection m_Connection;
private string m_Context;
private DateTimeOffset? m_ResetTime;
private bool m_WasReset = false;
private Stopwatch m_Stopwatch = new Stopwatch();
public override Task KeyDownAsync()
{
m_ResetTime = DateTimeOffset.UtcNow.AddSeconds(1);
return Task.FromResult(0);
}
public override Task KeyUpAsync()
{
return Task.Run(() =>
{
m_ResetTime = null;
if (m_WasReset)
{
m_WasReset = false;
return;
}
// Otherwise, toggle running state
if (m_Stopwatch.IsRunning)
{
m_Stopwatch.Stop();
}
else
{
m_Stopwatch.Start();
}
});
}
public override Task LoadAsync(StreamDeckConnection connection, string action, string context, JObject settings)
{
m_Connection = connection;
m_Context = context;
return Task.FromResult(0);
}
public override Task ProcessPropertyInspectorAsync(SendToPluginEvent propertyInspectorEvent)
{
return Task.FromResult(0);
}
public override async Task RunTickAsync()
{
if (m_ResetTime.HasValue)
{
if (m_ResetTime.Value < DateTimeOffset.UtcNow)
{
// Reset!
m_WasReset = true;
m_ResetTime = null;
m_Stopwatch.Reset();
}
}
TimeSpan elapsed = m_Stopwatch.Elapsed;
await m_Connection.SetTitleAsync(m_Stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), m_Context, SDKTarget.HardwareAndSoftware);
}
public override Task SaveAsync()
{
return Task.FromResult(0);
}
}
}