alielie
2025-07-04 0940f31b7eba1cf6b6a8c1fca72d97cf686e79f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Data.SQLite;
using System.IO;
 
namespace HMI.Simple.Util
{
    public class SerialDataHelper
    {
        private static string FileName = "SerialData";
        /// <summary>
        /// 防止多线程创建文件(但是不知道teststand会不会多进程调用)
        /// </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,
                    status TEXT NOT NULL,
                    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;
            }
        }
    }
}