Browse Source

设备状态采集

WIN-GH9CEESPLTB\Administrator 4 months ago
parent
commit
f846633c29

+ 6 - 1
MainForm/ClassFile/ProjectClass/GlobalContext.cs

@@ -25,13 +25,18 @@ namespace MainForm
         public static string MainDir = @"D:\MESFile\";
         public static string WorkLogDir = MainDir + @"WorkLog\";  // 运行日志
         public static string PlcLogDir = MainDir + @"PLCLog\";    // plc写入日志
-        public static string MqttLogDir = MainDir + @"MqttLog\";  // mqtt日志
         public static string ProcessDataDir = MainDir + @"ProcessData\";    // 加工数据日志
         public static string OneCheckDataDir = MainDir + @"OneCheckData\";  // 点检数据日志
         public static string SQLLogDir = MainDir + @"SQLLog\";    // sql日志
         public static string DBDir = @"D:\LocalDataBase\";
         public static string MESConfig = MainDir + @"MESConfig.ini";    // MES接口的配置文件
 
+        public static string MqttLogDir = MainDir + @"MqttLog\";  // mqtt日志
+        public static string MqttCallbackDir = MqttLogDir + @"Callback\";     // mqtt日志 - Callback内容
+        public static string MqttDeviceStateDir = MqttLogDir + @"设备状态\";  // mqtt日志 - 设备状态日志
+        public static string MqttInputBeginDir = MqttLogDir + @"节拍日志";    // mqtt日志 - 节拍日志
+        public static string MqttOperateLogDir = MqttLogDir + @"用户操作";    // mqtt日志 - 用户操作日志
+
         public static string MqttServerPath = AppDomain.CurrentDomain.BaseDirectory + @"\ThingsMode\MqttServer.exe";
         public static string MqttServerName = "MqttServer";
 

+ 11 - 4
MainForm/ClassFile/XiaomiAPI_IOT/Extend/XiaomiMqttClient_DeviceStateData.cs

@@ -1,4 +1,5 @@
 using Newtonsoft.Json;
