2026-04-28 10:23:58 +08:00
|
|
|
using YY.Admin.Helper;
|
2026-07-27 15:56:05 +08:00
|
|
|
using YY.Admin.Services.Service.EquipmentDb;
|
2026-04-28 10:23:58 +08:00
|
|
|
|
|
|
|
|
namespace YY.Admin.ViewModels.Dialogs
|
|
|
|
|
{
|
|
|
|
|
public class ServerSettingsDialogViewModel : BindableBase, IDialogAware
|
|
|
|
|
{
|
|
|
|
|
private const string DefaultWebSocketPath = "/websocket/scada-sync";
|
|
|
|
|
private string _loadedWebSocketUrl = string.Empty;
|
|
|
|
|
private string _loadedAutoWebSocketUrl = string.Empty;
|
|
|
|
|
public string Title => "服务器设置";
|
|
|
|
|
|
|
|
|
|
private string _ip = "127.0.0.1";
|
|
|
|
|
public string Ip
|
|
|
|
|
{
|
|
|
|
|
get => _ip;
|
|
|
|
|
set => SetProperty(ref _ip, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int _port = 8080;
|
|
|
|
|
public int Port
|
|
|
|
|
{
|
|
|
|
|
get => _port;
|
|
|
|
|
set => SetProperty(ref _port, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _webSocketUrl = string.Empty;
|
|
|
|
|
public string WebSocketUrl
|
|
|
|
|
{
|
|
|
|
|
get => _webSocketUrl;
|
|
|
|
|
set => SetProperty(ref _webSocketUrl, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _basePath = "/jeecg-boot";
|
|
|
|
|
public string BasePath
|
|
|
|
|
{
|
|
|
|
|
get => _basePath;
|
|
|
|
|
set => SetProperty(ref _basePath, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _errorMessage = string.Empty;
|
|
|
|
|
public string ErrorMessage
|
|
|
|
|
{
|
|
|
|
|
get => _errorMessage;
|
|
|
|
|
set => SetProperty(ref _errorMessage, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 15:28:20 +08:00
|
|
|
private bool _disconnectConnection;
|
|
|
|
|
public bool DisconnectConnection
|
|
|
|
|
{
|
|
|
|
|
get => _disconnectConnection;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _disconnectConnection, value))
|
|
|
|
|
{
|
|
|
|
|
RaisePropertyChanged(nameof(IsConnectionFieldsEnabled));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsConnectionFieldsEnabled => !DisconnectConnection;
|
|
|
|
|
|
2026-07-27 15:56:05 +08:00
|
|
|
private string _eqDeviceId = string.Empty;
|
|
|
|
|
public string EqDeviceId
|
|
|
|
|
{
|
|
|
|
|
get => _eqDeviceId;
|
|
|
|
|
set => SetProperty(ref _eqDeviceId, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _eqServerHost = "127.0.0.1";
|
|
|
|
|
public string EqServerHost
|
|
|
|
|
{
|
|
|
|
|
get => _eqServerHost;
|
|
|
|
|
set => SetProperty(ref _eqServerHost, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int _eqServerPort = 1433;
|
|
|
|
|
public int EqServerPort
|
|
|
|
|
{
|
|
|
|
|
get => _eqServerPort;
|
|
|
|
|
set => SetProperty(ref _eqServerPort, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _eqDbName = "MES_ShareDB";
|
|
|
|
|
public string EqDbName
|
|
|
|
|
{
|
|
|
|
|
get => _eqDbName;
|
|
|
|
|
set => SetProperty(ref _eqDbName, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _eqUsername = "sa";
|
|
|
|
|
public string EqUsername
|
|
|
|
|
{
|
|
|
|
|
get => _eqUsername;
|
|
|
|
|
set => SetProperty(ref _eqUsername, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _eqPassword = string.Empty;
|
|
|
|
|
public string EqPassword
|
|
|
|
|
{
|
|
|
|
|
get => _eqPassword;
|
|
|
|
|
set => SetProperty(ref _eqPassword, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:23:58 +08:00
|
|
|
public DelegateCommand SaveCommand { get; }
|
|
|
|
|
public DelegateCommand CancelCommand { get; }
|
|
|
|
|
public DialogCloseListener RequestClose { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ServerSettingsDialogViewModel()
|
|
|
|
|
{
|
|
|
|
|
SaveCommand = new DelegateCommand(Save);
|
|
|
|
|
CancelCommand = new DelegateCommand(() => RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCloseDialog() => true;
|
|
|
|
|
public void OnDialogClosed() { }
|
|
|
|
|
|
|
|
|
|
public void OnDialogOpened(IDialogParameters parameters)
|
|
|
|
|
{
|
|
|
|
|
var settings = ServerSettingsStore.Load();
|
|
|
|
|
_loadedAutoWebSocketUrl = ServerSettingsStore.BuildDefaultWebSocketUrl(settings.BaseScheme, settings.Ip, settings.Port, settings.BasePath, settings.WebSocketPath);
|
|
|
|
|
Ip = settings.Ip;
|
|
|
|
|
Port = settings.Port;
|
|
|
|
|
WebSocketUrl = string.IsNullOrWhiteSpace(settings.WebSocketUrl)
|
|
|
|
|
? _loadedAutoWebSocketUrl
|
|
|
|
|
: settings.WebSocketUrl;
|
|
|
|
|
_loadedWebSocketUrl = WebSocketUrl;
|
|
|
|
|
BasePath = string.IsNullOrWhiteSpace(settings.BasePath) ? "/jeecg-boot" : settings.BasePath;
|
2026-04-30 15:28:20 +08:00
|
|
|
DisconnectConnection = settings.DisconnectConnection;
|
2026-04-28 10:23:58 +08:00
|
|
|
ErrorMessage = string.Empty;
|
2026-07-27 15:56:05 +08:00
|
|
|
|
|
|
|
|
var eq = EquipmentDbSettingsStore.Load();
|
|
|
|
|
EqDeviceId = string.IsNullOrWhiteSpace(eq.DeviceId) ? Environment.MachineName : eq.DeviceId;
|
|
|
|
|
EqServerHost = eq.ServerHost;
|
|
|
|
|
EqServerPort = eq.ServerPort;
|
|
|
|
|
EqDbName = eq.DbName;
|
|
|
|
|
EqUsername = eq.Username;
|
|
|
|
|
EqPassword = eq.Password;
|
2026-04-28 10:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Save()
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = string.Empty;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Ip))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "IP 不能为空";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Port <= 0 || Port > 65535)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "端口号必须在 1-65535 之间";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-07-27 15:56:05 +08:00
|
|
|
if (EqServerPort <= 0 || EqServerPort > 65535)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "设备库端口号必须在 1-65535 之间";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-28 10:23:58 +08:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var basePath = BasePath?.Trim() ?? "/jeecg-boot";
|
|
|
|
|
var shouldAutoRebuildWs = string.IsNullOrWhiteSpace(WebSocketUrl)
|
|
|
|
|
|| string.Equals(WebSocketUrl.Trim(), _loadedAutoWebSocketUrl, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|| string.Equals(WebSocketUrl.Trim(), _loadedWebSocketUrl, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
var finalWsUrl = shouldAutoRebuildWs
|
|
|
|
|
? ServerSettingsStore.BuildDefaultWebSocketUrl("http", Ip.Trim(), Port, basePath, DefaultWebSocketPath)
|
|
|
|
|
: WebSocketUrl.Trim();
|
|
|
|
|
|
|
|
|
|
ServerSettingsStore.Save(new ServerSettingsStore.ServerSettingsModel
|
|
|
|
|
{
|
|
|
|
|
Ip = Ip.Trim(),
|
|
|
|
|
Port = Port,
|
|
|
|
|
BaseScheme = "http",
|
|
|
|
|
BasePath = basePath,
|
|
|
|
|
WebSocketUrl = finalWsUrl,
|
2026-04-30 15:28:20 +08:00
|
|
|
WebSocketPath = DefaultWebSocketPath,
|
|
|
|
|
DisconnectConnection = DisconnectConnection
|
2026-04-28 10:23:58 +08:00
|
|
|
});
|
2026-07-27 15:56:05 +08:00
|
|
|
|
|
|
|
|
EquipmentDbSettingsStore.Save(new EquipmentDbSettingsStore.EquipmentDbSettings
|
|
|
|
|
{
|
|
|
|
|
Enabled = true,
|
|
|
|
|
DeviceId = string.IsNullOrWhiteSpace(EqDeviceId) ? Environment.MachineName : EqDeviceId.Trim(),
|
|
|
|
|
ServerHost = EqServerHost?.Trim() ?? "127.0.0.1",
|
|
|
|
|
ServerPort = EqServerPort,
|
|
|
|
|
DbName = EqDbName?.Trim() ?? "MES_ShareDB",
|
|
|
|
|
Username = EqUsername?.Trim() ?? "sa",
|
|
|
|
|
Password = EqPassword ?? string.Empty,
|
|
|
|
|
ConnectTimeoutSeconds = 15
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-28 10:23:58 +08:00
|
|
|
RequestClose.Invoke(new DialogResult(ButtonResult.OK));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"保存失败:{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|