From 810bd3e497b669993710d635a907db9709694ff1 Mon Sep 17 00:00:00 2001 From: huangxianguo Date: Tue, 9 Jul 2024 11:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=97=A5=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MasstransferCommon/Model/Constant/Topics.cs | 107 +++++++++++++++++- .../Model/Entity/OperationLog.cs | 27 +++++ .../License/LicenseService.cs | 1 + .../LogExporter/Model/OperationLogData.cs | 21 ++++ .../LogExporter/OperationLogExporter.cs | 46 ++++++++ MasstransferExporter/Program.cs | 21 ++-- .../Mqtt/Client/MessageQueueHelper.cs | 3 +- .../Mqtt/Client/MqttClient.cs | 18 +-- 8 files changed, 221 insertions(+), 23 deletions(-) create mode 100644 MasstransferCommon/Model/Entity/OperationLog.cs create mode 100644 MasstransferExporter/LogExporter/Model/OperationLogData.cs create mode 100644 MasstransferExporter/LogExporter/OperationLogExporter.cs diff --git a/MasstransferCommon/Model/Constant/Topics.cs b/MasstransferCommon/Model/Constant/Topics.cs index 335ef0f..fd96d3c 100644 --- a/MasstransferCommon/Model/Constant/Topics.cs +++ b/MasstransferCommon/Model/Constant/Topics.cs @@ -2,16 +2,117 @@ /// /// 这里保存所有的 Mqtt Topic +/// topic 的基本结构构成 +/// up/{sn}/{cmd}/{version} +/// /// -public class Topics +public static class Topics { + // ReSharper disable once InconsistentNaming + private const string SN = "123456"; + + private const string Version = "1.0.0"; + + + /// + /// 上报生产数据 + /// + public const string ReportProductRecord = $"up/{SN}/100/{Version}"; + + /// + /// 图片上传 + /// + public const string ImageUpload = $"up/{SN}/102/{Version}"; + + /// + /// 查询图片 + /// + public const string QueryImage = $"down/{SN}/103/{Version}"; + + /// + /// 坐标文件上传 + /// + public const string CoordinateUpload = $"up/{SN}/104/{Version}"; + + /// + /// 查询坐标文件 + /// + public const string QueryCoordinate = $"down/{SN}/105/{Version}"; + + /// + /// 上报用户操作数据 + /// + public const string ReportOperationLog = $"up/{SN}/200/{Version}"; + + /// + /// 上传系统日志文件 + /// + public const string UploadLogFile = $"up/{SN}/201/{Version}"; + + /// + /// 查询系统日志文件 + /// + public const string QueryLogFile = $"down/{SN}/202/{Version}"; + + /// + /// 上报故障信息 + /// + public const string ReportFaultRecord = $"up/{SN}/203/{Version}"; + + + /// + /// 上报配置数据 + /// + public const string ReportConfigData = $"up/{SN}/400/{Version}"; + + /// + /// 下发配置数据 + /// + public const string DownloadConfigData = $"down/{SN}/401/{Version}"; + + /// + /// 下发远程控制指令 + /// + public const string RemoteControl = $"down/{SN}/402/{Version}"; + + /// + /// 下发OTA包 + /// + // ReSharper disable once InconsistentNaming + public const string IssuedOTAPackage = $"down/{SN}/500/{Version}"; + + + /// + /// 查询OTA信息 + /// + // ReSharper disable once InconsistentNaming + public const string QueryOTA = $"up/{SN}/501/{Version}"; + + + /// + /// OTA 更新反馈 + /// + // ReSharper disable once InconsistentNaming + public const string OTAUpgradeFeedback = $"up/{SN}/502/{Version}"; + + /// /// 更新证书事件 /// - public const string UpdateLicenseEvent = "UpdateLicenseEvent"; + public const string UpdateLicenseEvent = $"up/{SN}/503/{Version}"; /// /// 更新证书事件反馈 /// - public const string UpdateLicenseEventFeedback = "UpdateLicenseEventFeedback"; + public const string UpdateLicenseEventFeedback = $"up/{SN}/505/{Version}"; + + /// + /// 上行心跳信息 + /// + public const string HeartBeat = $"up/{SN}/600/{Version}"; + + /// + /// 上行系统状态信息 + /// + public const string ReportSystemStat = $"up/{SN}/601/{Version}"; } \ No newline at end of file diff --git a/MasstransferCommon/Model/Entity/OperationLog.cs b/MasstransferCommon/Model/Entity/OperationLog.cs new file mode 100644 index 0000000..dbc29f6 --- /dev/null +++ b/MasstransferCommon/Model/Entity/OperationLog.cs @@ -0,0 +1,27 @@ +using System.ComponentModel; +using SQLite; + +namespace MasstransferCommon.Model.Entity; + +/// +/// 用户操作日志 +/// +[Table("operation_logs")] +public class OperationLog : Entity +{ + [Column("user_id"), Description("用户ID")] + public string UserId { get; set; } + + [Column("user_name"), Description("用户名")] + public string UserName { get; set; } + + [Column("action"), Description("操作")] public string Action { get; set; } + + [Column("params"), Description("参数")] public string? Params { get; set; } + + [Column("exception"), Description("异常信息")] + public string? Exception { get; set; } + + [Column("operate_time"), Description("操作时间")] + public DateTime OperateTime { get; set; } +} \ No newline at end of file diff --git a/MasstransferExporter/License/LicenseService.cs b/MasstransferExporter/License/LicenseService.cs index 0a882e5..3d200ad 100644 --- a/MasstransferExporter/License/LicenseService.cs +++ b/MasstransferExporter/License/LicenseService.cs @@ -1,4 +1,5 @@ using MasstransferCommon.Model.Constant; +using MasstransferCommunicate.Mqtt.Client; using MasstransferCommunicate.Process.Service; using MasstransferInfrastructure.Mqtt.Client; diff --git a/MasstransferExporter/LogExporter/Model/OperationLogData.cs b/MasstransferExporter/LogExporter/Model/OperationLogData.cs new file mode 100644 index 0000000..9cf58ac --- /dev/null +++ b/MasstransferExporter/LogExporter/Model/OperationLogData.cs @@ -0,0 +1,21 @@ +namespace MasstransferExporter.LogExporter.Model; + +/// +/// 用户操作日志 +/// +public class OperationLogData +{ + public string UserName { get; set; } + + public DateTime ControlTimestamp { get; set; } + + public string ControlType { get; set; } + + public string ControlResult { get; set; } + + public string ControlTarget { get; set; } + + public string ControlParams { get; set; } + + public string ControlMessage { get; set; } +} \ No newline at end of file diff --git a/MasstransferExporter/LogExporter/OperationLogExporter.cs b/MasstransferExporter/LogExporter/OperationLogExporter.cs new file mode 100644 index 0000000..b5f9dca --- /dev/null +++ b/MasstransferExporter/LogExporter/OperationLogExporter.cs @@ -0,0 +1,46 @@ +using MasstransferCommon.Model.Constant; +using MasstransferCommon.Model.Entity; +using MasstransferCommon.Utils; +using MasstransferCommunicate.Mqtt.Client; +using MasstransferExporter.LogExporter.Model; +using MasstransferInfrastructure.Database.Sqlite; +using Masuit.Tools.DateTimeExt; + +namespace MasstransferExporter.LogExporter; + +/// +/// 操作日志 +/// +public class OperationLogExporter +{ + private static readonly SqliteHelper Helper = SqliteHelper.GetInstance(); + + /// + /// 定时上传操作日志 + /// + public static async Task ExportOperationLog() + { + var yesterday = DateTime.Today.AddDays(-7); + + + // 读取出昨天内的用户操作日志 + var logIds = Helper.Query( + "select id from operation_logs where create_time >= ? and create_time < ?", + yesterday.Ticks, DateTime.Today.Ticks); + + foreach (var log in logIds.Select(logId => Helper.GetById(logId.Id)).OfType()) + { + var data = new OperationLogData + { + ControlMessage = log.Exception, + ControlParams = log.Params, + ControlResult = log.Exception.Equals("null") ? "成功" : "异常", + ControlTimestamp = log.OperateTime, + ControlType = log.Action, + UserName = log.UserName + }; + + await MessageQueueHelper.Publish(Topics.ReportOperationLog, data); + } + } +} \ No newline at end of file diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index c58cb27..dcc707d 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -1,20 +1,21 @@ -using MasstransferExporter.RemoteControl; -using MasstransferExporter.RemoteControl.Model; +using MasstransferCommunicate.Mqtt.Client; +using MasstransferExporter.LogExporter; +using MasstransferInfrastructure.Mqtt.Model; class Program { - static void Main() + static async Task Main() { - Thread.Sleep(30000); - - var cmd = new LockCmd + var options = new MqttConnectOptions { - Action = 1, - ExpiryTime = 0, - LockType = 0 + ServerAddress = "cloud.haiju-tech.com", + Port = 8884, + EnableTls = false }; - RemoteLockService.HandleLockCmd(cmd); + await MessageQueueHelper.InitConnect(options); + + await OperationLogExporter.ExportOperationLog(); Console.WriteLine("按任意键退出"); diff --git a/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs b/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs index 4eea18b..f2aecdd 100644 --- a/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs +++ b/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs @@ -6,8 +6,9 @@ using MQTTnet; using MQTTnet.Client; using MQTTnet.Protocol; using Serilog; +using MqttClient = MasstransferInfrastructure.Mqtt.Client.MqttClient; -namespace MasstransferInfrastructure.Mqtt.Client; +namespace MasstransferCommunicate.Mqtt.Client; public class MessageQueueHelper { diff --git a/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs b/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs index 728d3d3..1c3f69e 100644 --- a/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs +++ b/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs @@ -25,14 +25,14 @@ class MqttClient { var clientId = DeviceInfoUtil.GenerateUniqueID(); - var caCert = GetCertificate(options.CaCert); - var clientCert = GetCertificate(options.ClientCert); - - var chain = new X509Certificate2Collection(new[] { caCert, clientCert }); + // var caCert = GetCertificate(options.CaCert); + // var clientCert = GetCertificate(options.ClientCert); + // + // var chain = new X509Certificate2Collection(new[] { caCert, clientCert }); return new MqttClientOptionsBuilder() .WithTcpServer(options.ServerAddress, options.Port) - .WithCredentials(options.UserName, options.Password) + // .WithCredentials(options.UserName, options.Password) .WithClientId(clientId) .WithCleanSession() .WithTlsOptions( @@ -40,7 +40,7 @@ class MqttClient { o.UseTls(options.EnableTls); o.WithSslProtocols(options.Protocols); - o.WithTrustChain(chain); + // o.WithTrustChain(chain); } ) .Build(); @@ -58,10 +58,10 @@ class MqttClient /// public async Task ConnectAsync(MqttConnectOptions options) { - var optionsBuilder = GetConnectionOptions(options); + var ops = GetConnectionOptions(options); var client = new MqttFactory().CreateMqttClient(); - var connectResult = await client.ConnectAsync(optionsBuilder); - if (!connectResult.IsSessionPresent) + var connectResult = await client.ConnectAsync(ops); + if (connectResult.ResultCode != MqttClientConnectResultCode.Success) { return false; }