This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
75 lines (61 loc) · 2.21 KB
/
Client.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
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LumeniteApiCsharp.Commands;
namespace LumeniteApiCsharp
{
public class Client
{
public char Prefix = '-';
public List<BaseDevice> Devices { get; private set; }
private readonly List<Commands.Command> _commands = new();
public Client()
{
char p = Utils.LoadPrefix();
if (p != ' ')
Prefix = p;
_commands.Add(new DeviceListCommand(this));
_commands.Add(new HelpCommand(this));
_commands.Add(new PowerAllCommand(this));
_commands.Add(new PowerCommand(this));
_commands.Add(new PrefixCommand(this));
}
public async Task Login(GatewayOptions options)
{
GatewayConnection gateway = new(options);
await gateway.Connect();
Devices = gateway.Devices;
Task.Run(() => WaitForCommand());
}
private void WaitForCommand()
{
Utils.Print($"Type '{Prefix}' before any command.", ConsoleColor.Yellow);
Utils.Print($"{Prefix}help for help.", ConsoleColor.Yellow);
while (true)
{
string line = Console.ReadLine();
if (line != null && line[0] == Prefix)
{
string[] arguments = line.Substring(1, line.Length - 1).ToLower().Split(' ');
string command = arguments[0];
for (int i = 0; i < _commands.Count; i++)
{
Commands.Command cmd = _commands[i];
cmd.Setup(arguments);
if (cmd.name == command || cmd.aliases.Contains(command))
{
cmd.Action();
}
}
}
}
}
public T GetDeviceById<T>(int id) where T : BaseDevice
{
return Devices.Where(x => x.Id == id).Cast<T>().FirstOrDefault();
}
}
public record GatewayOptions(string Url, int Port,bool Ssl, string Username, string Password);
}