237 lines
9.3 KiB
C#
Raw Permalink Normal View History

2025-10-11 18:25:59 +08:00
/*
* <copyright>
Company="公司"
2020-2025
Author=JCH
Path=XiaoZhiSharp_ConsoleApp.Core.TickTimer.cs
* <copyright>
* --------------------------------------------------------------
* JCH
* TickTimer.cs
* V1.0.0.0
* JCH
* 2025/9/25 17:37:10
*
* --------------------------------------------------------------
* 2025/9/25 17:37:10
* JCH
* V1.0.0.0
*
* --------------------------------------------------------------
*/
using Newtonsoft.Json.Linq;
using XiaoZhiSharp;
using XiaoZhiSharp.Kernels;
using XiaoZhiSharp.Utils;
namespace XiaoZhiSharp_ConsoleApp.Core
{
public class TickTimer
{
#region Singleton
private static volatile TickTimer instance;
private static readonly object syncRoot = new object();
private TickTimer()
{
}
public static TickTimer TT
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new TickTimer();
}
}
}
return instance;
}
}
#endregion Singleton
#region Properties
public PushTimer PushTimer { get { return PushFactory.Push.PushTimer; } }
#endregion Properties
private XiaoZhiAgent _agent;
private SystemMonitor monitor = new SystemMonitor();
public void RunTimer(XiaoZhiAgent agent)
{
_agent = agent;
string appsettingJson = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Configs", "AppSettingConfig.json"));
// 快速从json文件中获取配置信息
var appsetting = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(appsettingJson);
var pushTimerIntervalStr = appsetting?["App"]?["UploadSysInfoInterval"]?.ToString();
int pushTimerInterval = 1000; // 默认值1秒钟
if (int.TryParse(pushTimerIntervalStr, out pushTimerInterval))
{
PushTimer.PushTimerInterval = pushTimerInterval;
}
PushTimer.OnPushTimer += OnSystemMessagePushTickTimer;
PushTimer.IsPushTimer = true;
}
private void OnSystemMessagePushTickTimer(object? sender, EventArgs e)
{
try
{
var sessionId = string.Empty;
if (_agent.ConnectState != System.Net.WebSockets.WebSocketState.Open)
{
#region 使
var hardwareInfo = XiaoZhiSharp.Global.HardwareInfo;
hardwareInfo.RefreshCPUList(includePercentProcessorTime: true);
hardwareInfo.RefreshVideoControllerList();
hardwareInfo.RefreshMemoryStatus();
hardwareInfo.RefreshDriveList();
foreach (var cpuItem in hardwareInfo.CpuList)
{
//Console.WriteLine(cpuItem);
foreach (var cpuCore in cpuItem.CpuCoreList)
{
//Console.WriteLine(cpuCore);
}
}
var cpu = hardwareInfo.CpuList.Count > 0 ? hardwareInfo.CpuList.First().PercentProcessorTime : 0;
/*-----------------*/
foreach (var hardware in hardwareInfo.VideoControllerList)
{
//Console.WriteLine(hardware);
}
var gpu = 0d;//XiaoZhiSharp.Global.FormatBytes(hardwareInfo.VideoControllerList.Last().AdapterRAM);
var gpuInfo = monitor.GetHardwareInfo();
gpu = gpuInfo.GpuUsage;
/*-----------------*/
//Console.WriteLine(hardwareInfo.MemoryStatus);
double memory = ((double)hardwareInfo.MemoryStatus.TotalPhysical - hardwareInfo.MemoryStatus.AvailablePhysical) / hardwareInfo.MemoryStatus.TotalPhysical * 100;
/*-----------------*/
double totalDisk = 0;
double usedDisk = 0;
foreach (var drive in hardwareInfo.DriveList)
{
//Console.WriteLine(drive);
foreach (var partition in drive.PartitionList.Where(p => p.PrimaryPartition))
{
//Console.WriteLine(partition);
foreach (var volume in partition.VolumeList)
{
//Console.WriteLine(volume);
totalDisk += volume.Size;
usedDisk += (volume.Size - volume.FreeSpace);
}
}
}
var disk = usedDisk / totalDisk * 100;
var os = new XiaoZhiSharp.Models.OSModel
{
CPU = cpu,
GPU = gpu,
Memory = memory,
Disk = disk
};
#endregion 使
#region GPS定位信息
var timestamp = DateTime.Now;
var gps = new XiaoZhiSharp.Models.GPSModel
{
Year = timestamp.Year,
Month = timestamp.Month,
Day = timestamp.Day,
Hour = timestamp.Hour,
Minute = timestamp.Minute,
Second = timestamp.Second,
Longitude = 0,
Latitude = 0,
Altitude = 0,
Speed = 0,
Heading = 0,
//Course = 0,
//CrabAngle = 0,
GPSSatellitesInView = 0,
GPSSatellitesUsed = 0,
BDSatellitesInView = 0,
BDSatellitesUsed = 0,
PDOP = 0,
HDOP = 0,
VDOP = 0,
//TDOP = 0
};
string appsettingJson = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Configs", "AppSettingConfig.json"));
// 快速从json文件中获取配置信息
var appsetting = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(appsettingJson);
var gpsDataFilePath = appsetting?["App"]?["GPSDataFilePath"]?.ToString();
if (!string.IsNullOrEmpty(gpsDataFilePath) && File.Exists(gpsDataFilePath))
{
var gpsDataJson = File.ReadAllText(gpsDataFilePath);
gps = Newtonsoft.Json.JsonConvert.DeserializeObject<XiaoZhiSharp.Models.GPSModel>(gpsDataJson);
}
else
{
LogConsole.Warning("GPS数据文件路径无效或文件不存在无法获取GPS定位信息");
#if DEBUG
gpsDataFilePath = Path.Combine(Environment.CurrentDirectory, "Configs", "gpsdata.json");
var gpsDataJson = File.ReadAllText(gpsDataFilePath);
gps = Newtonsoft.Json.JsonConvert.DeserializeObject<XiaoZhiSharp.Models.GPSModel>(gpsDataJson);
#endif
}
#endregion GPS定位信息
#region /* --采用 Newtonsoft.Json.Linq 构建 JSON 对象--*/
/*
JObject jsonObj = new JObject
{
["session_id"] = sessionId,
["type"] = "msgpush",
["OS"] = new JObject
{
["CPU"] = cpu,
["GPU"] = gpu,
["Memory"] = memory,
["Disk"] = disk
},
["GPS"] = new JObject
{
["DateTime"] = cpu,
["GPU"] = gpu,
["Memory"] = memory,
["Disk"] = disk
}
};*/
#endregion /* --采用 Newtonsoft.Json.Linq 构建 JSON 对象--*/
var jsonObj = new XiaoZhiSharp.Models.MsgPushModel
{
SessionId = sessionId,
Type = "msgpush",
OS = os,
GPS = gps
};
string msg = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);
LogConsole.InfoLine(msg);
this._agent.ChatMessage(msg);
}
}
catch (Exception ex)
{
LogConsole.Warning(ex.Message);
}
}
}
}