weijw 5 months ago
parent
commit
d1a3e36048
5 changed files with 52 additions and 4 deletions
  1. 4 1
      ConfigHelper.cs
  2. 9 0
      MainForm.cs
  3. 26 0
      Model/PLCRecord.cs
  4. 11 1
      Model/WashRecord.cs
  5. 2 2
      config.xml

+ 4 - 1
ConfigHelper.cs

@@ -31,6 +31,7 @@ namespace xicheji
             var config = new Config
             {
                 ServicePort = xdoc.Root!.Element("Service")?.Attribute("Port")?.Value ?? "9999",
+                DBString = xdoc.Root!.Element("Service")?.Attribute("DBString")?.Value ?? "DataSource=xicheji.db",
 
                 Server = xdoc.Root!.Element("Platform")?.Attribute("Server")?.Value ?? "",
                 UploadInterval = int.Parse(xdoc.Root!.Element("Platform")?.Attribute("ReceiveInterval")?.Value ?? "2"),
@@ -79,6 +80,7 @@ namespace xicheji
             var xdoc = XDocument.Load(configFileName);
             var xService = xdoc.Root!.Element("Service");
             xService?.SetAttributeValue("Port", config.ServicePort);
+            xService?.SetAttributeValue("DBString", config.DBString);
 
             var xPlatform = xdoc.Root!.Element("Platform");
             xPlatform?.SetAttributeValue("Server", config.Server);
@@ -97,7 +99,7 @@ namespace xicheji
             xCamera?.SetAttributeValue("UserName", config.CameraUserName);
             xCamera?.SetAttributeValue("Password", config.CameraPassword);
 
-            xdoc.Save("config2.xml");
+            xdoc.Save(configFileName);
         }
 
         public static Variable? FindVariable(string variableName)
@@ -113,6 +115,7 @@ namespace xicheji
     public class Config
     {
         public string ServicePort { get; set; } = string.Empty;
+        public string DBString { get; set; } = string.Empty;
 
         public string Server { get; set; } = string.Empty;
         public int UploadInterval { get; internal set; }

+ 9 - 0
MainForm.cs

@@ -21,12 +21,17 @@ namespace xicheji
         private Plc? plc = null;
         private HikApi? hikApi = null;
         private string lastCarNo = string.Empty;
+        private IFreeSql db;
 
         public MainForm()
         {
             InitializeComponent();
             config = ConfigHelper.GetConfig();
             logWriter = new StreamWriter(DateTime.Now.ToString("yyyy_MM_dd") + ".log");
+            db = new FreeSql.FreeSqlBuilder()
+                .UseAutoSyncStructure(true)
+                .UseConnectionString(FreeSql.DataType.Sqlite, config.DBString)
+                .Build();
         }
 
         private void MainForm_Load(object sender, EventArgs e)
@@ -175,6 +180,8 @@ namespace xicheji
                 return;
             }
 
+            db.Insert(new PLCRecord() { Data = currentDataBlock }).ExecuteAffrows();
+
             lastReceiveTime = time;
 
             ReadError();
@@ -238,6 +245,8 @@ namespace xicheji
 
                 washRecords.Add(washRecord);
 
+                db.Insert(washRecord).ExecuteAffrows();
+
                 //洗车完成后重置车牌信息
                 lastCarNo = string.Empty;
             }

+ 26 - 0
Model/PLCRecord.cs

@@ -0,0 +1,26 @@
+using FreeSql.DataAnnotations;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace xicheji
+{
+    public class PLCRecord
+    {
+        /// <summary>
+        /// 数据库主键Id, 自增, 无需赋值
+        /// </summary>
+        [Column(IsIdentity = true)]
+        public int Id { get; set; }
+
+        /// <summary>
+        /// 单次的数据
+        /// </summary>
+        public byte[] Data { get; set; }
+
+        [Column(ServerTime = DateTimeKind.Utc, CanUpdate = false)]
+        public DateTime CreateTime { get; set; }
+    }
+}

+ 11 - 1
WashRecord.cs → Model/WashRecord.cs

@@ -1,4 +1,5 @@
-using System;
+using FreeSql.DataAnnotations;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -11,6 +12,12 @@ namespace xicheji
     /// </summary>
     public class WashRecord
     {
+        /// <summary>
+        /// 数据库主键Id, 自增, 无需赋值
+        /// </summary>
+        [Column(IsIdentity = true)]
+        public int Id { get; set; }
+
         /// <summary>
         /// 洗车机器mac地址
         /// </summary>
@@ -55,5 +62,8 @@ namespace xicheji
         /// 水压
         /// </summary>
         public int WaterPressure { get; set; }
+
+        [Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
+        public DateTime CreateTime { get; set; }
     }
 }

+ 2 - 2
config.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <Config>
-	<!--Service Port只能从配置文件修改, 然后重新运行程序-->
-	<Service Port="9999"/>
+	<!--Service 是初始化的配置只能从配置文件修改, 然后重新运行程序-->
+	<Service Port="9999" DBString="DataSource=xicheji.db"/>
 	<!--<Platform Server="http://iot.cyftcn.cn:3033/"  UploadInterval="2"/>-->
 	<Platform Server="http://172.16.2.241:8088/"  UploadInterval="2"/>
 	<!--Type: S7200,Logo0BA8,S7200Smart,S7300,S7400,S71200,S71500-->