遙控器關機
This commit is contained in:
parent
621bc4e4b7
commit
12f72d802c
@ -14,7 +14,8 @@ namespace DualScreenDemo
|
|||||||
public class CommandHandler
|
public class CommandHandler
|
||||||
{
|
{
|
||||||
public static bool readyForSongListInput = false;
|
public static bool readyForSongListInput = false;
|
||||||
|
private readonly int _maxHistoryLength = 6; // 最多保留 6 筆
|
||||||
|
private readonly Queue<string> _indataHistory = new Queue<string>();
|
||||||
private readonly SongListManager songListManager;
|
private readonly SongListManager songListManager;
|
||||||
|
|
||||||
public CommandHandler(SongListManager songListManager)
|
public CommandHandler(SongListManager songListManager)
|
||||||
@ -25,13 +26,7 @@ namespace DualScreenDemo
|
|||||||
//關機錨點
|
//關機錨點
|
||||||
public async Task ProcessData(string indata)
|
public async Task ProcessData(string indata)
|
||||||
{
|
{
|
||||||
string filePath = Path.Combine(Application.StartupPath, "dataLog.txt");
|
AddToHistory(indata);
|
||||||
if (CheckLogForShutdown(filePath))
|
|
||||||
{
|
|
||||||
Console.WriteLine("Shutdown condition met. Application will now close.");
|
|
||||||
ShutdownComputer();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (indata)
|
switch (indata)
|
||||||
{
|
{
|
||||||
case "A261A4":
|
case "A261A4":
|
||||||
@ -188,6 +183,29 @@ namespace DualScreenDemo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AddToHistory(string indata)
|
||||||
|
{
|
||||||
|
if (_indataHistory.Count >= _maxHistoryLength)
|
||||||
|
{
|
||||||
|
_indataHistory.Dequeue(); // 移除最舊的項目
|
||||||
|
}
|
||||||
|
_indataHistory.Enqueue(indata); // 添加新的項目
|
||||||
|
CheckSequenceforClose(); // 每次更新完 queue 後檢查
|
||||||
|
}
|
||||||
|
private void CheckSequenceforClose()
|
||||||
|
{
|
||||||
|
string[] targetSequence = { "A262A4", "A262A4", "A262A4", "A261A4", "A261A4", "A261A4" };
|
||||||
|
|
||||||
|
if (_indataHistory.Count == targetSequence.Length &&
|
||||||
|
_indataHistory.SequenceEqual(targetSequence))
|
||||||
|
{
|
||||||
|
// 👇 這裡就是條件符合時要做的事情
|
||||||
|
Console.WriteLine("Shutdown condition met. Application will now close.");
|
||||||
|
ShutdownComputer();
|
||||||
|
// 你可以呼叫其他方法、觸發事件、改狀態等等
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void InvokeAction(Action action)
|
void InvokeAction(Action action)
|
||||||
{
|
{
|
||||||
if (OverlayForm.MainForm.InvokeRequired)
|
if (OverlayForm.MainForm.InvokeRequired)
|
||||||
@ -895,20 +913,6 @@ private static void DisplaySongHistory()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool CheckLogForShutdown(string filePath)
|
|
||||||
{
|
|
||||||
if (File.Exists(filePath))
|
|
||||||
{
|
|
||||||
|
|
||||||
string content = File.ReadAllText(filePath).Replace(Environment.NewLine, "");
|
|
||||||
if (content.Length >= 6 && content.Substring(content.Length - 6) == "bbbaaa")
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ShutdownComputer()
|
public static void ShutdownComputer()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
Reference in New Issue
Block a user