From 139c3f867388049393a045ceb93c19d29a147723 Mon Sep 17 00:00:00 2001 From: Vector Von Date: Sun, 3 Jul 2022 22:15:55 +0800 Subject: [PATCH] add local mode --- Config.cs | 3 ++- LocalConnector.cs | 38 +++++++++++++++++++++++++++++++++++ MainWindow.xaml | 37 ++++++++++++++++++++++------------ MainWindow.xaml.cs | 49 ++++++++++++++++++++++++++++++++++++++++++++-- pack.iss | 2 +- 5 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 LocalConnector.cs diff --git a/Config.cs b/Config.cs index 265152c..aae4a81 100644 --- a/Config.cs +++ b/Config.cs @@ -3,8 +3,9 @@ internal class Config { public int mode = 0; - public string sn = ""; public string disk0 = ""; public string disk1 = ""; + public string sn = ""; + public string ip = ""; } } diff --git a/LocalConnector.cs b/LocalConnector.cs new file mode 100644 index 0000000..1f88984 --- /dev/null +++ b/LocalConnector.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Sockets; + + +namespace Topuino_Client_Windows +{ + internal class LocalConnector + { + internal LocalConnector(string addr) + { + client = new UdpClient(); + client.Connect(addr, 32737); + } + + private UdpClient client; + + internal void Send(byte[] data) + { + byte[] buff = new byte[data.Length + 4]; + Array.Copy(data, 0, buff, 4, data.Length); + buff[0] = 0x19; + buff[1] = 0x26; + buff[2] = 0x08; + buff[3] = 0x17; + + client.Send(buff, buff.Length); + } + + internal void Dispose() + { + client.Dispose(); + } + } +} diff --git a/MainWindow.xaml b/MainWindow.xaml index bd6b969..9ded18e 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -6,24 +6,25 @@ xmlns:local="clr-namespace:Topuino_Client_Windows" mc:Ignorable="d" Closing="Window_Closing" - Title="Topuino 客户端" Height="180" Width="400"> - + Title="Topuino 客户端" Height="270" Width="400"> + - - - - + + + + + + + + - - - - + 本地模式 + @@ -31,7 +32,17 @@ - + + + + + + + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 925ad46..34dae94 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -62,6 +62,7 @@ namespace Topuino_Client_Windows private int mode = 0; private string sn = ""; + private string ipAddr = ""; private List allDrives; private DriveInfo drive0; private DriveInfo drive1; @@ -73,6 +74,7 @@ namespace Topuino_Client_Windows private OnlineConnector? onlineConnector = null; private UsbConnector? usbConnector = null; + private LocalConnector? localConnector = null; private void LoadConfig() { @@ -138,6 +140,7 @@ namespace Topuino_Client_Windows } TextBox_DeviceSn.Text = initConfig.sn; + TextBox_DeviceIp.Text = initConfig.ip; } catch { @@ -288,7 +291,44 @@ namespace Topuino_Client_Windows private void LocalRun(MonitorData data) { + if (localConnector == null) + { + try + { + localConnector = new LocalConnector(ipAddr); + ShowConnected(); + } + catch + { + localConnector = null; + ShowDisconnected(); + return; + } + } + int size = Marshal.SizeOf(data); + byte[] bin = new byte[size]; + IntPtr ptr = IntPtr.Zero; + try + { + ptr = Marshal.AllocHGlobal(size); + Marshal.StructureToPtr(data, ptr, true); + Marshal.Copy(ptr, bin, 0, size); + } + finally + { + Marshal.FreeHGlobal(ptr); + } + try + { + localConnector.Send(bin); + } + catch + { + localConnector.Dispose(); + localConnector = null; + ShowDisconnected(); + } } @@ -319,10 +359,13 @@ namespace Topuino_Client_Windows { mode = 2; } - sn = TextBox_DeviceSn.Text; drive0 = ComboBox_Disk0.SelectedItem as DriveInfo; drive1 = ComboBox_Disk1.SelectedItem as DriveInfo; + + sn = TextBox_DeviceSn.Text; + + ipAddr = TextBox_DeviceIp.Text; } catch (Exception e) { @@ -360,9 +403,10 @@ namespace Topuino_Client_Windows { Config newConfig = new Config(); newConfig.mode = mode; - newConfig.sn = sn; newConfig.disk0 = drive0.Name; newConfig.disk1 = drive1.Name; + newConfig.sn = sn; + newConfig.ip = ipAddr; await File.WriteAllTextAsync("Config.json", JsonConvert.SerializeObject(newConfig, Formatting.Indented)); } @@ -370,6 +414,7 @@ namespace Topuino_Client_Windows { usbConnector?.Dispose(); onlineConnector?.Dispose(); + localConnector?.Dispose(); } private void Button_Save_Click(object sender, RoutedEventArgs e) diff --git a/pack.iss b/pack.iss index f5ebe73..6288a9f 100644 --- a/pack.iss +++ b/pack.iss @@ -2,7 +2,7 @@ ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "TopuinoClient" -#define MyAppVersion "0.2" +#define MyAppVersion "0.3" #define MyAppPublisher "VVZERO" #define MyAppURL "https://iot.vvzero.com" #define MyAppExeName "Topuino_Client_Windows.exe"