From c7ccffdfafb48729505f502688994018f23de2b1 Mon Sep 17 00:00:00 2001 From: huangxianguo Date: Tue, 24 Sep 2024 11:16:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4SN=E7=9A=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Model/Constant/Constants.cs | 6 --- MasstransferCommon/Model/Constant/Topics.cs | 54 +++++++++---------- MasstransferCommon/Utils/DeviceInfoUtil.cs | 12 +++++ .../DataExporter/CoordinateService.cs | 10 ++-- .../DataExporter/Model/DeviceParams.cs | 45 ++++++++++++++++ .../DataExporter/Model/SystemParamsDTO.cs | 2 + .../DataExporter/SystemParamsService.cs | 9 +++- .../ImageExporter/ImageService.cs | 6 ++- .../LogExporter/LogFileExporter.cs | 28 +++++++--- .../Mqtt/Client/MqttClient.cs | 6 +-- 10 files changed, 129 insertions(+), 49 deletions(-) delete mode 100644 MasstransferCommon/Model/Constant/Constants.cs create mode 100644 MasstransferExporter/DataExporter/Model/DeviceParams.cs diff --git a/MasstransferCommon/Model/Constant/Constants.cs b/MasstransferCommon/Model/Constant/Constants.cs deleted file mode 100644 index 14b036a..0000000 --- a/MasstransferCommon/Model/Constant/Constants.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace MasstransferCommon.Model.Constant; - -public class Constants -{ - public const string SN = "G5506771257"; -} \ No newline at end of file diff --git a/MasstransferCommon/Model/Constant/Topics.cs b/MasstransferCommon/Model/Constant/Topics.cs index d8b2bd2..df8a162 100644 --- a/MasstransferCommon/Model/Constant/Topics.cs +++ b/MasstransferCommon/Model/Constant/Topics.cs @@ -1,128 +1,128 @@ -namespace MasstransferCommon.Model.Constant; +using MasstransferSecurity.Utils; + +namespace MasstransferCommon.Model.Constant; /// /// 这里保存所有的 Mqtt Topic /// topic 的基本结构构成 -/// up/{sn}/{cmd}/{version} +/// up/{DeviceInfoUtil.GetDeviceSN()}/{cmd}/{version} /// /// public static class Topics { // ReSharper disable once InconsistentNaming - private const string SN = "G5506771257"; - - private const string Version = "1.0.0"; + private static string Version = "1.0.0"; /// /// 上报生产数据 /// - public const string ReportProductRecord = $"up/{SN}/100/{Version}"; + public static string ReportProductRecord = $"up/{DeviceInfoUtil.GetDeviceSN()}/100/{Version}"; /// /// 图片上传 /// - public const string ImageUpload = $"up/{SN}/102/{Version}"; + public static string ImageUpload = $"up/{DeviceInfoUtil.GetDeviceSN()}/102/{Version}"; /// /// 查询图片 /// - public const string QueryImage = $"down/{SN}/103/{Version}"; + public static string QueryImage = $"down/{DeviceInfoUtil.GetDeviceSN()}/103/{Version}"; /// /// 坐标文件上传 /// - public const string CoordinateUpload = $"up/{SN}/104/{Version}"; + public static string CoordinateUpload = $"up/{DeviceInfoUtil.GetDeviceSN()}/104/{Version}"; /// /// 查询坐标文件 /// - public const string QueryCoordinate = $"down/{SN}/105/{Version}"; + public static string QueryCoordinate = $"down/{DeviceInfoUtil.GetDeviceSN()}/105/{Version}"; /// /// 上报用户操作数据 /// - public const string ReportOperationLog = $"up/{SN}/200/{Version}"; + public static string ReportOperationLog = $"up/{DeviceInfoUtil.GetDeviceSN()}/200/{Version}"; /// /// 上传系统日志文件 /// - public const string UploadLogFile = $"up/{SN}/201/{Version}"; + public static string UploadLogFile = $"up/{DeviceInfoUtil.GetDeviceSN()}/201/{Version}"; /// /// 查询系统日志文件 /// - public const string QueryLogFile = $"down/{SN}/202/{Version}"; + public static string QueryLogFile = $"down/{DeviceInfoUtil.GetDeviceSN()}/202/{Version}"; /// /// 上报故障信息 /// - public const string ReportFaultRecord = $"up/{SN}/203/{Version}"; + public static string ReportFaultRecord = $"up/{DeviceInfoUtil.GetDeviceSN()}/203/{Version}"; /// /// 上报配置数据 /// - public const string ReportConfigData = $"up/{SN}/400/{Version}"; + public static string ReportConfigData = $"up/{DeviceInfoUtil.GetDeviceSN()}/400/{Version}"; /// /// 下发配置数据 /// - public const string DownloadConfigData = $"down/{SN}/401/{Version}"; + public static string DownloadConfigData = $"down/{DeviceInfoUtil.GetDeviceSN()}/401/{Version}"; /// /// 下发远程控制指令 /// - public const string RemoteControl = $"down/{SN}/402/{Version}"; + public static string RemoteControl = $"down/{DeviceInfoUtil.GetDeviceSN()}/402/{Version}"; /// /// 上报配置字典数据 /// - public const string ReportConfigDictionary = $"up/{SN}/403/{Version}"; + public static string ReportConfigDictionary = $"up/{DeviceInfoUtil.GetDeviceSN()}/403/{Version}"; /// /// 下发OTA包 /// // ReSharper disable once InconsistentNaming - public const string IssuedOTAPackage = $"down/{SN}/500/{Version}"; + public static string IssuedOTAPackage = $"down/{DeviceInfoUtil.GetDeviceSN()}/500/{Version}"; /// /// 查询OTA信息 /// // ReSharper disable once InconsistentNaming - public const string QueryOTA = $"up/{SN}/501/{Version}"; + public static string QueryOTA = $"up/{DeviceInfoUtil.GetDeviceSN()}/501/{Version}"; /// /// OTA 更新反馈 /// // ReSharper disable once InconsistentNaming - public const string OTAUpgradeFeedback = $"up/{SN}/502/{Version}"; + public static string OTAUpgradeFeedback = $"up/{DeviceInfoUtil.GetDeviceSN()}/502/{Version}"; /// /// 更新证书事件 /// - public const string UpdateLicenseEvent = $"up/{SN}/503/{Version}"; + public static string UpdateLicenseEvent = $"up/{DeviceInfoUtil.GetDeviceSN()}/503/{Version}"; /// /// 更新证书事件反馈 /// - public const string UpdateLicenseEventFeedback = $"up/{SN}/505/{Version}"; + public static string UpdateLicenseEventFeedback = $"up/{DeviceInfoUtil.GetDeviceSN()}/505/{Version}"; /// /// 上行心跳信息 /// - public const string HeartBeat = $"up/{SN}/600/{Version}"; + public static string HeartBeat = $"up/{DeviceInfoUtil.GetDeviceSN()}/600/{Version}"; /// /// 上行系统状态信息 /// - public const string ReportSystemStat = $"up/{SN}/601/{Version}"; - + public static string ReportSystemStat = $"up/{DeviceInfoUtil.GetDeviceSN()}/601/{Version}"; + /// /// 上行系统版本信息 /// - public const string ReportSystemVersion = $"up/{SN}/602/{Version}"; + public static string ReportSystemVersion = $"up/{DeviceInfoUtil.GetDeviceSN()}/602/{Version}"; } \ No newline at end of file diff --git a/MasstransferCommon/Utils/DeviceInfoUtil.cs b/MasstransferCommon/Utils/DeviceInfoUtil.cs index 18c4a84..c74ff76 100644 --- a/MasstransferCommon/Utils/DeviceInfoUtil.cs +++ b/MasstransferCommon/Utils/DeviceInfoUtil.cs @@ -1,6 +1,7 @@ using System.Management; using System.Security.Cryptography; using System.Text; +using MasstransferCommon.Utils; namespace MasstransferSecurity.Utils; @@ -9,6 +10,8 @@ namespace MasstransferSecurity.Utils; /// public sealed class DeviceInfoUtil { + private const string KeyPath = @"Software\Masstransfer\Device"; + public static string GenerateUniqueID() { var cpuId = GetCpuId(); @@ -87,4 +90,13 @@ public sealed class DeviceInfoUtil return builder.ToString(); } + + /// + /// 获取设备的序列号 + /// + /// + public static string GetDeviceSN() + { + return (string)RegistryHelper.ReadValue(KeyPath, "SerialNumber"); + } } \ No newline at end of file diff --git a/MasstransferExporter/DataExporter/CoordinateService.cs b/MasstransferExporter/DataExporter/CoordinateService.cs index 925209c..c037633 100644 --- a/MasstransferExporter/DataExporter/CoordinateService.cs +++ b/MasstransferExporter/DataExporter/CoordinateService.cs @@ -5,6 +5,7 @@ using MasstransferCommunicate.Minio; using MasstransferCommunicate.Mqtt.Client; using MasstransferExporter.DataExporter.Model; using MasstransferInfrastructure.Database.Sqlite; +using MasstransferSecurity.Utils; using Masuit.Tools; namespace MasstransferExporter.DataExporter; @@ -59,7 +60,7 @@ public class CoordinateService : Instant await File.WriteAllTextAsync(path, substrate.SubstrateMarkPointJson); - var fileName = $"{Constants.SN}/{batchCode}/{substrateCode}/MARK/mark.json"; + var fileName = $"{DeviceInfoUtil.GetDeviceSN()}/{batchCode}/{substrateCode}/MARK/mark.json"; await Minio.UploadFileAsync("coordinate", fileName, path); @@ -106,7 +107,7 @@ public class CoordinateService : Instant { await File.WriteAllTextAsync(path, wafer.CoordinateJson); - var fileName = $"{Constants.SN}/{wafer.BatchCode}/{wafer.WaferCode}/WAFER/wafer.json"; + var fileName = $"{DeviceInfoUtil.GetDeviceSN()}/{wafer.BatchCode}/{wafer.WaferCode}/WAFER/wafer.json"; await Minio.UploadFileAsync("coordinate", fileName, path); @@ -152,7 +153,7 @@ public class CoordinateService : Instant { await File.WriteAllTextAsync(path, substrate.CoordinateJson); - var fileName = $"{Constants.SN}/{substrate.BatchNo}/{substrate.SubstrateCode}/PCB/pcb.json"; + var fileName = $"{DeviceInfoUtil.GetDeviceSN()}/{substrate.BatchNo}/{substrate.SubstrateCode}/PCB/pcb.json"; await Minio.UploadFileAsync("coordinate", fileName, path); @@ -196,7 +197,8 @@ public class CoordinateService : Instant { await File.WriteAllTextAsync(path, record.ToJsonString()); - var fileName = $"{Constants.SN}/{record.BatchNo}/{record.SubstrateCode}/ROUTE/{record.Id}.json"; + var fileName = + $"{DeviceInfoUtil.GetDeviceSN()}/{record.BatchNo}/{record.SubstrateCode}/ROUTE/{record.Id}.json"; await Minio.UploadFileAsync("coordinate", fileName, path); diff --git a/MasstransferExporter/DataExporter/Model/DeviceParams.cs b/MasstransferExporter/DataExporter/Model/DeviceParams.cs new file mode 100644 index 0000000..46f9a6b --- /dev/null +++ b/MasstransferExporter/DataExporter/Model/DeviceParams.cs @@ -0,0 +1,45 @@ +using System.ComponentModel; +using MasstransferCommon.Model.Entity; +using SQLite; + +namespace MasstransferExporter.DataExporter.Model; + +[Table("device"), Description("设备信息")] +public class DeviceParams : Entity +{ + [Column("device_name"), Description("设备名称")] + public string? DeviceName { get; set; } + + [Column("device_type"), Description("设备型号")] + public string? DeviceType { get; set; } + + [Column("weight"), Description("设备重量(kg)")] + public int Weight { get; set; } + + [Column("input_pressure"), Description("输入压力(MPa)")] + public string? InputPressure { get; set; } + + [Column("length"), Description("设备长度(mm)")] + public int Length { get; set; } + + [Column("width"), Description("设备宽度(mm)")] + public int Width { get; set; } + + [Column("height"), Description("设备高度(mm)")] + public int Height { get; set; } + + [Column("power"), Description("设备功率(W)")] + public int Power { get; set; } + + [Column("voltage"), Description("设备电压(V)")] + public int Voltage { get; set; } + + [Column("current"), Description("设备电流(A)")] + public int Current { get; set; } + + [Column("date_of_manufacture"), Description("设备生产日期")] + public string? DateOfManufacture { get; set; } + + [Column("serial_number"), Description("设备序列号")] + public string? SerialNumber { get; set; } +} \ No newline at end of file diff --git a/MasstransferExporter/DataExporter/Model/SystemParamsDTO.cs b/MasstransferExporter/DataExporter/Model/SystemParamsDTO.cs index bf419a3..2c36b47 100644 --- a/MasstransferExporter/DataExporter/Model/SystemParamsDTO.cs +++ b/MasstransferExporter/DataExporter/Model/SystemParamsDTO.cs @@ -24,4 +24,6 @@ public class SystemParamsDTO public AxisVariables? AxisVariables { get; set; } public AxisPositionParams? AxisPositionParams { get; set; } + + public DeviceParams? DeviceParams { get; set; } } \ No newline at end of file diff --git a/MasstransferExporter/DataExporter/SystemParamsService.cs b/MasstransferExporter/DataExporter/SystemParamsService.cs index aa26a8e..b24cac0 100644 --- a/MasstransferExporter/DataExporter/SystemParamsService.cs +++ b/MasstransferExporter/DataExporter/SystemParamsService.cs @@ -148,7 +148,8 @@ public class SystemParamsService ScannerParams = GetScannerParams(), SystemParams = GetSystemParams(), AxisVariables = GetAxisVariables(), - AxisPositionParams = GetAxisPositionParams() + AxisPositionParams = GetAxisPositionParams(), + DeviceParams = GetDeviceParams() }; var systemParams = new ConfigData() @@ -235,4 +236,10 @@ public class SystemParamsService return Db.Query("select * from axis_position_params limit 1") .FirstOrDefault(); } + + private static DeviceParams? GetDeviceParams() + { + return Db.Query("select * from device limit 1") + .FirstOrDefault(); + } } \ No newline at end of file diff --git a/MasstransferExporter/ImageExporter/ImageService.cs b/MasstransferExporter/ImageExporter/ImageService.cs index 338e527..c79b668 100644 --- a/MasstransferExporter/ImageExporter/ImageService.cs +++ b/MasstransferExporter/ImageExporter/ImageService.cs @@ -7,6 +7,7 @@ using MasstransferCommunicate.Mqtt.Client; using MasstransferExporter.DataExporter.Model; using MasstransferExporter.ImageExporter.Model; using MasstransferInfrastructure.Database.Sqlite; +using MasstransferSecurity.Utils; namespace MasstransferExporter.ImageExporter; @@ -89,7 +90,8 @@ public class ImageService : Instant } // 上传文件 - var fileName = $"{Constants.SN}/{codeDir.Name}/{batchDir.Name}/{picType.ToUpper()}/{name}"; + var fileName = + $"{DeviceInfoUtil.GetDeviceSN()}/{codeDir.Name}/{batchDir.Name}/{picType.ToUpper()}/{name}"; await Minio.UploadFileAsync(bucket, fileName, path); @@ -233,7 +235,7 @@ public class ImageService : Instant foreach (var image in images) { var fileName = - $"{Constants.SN}/{codeDir.Name}/{batchCode.Name}/{type.Name.ToUpper()}/{image.Name}"; + $"{DeviceInfoUtil.GetDeviceSN()}/{codeDir.Name}/{batchCode.Name}/{type.Name.ToUpper()}/{image.Name}"; var path = image.FullName; diff --git a/MasstransferExporter/LogExporter/LogFileExporter.cs b/MasstransferExporter/LogExporter/LogFileExporter.cs index 0c4c851..9412e35 100644 --- a/MasstransferExporter/LogExporter/LogFileExporter.cs +++ b/MasstransferExporter/LogExporter/LogFileExporter.cs @@ -7,6 +7,7 @@ using MasstransferCommunicate.Minio; using MasstransferCommunicate.Mqtt.Client; using MasstransferExporter.LogExporter.Model; using MasstransferInfrastructure.Database.Sqlite; +using MasstransferSecurity.Utils; namespace MasstransferExporter.LogExporter; @@ -34,8 +35,7 @@ public class LogFileExporter : Instant { if (dto == null) return; - var path = - "C:\\workspace\\code_repos\\haiju\\MasstransferHost\\MasstransferDesktop\\bin\\Debug\\net7.0"; + var path = GetBaseDirectory(); var directory = new DirectoryInfo(path + "\\logs"); @@ -46,7 +46,7 @@ public class LogFileExporter : Instant foreach (var file in files) { var fileName = - $"{Constants.SN}/{TimeUtil.FormatTime(file.CreationTime, "yyyyMMdd")}/{dto.Module}/{file.Name}"; + $"{DeviceInfoUtil.GetDeviceSN()}/{TimeUtil.FormatTime(file.CreationTime, "yyyyMMdd")}/{dto.Module}/{file.Name}"; await Minio.UploadFileAsync("log", fileName, file.FullName); } @@ -59,8 +59,7 @@ public class LogFileExporter : Instant var yesterday = DateTime.Today.AddDays(-1); - var path = - "C:\\workspace\\code_repos\\haiju\\MasstransferHost\\MasstransferDesktop\\bin\\Debug\\net7.0"; + var path = GetBaseDirectory(); foreach (var level in Levels) { @@ -72,7 +71,8 @@ public class LogFileExporter : Instant foreach (var file in files) { - var fileName = $"{Constants.SN}/{TimeUtil.FormatTime(yesterday, "yyyyMMdd")}/{level}/{file.Name}"; + var fileName = + $"{DeviceInfoUtil.GetDeviceSN()}/{TimeUtil.FormatTime(yesterday, "yyyyMMdd")}/{level}/{file.Name}"; await Minio.UploadFileAsync("log", fileName, file.FullName); } @@ -128,4 +128,20 @@ public class LogFileExporter : Instant { EventBus.AddEventHandler(EventType.StartUp, ListenQueryLogEvent); } + + /// + /// 获取根目录路径 + /// + /// + private static string GetBaseDirectory() + { + var logParams = GetLogParams(); + return logParams?.Path ?? ""; + } + + private static LogParams? GetLogParams() + { + return Helper.Query("select * from log_params limit 1") + .FirstOrDefault(); + } } \ No newline at end of file diff --git a/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs b/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs index df5a7c3..774e57e 100644 --- a/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs +++ b/MasstransferInfrastructure/Mqtt/Client/MqttClient.cs @@ -1,7 +1,7 @@ -using MasstransferCommon.Model.Constant; -using MasstransferCommon.Utils; +using MasstransferCommon.Utils; using MasstransferCommunicate.Mqtt.Model; using MasstransferInfrastructure.Mqtt.Model; +using MasstransferSecurity.Utils; using Masuit.Tools; using MQTTnet; using MQTTnet.Client; @@ -28,7 +28,7 @@ internal class MqttClient { return new MqttClientOptionsBuilder() .WithTcpServer(options.ServerAddress, options.Port) - .WithClientId(Constants.SN) + .WithClientId(DeviceInfoUtil.GetDeviceSN()) .WithKeepAlivePeriod(TimeSpan.FromSeconds(60)) .WithCleanStart(true) .WithWillRetain(false)