111 lines
3.8 KiB
C#
111 lines
3.8 KiB
C#
|
|
using Newtonsoft.Json;
|
|||
|
|
using Newtonsoft.Json.Linq;
|
|||
|
|
using System.IO;
|
|||
|
|
using YY.Admin.Core.Util;
|
|||
|
|
|
|||
|
|
namespace YY.Admin.Services.Service.EquipmentDb;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设备库(SQL Server 中间库)本地连接配置。
|
|||
|
|
/// </summary>
|
|||
|
|
public static class EquipmentDbSettingsStore
|
|||
|
|
{
|
|||
|
|
public class EquipmentDbSettings
|
|||
|
|
{
|
|||
|
|
public bool Enabled { get; set; } = true;
|
|||
|
|
public string DeviceId { get; set; } = string.Empty;
|
|||
|
|
public string ServerHost { get; set; } = "127.0.0.1";
|
|||
|
|
public int ServerPort { get; set; } = 1433;
|
|||
|
|
public string DbName { get; set; } = "MES_ShareDB";
|
|||
|
|
public string Username { get; set; } = "sa";
|
|||
|
|
public string Password { get; set; } = string.Empty;
|
|||
|
|
public int ConnectTimeoutSeconds { get; set; } = 15;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GetUserConfigPath()
|
|||
|
|
{
|
|||
|
|
var dir = AppWritablePaths.EnsureDirectoryExists(AppWritablePaths.ConfigurationDirectory);
|
|||
|
|
return Path.Combine(dir, "equipment-db.json");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static EquipmentDbSettings Load()
|
|||
|
|
{
|
|||
|
|
var path = GetUserConfigPath();
|
|||
|
|
if (!File.Exists(path))
|
|||
|
|
{
|
|||
|
|
return new EquipmentDbSettings
|
|||
|
|
{
|
|||
|
|
DeviceId = Environment.MachineName
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var settings = JsonConvert.DeserializeObject<EquipmentDbSettings>(File.ReadAllText(path))
|
|||
|
|
?? new EquipmentDbSettings();
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.DeviceId))
|
|||
|
|
{
|
|||
|
|
settings.DeviceId = Environment.MachineName;
|
|||
|
|
}
|
|||
|
|
return settings;
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
return new EquipmentDbSettings { DeviceId = Environment.MachineName };
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void Save(EquipmentDbSettings settings)
|
|||
|
|
{
|
|||
|
|
if (settings == null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(settings));
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.DeviceId))
|
|||
|
|
{
|
|||
|
|
settings.DeviceId = Environment.MachineName;
|
|||
|
|
}
|
|||
|
|
var path = GetUserConfigPath();
|
|||
|
|
File.WriteAllText(path, JsonConvert.SerializeObject(settings, Formatting.Indented));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string BuildConnectionString(EquipmentDbSettings s)
|
|||
|
|
{
|
|||
|
|
var timeout = s.ConnectTimeoutSeconds > 0 ? s.ConnectTimeoutSeconds : 15;
|
|||
|
|
return $"Server={s.ServerHost},{s.ServerPort};Database={s.DbName};User Id={s.Username};Password={s.Password};" +
|
|||
|
|
$"Encrypt=False;TrustServerCertificate=True;Connect Timeout={timeout};";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 从 bundled appsettings 的 EquipmentDb 节点合并默认值(若有)。
|
|||
|
|
/// </summary>
|
|||
|
|
public static EquipmentDbSettings LoadMergedWithAppSettings(JObject? root)
|
|||
|
|
{
|
|||
|
|
var settings = Load();
|
|||
|
|
if (root?["EquipmentDb"] is not JObject eq)
|
|||
|
|
{
|
|||
|
|
return settings;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.ServerHost) || settings.ServerHost == "127.0.0.1")
|
|||
|
|
{
|
|||
|
|
settings.ServerHost = eq.Value<string>("ServerHost") ?? settings.ServerHost;
|
|||
|
|
}
|
|||
|
|
if (settings.ServerPort == 1433 && eq.Value<int?>("ServerPort") is int p)
|
|||
|
|
{
|
|||
|
|
settings.ServerPort = p;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.DbName) || settings.DbName == "MES_ShareDB")
|
|||
|
|
{
|
|||
|
|
settings.DbName = eq.Value<string>("DbName") ?? settings.DbName;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.Username) || settings.Username == "sa")
|
|||
|
|
{
|
|||
|
|
settings.Username = eq.Value<string>("Username") ?? settings.Username;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrWhiteSpace(settings.Password))
|
|||
|
|
{
|
|||
|
|
settings.Password = eq.Value<string>("Password") ?? settings.Password;
|
|||
|
|
}
|
|||
|
|
return settings;
|
|||
|
|
}
|
|||
|
|
}
|