Skip to content

Commit

Permalink
💾 Feat: 启动后前5s观察是否已有主控设备
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Nov 13, 2022
1 parent d0b2952 commit bb7c061
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions Services/DevicesManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Avalonia.Threading;
using KitX.Web.Rules;
using KitX_Dashboard.Data;
using KitX_Dashboard.Views.Pages.Controls;
using Serilog;
using System;
using System.Collections.Generic;
using System.Timers;
using System.Threading;
using Timer = System.Timers.Timer;

namespace KitX_Dashboard.Services
{
Expand Down Expand Up @@ -93,9 +95,9 @@ internal static void KeepCheckAndRemove()
foreach (var item in Program.DeviceCards)
{
if (item.viewModel.DeviceInfo.DeviceMacAddress.Equals(
WebServer.DefaultDeviceInfoStruct.DeviceMacAddress)
DevicesServer.DefaultDeviceInfoStruct.DeviceMacAddress)
&& item.viewModel.DeviceInfo.DeviceName.Equals(
WebServer.DefaultDeviceInfoStruct.DeviceName))
DevicesServer.DefaultDeviceInfoStruct.DeviceName))
{
localMachine = item;
break;
Expand Down Expand Up @@ -125,6 +127,75 @@ internal static void Update(DeviceInfoStruct deviceInfo)
{
deviceInfoStructs.Enqueue(deviceInfo);
}

/// <summary>
/// 观察主控
/// </summary>
internal static void Watch4MainDevice()
{
new Thread(() =>
{
try
{
int checkedTime = 0;
bool hadMainDevice = false;
DateTime earliestBuiltServerTime = DateTime.Now;
int serverPort = 0;
Timer timer = new()
{
Interval = 1000,
AutoReset = true
};
timer.Elapsed += (_, _) =>
{
try
{
foreach (var item in Program.DeviceCards)
{
if (item.viewModel.deviceInfo.IsMainDevice)
{
if (item.viewModel.deviceInfo.DeviceServerBuildTime < earliestBuiltServerTime)
{
serverPort = item.viewModel.deviceInfo.DeviceServerPort;
}
hadMainDevice = true;
}
}
++checkedTime;
if (checkedTime == 5)
{
timer.Stop();
WatchingOver(hadMainDevice, serverPort);
}
}
catch (Exception e)
{
Log.Error("In Watch4MainDevice", e);
}
};
timer.Start();
}
catch (Exception ex)
{
Log.Error("In Watch4MainDevice", ex);
}
}).Start();
}

/// <summary>
/// 观察结束
/// </summary>
internal static void WatchingOver(bool hadMainDevice, int serverPort)
{
if (hadMainDevice)
{

}
else
{

}
}
}
}

Expand Down

0 comments on commit bb7c061

Please sign in to comment.