You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Configuration;
using Sln.Wcs.Model.Domain;
using Sln.Wcs.Repository.service;
using Sln.Wcs.Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Sln.Wcs.UI.ViewModels;
public partial class MainViewModel : ObservableObject
{
private readonly SerilogHelper _log;
private readonly IConfiguration _config;
private readonly IBaseDeviceInfoService _deviceInfoService;
private readonly ISqlSugarClient _db;
[ObservableProperty]
private string _appTitle = "基于多场景应用的 WCS 通用平台";
[ObservableProperty]
private string _appVersion = "V1.0.0";
[ObservableProperty]
private string _statusMessage = "系统就绪,点击'加载设备'获取设备列表";
[ObservableProperty]
private int _deviceCount;
[ObservableProperty]
private ObservableCollection<DeviceSummary> _devices = new();
public MainViewModel(
SerilogHelper log,
IConfiguration config,
IBaseDeviceInfoService deviceInfoService,
ISqlSugarClient db)
{
_log = log;
_config = config;
_deviceInfoService = deviceInfoService;
_db = db;
LoadDevicesCommand = new AsyncRelayCommand(LoadDevicesAsync);
}
public AsyncRelayCommand LoadDevicesCommand { get; }
private async System.Threading.Tasks.Task LoadDevicesAsync()
{
StatusMessage = "正在加载设备列表...";
try
{
var devices = await System.Threading.Tasks.Task.Run(() =>
_deviceInfoService.GetDeviceInfos(x => x.isFlag == 1).ToList());
Devices.Clear();
foreach (var d in devices)
{
Devices.Add(new DeviceSummary
{
DeviceCode = d.deviceCode,
DeviceName = d.deviceName ?? "",
DeviceType = ParseDeviceType(d.deviceType),
HostCode = d.hostCode,
IsFlag = d.isFlag == 1
});
}
DeviceCount = Devices.Count;
StatusMessage = $"已加载 {DeviceCount} 台设备";
_log.Info($"UI: 设备列表加载完成,共{DeviceCount}台");
}
catch (Exception ex)
{
StatusMessage = $"加载设备失败: {ex.Message}";
_log.Error($"UI: 设备列表加载失败 - {ex.Message}");
}
}
[RelayCommand]
private void RefreshStatus()
{
var apolloEnv = _config["apollo:Env"] ?? "未知";
var logPath = _config["logPath"] ?? "未配置";
StatusMessage = $"Apollo环境: {apolloEnv} | 日志路径: {logPath} | 已连接设备: {DeviceCount}";
}
private static string ParseDeviceType(int? deviceType) => deviceType switch
{
0 => "输送线",
1 => "AGV",
2 => "提升机",
_ => "未知"
};
}
public class DeviceSummary
{
public string DeviceCode { get; set; } = string.Empty;
public string DeviceName { get; set; } = string.Empty;
public string DeviceType { get; set; } = string.Empty;
public string HostCode { get; set; } = string.Empty;
public bool IsFlag { get; set; }
}