添加用户操作日志功能

This commit is contained in:
huangxianguo 2024-07-09 11:41:08 +08:00
parent 14b49d38a2
commit 810bd3e497
8 changed files with 221 additions and 23 deletions

View File

@ -2,16 +2,117 @@
/// <summary>
/// 这里保存所有的 Mqtt Topic
/// topic 的基本结构构成
/// up/{sn}/{cmd}/{version}
///
/// </summary>
public class Topics
public static class Topics
{
// ReSharper disable once InconsistentNaming
private const string SN = "123456";
private const string Version = "1.0.0";
/// <summary>
/// 上报生产数据
/// </summary>
public const string ReportProductRecord = $"up/{SN}/100/{Version}";
/// <summary>
/// 图片上传
/// </summary>
public const string ImageUpload = $"up/{SN}/102/{Version}";
/// <summary>
/// 查询图片
/// </summary>
public const string QueryImage = $"down/{SN}/103/{Version}";
/// <summary>
/// 坐标文件上传
/// </summary>
public const string CoordinateUpload = $"up/{SN}/104/{Version}";
/// <summary>
/// 查询坐标文件
/// </summary>
public const string QueryCoordinate = $"down/{SN}/105/{Version}";
/// <summary>
/// 上报用户操作数据
/// </summary>
public const string ReportOperationLog = $"up/{SN}/200/{Version}";
/// <summary>
/// 上传系统日志文件
/// </summary>
public const string UploadLogFile = $"up/{SN}/201/{Version}";
/// <summary>
/// 查询系统日志文件
/// </summary>
public const string QueryLogFile = $"down/{SN}/202/{Version}";
/// <summary>
/// 上报故障信息
/// </summary>
public const string ReportFaultRecord = $"up/{SN}/203/{Version}";
/// <summary>
/// 上报配置数据
/// </summary>
public const string ReportConfigData = $"up/{SN}/400/{Version}";
/// <summary>
/// 下发配置数据
/// </summary>
public const string DownloadConfigData = $"down/{SN}/401/{Version}";
/// <summary>
/// 下发远程控制指令
/// </summary>
public const string RemoteControl = $"down/{SN}/402/{Version}";
/// <summary>
/// 下发OTA包
/// </summary>
// ReSharper disable once InconsistentNaming
public const string IssuedOTAPackage = $"down/{SN}/500/{Version}";
/// <summary>
/// 查询OTA信息
/// </summary>
// ReSharper disable once InconsistentNaming
public const string QueryOTA = $"up/{SN}/501/{Version}";
/// <summary>
/// OTA 更新反馈
/// </summary>
// ReSharper disable once InconsistentNaming
public const string OTAUpgradeFeedback = $"up/{SN}/502/{Version}";
/// <summary>
/// 更新证书事件
/// </summary>
public const string UpdateLicenseEvent = "UpdateLicenseEvent";
public const string UpdateLicenseEvent = $"up/{SN}/503/{Version}";
/// <summary>
/// 更新证书事件反馈
/// </summary>
public const string UpdateLicenseEventFeedback = "UpdateLicenseEventFeedback";
public const string UpdateLicenseEventFeedback = $"up/{SN}/505/{Version}";
/// <summary>
/// 上行心跳信息
/// </summary>
public const string HeartBeat = $"up/{SN}/600/{Version}";
/// <summary>
/// 上行系统状态信息
/// </summary>
public const string ReportSystemStat = $"up/{SN}/601/{Version}";
}

View File

@ -0,0 +1,27 @@
using System.ComponentModel;
using SQLite;
namespace MasstransferCommon.Model.Entity;
/// <summary>
/// 用户操作日志
/// </summary>
[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; }
}

View File

@ -1,4 +1,5 @@
using MasstransferCommon.Model.Constant;
using MasstransferCommunicate.Mqtt.Client;
using MasstransferCommunicate.Process.Service;
using MasstransferInfrastructure.Mqtt.Client;

View File

@ -0,0 +1,21 @@
namespace MasstransferExporter.LogExporter.Model;
/// <summary>
/// 用户操作日志
/// </summary>
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; }
}

View File

@ -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;
/// <summary>
/// 操作日志
/// </summary>
public class OperationLogExporter
{
private static readonly SqliteHelper Helper = SqliteHelper.GetInstance();
/// <summary>
/// 定时上传操作日志
/// </summary>
public static async Task ExportOperationLog()
{
var yesterday = DateTime.Today.AddDays(-7);
// 读取出昨天内的用户操作日志
var logIds = Helper.Query<OperationLog>(
"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<OperationLog>(logId.Id)).OfType<OperationLog>())
{
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);
}
}
}

View File

@ -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("按任意键退出");

View File

@ -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
{

View File

@ -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
/// <param name="options"></param>
public async Task<bool> 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;
}