2
0
Files
controlmymonitormanagement/Library/Method/CMMCommand.cs

203 lines
5.8 KiB
C#
Raw Normal View History

2022-05-23 00:58:58 +08:00
using CMM.Library.Base;
using CMM.Library.Helpers;
using CMM.Library.ViewModel;
using System.IO;
2023-07-02 22:17:57 +08:00
namespace CMM.Library.Method;
/// <summary>
/// Control My Monitor Management Command
/// </summary>
public static class CMMCommand
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
static readonly string CMMTmpFolder = Path.Combine(Path.GetTempPath(), $"CMM");
static readonly string CMMexe = Path.Combine(CMMTmpFolder, "ControlMyMonitor.exe");
static readonly string CMMsMonitors = Path.Combine(CMMTmpFolder, "smonitors.tmp");
public static async Task ScanMonitor()
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
await BytesToFileAsync(new(CMMexe));
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /smonitors {CMMsMonitors}");
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static Task PowerOn(string monitorSN)
2023-07-02 22:17:57 +08:00
{
2023-07-03 01:51:09 +08:00
return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 1");
2023-07-02 22:17:57 +08:00
}
2023-07-03 01:51:09 +08:00
public static Task Sleep(string monitorSN)
2023-07-02 22:17:57 +08:00
{
2023-07-03 01:51:09 +08:00
return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 4");
2023-07-02 22:17:57 +08:00
}
2023-07-05 20:45:59 +08:00
private static async Task<string> GetMonitorValue(string monitorSN, int? reTry = 0)
2023-07-02 22:17:57 +08:00
{
2023-07-05 20:45:59 +08:00
var value = string.Empty;
while (reTry <= 5)
{
var cmdFileName = Path.Combine(CMMTmpFolder, $"{Guid.NewGuid()}.bat");
var cmd = $"{CMMexe} /GetValue {monitorSN} D6\r\n" +
$"echo %errorlevel%";
File.WriteAllText(cmdFileName, cmd);
var values = await ConsoleHelper.ExecuteCommand(cmdFileName);
File.Delete(cmdFileName);
value = values.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
if (!string.IsNullOrEmpty(value) && value != "0") return value;
await Task.Delay(500);
await GetMonitorValue(monitorSN, reTry++);
};
return value;
2023-07-03 01:51:09 +08:00
}
public static async Task<string> GetMonPowerStatus(string monitorSN)
{
var status = await GetMonitorValue(monitorSN);
return status switch
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
"1" => "PowerOn",
"4" => "Sleep",
"5" => "PowerOff",
_ => string.Empty
};
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static async Task ScanMonitorStatus(IEnumerable<XMonitor> monitors)
{
var taskList = monitors.Select(x =>
{
return ScanMonitorStatus($"{CMMTmpFolder}\\{x.SerialNumber}.tmp", x);
});
await Task.WhenAll(taskList);
2023-07-02 22:17:57 +08:00
}
2023-07-03 01:51:09 +08:00
static async Task ScanMonitorStatus(string savePath, XMonitor mon)
2023-07-02 22:17:57 +08:00
{
2023-07-03 01:51:09 +08:00
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /sjson {savePath} {mon.MonitorID}");
var monitorModel = JsonHelper.JsonFormFile<IEnumerable<SMonitorModel>>(savePath);
var status = monitorModel.ReadMonitorStatus();
mon.Status = new ObservableRangeCollection<XMonitorStatus>(status);
2023-07-02 22:17:57 +08:00
}
/// <summary>
/// 取得螢幕狀態
/// </summary>
2023-07-03 01:51:09 +08:00
public static IEnumerable<XMonitorStatus> ReadMonitorStatus(this IEnumerable<SMonitorModel> monitorModel)
2023-07-02 22:17:57 +08:00
{
2023-07-03 01:51:09 +08:00
foreach (var m in monitorModel)
{
yield return new XMonitorStatus
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
VCP_Code = m.VCPCode,
VCPCodeName = m.VCPCodeName,
Read_Write = m.ReadWrite,
CurrentValue = TryGetInt(m.CurrentValue),
MaximumValue = TryGetInt(m.MaximumValue),
PossibleValues = TryGetArrStr(m.PossibleValues),
};
2023-07-02 22:17:57 +08:00
}
2023-07-03 01:51:09 +08:00
IEnumerable<int> TryGetArrStr(string str)
2023-07-02 22:17:57 +08:00
{
2023-07-03 01:51:09 +08:00
return str.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(x => TryGetInt(x))
.Where(x => x != null)
.Select(x => (int)x)
.ToList();
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
int? TryGetInt(string str)
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
return int.TryParse(str, out var value)
? value
: null;
2023-07-02 22:17:57 +08:00
}
}
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
/// <summary>
/// 取得螢幕清單
/// </summary>
public static async Task<IEnumerable<XMonitor>> ReadMonitorsData()
{
var monitors = new List<XMonitor>();
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
if (!File.Exists(CMMsMonitors)) return monitors;
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
XMonitor mon = null;
string context;
foreach (var line in await File.ReadAllLinesAsync(CMMsMonitors))
{
var sp = line.Split(":", StringSplitOptions.RemoveEmptyEntries);
try
{
if (sp.Length != 2 || string.IsNullOrEmpty(sp[1])) continue;
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
context = sp[1].Substring(2, sp[1].Length - 3);
}
catch
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
continue;
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
if (sp[0].StartsWith("Monitor Device Name"))
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
mon = new XMonitor();
mon.MonitorDeviceName = context;
continue;
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
if (sp[0].StartsWith("Monitor Name"))
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
mon.MonitorName = context;
continue;
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
if (sp[0].StartsWith("Serial Number"))
{
mon.SerialNumber = context;
continue;
}
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
if (sp[0].StartsWith("Adapter Name"))
2022-05-23 00:58:58 +08:00
{
2023-07-02 22:17:57 +08:00
mon.AdapterName = context;
continue;
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
if (sp[0].StartsWith("Monitor ID"))
{
mon.MonitorID = context;
monitors.Add(mon);
continue;
}
2022-05-23 00:58:58 +08:00
}
2023-07-02 22:17:57 +08:00
return monitors;
}
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
static void BytesToFile(FileInfo fi)
{
fi.Refresh();
if (fi.Exists) return;
if (!fi.Directory.Exists) fi.Directory.Create();
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
File.WriteAllBytes(fi.FullName, fi.Name.ResourceToByteArray());
}
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
static async Task BytesToFileAsync(FileInfo fi)
{
fi.Refresh();
if (fi.Exists) return;
if (!fi.Directory.Exists) fi.Directory.Create();
2022-05-23 00:58:58 +08:00
2023-07-02 22:17:57 +08:00
await File.WriteAllBytesAsync(fi.FullName, fi.Name.ResourceToByteArray());
2022-05-23 00:58:58 +08:00
}
}