2025-07-06 11:09:57 +08:00
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
using PMSWPF.Config;
|
|
|
|
|
using PMSWPF.Data;
|
|
|
|
|
using PMSWPF.Helper;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-07-07 21:24:45 +08:00
|
|
|
using PMSWPF.Services;
|
2025-07-06 11:09:57 +08:00
|
|
|
|
2025-06-23 13:42:02 +08:00
|
|
|
namespace PMSWPF.ViewModels;
|
|
|
|
|
|
2025-07-06 11:09:57 +08:00
|
|
|
public partial class SettingViewModel : ViewModelBase
|
2025-06-23 13:42:02 +08:00
|
|
|
{
|
2025-07-06 11:09:57 +08:00
|
|
|
private ConnectionSettings _connectionSettings;
|
2025-07-07 21:24:45 +08:00
|
|
|
private readonly S7BackgroundService _s7BackgroundService;
|
|
|
|
|
private readonly MqttBackgroundService _mqttBackgroundService;
|
2025-07-08 17:33:20 +08:00
|
|
|
private readonly OpcUaBackgroundService _opcUaBackgroundService;
|
2025-07-06 11:09:57 +08:00
|
|
|
|
2025-07-08 17:33:20 +08:00
|
|
|
public SettingViewModel(S7BackgroundService s7BackgroundService, MqttBackgroundService mqttBackgroundService, OpcUaBackgroundService opcUaBackgroundService)
|
2025-07-06 11:09:57 +08:00
|
|
|
{
|
|
|
|
|
_connectionSettings = ConnectionSettings.Load();
|
|
|
|
|
AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
|
2025-07-07 21:24:45 +08:00
|
|
|
_s7BackgroundService = s7BackgroundService;
|
|
|
|
|
_mqttBackgroundService = mqttBackgroundService;
|
2025-07-08 17:33:20 +08:00
|
|
|
_opcUaBackgroundService = opcUaBackgroundService;
|
2025-07-12 12:55:46 +08:00
|
|
|
Themes = new List<string> { "浅色", "深色", "跟随系统" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> Themes { get; }
|
|
|
|
|
|
|
|
|
|
public string SelectedTheme
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.Theme;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.Theme != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.Theme = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
ThemeHelper.ApplyTheme(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-06 11:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> AvailableDbTypes { get; set; }
|
|
|
|
|
|
|
|
|
|
public string SelectedDbType
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.DbType;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.DbType != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.DbType = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Server
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.Server;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.Server != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.Server = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Port
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.Port;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.Port != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.Port = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string UserId
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.UserId;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.UserId != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.UserId = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Password
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.Password;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.Password != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.Password = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Database
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.Database;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.Database != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.Database = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:15:27 +08:00
|
|
|
public bool EnableS7Service
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.EnableS7Service;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.EnableS7Service != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.EnableS7Service = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
2025-07-07 21:24:45 +08:00
|
|
|
if (value)
|
|
|
|
|
{
|
2025-07-16 11:12:03 +08:00
|
|
|
// _s7BackgroundService.StartService();
|
2025-07-07 21:24:45 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-16 11:12:03 +08:00
|
|
|
// _s7BackgroundService.StopService();
|
2025-07-07 21:24:45 +08:00
|
|
|
}
|
2025-07-07 21:15:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool EnableMqttService
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.EnableMqttService;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.EnableMqttService != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.EnableMqttService = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
2025-07-07 21:24:45 +08:00
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
_mqttBackgroundService.StartService();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_mqttBackgroundService.StopService();
|
|
|
|
|
}
|
2025-07-07 21:15:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 17:33:20 +08:00
|
|
|
public bool EnableOpcUaService
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.EnableOpcUaService;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.EnableOpcUaService != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.EnableOpcUaService = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
_connectionSettings.Save();
|
2025-07-16 13:15:29 +08:00
|
|
|
// if (value)
|
|
|
|
|
// {
|
|
|
|
|
// _opcUaBackgroundService.StartService();
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// _opcUaBackgroundService.StopService();
|
|
|
|
|
// }
|
2025-07-08 17:33:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 20:48:14 +08:00
|
|
|
public bool MinimizeToTrayOnClose
|
|
|
|
|
{
|
|
|
|
|
get => _connectionSettings.MinimizeToTrayOnClose;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_connectionSettings.MinimizeToTrayOnClose != value)
|
|
|
|
|
{
|
|
|
|
|
_connectionSettings.MinimizeToTrayOnClose = value;
|
|
|
|
|
OnPropertyChanged(nameof(MinimizeToTrayOnClose));
|
|
|
|
|
_connectionSettings.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:09:57 +08:00
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task TestConnection()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
{
|
|
|
|
|
await db.Ado.OpenAsync();
|
2025-07-06 15:36:53 +08:00
|
|
|
NotificationHelper.ShowSuccess("连接成功!");
|
2025-07-06 11:09:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
NotificationHelper.ShowError($"连接失败:{ex.Message}", ex);
|
2025-07-06 11:09:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-12 12:55:46 +08:00
|
|
|
}
|