From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
SimpleUtil/HMI.SerialData.Util/SerialDataHelper.cs | 132 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 132 insertions(+), 0 deletions(-)
diff --git a/SimpleUtil/HMI.SerialData.Util/SerialDataHelper.cs b/SimpleUtil/HMI.SerialData.Util/SerialDataHelper.cs
new file mode 100644
index 0000000..178c70d
--- /dev/null
+++ b/SimpleUtil/HMI.SerialData.Util/SerialDataHelper.cs
@@ -0,0 +1,132 @@
+锘縰sing System;
+using System.Data.SQLite;
+using System.IO;
+
+namespace HMI.Simple.Util
+{
+ public class SerialDataHelper
+ {
+ private static string FileName = "SerialData";
+ /// <summary>
+ /// 闃叉澶氱嚎绋嬪垱寤烘枃浠讹紙浣嗘槸涓嶇煡閬搕eststand浼氫笉浼氬杩涚▼璋冪敤锛�
+ /// </summary>
+ private static object fileLock = new object();
+
+ private static string createTableSql = @"CREATE TABLE IF NOT EXISTS serialData (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ sn TEXT NOT NULL,
+ station TEXT NOT NULL,
+ step TEXT NOT NULL,
+ limitDown TEXT,
+ limitUp TEXT,
+ unit TEXT,
+ offset TEXT,
+ value TEXT,
+ status TEXT,
+ finalStatus TEXT,
+ consumeTime BIGINT,
+ totalConsumeTime BIGINT,
+ time DATETIME NOT NULL);";
+
+ private static string GetConnectionStr(string dbFile)
+ {
+ return $"DataSource={dbFile};Version=3;Pooling=False;Max Pool Size=100";
+ }
+
+ private static void CreateDbFile(string dbFile)
+ {
+ lock (fileLock)
+ {
+ if (!File.Exists(dbFile))
+ {
+ string dir = Path.GetDirectoryName(dbFile);
+ if (!Directory.Exists(dir))
+ {
+ Directory.CreateDirectory(dir);
+ }
+ SQLiteConnection.CreateFile(dbFile);
+
+ using (SQLiteConnection conn = new SQLiteConnection())
+ using (SQLiteCommand cmd = new SQLiteCommand(conn))
+ {
+ conn.ConnectionString = GetConnectionStr(dbFile);
+ conn.Open();
+ cmd.CommandText = createTableSql;
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ }
+
+ private static string GetDbFile()
+ {
+ string value = RegUtil.GetLocaltion();
+ string path = Path.Combine(value, "Config", "DataBoard", $"{FileName}_{DateTime.Now:yyyy-MM}.db");
+ if (!File.Exists(path))
+ {
+ CreateDbFile(path);
+ }
+ return path;
+ }
+
+ public static bool InsertSerialData(string sn, string station, string step, string status, out string errMsg)
+ {
+ errMsg = string.Empty;
+ try
+ {
+ string dbFile = GetDbFile();
+ using (SQLiteConnection conn = new SQLiteConnection())
+ using (SQLiteCommand cmd = new SQLiteCommand(conn))
+ {
+ conn.ConnectionString = GetConnectionStr(dbFile);
+ conn.Open();
+ cmd.CommandText =
+ $@"INSERT INTO serialData (sn, station, step, status, time) VALUES ('{sn}', '{station}', '{step}', '{status}', '{DateTime.Now:yyyy-MM-dd HH:mm:ss}');";
+ if (cmd.ExecuteNonQuery() > 0)
+ {
+ return true;
+ }
+ errMsg = "鎻掑叆澶辫触";
+ return false;
+ }
+ }
+ catch (Exception ex)
+ {
+ errMsg = ex.Message;
+ return false;
+ }
+ }
+
+ public static bool UpdateStatusBySn(string dbFile, string sn, string status, out string errMsg)
+ {
+ errMsg = string.Empty;
+ try
+ {
+ if (!File.Exists(dbFile))
+ {
+ errMsg = "鏁版嵁搴撴枃浠朵笉瀛樺湪";
+ return false;
+ }
+ using (SQLiteConnection conn = new SQLiteConnection())
+ using (SQLiteCommand cmd = new SQLiteCommand(conn))
+ {
+ conn.ConnectionString = GetConnectionStr(dbFile);
+ conn.Open();
+ cmd.CommandText =
+ $@"UPDATE serialData SET status='{status}', time='{DateTime.Now:yyyy-MM-dd HH:mm:ss}' WHERE sn='{sn}';";
+ if (cmd.ExecuteNonQuery() > 0)
+ {
+ return true;
+ }
+ errMsg = "鏇存柊澶辫触";
+ return false;
+ }
+ }
+ catch (Exception ex)
+ {
+ errMsg = ex.Message;
+ return false;
+ }
+ }
+ }
+}
--
Gitblit v1.9.1