177 lines
7.1 KiB
C#
177 lines
7.1 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace PetWashControl.Services;
|
|
|
|
public class ConfigurationService
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = true
|
|
};
|
|
|
|
private static readonly string ConfigFilePath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
|
|
|
public string ApiBaseUrl { get; set; } = "http://101.132.182.216:8080/";
|
|
public string MqttBrokerHost { get; set; } = "101.132.182.216";
|
|
public int MqttBrokerPort { get; set; } = 1883;
|
|
public string MqttApiKey { get; set; } = "dc240ab5ec";
|
|
public string MqttId { get; set; } = "13064";
|
|
public string MqttClientId { get; set; } = "PetWashControl";
|
|
public string ModbusIpAddress { get; set; } = "127.0.0.1";
|
|
public int ModbusPort { get; set; } = 502;
|
|
public byte ModbusSlaveId { get; set; } = 1;
|
|
public int ModbusConnectTimeoutMs { get; set; } = 5000;
|
|
public int ModbusReadTimeoutMs { get; set; } = 3000;
|
|
public int PaymentCheckIntervalSeconds { get; set; } = 2;
|
|
public int WashSimulationSeconds { get; set; } = 10;
|
|
public int FirstSprayWaterTime { get; set; } = 2;
|
|
public int AfterShampoo1SprayTime { get; set; } = 2;
|
|
public int AfterShampoo2SprayTime { get; set; } = 2;
|
|
public int AfterShampoo3SprayTime { get; set; } = 2;
|
|
public int SprayShampoo1Time { get; set; } = 1;
|
|
public int SprayShampoo2Time { get; set; } = 1;
|
|
public int SprayShampoo3Time { get; set; } = 1;
|
|
public int ColdAirTime { get; set; } = 2;
|
|
public int HotAirTime { get; set; } = 5;
|
|
public int UvSterilizationTime { get; set; } = 3;
|
|
public string AdminUsername { get; set; } = "admin";
|
|
public string AdminPassword { get; set; } = "123456";
|
|
public int AdminMaxFailedAttempts { get; set; } = 5;
|
|
public int AdminLockoutMinutes { get; set; } = 15;
|
|
|
|
public ConfigurationService()
|
|
{
|
|
Load();
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
var fileContent = JsonSerializer.Serialize(ToModel(), JsonOptions);
|
|
File.WriteAllText(ConfigFilePath, fileContent);
|
|
}
|
|
|
|
private void Load()
|
|
{
|
|
if (!File.Exists(ConfigFilePath))
|
|
{
|
|
Save();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var fileContent = File.ReadAllText(ConfigFilePath);
|
|
var model = JsonSerializer.Deserialize<ConfigurationModel>(fileContent, JsonOptions);
|
|
if (model == null)
|
|
{
|
|
Save();
|
|
return;
|
|
}
|
|
|
|
ApplyModel(model);
|
|
}
|
|
catch
|
|
{
|
|
Save();
|
|
}
|
|
}
|
|
|
|
private void ApplyModel(ConfigurationModel model)
|
|
{
|
|
ApiBaseUrl = model.ApiBaseUrl;
|
|
MqttBrokerHost = model.MqttBrokerHost;
|
|
MqttBrokerPort = model.MqttBrokerPort;
|
|
MqttApiKey = model.MqttApiKey;
|
|
MqttId = model.MqttId;
|
|
MqttClientId = model.MqttClientId;
|
|
ModbusIpAddress = model.ModbusIpAddress;
|
|
ModbusPort = model.ModbusPort;
|
|
ModbusSlaveId = model.ModbusSlaveId;
|
|
ModbusConnectTimeoutMs = model.ModbusConnectTimeoutMs;
|
|
ModbusReadTimeoutMs = model.ModbusReadTimeoutMs;
|
|
PaymentCheckIntervalSeconds = model.PaymentCheckIntervalSeconds;
|
|
WashSimulationSeconds = model.WashSimulationSeconds;
|
|
FirstSprayWaterTime = model.FirstSprayWaterTime;
|
|
AfterShampoo1SprayTime = model.AfterShampoo1SprayTime;
|
|
AfterShampoo2SprayTime = model.AfterShampoo2SprayTime;
|
|
AfterShampoo3SprayTime = model.AfterShampoo3SprayTime;
|
|
SprayShampoo1Time = model.SprayShampoo1Time;
|
|
SprayShampoo2Time = model.SprayShampoo2Time;
|
|
SprayShampoo3Time = model.SprayShampoo3Time;
|
|
ColdAirTime = model.ColdAirTime;
|
|
HotAirTime = model.HotAirTime;
|
|
UvSterilizationTime = model.UvSterilizationTime;
|
|
AdminUsername = model.AdminUsername;
|
|
AdminPassword = model.AdminPassword;
|
|
AdminMaxFailedAttempts = Math.Max(1, model.AdminMaxFailedAttempts);
|
|
AdminLockoutMinutes = Math.Max(1, model.AdminLockoutMinutes);
|
|
}
|
|
|
|
private ConfigurationModel ToModel()
|
|
{
|
|
return new ConfigurationModel
|
|
{
|
|
ApiBaseUrl = ApiBaseUrl,
|
|
MqttBrokerHost = MqttBrokerHost,
|
|
MqttBrokerPort = MqttBrokerPort,
|
|
MqttApiKey = MqttApiKey,
|
|
MqttId = MqttId,
|
|
MqttClientId = MqttClientId,
|
|
ModbusIpAddress = ModbusIpAddress,
|
|
ModbusPort = ModbusPort,
|
|
ModbusSlaveId = ModbusSlaveId,
|
|
ModbusConnectTimeoutMs = ModbusConnectTimeoutMs,
|
|
ModbusReadTimeoutMs = ModbusReadTimeoutMs,
|
|
PaymentCheckIntervalSeconds = PaymentCheckIntervalSeconds,
|
|
WashSimulationSeconds = WashSimulationSeconds,
|
|
FirstSprayWaterTime = FirstSprayWaterTime,
|
|
AfterShampoo1SprayTime = AfterShampoo1SprayTime,
|
|
AfterShampoo2SprayTime = AfterShampoo2SprayTime,
|
|
AfterShampoo3SprayTime = AfterShampoo3SprayTime,
|
|
SprayShampoo1Time = SprayShampoo1Time,
|
|
SprayShampoo2Time = SprayShampoo2Time,
|
|
SprayShampoo3Time = SprayShampoo3Time,
|
|
ColdAirTime = ColdAirTime,
|
|
HotAirTime = HotAirTime,
|
|
UvSterilizationTime = UvSterilizationTime,
|
|
AdminUsername = AdminUsername,
|
|
AdminPassword = AdminPassword,
|
|
AdminMaxFailedAttempts = Math.Max(1, AdminMaxFailedAttempts),
|
|
AdminLockoutMinutes = Math.Max(1, AdminLockoutMinutes)
|
|
};
|
|
}
|
|
|
|
private sealed class ConfigurationModel
|
|
{
|
|
public string ApiBaseUrl { get; set; } = "http://101.132.182.216:8080/";
|
|
public string MqttBrokerHost { get; set; } = "101.132.182.216";
|
|
public int MqttBrokerPort { get; set; } = 1883;
|
|
public string MqttApiKey { get; set; } = "dc240ab5ec";
|
|
public string MqttId { get; set; } = "13064";
|
|
public string MqttClientId { get; set; } = "PetWashControl";
|
|
public string ModbusIpAddress { get; set; } = "127.0.0.1";
|
|
public int ModbusPort { get; set; } = 502;
|
|
public byte ModbusSlaveId { get; set; } = 1;
|
|
public int ModbusConnectTimeoutMs { get; set; } = 5000;
|
|
public int ModbusReadTimeoutMs { get; set; } = 3000;
|
|
public int PaymentCheckIntervalSeconds { get; set; } = 2;
|
|
public int WashSimulationSeconds { get; set; } = 10;
|
|
public int FirstSprayWaterTime { get; set; } = 2;
|
|
public int AfterShampoo1SprayTime { get; set; } = 2;
|
|
public int AfterShampoo2SprayTime { get; set; } = 2;
|
|
public int AfterShampoo3SprayTime { get; set; } = 2;
|
|
public int SprayShampoo1Time { get; set; } = 1;
|
|
public int SprayShampoo2Time { get; set; } = 1;
|
|
public int SprayShampoo3Time { get; set; } = 1;
|
|
public int ColdAirTime { get; set; } = 2;
|
|
public int HotAirTime { get; set; } = 5;
|
|
public int UvSterilizationTime { get; set; } = 3;
|
|
public string AdminUsername { get; set; } = "admin";
|
|
public string AdminPassword { get; set; } = "123456";
|
|
public int AdminMaxFailedAttempts { get; set; } = 5;
|
|
public int AdminLockoutMinutes { get; set; } = 15;
|
|
}
|
|
}
|