forked from kwsch/SysBot.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchRoutineExecutor.cs
More file actions
95 lines (82 loc) · 3.7 KB
/
SwitchRoutineExecutor.cs
File metadata and controls
95 lines (82 loc) · 3.7 KB
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
86
87
88
89
90
91
92
93
94
95
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SysBot.Base
{
/// <summary>
/// Commands a Bot to a perform a routine asynchronously.
/// </summary>
public abstract class SwitchRoutineExecutor<T> where T : SwitchBotConfig
{
public readonly SwitchConnectionAsync Connection;
public readonly T Config;
protected SwitchRoutineExecutor(T cfg)
{
Config = cfg;
Connection = new SwitchConnectionAsync(cfg.IP, cfg.Port);
}
public string LastLogged { get; private set; } = "Not Started";
public DateTime LastTime { get; private set; } = DateTime.Now;
protected void ReportStatus() => LastTime = DateTime.Now;
public void Log(string message)
{
Connection.Log(message);
LastLogged = message;
LastTime = DateTime.Now;
}
/// <summary>
/// Connects to the console, then runs the bot.
/// </summary>
/// <param name="token">Cancel this token to have the bot stop looping.</param>
public async Task RunAsync(CancellationToken token)
{
Connection.Connect();
Log("Initializing connection with console...");
await EchoCommands(false, token).ConfigureAwait(false);
await MainLoop(token).ConfigureAwait(false);
Connection.Disconnect();
}
protected abstract Task MainLoop(CancellationToken token);
public abstract void SoftStop();
public async Task Click(SwitchButton b, int delay, CancellationToken token)
{
await Connection.SendAsync(SwitchCommand.Click(b), token).ConfigureAwait(false);
await Task.Delay(delay, token).ConfigureAwait(false);
}
public async Task PressAndHold(SwitchButton b, int hold, int delay, CancellationToken token)
{
// Set hold delay
var delaycgf = SwitchCommand.Configure(SwitchConfigureParameter.buttonClickSleepTime, hold);
await Connection.SendAsync(delaycgf, token).ConfigureAwait(false);
// Press the button
await Click(b, delay, token).ConfigureAwait(false);
// Reset delay
delaycgf = SwitchCommand.Configure(SwitchConfigureParameter.buttonClickSleepTime, 50); // 50 ms
await Connection.SendAsync(delaycgf, token).ConfigureAwait(false);
}
public async Task DaisyChainCommands(int Delay, SwitchButton[] buttons, CancellationToken token)
{
SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, Delay);
var commands = buttons.Select(SwitchCommand.Click).ToArray();
var chain = commands.SelectMany(x => x).ToArray();
await Connection.SendAsync(chain, token).ConfigureAwait(false);
SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, 0);
}
public async Task SetStick(SwitchStick stick, short x, short y, int delay, CancellationToken token)
{
var cmd = SwitchCommand.SetStick(stick, x, y);
await Connection.SendAsync(cmd, token).ConfigureAwait(false);
await Task.Delay(delay, token).ConfigureAwait(false);
}
public async Task DetachController(CancellationToken token)
{
await Connection.SendAsync(SwitchCommand.DetachController(), token).ConfigureAwait(false);
}
public async Task EchoCommands(bool value, CancellationToken token)
{
var cmd = SwitchCommand.Configure(SwitchConfigureParameter.echoCommands, value ? 1 : 0);
await Connection.SendAsync(cmd, token).ConfigureAwait(false);
}
}
}