zs
2024-11-21 9f5cd73648a4dd6ad80d5c8e1c14746a8a27b1b6
DataCapture_MA/Form1.cs
@@ -15,6 +15,8 @@
using DataCapture_MA.Entity;
using DataCapture_MA.DataHnadle;
using DataCapture_MA.AutoMapperConfig;
using System.IO;
using IWshRuntimeLibrary;
namespace DataCapture_MA
{
@@ -27,20 +29,65 @@
        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) // 只显示IPv4地址
                {
                    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} 的IP地址,将默认选中第一个IP。", "提示", 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); // 创建快捷方式
            StartReceive(); // 启动程序
            CreateDatabase();  // 初始化数据库
            // 删除一个月前的数据
            new Thread(AutoDeleteHistoryDate.Handler).Start();
        }
        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()
@@ -77,6 +124,10 @@
        private void start_connect_Click(object sender, EventArgs e)
        {
            StartReceive();
        }
        public void StartReceive()
        {
            try
            {
                if (skt != null)
@@ -111,7 +162,7 @@
                Log4NetHelper.WriteInfoLog(LogType.Receive, "开启UDP通信成功" + iPEndPoint.ToString());
                ipSelect.Enabled = false;
                //ipSelect.Enabled = false;
                local_port.Enabled = false;
                to_ipTxt.Enabled = false;
                to_portTxt.Enabled = false;
@@ -121,7 +172,7 @@
                colse_connect.Enabled = true;
                r_showBox.Text += "数据接收已启动\n";
            }
            catch(Exception ex)
            catch (Exception ex)
            {
                Log4NetHelper.WriteInfoLog(LogType.Receive, "开启UDP通信失败" + ex);
                MessageBox.Show(ex.Message);
@@ -130,27 +181,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;
            }
        }