147 lines
6.1 KiB
C#
147 lines
6.1 KiB
C#
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using VOL.Core.Configuration;
|
||
using VOL.Core.Const;
|
||
using VOL.Core.DBManager;
|
||
using VOL.Core.Enums;
|
||
using VOL.Core.Extensions;
|
||
using VOL.Core.WorkFlow;
|
||
using VOL.Entity.DomainModels;
|
||
|
||
namespace VOL.Core.Services
|
||
{
|
||
/// <summary>
|
||
/// 通过内部队列写入心率数据
|
||
/// </summary>
|
||
public static class HeartRateQueueData
|
||
{
|
||
public static ConcurrentQueue<Ai_HeartRateData> heartRateQueueData = new ConcurrentQueue<Ai_HeartRateData>();
|
||
static HeartRateQueueData()
|
||
{
|
||
Task.Run(() =>
|
||
{
|
||
Start();
|
||
if (DBType.Name != "MsSql")
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
DBServerProvider.SqlDapper.ExcuteNonQuery("set global local_infile = 'ON';", null);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"日志启动调用mysql数据库异常:{ex.Message},{ex.StackTrace}");
|
||
}
|
||
});
|
||
}
|
||
|
||
public static void Add(Ai_HeartRateData heartRateData)
|
||
{
|
||
heartRateQueueData.Enqueue(heartRateData);
|
||
}
|
||
|
||
private static void Start()
|
||
{
|
||
DataTable queueTable = CreateEmptyTable();
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
if (heartRateQueueData.Count() > 0)
|
||
{
|
||
DequeueToTable(queueTable);
|
||
}
|
||
else
|
||
{
|
||
Thread.Sleep(1000); // 如果没有数据,等待 1 秒
|
||
if (queueTable.Rows.Count == 0) { continue; }
|
||
}
|
||
|
||
// 每 5 秒写一次数据
|
||
DBServerProvider.SqlDapper.BulkInsert(queueTable, "Ai_HeartRateData", SqlBulkCopyOptions.Default);
|
||
queueTable.Clear();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"心率数据同步失败:{ex.Message}");
|
||
Logger.Error($"心率数据同步失败:{ex.Message}");
|
||
Logger.Info(Enums.LoggerType.Add, $"Data:{queueTable.Serialize()}");
|
||
queueTable.Clear();
|
||
Thread.Sleep(5000); // 如果发生异常,稍作等待后继续
|
||
}
|
||
}
|
||
}
|
||
|
||
private static void DequeueToTable(DataTable queueTable)
|
||
{
|
||
// 从队列中取出日志
|
||
heartRateQueueData.TryDequeue(out Ai_HeartRateData heartRateData);
|
||
|
||
DataRow row = queueTable.NewRow();
|
||
|
||
// 填充 DataRow 的各个列
|
||
row["schoolcode"] = heartRateData.SchoolCode;
|
||
row["code"] = heartRateData.Code;
|
||
row["classroomrecordid"] = heartRateData.ClassRoomRecordId;
|
||
row["gradeid"] = heartRateData.GradeId;
|
||
row["classid"] = heartRateData.ClassId;
|
||
row["gradename"] = heartRateData.GradeName;
|
||
row["classname"] = heartRateData.ClassName;
|
||
//row["studentcount"] = heartRateData.StudentCount;
|
||
row["teacherid"] = heartRateData.TeacherId;
|
||
row["studentno"] = heartRateData.StudentNo;
|
||
row["studentname"] = heartRateData.StudentName;
|
||
row["sex"] = (int)heartRateData.Sex;
|
||
//row["starttime"] = heartRateData.StartTime;
|
||
//row["endtime"] = heartRateData.EndTime;
|
||
row["scoretime"] = heartRateData.ScoreTime;
|
||
row["motionduration"] = heartRateData.MotionDuration;
|
||
row["consumption"] = heartRateData.Consumption;
|
||
row["strength"] = heartRateData.Strength;
|
||
row["value"] = heartRateData.Value;
|
||
row["remarks"] = heartRateData.Remarks;
|
||
|
||
// 添加到 DataTable
|
||
queueTable.Rows.Add(row);
|
||
}
|
||
|
||
private static DataTable CreateEmptyTable()
|
||
{
|
||
DataTable queueTable = new DataTable();
|
||
|
||
// 定义与数据库中小写字段匹配的列
|
||
queueTable.Columns.Add("schoolcode", typeof(string)); // 学校Code
|
||
queueTable.Columns.Add("code", typeof(string)); // AI设备的唯一编码
|
||
queueTable.Columns.Add("classroomrecordid", typeof(int)); // 课堂记录Id
|
||
queueTable.Columns.Add("gradeid", typeof(int)); // 年级编号
|
||
queueTable.Columns.Add("classid", typeof(int)); // 班级Id
|
||
queueTable.Columns.Add("gradename", typeof(string)); // 年级名称
|
||
queueTable.Columns.Add("classname", typeof(string)); // 班级名称
|
||
//queueTable.Columns.Add("studentcount", typeof(int)); // 班级人数
|
||
queueTable.Columns.Add("teacherid", typeof(int)); // 老师Id
|
||
queueTable.Columns.Add("studentno", typeof(string)); // 学号
|
||
queueTable.Columns.Add("studentname", typeof(string)); // 姓名
|
||
queueTable.Columns.Add("sex", typeof(int)); // 性别(SexType,假设是int类型)
|
||
//queueTable.Columns.Add("starttime", typeof(DateTime)); // 开始时间
|
||
//queueTable.Columns.Add("endtime", typeof(DateTime)); // 结束时间
|
||
queueTable.Columns.Add("scoretime", typeof(DateTime)); // 测试时间
|
||
queueTable.Columns.Add("motionduration", typeof(int)); // 运动时长/分钟
|
||
queueTable.Columns.Add("consumption", typeof(int)); // 运动消耗
|
||
queueTable.Columns.Add("strength", typeof(double)); // 运动强度
|
||
queueTable.Columns.Add("value", typeof(double)); // 心率
|
||
queueTable.Columns.Add("remarks", typeof(string)); // 备注
|
||
|
||
return queueTable;
|
||
}
|
||
|
||
}
|
||
}
|