+using System;
 
 namespace MainForm.ClassFile.XiaomiAPI
 {
@@ -23,20 +24,26 @@ namespace MainForm.ClassFile.XiaomiAPI
         /// <param name="msg">事件数据;Json</param>
         /// <param name="dataId">自定义事件Id;如:guid</param>
         /// <returns></returns>
-        public static int Write_DeviceStateData(DeviceStateDataRequest request, string dataId = "")
+        public static (int, string) Write_DeviceStateData(DeviceStateDataRequest request, string deviceCode = "")
         {
             int result = 0;
+            string resultStr = "";
 
             string msg = JsonConvert.SerializeObject(request);
             byte[] bytes = ToUTF8(msg);
 
             // 发送
-            if (string.IsNullOrEmpty(dataId))
+            if (string.IsNullOrEmpty(deviceCode))
                 result = Write(DeviceStateDataId, bytes);
             else
-                result = WriteWithDataId(DeviceStateDataId, bytes, dataId);
+                result = WriteWithDeviceCode(DeviceStateDataId, bytes, deviceCode);
 
-            return result;
+            var try1 = Enum.TryParse(result.ToString(), out XiaomiMqttResponse_ErrCode errCode);
+            if (try1)
+                resultStr = errCode.ToString();
+            else
+                resultStr = XiaomiMqttResponse_ErrCode.ERR_UNKOWN.ToString();
+            return (result, resultStr);
         }
 
         /// <summary>

+ 24 - 0
MainForm/ClassFile/XiaomiAPI_IOT/XiaomiMqttClient.cs

@@ -218,6 +218,30 @@ namespace MainForm.ClassFile.XiaomiAPI
         // 配合使用ToUTF8,发送带中文的value
         [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
         public static extern int WriteWithDataId(string id, byte[] msg, string dataId);
+
+        /// <summary>
+        /// 发送数据-With DeviceCode
+        /// 英文
+        /// </summary>
+        /// <param name="id">事件标识;由固定前缀、分类层级(对应层级的英⽂名称)、事件ID/属性ID拼接⽽成</param>
+        /// <param name="value">事件数据</param>
+        /// <param name="deviceCode">设备编号;如:</param>
+        /// <returns></returns>
+        [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDeviceCode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int WriteWithDeviceCode(string id, string msg, string deviceCode);
+
+        /// <summary>
+        /// 发送数据-With DeviceCode
+        /// 中文
+        /// </summary>
+        /// <param name="id">事件标识;由固定前缀、分类层级(对应层级的英⽂名称)、事件ID/属性ID拼接⽽成</param>
+        /// <param name="value">事件数据</param>
+        /// <param name="deviceCode">设备编号;如:</param>
+        /// <returns></returns>
+        // 配合使用ToUTF8,发送带中文的value
+        [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDeviceCode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int WriteWithDeviceCode(string id, byte[] msg, string deviceCode);
+
         #endregion 4、发送数据
 
         #region 其他方法

+ 110 - 133
MainForm/FaForm/Form_Home.Designer.cs

@@ -77,6 +77,8 @@
             this.colTime = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.colMessage = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.imageListState = new System.Windows.Forms.ImageList(this.components);
+            this.uiLabel2 = new Sunny.UI.UILabel();
+            this.lblDeviceStates = new Sunny.UI.UILabel();
             this.panel1.SuspendLayout();
             this.groupBox3.SuspendLayout();
             this.panel2.SuspendLayout();
@@ -115,14 +117,15 @@
             this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
             this.panel1.Controls.Add(this.groupBox3);
-            this.panel1.Location = new System.Drawing.Point(9, 10);
-            this.panel1.Margin = new System.Windows.Forms.Padding(2);
+            this.panel1.Location = new System.Drawing.Point(14, 15);
             this.panel1.Name = "panel1";
-            this.panel1.Size = new System.Drawing.Size(1026, 122);
+            this.panel1.Size = new System.Drawing.Size(1539, 183);
             this.panel1.TabIndex = 0;
             // 
             // groupBox3
             // 
+            this.groupBox3.Controls.Add(this.lblDeviceStates);
+            this.groupBox3.Controls.Add(this.uiLabel2);
             this.groupBox3.Controls.Add(this.txt_CurSupplierCode);
             this.groupBox3.Controls.Add(this.label20);
             this.groupBox3.Controls.Add(this.currentMtltmrk);
@@ -134,10 +137,8 @@
             this.groupBox3.Dock = System.Windows.Forms.DockStyle.Fill;
             this.groupBox3.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.groupBox3.Location = new System.Drawing.Point(0, 0);
-            this.groupBox3.Margin = new System.Windows.Forms.Padding(2);
             this.groupBox3.Name = "groupBox3";
-            this.groupBox3.Padding = new System.Windows.Forms.Padding(2);
-            this.groupBox3.Size = new System.Drawing.Size(1026, 122);
+            this.groupBox3.Size = new System.Drawing.Size(1539, 183);
             this.groupBox3.TabIndex = 2;
             this.groupBox3.TabStop = false;
             this.groupBox3.Text = "当前加工订单信息";
@@ -145,20 +146,18 @@
             // txt_CurSupplierCode
             // 
             this.txt_CurSupplierCode.Enabled = false;
-            this.txt_CurSupplierCode.Location = new System.Drawing.Point(433, 80);
-            this.txt_CurSupplierCode.Margin = new System.Windows.Forms.Padding(2);
+            this.txt_CurSupplierCode.Location = new System.Drawing.Point(650, 120);
             this.txt_CurSupplierCode.Name = "txt_CurSupplierCode";
-            this.txt_CurSupplierCode.Size = new System.Drawing.Size(176, 27);
+            this.txt_CurSupplierCode.Size = new System.Drawing.Size(262, 36);
             this.txt_CurSupplierCode.TabIndex = 95;
             this.txt_CurSupplierCode.Visible = false;
             // 
             // label20
             // 
             this.label20.AutoSize = true;
-            this.label20.Location = new System.Drawing.Point(338, 83);
-            this.label20.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label20.Location = new System.Drawing.Point(507, 124);
             this.label20.Name = "label20";
-            this.label20.Size = new System.Drawing.Size(88, 20);
+            this.label20.Size = new System.Drawing.Size(128, 30);
             this.label20.TabIndex = 94;
             this.label20.Text = "供应商代码:";
             this.label20.Visible = false;
@@ -166,48 +165,43 @@
             // currentMtltmrk
             // 
             this.currentMtltmrk.Enabled = false;
-            this.currentMtltmrk.Location = new System.Drawing.Point(433, 39);
-            this.currentMtltmrk.Margin = new System.Windows.Forms.Padding(2);
+            this.currentMtltmrk.Location = new System.Drawing.Point(650, 58);
             this.currentMtltmrk.Name = "currentMtltmrk";
-            this.currentMtltmrk.Size = new System.Drawing.Size(176, 27);
+            this.currentMtltmrk.Size = new System.Drawing.Size(262, 36);
             this.currentMtltmrk.TabIndex = 10;
             // 
             // label3
             // 
             this.label3.AutoSize = true;
-            this.label3.Location = new System.Drawing.Point(353, 42);
-            this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label3.Location = new System.Drawing.Point(530, 63);
             this.label3.Name = "label3";
-            this.label3.Size = new System.Drawing.Size(73, 20);
+            this.label3.Size = new System.Drawing.Size(106, 30);
             this.label3.TabIndex = 7;
             this.label3.Text = "产品型号:";
             // 
             // currentBN
             // 
             this.currentBN.Enabled = false;
-            this.currentBN.Location = new System.Drawing.Point(119, 80);
-            this.currentBN.Margin = new System.Windows.Forms.Padding(2);
+            this.currentBN.Location = new System.Drawing.Point(178, 120);
             this.currentBN.Name = "currentBN";
-            this.currentBN.Size = new System.Drawing.Size(176, 27);
+            this.currentBN.Size = new System.Drawing.Size(262, 36);
             this.currentBN.TabIndex = 3;
             this.currentBN.Visible = false;
             // 
             // currentWC
             // 
             this.currentWC.Enabled = false;
-            this.currentWC.Location = new System.Drawing.Point(119, 39);
-            this.currentWC.Margin = new System.Windows.Forms.Padding(2);
+            this.currentWC.Location = new System.Drawing.Point(178, 58);
             this.currentWC.Name = "currentWC";
-            this.currentWC.Size = new System.Drawing.Size(176, 27);
+            this.currentWC.Size = new System.Drawing.Size(262, 36);
             this.currentWC.TabIndex = 2;
             // 
             // label2
             // 
             this.label2.AutoSize = true;
-            this.label2.Location = new System.Drawing.Point(46, 83);
-            this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label2.Location = new System.Drawing.Point(69, 124);
             this.label2.Name = "label2";
-            this.label2.Size = new System.Drawing.Size(69, 20);
+            this.label2.Size = new System.Drawing.Size(101, 30);
             this.label2.TabIndex = 1;
             this.label2.Text = "批次号:";
             this.label2.Visible = false;
@@ -215,10 +209,9 @@
             // label1
             // 
             this.label1.AutoSize = true;
-            this.label1.Location = new System.Drawing.Point(19, 42);
-            this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label1.Location = new System.Drawing.Point(28, 63);
             this.label1.Name = "label1";
-            this.label1.Size = new System.Drawing.Size(88, 20);
+            this.label1.Size = new System.Drawing.Size(128, 30);
             this.label1.TabIndex = 0;
             this.label1.Text = "车间订单号:";
             // 
@@ -227,10 +220,9 @@
             this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
             this.panel2.Controls.Add(this.groupBox2);
-            this.panel2.Location = new System.Drawing.Point(9, 455);
-            this.panel2.Margin = new System.Windows.Forms.Padding(2);
+            this.panel2.Location = new System.Drawing.Point(14, 682);
             this.panel2.Name = "panel2";
-            this.panel2.Size = new System.Drawing.Size(1026, 100);
+            this.panel2.Size = new System.Drawing.Size(1539, 150);
             this.panel2.TabIndex = 1;
             // 
             // groupBox2
@@ -248,11 +240,9 @@
             this.groupBox2.Controls.Add(this.groupBox13);
             this.groupBox2.Controls.Add(this.groupBox11);
             this.groupBox2.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.groupBox2.Location = new System.Drawing.Point(2, 2);
-            this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox2.Location = new System.Drawing.Point(3, 3);
             this.groupBox2.Name = "groupBox2";
-            this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
-            this.groupBox2.Size = new System.Drawing.Size(902, 94);
+            this.groupBox2.Size = new System.Drawing.Size(1353, 141);
             this.groupBox2.TabIndex = 1;
             this.groupBox2.TabStop = false;
             this.groupBox2.Text = "设备状态";
@@ -261,12 +251,10 @@
             // 
             this.groupBox15.Controls.Add(this.picAgvMqtt);
             this.groupBox15.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox15.Location = new System.Drawing.Point(245, 24);
-            this.groupBox15.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox15.Location = new System.Drawing.Point(368, 36);
             this.groupBox15.Name = "groupBox15";
-            this.groupBox15.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox15.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox15.Size = new System.Drawing.Size(90, 60);
+            this.groupBox15.Size = new System.Drawing.Size(135, 90);
             this.groupBox15.TabIndex = 14;
             this.groupBox15.TabStop = false;
             this.groupBox15.Text = "AGV Mqtt";
@@ -276,10 +264,9 @@
             this.picAgvMqtt.ErrorImage = ((System.Drawing.Image)(resources.GetObject("picAgvMqtt.ErrorImage")));
             this.picAgvMqtt.Image = ((System.Drawing.Image)(resources.GetObject("picAgvMqtt.Image")));
             this.picAgvMqtt.InitialImage = ((System.Drawing.Image)(resources.GetObject("picAgvMqtt.InitialImage")));
-            this.picAgvMqtt.Location = new System.Drawing.Point(32, 24);
-            this.picAgvMqtt.Margin = new System.Windows.Forms.Padding(2);
+            this.picAgvMqtt.Location = new System.Drawing.Point(48, 36);
             this.picAgvMqtt.Name = "picAgvMqtt";
-            this.picAgvMqtt.Size = new System.Drawing.Size(26, 28);
+            this.picAgvMqtt.Size = new System.Drawing.Size(39, 42);
             this.picAgvMqtt.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.picAgvMqtt.TabIndex = 10;
             this.picAgvMqtt.TabStop = false;
@@ -288,12 +275,10 @@
             // 
             this.groupBox14.Controls.Add(this.picAgvHttp);
             this.groupBox14.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox14.Location = new System.Drawing.Point(156, 24);
-            this.groupBox14.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox14.Location = new System.Drawing.Point(234, 36);
             this.groupBox14.Name = "groupBox14";
-            this.groupBox14.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox14.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox14.Size = new System.Drawing.Size(85, 60);
+            this.groupBox14.Size = new System.Drawing.Size(128, 90);
             this.groupBox14.TabIndex = 13;
             this.groupBox14.TabStop = false;
             this.groupBox14.Text = "AGV Http";
@@ -303,10 +288,9 @@
             this.picAgvHttp.ErrorImage = ((System.Drawing.Image)(resources.GetObject("picAgvHttp.ErrorImage")));
             this.picAgvHttp.Image = ((System.Drawing.Image)(resources.GetObject("picAgvHttp.Image")));
             this.picAgvHttp.InitialImage = ((System.Drawing.Image)(resources.GetObject("picAgvHttp.InitialImage")));
-            this.picAgvHttp.Location = new System.Drawing.Point(28, 24);
-            this.picAgvHttp.Margin = new System.Windows.Forms.Padding(2);
+            this.picAgvHttp.Location = new System.Drawing.Point(42, 36);
             this.picAgvHttp.Name = "picAgvHttp";
-            this.picAgvHttp.Size = new System.Drawing.Size(26, 28);
+            this.picAgvHttp.Size = new System.Drawing.Size(39, 42);
             this.picAgvHttp.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.picAgvHttp.TabIndex = 10;
             this.picAgvHttp.TabStop = false;
@@ -315,12 +299,10 @@
             // 
             this.groupBox1.Controls.Add(this.picIot);
             this.groupBox1.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox1.Location = new System.Drawing.Point(88, 24);
-            this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox1.Location = new System.Drawing.Point(132, 36);
             this.groupBox1.Name = "groupBox1";
-            this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox1.Size = new System.Drawing.Size(64, 60);
+            this.groupBox1.Size = new System.Drawing.Size(96, 90);
             this.groupBox1.TabIndex = 12;
             this.groupBox1.TabStop = false;
             this.groupBox1.Text = "IOT";
@@ -330,10 +312,9 @@
             this.picIot.ErrorImage = ((System.Drawing.Image)(resources.GetObject("picIot.ErrorImage")));
             this.picIot.Image = ((System.Drawing.Image)(resources.GetObject("picIot.Image")));
             this.picIot.InitialImage = ((System.Drawing.Image)(resources.GetObject("picIot.InitialImage")));
-            this.picIot.Location = new System.Drawing.Point(18, 24);
-            this.picIot.Margin = new System.Windows.Forms.Padding(2);
+            this.picIot.Location = new System.Drawing.Point(27, 36);
             this.picIot.Name = "picIot";
-            this.picIot.Size = new System.Drawing.Size(26, 28);
+            this.picIot.Size = new System.Drawing.Size(39, 42);
             this.picIot.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.picIot.TabIndex = 10;
             this.picIot.TabStop = false;
@@ -342,12 +323,10 @@
             // 
             this.groupBox5.Controls.Add(this.picMESStatus);
             this.groupBox5.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox5.Location = new System.Drawing.Point(20, 24);
-            this.groupBox5.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox5.Location = new System.Drawing.Point(30, 36);
             this.groupBox5.Name = "groupBox5";
-            this.groupBox5.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox5.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox5.Size = new System.Drawing.Size(64, 60);
+            this.groupBox5.Size = new System.Drawing.Size(96, 90);
             this.groupBox5.TabIndex = 0;
             this.groupBox5.TabStop = false;
             this.groupBox5.Text = "MES";
@@ -357,10 +336,9 @@
             this.picMESStatus.ErrorImage = ((System.Drawing.Image)(resources.GetObject("picMESStatus.ErrorImage")));
             this.picMESStatus.Image = ((System.Drawing.Image)(resources.GetObject("picMESStatus.Image")));
             this.picMESStatus.InitialImage = ((System.Drawing.Image)(resources.GetObject("picMESStatus.InitialImage")));
-            this.picMESStatus.Location = new System.Drawing.Point(18, 24);
-            this.picMESStatus.Margin = new System.Windows.Forms.Padding(2);
+            this.picMESStatus.Location = new System.Drawing.Point(27, 36);
             this.picMESStatus.Name = "picMESStatus";
-            this.picMESStatus.Size = new System.Drawing.Size(26, 28);
+            this.picMESStatus.Size = new System.Drawing.Size(39, 42);
             this.picMESStatus.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.picMESStatus.TabIndex = 10;
             this.picMESStatus.TabStop = false;
@@ -369,12 +347,10 @@
             // 
             this.groupBox7.Controls.Add(this.pictureBox6);
             this.groupBox7.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox7.Location = new System.Drawing.Point(680, 24);
-            this.groupBox7.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox7.Location = new System.Drawing.Point(1020, 36);
             this.groupBox7.Name = "groupBox7";
-            this.groupBox7.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox7.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox7.Size = new System.Drawing.Size(64, 60);
+            this.groupBox7.Size = new System.Drawing.Size(96, 90);
             this.groupBox7.TabIndex = 1;
             this.groupBox7.TabStop = false;
             this.groupBox7.Text = "PLC6";
@@ -385,10 +361,9 @@
             this.pictureBox6.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox6.ErrorImage")));
             this.pictureBox6.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox6.Image")));
             this.pictureBox6.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox6.InitialImage")));
-            this.pictureBox6.Location = new System.Drawing.Point(18, 24);
-            this.pictureBox6.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox6.Location = new System.Drawing.Point(27, 36);
             this.pictureBox6.Name = "pictureBox6";
-            this.pictureBox6.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox6.Size = new System.Drawing.Size(39, 42);
             this.pictureBox6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox6.TabIndex = 16;
             this.pictureBox6.TabStop = false;
@@ -397,12 +372,10 @@
             // 
             this.groupBox12.Controls.Add(this.pictureBox8);
             this.groupBox12.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox12.Location = new System.Drawing.Point(816, 24);
-            this.groupBox12.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox12.Location = new System.Drawing.Point(1224, 36);
             this.groupBox12.Name = "groupBox12";
-            this.groupBox12.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox12.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox12.Size = new System.Drawing.Size(64, 60);
+            this.groupBox12.Size = new System.Drawing.Size(96, 90);
             this.groupBox12.TabIndex = 1;
             this.groupBox12.TabStop = false;
             this.groupBox12.Text = "PLC8";
@@ -413,10 +386,9 @@
             this.pictureBox8.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox8.ErrorImage")));
             this.pictureBox8.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox8.Image")));
             this.pictureBox8.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox8.InitialImage")));
-            this.pictureBox8.Location = new System.Drawing.Point(19, 24);
-            this.pictureBox8.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox8.Location = new System.Drawing.Point(28, 36);
             this.pictureBox8.Name = "pictureBox8";
-            this.pictureBox8.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox8.Size = new System.Drawing.Size(39, 42);
             this.pictureBox8.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox8.TabIndex = 18;
             this.pictureBox8.TabStop = false;
@@ -425,12 +397,10 @@
             // 
             this.groupBox9.Controls.Add(this.pictureBox7);
             this.groupBox9.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox9.Location = new System.Drawing.Point(748, 24);
-            this.groupBox9.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox9.Location = new System.Drawing.Point(1122, 36);
             this.groupBox9.Name = "groupBox9";
-            this.groupBox9.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox9.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox9.Size = new System.Drawing.Size(64, 60);
+            this.groupBox9.Size = new System.Drawing.Size(96, 90);
             this.groupBox9.TabIndex = 1;
             this.groupBox9.TabStop = false;
             this.groupBox9.Text = "PLC7";
@@ -441,10 +411,9 @@
             this.pictureBox7.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox7.ErrorImage")));
             this.pictureBox7.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox7.Image")));
             this.pictureBox7.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox7.InitialImage")));
-            this.pictureBox7.Location = new System.Drawing.Point(20, 24);
-            this.pictureBox7.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox7.Location = new System.Drawing.Point(30, 36);
             this.pictureBox7.Name = "pictureBox7";
-            this.pictureBox7.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox7.Size = new System.Drawing.Size(39, 42);
             this.pictureBox7.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox7.TabIndex = 17;
             this.pictureBox7.TabStop = false;
@@ -453,12 +422,10 @@
             // 
             this.groupBox8.Controls.Add(this.picPLC);
             this.groupBox8.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox8.Location = new System.Drawing.Point(339, 24);
-            this.groupBox8.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox8.Location = new System.Drawing.Point(508, 36);
             this.groupBox8.Name = "groupBox8";
-            this.groupBox8.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox8.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox8.Size = new System.Drawing.Size(64, 60);
+            this.groupBox8.Size = new System.Drawing.Size(96, 90);
             this.groupBox8.TabIndex = 1;
             this.groupBox8.TabStop = false;
             this.groupBox8.Text = "PLC";
@@ -468,10 +435,9 @@
             this.picPLC.ErrorImage = ((System.Drawing.Image)(resources.GetObject("picPLC.ErrorImage")));
             this.picPLC.Image = ((System.Drawing.Image)(resources.GetObject("picPLC.Image")));
             this.picPLC.InitialImage = ((System.Drawing.Image)(resources.GetObject("picPLC.InitialImage")));
-            this.picPLC.Location = new System.Drawing.Point(20, 24);
-            this.picPLC.Margin = new System.Windows.Forms.Padding(2);
+            this.picPLC.Location = new System.Drawing.Point(30, 36);
             this.picPLC.Name = "picPLC";
-            this.picPLC.Size = new System.Drawing.Size(26, 28);
+            this.picPLC.Size = new System.Drawing.Size(39, 42);
             this.picPLC.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.picPLC.TabIndex = 0;
             this.picPLC.TabStop = false;
@@ -480,12 +446,10 @@
             // 
             this.groupBox6.Controls.Add(this.pictureBox2);
             this.groupBox6.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox6.Location = new System.Drawing.Point(407, 24);
-            this.groupBox6.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox6.Location = new System.Drawing.Point(610, 36);
             this.groupBox6.Name = "groupBox6";
-            this.groupBox6.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox6.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox6.Size = new System.Drawing.Size(64, 60);
+            this.groupBox6.Size = new System.Drawing.Size(96, 90);
             this.groupBox6.TabIndex = 11;
             this.groupBox6.TabStop = false;
             this.groupBox6.Text = "PLC2";
@@ -496,10 +460,9 @@
             this.pictureBox2.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox2.ErrorImage")));
             this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
             this.pictureBox2.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox2.InitialImage")));
-            this.pictureBox2.Location = new System.Drawing.Point(20, 24);
-            this.pictureBox2.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox2.Location = new System.Drawing.Point(30, 36);
             this.pictureBox2.Name = "pictureBox2";
-            this.pictureBox2.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox2.Size = new System.Drawing.Size(39, 42);
             this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox2.TabIndex = 12;
             this.pictureBox2.TabStop = false;
@@ -508,12 +471,10 @@
             // 
             this.groupBox10.Controls.Add(this.pictureBox3);
             this.groupBox10.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox10.Location = new System.Drawing.Point(475, 24);
-            this.groupBox10.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox10.Location = new System.Drawing.Point(712, 36);
             this.groupBox10.Name = "groupBox10";
-            this.groupBox10.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox10.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox10.Size = new System.Drawing.Size(64, 60);
+            this.groupBox10.Size = new System.Drawing.Size(96, 90);
             this.groupBox10.TabIndex = 1;
             this.groupBox10.TabStop = false;
             this.groupBox10.Text = "PLC3";
@@ -524,10 +485,9 @@
             this.pictureBox3.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox3.ErrorImage")));
             this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
             this.pictureBox3.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox3.InitialImage")));
-            this.pictureBox3.Location = new System.Drawing.Point(18, 24);
-            this.pictureBox3.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox3.Location = new System.Drawing.Point(27, 36);
             this.pictureBox3.Name = "pictureBox3";
-            this.pictureBox3.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox3.Size = new System.Drawing.Size(39, 42);
             this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox3.TabIndex = 13;
             this.pictureBox3.TabStop = false;
@@ -536,12 +496,10 @@
             // 
             this.groupBox13.Controls.Add(this.pictureBox5);
             this.groupBox13.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox13.Location = new System.Drawing.Point(611, 24);
-            this.groupBox13.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox13.Location = new System.Drawing.Point(916, 36);
             this.groupBox13.Name = "groupBox13";
-            this.groupBox13.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox13.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox13.Size = new System.Drawing.Size(64, 60);
+            this.groupBox13.Size = new System.Drawing.Size(96, 90);
             this.groupBox13.TabIndex = 1;
             this.groupBox13.TabStop = false;
             this.groupBox13.Text = "PLC5";
@@ -552,10 +510,9 @@
             this.pictureBox5.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox5.ErrorImage")));
             this.pictureBox5.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox5.Image")));
             this.pictureBox5.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox5.InitialImage")));
-            this.pictureBox5.Location = new System.Drawing.Point(20, 24);
-            this.pictureBox5.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox5.Location = new System.Drawing.Point(30, 36);
             this.pictureBox5.Name = "pictureBox5";
-            this.pictureBox5.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox5.Size = new System.Drawing.Size(39, 42);
             this.pictureBox5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox5.TabIndex = 15;
             this.pictureBox5.TabStop = false;
@@ -564,12 +521,10 @@
             // 
             this.groupBox11.Controls.Add(this.pictureBox4);
             this.groupBox11.Font = new System.Drawing.Font("微软雅黑", 9.6F);
-            this.groupBox11.Location = new System.Drawing.Point(543, 24);
-            this.groupBox11.Margin = new System.Windows.Forms.Padding(2);
+            this.groupBox11.Location = new System.Drawing.Point(814, 36);
             this.groupBox11.Name = "groupBox11";
-            this.groupBox11.Padding = new System.Windows.Forms.Padding(2);
             this.groupBox11.RightToLeft = System.Windows.Forms.RightToLeft.No;
-            this.groupBox11.Size = new System.Drawing.Size(64, 60);
+            this.groupBox11.Size = new System.Drawing.Size(96, 90);
             this.groupBox11.TabIndex = 1;
             this.groupBox11.TabStop = false;
             this.groupBox11.Text = "PLC4";
@@ -580,10 +535,9 @@
             this.pictureBox4.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox4.ErrorImage")));
             this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
             this.pictureBox4.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox4.InitialImage")));
-            this.pictureBox4.Location = new System.Drawing.Point(20, 24);
-            this.pictureBox4.Margin = new System.Windows.Forms.Padding(2);
+            this.pictureBox4.Location = new System.Drawing.Point(30, 36);
             this.pictureBox4.Name = "pictureBox4";
-            this.pictureBox4.Size = new System.Drawing.Size(26, 28);
+            this.pictureBox4.Size = new System.Drawing.Size(39, 42);
             this.pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
             this.pictureBox4.TabIndex = 14;
             this.pictureBox4.TabStop = false;
@@ -594,10 +548,9 @@
             | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
             this.panel3.Controls.Add(this.groupBox4);
-            this.panel3.Location = new System.Drawing.Point(9, 137);
-            this.panel3.Margin = new System.Windows.Forms.Padding(2);
+            this.panel3.Location = new System.Drawing.Point(14, 206);
             this.panel3.Name = "panel3";
-            this.panel3.Size = new System.Drawing.Size(1026, 313);
+            this.panel3.Size = new System.Drawing.Size(1539, 470);
             this.panel3.TabIndex = 2;
             // 
             // groupBox4
@@ -606,10 +559,8 @@
             this.groupBox4.Dock = System.Windows.Forms.DockStyle.Fill;
             this.groupBox4.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.groupBox4.Location = new System.Drawing.Point(0, 0);
-            this.groupBox4.Margin = new System.Windows.Forms.Padding(2);
             this.groupBox4.Name = "groupBox4";
-            this.groupBox4.Padding = new System.Windows.Forms.Padding(2);
-            this.groupBox4.Size = new System.Drawing.Size(1026, 313);
+            this.groupBox4.Size = new System.Drawing.Size(1539, 470);
             this.groupBox4.TabIndex = 1;
             this.groupBox4.TabStop = false;
             this.groupBox4.Text = "数据采集";
@@ -638,8 +589,7 @@
             this.systemLog.Dock = System.Windows.Forms.DockStyle.Fill;
             this.systemLog.EnableHeadersVisualStyles = false;
             this.systemLog.GridColor = System.Drawing.Color.Gainsboro;
-            this.systemLog.Location = new System.Drawing.Point(2, 22);
-            this.systemLog.Margin = new System.Windows.Forms.Padding(2);
+            this.systemLog.Location = new System.Drawing.Point(3, 32);
             this.systemLog.Name = "systemLog";
             this.systemLog.ReadOnly = true;
             dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
@@ -657,7 +607,7 @@
             this.systemLog.RowsDefaultCellStyle = dataGridViewCellStyle4;
             this.systemLog.RowTemplate.Height = 27;
             this.systemLog.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
-            this.systemLog.Size = new System.Drawing.Size(1022, 289);
+            this.systemLog.Size = new System.Drawing.Size(1533, 435);
             this.systemLog.TabIndex = 0;
             // 
             // colDate
@@ -697,16 +647,41 @@
             this.imageListState.Images.SetKeyName(1, "light_green.png");
             this.imageListState.Images.SetKeyName(2, "light_red.png");
             // 
+            // uiLabel2
+            // 
+            this.uiLabel2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+            this.uiLabel2.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.uiLabel2.ForeColor = System.Drawing.SystemColors.InfoText;
+            this.uiLabel2.Location = new System.Drawing.Point(1093, 48);
+            this.uiLabel2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+            this.uiLabel2.Name = "uiLabel2";
+            this.uiLabel2.Size = new System.Drawing.Size(177, 50);
+            this.uiLabel2.TabIndex = 96;
+            this.uiLabel2.Text = "设备状态:";
+            this.uiLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
+            // 
+            // lblDeviceStates
+            // 
+            this.lblDeviceStates.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+            this.lblDeviceStates.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.lblDeviceStates.ForeColor = System.Drawing.Color.Blue;
+            this.lblDeviceStates.Location = new System.Drawing.Point(1249, 48);
+            this.lblDeviceStates.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+            this.lblDeviceStates.Name = "lblDeviceStates";
+            this.lblDeviceStates.Size = new System.Drawing.Size(261, 50);
+            this.lblDeviceStates.TabIndex = 97;
+            this.lblDeviceStates.Text = "未初始状态";
+            this.lblDeviceStates.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
+            // 
             // Form_Home
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.BackColor = System.Drawing.Color.White;
-            this.ClientSize = new System.Drawing.Size(1044, 561);
+            this.ClientSize = new System.Drawing.Size(1566, 842);
             this.Controls.Add(this.panel3);
             this.Controls.Add(this.panel2);
             this.Controls.Add(this.panel1);
-            this.Margin = new System.Windows.Forms.Padding(2);
             this.Name = "Form_Home";
             this.Text = "Form_Home";
             this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form_Home_FormClosed);
@@ -792,5 +767,7 @@
         private System.Windows.Forms.PictureBox picAgvHttp;
         private System.Windows.Forms.GroupBox groupBox15;
         private System.Windows.Forms.PictureBox picAgvMqtt;
+        private Sunny.UI.UILabel lblDeviceStates;
+        private Sunny.UI.UILabel uiLabel2;
     }
 }

+ 217 - 158
MainForm/FaForm/Form_Home.cs

@@ -36,6 +36,7 @@ using Org.BouncyCastle.Asn1.IsisMtt;
 using System.Web.Services.Description;
 using System.Numerics;
 using MathNet.Numerics.RootFinding;
+using HslCommunication.Enthernet;
 
 /*
  * 注:本源码对外提供,所以有些地方使用中文命名方法及变量
@@ -117,6 +118,12 @@ namespace MainForm
 
 
         Dictionary<int, Inovance_EIP> FunsEip = new Dictionary<int, Inovance_EIP>();
+
+        /// <summary>
+        /// 单机用-设备状态
+        /// </summary>
+        XiaomiDeviceState xmDeviceState = XiaomiDeviceState.Uninitialized;
+
         #endregion 变量
 
         #region 窗体基础事件
@@ -638,6 +645,12 @@ namespace MainForm
             return deviceState == 0;  // 点检时该值不为0
         }
 
+        /// <summary>
+        /// 采集到的设备状态
+        /// </summary>
+        private string _DeviceStates = "未知状态";
+        private string _DeviceStates_Old = "未知状态";
+
         /// <summary>
         /// 获取设备报警数据与获取设备运行信息
         /// </summary>
@@ -651,175 +664,221 @@ namespace MainForm
             /// 上位机心跳
             /// 获取设备报警数据与状态信息
             string stationNameStr = "获取设备报警数据与状态信息";
+            // 已连接到PLC
             while (IsRun)
             {
                 try
                 {
-                    if (!GlobalContext._IsCon_plc1Alarm)
-                    {
-                        UpdatePLCMonitor(1, -2, 0);
-                        continue;
-                    }
-
+                    //if (!GlobalContext._IsCon_plc1Alarm)
+                    //{
+                    //    UpdatePLCMonitor(1, -2, 0);
+                    //    continue;
+                    //}
                     if (plc1Alarm.IsConnected)  // 检查PLC是否已连接上
                     {
-                        DateTime dtNow = DateTime.Now;
-                        #region 获取设备运行信息
-                        try
+                        #region 主页展示设备运行状态并上传到IOT中
+                        switch (xmDeviceState)
                         {
-                            LineWorkingData_ThisTime lineWorkingData1 = new LineWorkingData_ThisTime();
-                            lineWorkingData1.GUID = Guid.NewGuid().ToString();
-                            lineWorkingData1.LineName = GlobalContext.LineCode;
-
-                            // 
-                            lineWorkingData1.BootTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5500);     // 本次开机时间(整线)D5500 h
-                            lineWorkingData1.NormalTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5502);   // 本次开机运行时间(整线)D5502 h
-                            lineWorkingData1.StandbyTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5504);  // 本次开机待机时间(整线)D5504 h
-                            lineWorkingData1.FaultTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5506);    // 本次开机故障时间(整线)D5506 h
-                            lineWorkingData1.MaterialShortageTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5508);  // 本次开机缺料时间(整线)D5508 h
-                            lineWorkingData1.MaintenanceTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5510);       // 本次开机维修时间(整线)D5510 h
-                            lineWorkingData1.FaultNumber = plc1Alarm.ReadHoldingRegisters<short>(5514);      // 本次开机故障停机次数(整线)D5514
-                            lineWorkingData1.OutputNumber = plc1Alarm.ReadHoldingRegisters<short>(5700);     // 本次开机产量(整线) D5700
-                            lineWorkingData1.QualifiedNumber = plc1Alarm.ReadHoldingRegisters<short>(5704);  // 本次开机合格数量(整线) D5704
-                            lineWorkingData1.QualifiedRate = plc1Alarm.ReadHoldingRegisters<float>(5710);    // 本次开机合格率(整线) D5710
-                            lineWorkingData1.DesignRhythm = plc1Alarm.ReadHoldingRegisters<float>(5714);     // 设计节拍(整线) D5714
-                            lineWorkingData1.RealityRhythm = plc1Alarm.ReadHoldingRegisters<float>(5716);    // 本次开机实际节拍(整线) D5716
-                            lineWorkingData1.CreateTime = DateTime.Now;
-
-                            string lineWorkingData1_Str = JsonConvert.SerializeObject(lineWorkingData1);
-
-                            // UI展示-展示到设备状态页
-                            if (string.IsNullOrEmpty(lineWorkingData1_OldStr))  //  软件启动后第一次运行
-                            {
-                                // 查询数据库最新一条数据,确定是不是更新
-                                string qSql = @"SELECT top(1) [GUID]
-                                  ,[LineName]
-                                  ,[BootTimeLong]
-                                  ,[NormalTimeLong]
-                                  ,[StandbyTimeLong]
-                                  ,[FaultTimeLong]
-                                  ,[MaterialShortageTimeLong]
-                                  ,[MaintenanceTimeLong]
-                                  ,[FaultNumber]
-                                  ,[OutputNumber]
-                                  ,[QualifiedNumber]
-                                  ,[QualifiedRate]
-                                  ,[DesignRhythm]
-                                  ,[RealityRhythm]
-                                  ,[CreateTime]
-                              FROM [LineWorkingData]
-                              where [CreateTime] > '{0}'
-                              and [LineName]='{1}'
-                              order by [CreateTime] desc
-                            ";
-                                qSql = string.Format(qSql, DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00", lineWorkingData1.LineName);
-
-                                var ds = SQLHelper_New.Query(qSql, null);
-                                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
-                                {
-                                    var dataDBlast = new LineWorkingData_ThisTime();
-                                    dataDBlast.GUID = ds.Tables[0].Rows[0][0].ToString();                             // 主键
-                                    dataDBlast.LineName = ds.Tables[0].Rows[0][1].ToString();                         // 线体名称
-                                    dataDBlast.BootTimeLong = Convert.ToSingle(ds.Tables[0].Rows[0][2].ToString());   // 本次开机时间(整线)
-                                    dataDBlast.CreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0][14].ToString());  // 创建时间
+                            case XiaomiDeviceState.Uninitialized:  // 未初始化状态(未初始状态,需先初始化装备才能运行)
+                                _DeviceStates = "未初始化状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Initializing:   // 初始化状态(初始化进行中)
+                                _DeviceStates = "初始化状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Initialized:    // 初始化完成状态(初始化完成)
+                                _DeviceStates = "初始化完成状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Running:        // 运行状态(正常运行中)
+                                _DeviceStates = "运行状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Paused:         // 暂停状态(设备运行中人工操作暂停,进入此状态)
+                                _DeviceStates = "暂停状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Fault:          // 故障状态(发生故障后进入此状态,同时停止运行)
+                                _DeviceStates = "故障状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                            case XiaomiDeviceState.Alarm:          // 警报状态(产生报警后进入此状态,同时停止运行)
+                                _DeviceStates = "警报状态";
+                                lblDeviceStates.Text = _DeviceStates;
+                                break;
+                        }
 
-                                    if (lineWorkingData1.BootTimeLong > dataDBlast.BootTimeLong)  // 需要更新的情况;不需要更新的走后面的插入
-                                    {
-                                        dataDBlast.BootTimeLong = lineWorkingData1.BootTimeLong;        // 本次开机时间(整线)
-                                        dataDBlast.NormalTimeLong = lineWorkingData1.NormalTimeLong;    // 本次开机运行时间(整线)
-                                        dataDBlast.StandbyTimeLong = lineWorkingData1.StandbyTimeLong;  // 本次开机待机时间(整线)
-                                        dataDBlast.FaultTimeLong = lineWorkingData1.FaultTimeLong;      // 本次开机故障时间(整线)
-                                        dataDBlast.MaterialShortageTimeLong = lineWorkingData1.MaterialShortageTimeLong;  // 本次开机缺料时间(整线)
-                                        dataDBlast.MaintenanceTimeLong = lineWorkingData1.MaintenanceTimeLong;            // 本次开机维修时间(整线)
-                                        dataDBlast.FaultNumber = lineWorkingData1.FaultNumber;          // 本次开机故障停机次数(整线)
-                                        dataDBlast.OutputNumber = lineWorkingData1.OutputNumber;        // 本次开机产量(整线)
-                                        dataDBlast.QualifiedNumber = lineWorkingData1.QualifiedNumber;  // 本次开机合格数量(整线)
-                                        dataDBlast.QualifiedRate = lineWorkingData1.QualifiedRate;      // 本次开机合格率(整线)
-                                        dataDBlast.DesignRhythm = lineWorkingData1.DesignRhythm;        // 设计节拍(整线)
-                                        dataDBlast.RealityRhythm = lineWorkingData1.RealityRhythm;      // 本次开机实际节拍(整线)
-
-                                        string usql = dataDBlast.ToStringUpdate();
-                                        SQLHelper_New.ExecuteSQL(usql, null);
-                                        lineWorkingData1_OldStr = JsonConvert.SerializeObject(dataDBlast);
-
-                                        AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕![{lineWorkingData1_OldStr}]");
-                                        //AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕!");
-                                    }
-                                }
-                                else
-                                {
-                                    // 插入
-                                    SQLHelper_New.ExecuteSQL(lineWorkingData1.ToStringInsert(), null);
-                                    lineWorkingData1_OldStr = String.Copy(lineWorkingData1_Str);
-                                    AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕![{lineWorkingData1_OldStr}]");
-                                    //AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕!");
-                                }
-                            }
-                            else if (!lineWorkingData1_Str.Equals(lineWorkingData1_OldStr))  // 非“软件启动后第一次运行”
+                        if (!_DeviceStates.Equals(_DeviceStates_Old))
+                        {
+                            var iotResult = SaveDeviceStateData(stationNameStr, xmDeviceState);  // 上传+保存
+                            if (iotResult.Item1 == 1)
                             {
-                                LineWorkingData_ThisTime lineWorkingData1_Old = string.IsNullOrEmpty(lineWorkingData1_OldStr) ? lineWorkingData1 : JsonConvert.DeserializeObject<LineWorkingData_ThisTime>(lineWorkingData1_OldStr);  // 上次的状态信息
-
-                                //// 本次开机设备运行情况
-                                //LineWorkingData1_ThisTime lineWorkingData_UI = JsonConvert.DeserializeObject<LineWorkingData1_ThisTime>(lineWorkingData1_OldStr);
-                                //Task.Run(() =>
-                                //{
-                                //    if (Form_Main.formDevAlarm != null && !Form_Main.formDevAlarm.IsDisposed && Form_Main.formDevAlarm.Visible)
-                                //    {
-                                //        Form_Main.formDevAlarm.UpdDeviceStatus_ThisTime(lineWorkingData_UI);  // UI更新
-                                //    }
-                                //});
-
-                                // 本日设备运行情况
-                                // 存数据库(开机时间>上次的开机时间,则更新上次记录;<则作为新数据插入)
-                                if (lineWorkingData1.BootTimeLong > lineWorkingData1_Old.BootTimeLong)
-                                {
-                                    // 更新
-                                    lineWorkingData1_Old.BootTimeLong = lineWorkingData1.BootTimeLong;        // 本次开机时间(整线)
-                                    lineWorkingData1_Old.NormalTimeLong = lineWorkingData1.NormalTimeLong;    // 本次开机运行时间(整线)
-                                    lineWorkingData1_Old.StandbyTimeLong = lineWorkingData1.StandbyTimeLong;  // 本次开机待机时间(整线)
-                                    lineWorkingData1_Old.FaultTimeLong = lineWorkingData1.FaultTimeLong;      // 本次开机故障时间(整线)
-                                    lineWorkingData1_Old.MaterialShortageTimeLong = lineWorkingData1.MaterialShortageTimeLong;  // 本次开机缺料时间(整线)
-                                    lineWorkingData1_Old.MaintenanceTimeLong = lineWorkingData1.MaintenanceTimeLong;            // 本次开机维修时间(整线)
-                                    lineWorkingData1_Old.FaultNumber = lineWorkingData1.FaultNumber;          // 本次开机故障停机次数(整线)
-                                    lineWorkingData1_Old.OutputNumber = lineWorkingData1.OutputNumber;        // 本次开机产量(整线)
-                                    lineWorkingData1_Old.QualifiedNumber = lineWorkingData1.QualifiedNumber;  // 本次开机合格数量(整线)
-                                    lineWorkingData1_Old.QualifiedRate = lineWorkingData1.QualifiedRate;      // 本次开机合格率(整线)
-                                    lineWorkingData1_Old.DesignRhythm = lineWorkingData1.DesignRhythm;        // 设计节拍(整线)
-                                    lineWorkingData1_Old.RealityRhythm = lineWorkingData1.RealityRhythm;      // 本次开机实际节拍(整线)
-
-                                    SQLHelper_New.ExecuteSQL(lineWorkingData1_Old.ToStringUpdate(), null);
-                                    lineWorkingData1_OldStr = JsonConvert.SerializeObject(lineWorkingData1_Old);
-                                    AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕![{lineWorkingData1_OldStr}]");
-                                    //AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕!");
-                                }
-                                else if (lineWorkingData1.BootTimeLong < lineWorkingData1_Old.BootTimeLong)
-                                {
-                                    // 插入
-                                    SQLHelper_New.ExecuteSQL(lineWorkingData1.ToStringInsert(), null);
-                                    lineWorkingData1_OldStr = String.Copy(lineWorkingData1_Str);
-                                    AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕![{lineWorkingData1_OldStr}]");
-                                    //AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕!");
-                                }
-
-                                await Task.Run(() =>
-                                {
-                                    try
-                                    {
-                                        if (Form_Main.formDevAlarm != null && !Form_Main.formDevAlarm.IsDisposed && Form_Main.formDevAlarm.Visible)
-                                        {
-                                            Form_Main.formDevAlarm.UpdDeviceStatus_Today();  // UI更新
-                                        }
-                                    }
-                                    catch { }
-                                });
+                                _DeviceStates_Old = _DeviceStates;
+                                AddMessage_Station(stationNameStr, LogType.Info, stationNameStr + $"_上传设备状态到Iot成功!");
                             }
+                            else
+                                AddMessage_Station(stationNameStr, LogType.Info, stationNameStr + $"_上传设备状态到Iot失败!报错信息:[{iotResult.Item1}]_{iotResult.Item2}");
                         }
-                        catch (Exception ex)
-                        {
-                            string str = ex.StackTrace;
-                            AddMessage_Station(stationNameStr, LogType.Error, $"PLC1_{stationNameStr}_获取设备运行信息出错!错误信息:" + ex.Message.ToString() + "异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
-                        }
-                        #endregion 获取设备运行信息
+                        #endregion 主页展示设备运行状态并上传到IOT中
+
+                        DateTime dtNow = DateTime.Now;
+                        //#region 获取设备运行信息
+                        //try
+                        //{
+                        //    LineWorkingData_ThisTime lineWorkingData1 = new LineWorkingData_ThisTime();
+                        //    lineWorkingData1.GUID = Guid.NewGuid().ToString();
+                        //    lineWorkingData1.LineName = GlobalContext.LineCode;
+
+                        //    // 
+                        //    lineWorkingData1.BootTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5500);     // 本次开机时间(整线)D5500 h
+                        //    lineWorkingData1.NormalTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5502);   // 本次开机运行时间(整线)D5502 h
+                        //    lineWorkingData1.StandbyTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5504);  // 本次开机待机时间(整线)D5504 h
+                        //    lineWorkingData1.FaultTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5506);    // 本次开机故障时间(整线)D5506 h
+                        //    lineWorkingData1.MaterialShortageTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5508);  // 本次开机缺料时间(整线)D5508 h
+                        //    lineWorkingData1.MaintenanceTimeLong = plc1Alarm.ReadHoldingRegisters<float>(5510);       // 本次开机维修时间(整线)D5510 h
+                        //    lineWorkingData1.FaultNumber = plc1Alarm.ReadHoldingRegisters<short>(5514);      // 本次开机故障停机次数(整线)D5514
+                        //    lineWorkingData1.OutputNumber = plc1Alarm.ReadHoldingRegisters<short>(5700);     // 本次开机产量(整线) D5700
+                        //    lineWorkingData1.QualifiedNumber = plc1Alarm.ReadHoldingRegisters<short>(5704);  // 本次开机合格数量(整线) D5704
+                        //    lineWorkingData1.QualifiedRate = plc1Alarm.ReadHoldingRegisters<float>(5710);    // 本次开机合格率(整线) D5710
+                        //    lineWorkingData1.DesignRhythm = plc1Alarm.ReadHoldingRegisters<float>(5714);     // 设计节拍(整线) D5714
+                        //    lineWorkingData1.RealityRhythm = plc1Alarm.ReadHoldingRegisters<float>(5716);    // 本次开机实际节拍(整线) D5716
+                        //    lineWorkingData1.CreateTime = DateTime.Now;
+
+                        //    string lineWorkingData1_Str = JsonConvert.SerializeObject(lineWorkingData1);
+
+                        //    // UI展示-展示到设备状态页
+                        //    if (string.IsNullOrEmpty(lineWorkingData1_OldStr))  //  软件启动后第一次运行
+                        //    {
+                        //        // 查询数据库最新一条数据,确定是不是更新
+                        //        string qSql = @"SELECT top(1) [GUID]
+                        //          ,[LineName]
+                        //          ,[BootTimeLong]
+                        //          ,[NormalTimeLong]
+                        //          ,[StandbyTimeLong]
+                        //          ,[FaultTimeLong]
+                        //          ,[MaterialShortageTimeLong]
+                        //          ,[MaintenanceTimeLong]
+                        //          ,[FaultNumber]
+                        //          ,[OutputNumber]
+                        //          ,[QualifiedNumber]
+                        //          ,[QualifiedRate]
+                        //          ,[DesignRhythm]
+                        //          ,[RealityRhythm]
+                        //          ,[CreateTime]
+                        //      FROM [LineWorkingData]
+                        //      where [CreateTime] > '{0}'
+                        //      and [LineName]='{1}'
+                        //      order by [CreateTime] desc
+                        //    ";
+                        //        qSql = string.Format(qSql, DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00", lineWorkingData1.LineName);
+
+                        //        var ds = SQLHelper_New.Query(qSql, null);
+                        //        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
+                        //        {
+                        //            var dataDBlast = new LineWorkingData_ThisTime();
+                        //            dataDBlast.GUID = ds.Tables[0].Rows[0][0].ToString();                             // 主键
+                        //            dataDBlast.LineName = ds.Tables[0].Rows[0][1].ToString();                         // 线体名称
+                        //            dataDBlast.BootTimeLong = Convert.ToSingle(ds.Tables[0].Rows[0][2].ToString());   // 本次开机时间(整线)
+                        //            dataDBlast.CreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0][14].ToString());  // 创建时间
+
+                        //            if (lineWorkingData1.BootTimeLong > dataDBlast.BootTimeLong)  // 需要更新的情况;不需要更新的走后面的插入
+                        //            {
+                        //                dataDBlast.BootTimeLong = lineWorkingData1.BootTimeLong;        // 本次开机时间(整线)
+                        //                dataDBlast.NormalTimeLong = lineWorkingData1.NormalTimeLong;    // 本次开机运行时间(整线)
+                        //                dataDBlast.StandbyTimeLong = lineWorkingData1.StandbyTimeLong;  // 本次开机待机时间(整线)
+                        //                dataDBlast.FaultTimeLong = lineWorkingData1.FaultTimeLong;      // 本次开机故障时间(整线)
+                        //                dataDBlast.MaterialShortageTimeLong = lineWorkingData1.MaterialShortageTimeLong;  // 本次开机缺料时间(整线)
+                        //                dataDBlast.MaintenanceTimeLong = lineWorkingData1.MaintenanceTimeLong;            // 本次开机维修时间(整线)
+                        //                dataDBlast.FaultNumber = lineWorkingData1.FaultNumber;          // 本次开机故障停机次数(整线)
+                        //                dataDBlast.OutputNumber = lineWorkingData1.OutputNumber;        // 本次开机产量(整线)
+                        //                dataDBlast.QualifiedNumber = lineWorkingData1.QualifiedNumber;  // 本次开机合格数量(整线)
+                        //                dataDBlast.QualifiedRate = lineWorkingData1.QualifiedRate;      // 本次开机合格率(整线)
+                        //                dataDBlast.DesignRhythm = lineWorkingData1.DesignRhythm;        // 设计节拍(整线)
+                        //                dataDBlast.RealityRhythm = lineWorkingData1.RealityRhythm;      // 本次开机实际节拍(整线)
+
+                        //                string usql = dataDBlast.ToStringUpdate();
+                        //                SQLHelper_New.ExecuteSQL(usql, null);
+                        //                lineWorkingData1_OldStr = JsonConvert.SerializeObject(dataDBlast);
+
+                        //                AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕![{lineWorkingData1_OldStr}]");
+                        //                //AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕!");
+                        //            }
+                        //        }
+                        //        else
+                        //        {
+                        //            // 插入
+                        //            SQLHelper_New.ExecuteSQL(lineWorkingData1.ToStringInsert(), null);
+                        //            lineWorkingData1_OldStr = String.Copy(lineWorkingData1_Str);
+                        //            AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕![{lineWorkingData1_OldStr}]");
+                        //            //AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕!");
+                        //        }
+                        //    }
+                        //    else if (!lineWorkingData1_Str.Equals(lineWorkingData1_OldStr))  // 非“软件启动后第一次运行”
+                        //    {
+                        //        LineWorkingData_ThisTime lineWorkingData1_Old = string.IsNullOrEmpty(lineWorkingData1_OldStr) ? lineWorkingData1 : JsonConvert.DeserializeObject<LineWorkingData_ThisTime>(lineWorkingData1_OldStr);  // 上次的状态信息
+
+                        //        //// 本次开机设备运行情况
+                        //        //LineWorkingData1_ThisTime lineWorkingData_UI = JsonConvert.DeserializeObject<LineWorkingData1_ThisTime>(lineWorkingData1_OldStr);
+                        //        //Task.Run(() =>
+                        //        //{
+                        //        //    if (Form_Main.formDevAlarm != null && !Form_Main.formDevAlarm.IsDisposed && Form_Main.formDevAlarm.Visible)
+                        //        //    {
+                        //        //        Form_Main.formDevAlarm.UpdDeviceStatus_ThisTime(lineWorkingData_UI);  // UI更新
+                        //        //    }
+                        //        //});
+
+                        //        // 本日设备运行情况
+                        //        // 存数据库(开机时间>上次的开机时间,则更新上次记录;<则作为新数据插入)
+                        //        if (lineWorkingData1.BootTimeLong > lineWorkingData1_Old.BootTimeLong)
+                        //        {
+                        //            // 更新
+                        //            lineWorkingData1_Old.BootTimeLong = lineWorkingData1.BootTimeLong;        // 本次开机时间(整线)
+                        //            lineWorkingData1_Old.NormalTimeLong = lineWorkingData1.NormalTimeLong;    // 本次开机运行时间(整线)
+                        //            lineWorkingData1_Old.StandbyTimeLong = lineWorkingData1.StandbyTimeLong;  // 本次开机待机时间(整线)
+                        //            lineWorkingData1_Old.FaultTimeLong = lineWorkingData1.FaultTimeLong;      // 本次开机故障时间(整线)
+                        //            lineWorkingData1_Old.MaterialShortageTimeLong = lineWorkingData1.MaterialShortageTimeLong;  // 本次开机缺料时间(整线)
+                        //            lineWorkingData1_Old.MaintenanceTimeLong = lineWorkingData1.MaintenanceTimeLong;            // 本次开机维修时间(整线)
+                        //            lineWorkingData1_Old.FaultNumber = lineWorkingData1.FaultNumber;          // 本次开机故障停机次数(整线)
+                        //            lineWorkingData1_Old.OutputNumber = lineWorkingData1.OutputNumber;        // 本次开机产量(整线)
+                        //            lineWorkingData1_Old.QualifiedNumber = lineWorkingData1.QualifiedNumber;  // 本次开机合格数量(整线)
+                        //            lineWorkingData1_Old.QualifiedRate = lineWorkingData1.QualifiedRate;      // 本次开机合格率(整线)
+                        //            lineWorkingData1_Old.DesignRhythm = lineWorkingData1.DesignRhythm;        // 设计节拍(整线)
+                        //            lineWorkingData1_Old.RealityRhythm = lineWorkingData1.RealityRhythm;      // 本次开机实际节拍(整线)
+
+                        //            SQLHelper_New.ExecuteSQL(lineWorkingData1_Old.ToStringUpdate(), null);
+                        //            lineWorkingData1_OldStr = JsonConvert.SerializeObject(lineWorkingData1_Old);
+                        //            AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕![{lineWorkingData1_OldStr}]");
+                        //            //AddMessage_Station(stationNameStr, LogType.Info, $"更新{BodyRun}完毕!");
+                        //        }
+                        //        else if (lineWorkingData1.BootTimeLong < lineWorkingData1_Old.BootTimeLong)
+                        //        {
+                        //            // 插入
+                        //            SQLHelper_New.ExecuteSQL(lineWorkingData1.ToStringInsert(), null);
+                        //            lineWorkingData1_OldStr = String.Copy(lineWorkingData1_Str);
+                        //            AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕![{lineWorkingData1_OldStr}]");
+                        //            //AddMessage_Station(stationNameStr, LogType.Info, $"保存{BodyRun}完毕!");
+                        //        }
+
+                        //        await Task.Run(() =>
+                        //        {
+                        //            try
+                        //            {
+                        //                if (Form_Main.formDevAlarm != null && !Form_Main.formDevAlarm.IsDisposed && Form_Main.formDevAlarm.Visible)
+                        //                {
+                        //                    Form_Main.formDevAlarm.UpdDeviceStatus_Today();  // UI更新
+                        //                }
+                        //            }
+                        //            catch { }
+                        //        });
+                        //    }
+                        //}
+                        //catch (Exception ex)
+                        //{
+                        //    string str = ex.StackTrace;
+                        //    AddMessage_Station(stationNameStr, LogType.Error, $"PLC1_{stationNameStr}_获取设备运行信息出错!错误信息:" + ex.Message.ToString() + "异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
+                        //}
+                        //#endregion 获取设备运行信息
 
                         #region 报警数据
                         try

+ 1 - 1
MainForm/FaForm/Form_Home.resx

@@ -8367,7 +8367,7 @@
         AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
         LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
         ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAACQ
-        PgAAAk1TRnQBSQFMAgEBAwEAAbgBCAG4AQgBIAEAASABAAT/ASEBAAj/AUIBTQE2BwABNgMAASgDAAGA
+        PgAAAk1TRnQBSQFMAgEBAwEAAcABCAHAAQgBIAEAASABAAT/ASEBAAj/AUIBTQE2BwABNgMAASgDAAGA
         AwABIAMAAQEBAAEgBgABQP8A/wA8AAP5Af8DzgH/A6gB/wOLAf8DeAH/A3EB/wNxAf8DeAH/A4sB/wOo
         Af8DzwH/A/kB/1AAAfcB+wH3Af8BuwHgAbYB/wGGAcgBfAH/AV0BtQFQAf8BQgGpATMB/wE5AaUBKQH/
         ATkBpQEpAf8BQwGqATQB/wFdAbYBUAH/AYUByAF7Af8BvAHhAbcB/wH3AfsB9wH/UAAB9gH1AfsB/wGw

+ 94 - 0
MainForm/FaForm/Form_Home_SaveData.cs

@@ -11,6 +11,8 @@ using MainForm.ClassFile.XiaomiAPI_MES;
 using static MainForm.ClassFile.XiaomiAPI.XiaomiMqttClient_Extend;
 using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationInbound;
 using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationOutbound;
+using System.IO;
+using MainForm.ClassFile.FALibraryClass;
 
 namespace MainForm
 {
@@ -714,6 +716,98 @@ namespace MainForm
         }
         #endregion 点检数据
 
+        #region 设备状态数据
+        /// <summary>
+        /// 记录上次保存 设备状态数据CSV 的时间
+        /// </summary>
+        private DateTime dtSaveDeviceStateCSV_Old = DateTime.Now.AddMinutes(-5);
+        /// <summary>
+        /// 上传设备状态数据
+        /// 上传IOT + 5分钟记录一次数据到txt中
+        /// </summary>
+        /// <param name="stationNameStr">工站信息</param>
+        /// <param name="deviceState">设备状态</param>
+        /// <param name="fault_code">故障编码</param>
+        /// <param name="fault_tm">故障发⽣时间</param>
+        /// <returns>上位机发送1代表OK;2代表上传客户MES失败;3代表上位机保存数据失败;4代表上位机报警;</returns>
+
+        public (int, string) SaveDeviceStateData(string stationNameStr, XiaomiDeviceState deviceState, string fault_code = "A40001", string fault_tm = "")
+        {
+            int result = 0;
+            string msg = "";
+
+            DateTime dtNow = DateTime.Now;
+            try
+            {
+                // 上传到IOT
+                if (GlobalContext.IsUseIot && GlobalContext.IsMqttDeviceState)
+                {
+                    DeviceStateDataRequest request = new DeviceStateDataRequest();
+                    if (GlobalContext.IsUsePLC1)
+                        request.station = GlobalContext.S1_station;    // 工位ID(可配置)
+                    if (GlobalContext.IsUsePLC2)
+                        request.station = GlobalContext.S2_station;    // 工位ID(可配置)
+                    if (GlobalContext.IsUsePLC3)
+                        request.station = GlobalContext.S3_station;    // 工位ID(可配置)
+                    if (GlobalContext.IsUsePLC4)
+                    {
+                        request.station = GlobalContext.S4_1_station;  // ⼯位ID(可配置)
+                                                                       //request.station = GlobalContext.S4_3_station;  // ⼯位ID(可配置)
+                                                                       //request.station = GlobalContext.S4_4_station;  // ⼯位ID(可配置)
+                                                                       //request.station = GlobalContext.S4_5_station;  // ⼯位ID(可配置)
+                    }
+                    if (GlobalContext.IsUsePLC5)
+                        request.station = GlobalContext.S5_station;    // 工位ID(可配置)
+
+                    request.state = deviceState.ToString();  // 设备状态
+                    request.time = dtNow.ToString("yyyy-MM-dd HH:mm:ss.fff");  // 状态切换时的时间 2022-06-01 14:27:57.283
+                    if (deviceState == XiaomiDeviceState.Fault || deviceState == XiaomiDeviceState.Alarm)
+                    {
+                        request.fault_code = fault_code;  // 对应的故障编码
+                        request.fault_tm = fault_tm;      // 故障发⽣时间(需要实际的发生时间)
+                    }
+
+                    // 上传
+                    (int, string) iotResult = XiaomiMqttClient_Extend.Write_DeviceStateData(request);
+
+                    result = iotResult.Item1 == 0 ? 1 : 2;
+                    msg = $"[{iotResult.Item1}]{iotResult.Item2}";
+
+                    // 5分钟记录一次数据到txt中
+                    if (dtSaveDeviceStateCSV_Old < dtNow)
+                    {
+                        string fileDir = GlobalContext.MqttDeviceStateDir + dtNow.ToString("yyyy_MM");  // 文件夹路径
+                        if (!Directory.Exists(fileDir))
+                            Directory.CreateDirectory(fileDir);
+
+                        string filePath = fileDir + dtNow.ToString("yyyy_MM_dd");                       // 文件路径
+
+                        StringBuilder sbData = new StringBuilder();
+                        if (!File.Exists(filePath))
+                            sbData.AppendLine("工位ID,设备状态,状态切换时的时间,故障编码,故障发生时间,上传结果");
+
+                        string iotResultMsg = $"[{iotResult.Item1}]{iotResult.Item2}";
+                        sbData.AppendLine($"{request.station},{request.state},{request.time},{request.fault_code},{request.fault_tm},{iotResultMsg}");  // 文件内容
+
+                        // 保存到CSV文件中
+                        string newLineData = sbData.ToString();  // 文件内容
+                        CSVHelper.CSVFile_AddLog(filePath, newLineData);
+                    }
+                }
+                else
+                    return (1, "OK;未启用上传Iot");
+            }
+            catch (Exception ex)
+            {
+                result = 4;
+                string str = ex.StackTrace;
+                msg = $"错误信息:" + ex.Message.ToString() + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1);
+                OnMessage(LogType.Error, stationNameStr + $"_PLC上传Iot设备状态数据报错!错误信息:" + ex.Message.ToString() + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
+            }
+            return (result, msg);
+        }
+        #endregion 设备状态数据
+
         #region 报警数据
         /// <summary>
         /// 添加报警数据ByDB(提交到MES+本地保存)

+ 1 - 0
MainForm/MainForm.csproj

@@ -237,6 +237,7 @@
     <Compile Include="ClassFile\DbHelper\JsonFileHelper.cs" />
     <Compile Include="ClassFile\DbHelper\PracticeContext.cs" />
     <Compile Include="ClassFile\DbHelper\SqlHelper.cs" />
+    <Compile Include="ClassFile\FALibraryClass\CSVHelper.cs" />
     <Compile Include="ClassFile\FALibraryClass\InovancePLC.cs" />
     <Compile Include="ClassFile\FALibraryClass\Md5Helper.cs" />
     <Compile Include="ClassFile\FileIOHelper.cs" />