From aee9f67e702f6467aa1948777251fa1bf85353ec Mon Sep 17 00:00:00 2001 From: zs <zhousong@weben-smart.com> Date: 周四, 21 11月 2024 17:19:55 +0800 Subject: [PATCH] 修改图片 --- DataCapture_MA/Form1.cs | 154 ++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 132 insertions(+), 22 deletions(-) diff --git a/DataCapture_MA/Form1.cs b/DataCapture_MA/Form1.cs index c737007..f6a8d4a 100644 --- a/DataCapture_MA/Form1.cs +++ b/DataCapture_MA/Form1.cs @@ -15,11 +15,15 @@ using DataCapture_MA.Entity; using DataCapture_MA.DataHnadle; using DataCapture_MA.AutoMapperConfig; +using System.IO; +using IWshRuntimeLibrary; +using Microsoft.Win32; namespace DataCapture_MA { public partial class CaptrueForm : Form { + private CancellationTokenSource _cts = new CancellationTokenSource(); bool RunningFlag = false; Socket skt = WZ.Useful.Commons.NetworkUtil.CreateUdpSocket(); EndPoint RemotePoint = null; @@ -27,22 +31,116 @@ public CaptrueForm() { InitializeComponent(); + var hostName = Dns.GetHostName(); var ips = Dns.GetHostAddresses(hostName); + string targetSubnet = "10.133.13"; // 鐩爣缃戞 + + bool foundTargetSubnet = false; + foreach (var ip in ips) { if (ip.AddressFamily == AddressFamily.InterNetwork) // 鍙樉绀篒Pv4鍦板潃 { ipSelect.Items.Add(ip); + // 妫�鏌ユ槸鍚﹀寘鍚洰鏍囩綉娈� + if (ip.ToString().StartsWith(targetSubnet)) + { + ipSelect.SelectedItem = ip; // 閫変腑鍖归厤鐨� IP + foundTargetSubnet = true; + } } } - ipSelect.SelectedIndex = 0; - to_ipTxt.Text = ipSelect.SelectedItem.ToString(); - AutoMapper.Mapper.Initialize((cfg) => { + + if (!foundTargetSubnet && ipSelect.Items.Count > 0) + { + //MessageBox.Show($"鏈壘鍒扮綉娈典负 {targetSubnet} 鐨処P鍦板潃锛屽皢榛樿閫変腑绗竴涓狪P銆�", "鎻愮ず", MessageBoxButtons.OK, MessageBoxIcon.Information); + ipSelect.SelectedIndex = 0; + } + to_ipTxt.Text = ipSelect.SelectedItem?.ToString(); + + // 鍒濆鍖� AutoMapper + AutoMapper.Mapper.Initialize(cfg => + { cfg.AddProfile<MapperConfiguration>(); }); + + ////鍒涘缓蹇嵎鏂瑰紡 + //string shortcutName = "MaDataCapture"; // 蹇嵎鏂瑰紡鍚嶇О + //string targetPath = Application.ExecutablePath; // 绋嬪簭璺緞 + //string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // 鍚姩鏂囦欢澶硅矾寰� + //CreateShortcut(shortcutName, targetPath, startupPath); // 鍒涘缓蹇嵎鏂瑰紡 + + SetRegistryAutoStart("MaDataCapture", Application.ExecutablePath); // 娉ㄥ唽琛ㄦ柟寮忓疄鐜拌嚜鍚姩 + + StartReceive(); // 鍚姩绋嬪簭 + CreateDatabase(); // 鍒濆鍖栨暟鎹簱 + + Log4NetHelper.WriteErrorLog(LogType.Receive, "绋嬪簭鍚姩:" + DateTime.Now); + // 鍒犻櫎涓�涓湀鍓嶇殑鏁版嵁 + new Thread(AutoDeleteHistoryDate.Handler).Start(); + + this.FormClosing += CaptrueForm_FormClosing; + this.FormClosed += CaptrueForm_FormClosed; + } + /// <summary> + /// 璁剧疆娉ㄥ唽琛ㄥ疄鐜拌嚜鍚姩 + /// </summary> + /// <param name="appName">搴旂敤绋嬪簭鍚嶇О</param> + /// <param name="appPath">搴旂敤绋嬪簭璺緞</param> + private void SetRegistryAutoStart(string appName, string appPath) + { + try + { + // 鎵撳紑娉ㄥ唽琛ㄨ矾寰� + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + + if (key == null) + { + //throw new Exception("鏃犳硶璁块棶娉ㄥ唽琛ㄨ矾寰勶紒"); + Log4NetHelper.WriteErrorLog(LogType.Receive, "鏃犳硶璁块棶娉ㄥ唽琛ㄨ矾寰�:" + DateTime.Now); + } + + // 璁剧疆娉ㄥ唽琛ㄩ敭鍊� + key.SetValue(appName, appPath); + + key.Close(); + } + catch (Exception ex) + { + MessageBox.Show($"璁剧疆寮�鏈鸿嚜鍚姩澶辫触锛歿ex.Message}锛岃杩涜鎵嬪姩鍚姩", "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void CaptrueForm_FormClosing(object sender, FormClosingEventArgs e) + { + Log4NetHelper.WriteErrorLog(LogType.Receive, "绋嬪簭鍏抽棴:" + DateTime.Now); + // 閫氱煡鍚庡彴浠诲姟鍙栨秷 + _cts.Cancel(); + + // 纭繚浠诲姟瀹屾垚 + Task.WaitAll(Task.WhenAll()); } + private void CaptrueForm_FormClosed(object sender, FormClosedEventArgs e) + { + Log4NetHelper.WriteErrorLog(LogType.Receive, "绋嬪簭閫�鍑�:" + DateTime.Now); + // 纭繚绋嬪簭閫�鍑� + Environment.Exit(0); + } + + public static void CreateShortcut(string shortcutName, string targetPath, string startupPath) + { + string shortcutPath = Path.Combine(startupPath, shortcutName + ".lnk"); + if (System.IO.File.Exists(shortcutPath)) + return; + + WshShell shell = new WshShell(); + IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); + shortcut.TargetPath = targetPath; + shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); + shortcut.Save(); + } + private void ReceiveHandle() { //鎺ユ敹鏁版嵁澶勭悊绾跨▼ @@ -76,6 +174,10 @@ } private void start_connect_Click(object sender, EventArgs e) + { + StartReceive(); + } + public void StartReceive() { try { @@ -111,7 +213,7 @@ Log4NetHelper.WriteInfoLog(LogType.Receive, "寮�鍚疷DP閫氫俊鎴愬姛" + iPEndPoint.ToString()); - ipSelect.Enabled = false; + //ipSelect.Enabled = false; local_port.Enabled = false; to_ipTxt.Enabled = false; to_portTxt.Enabled = false; @@ -121,7 +223,7 @@ colse_connect.Enabled = true; r_showBox.Text += "鏁版嵁鎺ユ敹宸插惎鍔╘n"; } - catch(Exception ex) + catch (Exception ex) { Log4NetHelper.WriteInfoLog(LogType.Receive, "寮�鍚疷DP閫氫俊澶辫触" + ex); MessageBox.Show(ex.Message); @@ -130,27 +232,35 @@ private void CreateDatabase() { - using (var context = new DbModel()) + try { - // 纭繚鏁版嵁搴撳拰琛ㄥ瓨鍦紝濡傛灉涓嶅瓨鍦ㄥ垯鍒涘缓 - context.Database.EnsureCreated(); - - // 娣诲姞涓�浜涙暟鎹� - context.RobotInfo.Add(new RobotInfo + using (var context = new DbModel()) { - WarnningNum = 0, - //RobotStatus = "Normal", - RobotModel = "Model X", - RobotSerialNo = "123456", - WarnningContent = "娓呴櫎瀵屾枃鏈鏁版嵁", - CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), - IsDelete = false - }); + // 纭繚鏁版嵁搴撳拰琛ㄥ瓨鍦紝濡傛灉涓嶅瓨鍦ㄥ垯鍒涘缓 + context.Database.EnsureCreated(); - // 淇濆瓨鏇存敼 - context.SaveChanges(); + // 娣诲姞涓�浜涙暟鎹� + context.RobotInfo.Add(new RobotInfo + { + WarnningNum = 0, + //RobotStatus = "Normal", + RobotModel = "Model X", + RobotSerialNo = "123456", + WarnningContent = "娓呴櫎瀵屾枃鏈鏁版嵁", + CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + CreateStamp = DateTimeOffset.Now.ToUnixTimeSeconds(), + IsDelete = false + }); - var xx = context.RobotInfo.ToList(); + // 淇濆瓨鏇存敼 + context.SaveChanges(); + + var xx = context.RobotInfo.ToList(); + } + } + catch (Exception ex) + { + r_showBox.Text = "鏁版嵁搴撳垵濮嬪寲寮傚父"+ ex.Message; } } -- Gitblit v1.9.3