-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquidctlCLIWrapper.cs
87 lines (70 loc) · 3.12 KB
/
LiquidctlCLIWrapper.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
86
87
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LiquidCtlAfterburnerPlugin
{
internal static class LiquidctlCLIWrapper
{
public static string liquidctlexe = $"{Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)}\\liquidctl.exe";
private static Dictionary<string, Process> liquidctlBackends = new Dictionary<string, Process>();
internal static void Initialize()
{
LiquidctlCall($"--json initialize all");
}
internal static List<LiquidctlStatusJSON> ReadStatus()
{
Process process = LiquidctlCall($"--json status");
return JsonConvert.DeserializeObject<List<LiquidctlStatusJSON>>(process.StandardOutput.ReadToEnd());
}
internal static List<LiquidctlStatusJSON> ReadStatus(string address)
{
Process process = GetLiquidCtlBackend(address);
process.StandardInput.WriteLine("status");
JObject result = JObject.Parse(process.StandardOutput.ReadLine());
if ((string)result.SelectToken("status") == "success")
return result.SelectToken("data").ToObject<List<LiquidctlStatusJSON>>();
throw new Exception((string)result.SelectToken("data"));
}
private static Process GetLiquidCtlBackend(string address) {
Process process = liquidctlBackends.ContainsKey(address) ? liquidctlBackends[address] : null;
if (process != null && !process.HasExited) {
return process;
}
if (process != null) {
liquidctlBackends.Remove(address);
}
process = createLiquidCtlProcess();
process.StartInfo.Arguments = $"--json --address {address} interactive";
liquidctlBackends.Add(address, process);
process.Start();
return process;
}
private static Process LiquidctlCall(string arguments)
{
//File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".log", $"{DateTime.Now:s}: Invoking liquidctl\r\n");
Process process = createLiquidCtlProcess();
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"liquidctl returned non-zero exit code {process.ExitCode}. Last stderr output:\n{process.StandardError.ReadToEnd()}");
}
return process;
}
private static Process createLiquidCtlProcess() {
Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.FileName = liquidctlexe;
return process;
}
}
}