This commit is contained in:
Vector Von 2022-06-30 10:22:12 +08:00
parent 0a437ecec2
commit 9528b1f636
5 changed files with 140 additions and 24 deletions

View File

@ -2,6 +2,7 @@
{ {
internal class Config internal class Config
{ {
public int mode = 0;
public string sn = ""; public string sn = "";
public string disk0 = ""; public string disk0 = "";
public string disk1 = ""; public string disk1 = "";

View File

@ -6,25 +6,32 @@
xmlns:local="clr-namespace:Topuino_Client_Windows" xmlns:local="clr-namespace:Topuino_Client_Windows"
mc:Ignorable="d" mc:Ignorable="d"
Closing="Window_Closing" Closing="Window_Closing"
Title="Topuino 客户端" Height="150" Width="400"> Title="Topuino 客户端" Height="180" Width="400">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5"> <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5">
<Label Content="运行模式:" VerticalAlignment="Center" />
<RadioButton x:Name="RadioButton_UsbMode" VerticalAlignment="Center" Margin="5,0" IsChecked="True">USB 模式</RadioButton>
<RadioButton x:Name="RadioButton_OnlineMode" VerticalAlignment="Center" Margin="5,0">在线模式</RadioButton>
<RadioButton x:Name="RadioButton_LocalMode" VerticalAlignment="Center" Margin="5,0">本地模式</RadioButton>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5">
<TextBlock Text="设备 SN" VerticalAlignment="Center"/> <TextBlock Text="设备 SN" VerticalAlignment="Center"/>
<TextBox x:Name="TextBox_DeviceSn" Width="80" VerticalAlignment="Center" /> <TextBox x:Name="TextBox_DeviceSn" Width="80" VerticalAlignment="Center" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5"> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5">
<TextBlock Text="0 号磁盘:" VerticalAlignment="Center"/> <TextBlock Text="0 号磁盘:" VerticalAlignment="Center"/>
<ComboBox x:Name="ComboBox_Disk0" Height="20" Width="60" VerticalAlignment="Center"/> <ComboBox x:Name="ComboBox_Disk0" Height="20" Width="60" VerticalAlignment="Center"/>
<Separator Width="10" Background="Transparent" /> <Separator Width="10" Background="Transparent" />
<TextBlock Text="1 号磁盘:" VerticalAlignment="Center"/> <TextBlock Text="1 号磁盘:" VerticalAlignment="Center"/>
<ComboBox x:Name="ComboBox_Disk1" Height="20" Width="60" VerticalAlignment="Center"/> <ComboBox x:Name="ComboBox_Disk1" Height="20" Width="60" VerticalAlignment="Center"/>
</StackPanel> </StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5"> <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
<Button Content="保存" Click="Button_Save_Click" Height="20" Width="40" /> <Button Content="保存" Click="Button_Save_Click" Height="20" Width="40" />
<Separator Width="10" Background="Transparent" /> <Separator Width="10" Background="Transparent" />
<Button Content="隐藏" Click="Button_Hide_Click" Height="20" Width="40" /> <Button Content="隐藏" Click="Button_Hide_Click" Height="20" Width="40" />

View File

@ -56,38 +56,72 @@ namespace Topuino_Client_Windows
private NotifyIcon trayIon = new NotifyIcon(); private NotifyIcon trayIon = new NotifyIcon();
private int mode = 0;
private string sn = ""; private string sn = "";
private List<DriveInfo> allDrives; private List<DriveInfo> allDrives;
private DriveInfo? drive0 = null; private DriveInfo? drive0 = null;
private DriveInfo? drive1 = null; private DriveInfo? drive1 = null;
private Config? initConfig = null;
private Thread? refreshThread = null; private Thread? refreshThread = null;
private ManualResetEvent requestStop = new ManualResetEvent(false); private ManualResetEvent requestStop = new ManualResetEvent(false);
private ManualResetEvent stopDone = new ManualResetEvent(false); private ManualResetEvent stopDone = new ManualResetEvent(false);
private PublicComm onlineClient = new PublicComm();
private UsbComm usbClient = new UsbComm();
private void LoadConfig() private void LoadConfig()
{ {
try try
{ {
initConfig = JsonConvert.DeserializeObject<Config>(File.ReadAllText("Config.json")); Config? initConfig = JsonConvert.DeserializeObject<Config>(File.ReadAllText("Config.json"));
if (initConfig == null) if (initConfig == null)
{ {
throw new Exception(); throw new Exception();
} }
switch (initConfig.mode)
{
case 0:
RadioButton_UsbMode.IsChecked = true;
mode = 0;
break;
case 1:
RadioButton_OnlineMode.IsChecked = true;
mode = 1;
break;
case 2:
RadioButton_LocalMode.IsChecked = true;
mode = 2;
break;
default:
RadioButton_UsbMode.IsChecked = true;
mode = 0;
break;
}
// check if drivers missing // check if drivers missing
bool disk0Found = false;
bool disk1Fount = false;
foreach (DriveInfo drive in allDrives) foreach (DriveInfo drive in allDrives)
{ {
if (drive.Name != initConfig.disk0 && drive.Name != initConfig.disk1) if (drive.Name == initConfig.disk0)
{ {
initConfig.disk0 = drive.Name; disk0Found = true;
initConfig.disk1 = drive.Name; }
break; if (drive.Name == initConfig.disk1)
{
disk1Fount = true;
} }
} }
if (!disk0Found)
{
initConfig.disk0 = allDrives[0].Name;
}
if (!disk1Fount)
{
initConfig.disk1 = allDrives[0].Name;
}
for (int i = 0; i < allDrives.Count; i++) for (int i = 0; i < allDrives.Count; i++)
{ {
@ -149,25 +183,62 @@ namespace Topuino_Client_Windows
} }
#pragma warning disable CS8602 // Dereference of a possibly null reference. #pragma warning disable CS8602 // Dereference of a possibly null reference.
Dictionary<string, string> statusInfo = new Dictionary<string, string>(); MonitorData data = new MonitorData
statusInfo.Add("SN", sn); {
statusInfo.Add("CPU_PERCENT", ((int)cpuCounter.NextValue()).ToString()); sn = sn,
statusInfo.Add("MEM_PERCENT", ((int)ramPercentUsed).ToString()); cpuPercent = (byte)cpuCounter.NextValue(),
statusInfo.Add("DISK_PERCENT", ((int)((double)(drive0.TotalSize - drive0.AvailableFreeSpace) / drive0.TotalSize * 100)).ToString()); memPercent = (byte)ramPercentUsed,
statusInfo.Add("DISK1_PERCENT", ((int)((double)(drive1.TotalSize - drive1.AvailableFreeSpace) / drive1.TotalSize * 100)).ToString()); disk0Percent = (byte)((double)(drive0.TotalSize - drive0.AvailableFreeSpace) / drive0.TotalSize * 100),
statusInfo.Add("DISK_READ_RATE", ((int)diskReadCounter.NextValue()).ToString()); disk1Percent = (byte)((double)(drive1.TotalSize - drive1.AvailableFreeSpace) / drive1.TotalSize * 100),
statusInfo.Add("DISK_WRITE_RATE", ((int)diskWriteCounter.NextValue()).ToString()); diskReadRate = (uint)diskReadCounter.NextValue(),
statusInfo.Add("NET_SENT_RATE", ((int)(netBytesSentAfter - netBytesSentBefore)).ToString()); diskWriteRate = (uint)diskWriteCounter.NextValue(),
statusInfo.Add("NET_RECV_RATE", ((int)(netBytesSentAfter - netBytesSentBefore)).ToString()); netSentRate = (uint)(netBytesSentAfter - netBytesSentBefore),
netRecvRate = (uint)(netBytesReceiveAfter - netBytesReceiveBefore),
};
#pragma warning restore CS8602 // Dereference of a possibly null reference. #pragma warning restore CS8602 // Dereference of a possibly null reference.
PublicComm connection = new PublicComm();
connection.Post(statusInfo);
} }
stopDone.Set(); stopDone.Set();
} }
private struct MonitorData
{
public string sn;
public byte cpuPercent;
public byte memPercent;
public byte disk0Percent;
public byte disk1Percent;
public uint diskReadRate;
public uint diskWriteRate;
public uint netSentRate;
public uint netRecvRate;
}
private void UsbRun(MonitorData data)
{
}
private void OnlineRun(MonitorData data)
{
Dictionary<string, string> statusInfo = new Dictionary<string, string>();
statusInfo.Add("SN", data.sn);
statusInfo.Add("CPU_PERCENT", data.cpuPercent.ToString());
statusInfo.Add("MEM_PERCENT", data.memPercent.ToString());
statusInfo.Add("DISK_PERCENT", data.disk0Percent.ToString());
statusInfo.Add("DISK1_PERCENT", data.disk1Percent.ToString());
statusInfo.Add("DISK_READ_RATE", data.diskReadRate.ToString());
statusInfo.Add("DISK_WRITE_RATE", data.diskWriteRate.ToString());
statusInfo.Add("NET_SENT_RATE", data.netSentRate.ToString());
statusInfo.Add("NET_RECV_RATE", data.netRecvRate.ToString());
}
private void OfflineRun(MonitorData data)
{
}
public void ShowErrorBox(string msg) public void ShowErrorBox(string msg)
{ {
System.Windows.MessageBox.Show( System.Windows.MessageBox.Show(
@ -202,6 +273,7 @@ namespace Topuino_Client_Windows
if (!drive0.IsReady || !drive1.IsReady) if (!drive0.IsReady || !drive1.IsReady)
{ {
ShowErrorBox("磁盘未就绪"); ShowErrorBox("磁盘未就绪");
Mouse.OverrideCursor = null;
return; return;
} }
#pragma warning restore CS8602 // Dereference of a possibly null reference. #pragma warning restore CS8602 // Dereference of a possibly null reference.
@ -215,6 +287,29 @@ namespace Topuino_Client_Windows
private async void SaveConfig() private async void SaveConfig()
{ {
Config newConfig = new Config(); Config newConfig = new Config();
newConfig.mode = 0;
if (RadioButton_UsbMode.IsChecked != null)
{
if ((bool)RadioButton_UsbMode.IsChecked)
{
newConfig.mode = 0;
}
}
if (RadioButton_OnlineMode.IsChecked != null)
{
if ((bool)RadioButton_OnlineMode.IsChecked)
{
newConfig.mode = 1;
}
}
if (RadioButton_LocalMode.IsChecked != null)
{
if ((bool)RadioButton_LocalMode.IsChecked)
{
newConfig.mode = 2;
}
}
newConfig.sn = sn; newConfig.sn = sn;
#pragma warning disable CS8602 // Dereference of a possibly null reference. #pragma warning disable CS8602 // Dereference of a possibly null reference.
newConfig.disk0 = drive0.Name; newConfig.disk0 = drive0.Name;

View File

@ -1,4 +1,5 @@
{ {
"mode": 0,
"sn": "V0000T0000", "sn": "V0000T0000",
"disk0": "C:\\", "disk0": "C:\\",
"disk1": "C:\\" "disk1": "C:\\"

12
UsbComm.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Topuino_Client_Windows
{
internal class UsbComm
{
}
}