Files
WCS/Plugins/Wcs/Plugin.Cowain.Wcs/ViewModels/WcsRealStationViewModel.cs

207 lines
7.8 KiB
C#
Raw Normal View History

2026-03-02 09:13:29 +08:00
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Cowain.Base.ViewModels;
using Ke.Bee.Localization.Localizer.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Plugin.Cowain.Wcs.IServices;
using System.Collections.ObjectModel;
namespace Plugin.Cowain.Wcs.ViewModels;
public partial class WcsRealStationViewModel : PageViewModelBase
{
2026-03-02 10:56:30 +08:00
[ObservableProperty]
private double _cellWidthSize = 130;
[ObservableProperty]
private double _cellHeightSize = 60;
2026-03-02 09:13:29 +08:00
[ObservableProperty]
private ObservableCollection<StationViewModel>? _stations;
[ObservableProperty]
private ObservableCollection<TaskViewModel> _taskList = new();
[ObservableProperty]
private ObservableCollection<LayoutViewModel>? _layoutList;
[ObservableProperty]
private int _height = 400;
[ObservableProperty]
private int _width = 1000;
private readonly ILocalizer _l;
private IServiceScopeFactory _scopeFactory;
private bool _isProcessing;
2026-03-02 10:56:30 +08:00
2026-03-02 09:13:29 +08:00
public WcsRealStationViewModel(ILocalizer localizer, IServiceScopeFactory scopeFactory)
{
_l = localizer;
_scopeFactory = scopeFactory;
RefreshCommand.ExecuteAsync(1);
2026-03-02 10:56:30 +08:00
DispatcherTimer.Run(new Func<bool>(() =>
{
RefreshCommand.ExecuteAsync(1);
return true;
}), TimeSpan.FromSeconds(1), DispatcherPriority.Default);
2026-03-02 09:13:29 +08:00
}
[RelayCommand]
2026-03-02 10:56:30 +08:00
private async Task RefreshAsync(int pageIndex)
2026-03-02 09:13:29 +08:00
{
if (_isProcessing)
return;
_isProcessing = true;
try
{
using var scope = _scopeFactory.CreateScope();
var stationService = scope.ServiceProvider.GetRequiredService<IStationService>();
if (Stations == null)
{
var stationList = await stationService.GetAllAsync();
Stations = new ObservableCollection<StationViewModel>(stationList);
}
var taskService = scope.ServiceProvider.GetRequiredService<IRgvTaskService>();
var taskList = await taskService.GetAllAsync();
TaskList.Clear();
2026-03-02 10:56:30 +08:00
foreach (var item in taskList.OrderByDescending(x => x.Id).Take(1))
2026-03-02 09:13:29 +08:00
{
item.Actions = new(taskService.ActionToList(item.Action) ?? new List<RgvActionViewModel>());
item.SelectedAction = item.Actions.FirstOrDefault(x => x.Id == item.ExecuteAction);
TaskList?.Add(item);
}
var rgvService = scope.ServiceProvider.GetRequiredService<IRgvService>();
var rgvs = await rgvService.GetAllAsync();
var stations = await stationService.GetAllAsync();
if (LayoutList == null)
{
LayoutList = new ObservableCollection<LayoutViewModel>();
}
// 1. 处理站点StationViewModel
foreach (var station in stations)
{
var exist = LayoutList.OfType<StationViewModel>().FirstOrDefault(x => x.Id == station.Id);
if (exist != null)
{
exist.LayOutX = station.LayOutX;
exist.LayOutY = station.LayOutY;
exist.StationName = station.StationName;
exist.StationCode = station.StationCode;
exist.Status = station.Status;
exist.QrCode = station.QrCode;
2026-03-02 10:56:30 +08:00
exist.ProcessName = station.ProcessName;
2026-03-02 09:13:29 +08:00
// 可同步更多属性
}
else
{
LayoutList.Add(station);
}
}
// 移除已不存在的站点
for (int i = LayoutList.Count - 1; i >= 0; i--)
{
if (LayoutList[i] is StationViewModel svm && !stations.Any(x => x.Id == svm.Id))
{
LayoutList.RemoveAt(i);
}
}
// 2. 处理RGVRgvViewModel
foreach (var rgv in rgvs)
{
// 计算FromStationPoint
2026-03-02 10:56:30 +08:00
var fromStation = stations.FirstOrDefault(s => s.Id == rgv.FromStationId1);
2026-03-02 09:13:29 +08:00
if (fromStation != null)
{
2026-03-02 10:56:30 +08:00
double x = fromStation.LayOutX * CellWidthSize + CellWidthSize / 2;
double y = fromStation.LayOutY * CellHeightSize + CellHeightSize / 2;
rgv.FromStationPoint1 = new Avalonia.Point(x, y);
2026-03-02 09:13:29 +08:00
}
// 计算ToStationPoint
2026-03-02 10:56:30 +08:00
var toStation = stations.FirstOrDefault(s => s.Id == rgv.ToStationId1);
2026-03-02 09:13:29 +08:00
if (toStation != null)
{
2026-03-02 10:56:30 +08:00
double x = toStation.LayOutX * CellWidthSize + CellWidthSize / 2;
double y = toStation.LayOutY * CellHeightSize + CellHeightSize / 2;
rgv.ToStationPoint1 = new Avalonia.Point(x, y);
2026-03-02 09:13:29 +08:00
}
2026-03-02 10:56:30 +08:00
// 计算FromStationPoint
var fromStation2 = stations.FirstOrDefault(s => s.Id == rgv.FromStationId2);
if (fromStation2 != null)
2026-03-02 09:13:29 +08:00
{
2026-03-02 10:56:30 +08:00
double x = fromStation2.LayOutX * CellWidthSize + CellWidthSize / 2;
double y = fromStation2.LayOutY * CellHeightSize + CellHeightSize / 2;
rgv.FromStationPoint2 = new Avalonia.Point(x, y);
2026-03-02 09:13:29 +08:00
}
2026-03-02 10:56:30 +08:00
// 计算ToStationPoint
var toStation2 = stations.FirstOrDefault(s => s.Id == rgv.ToStationId2);
if (toStation2 != null)
{
double x = toStation2.LayOutX * CellWidthSize + CellWidthSize / 2;
double y = toStation2.LayOutY * CellHeightSize + CellHeightSize / 2;
rgv.ToStationPoint2 = new Avalonia.Point(x, y);
}
2026-03-02 09:13:29 +08:00
var exist = LayoutList.OfType<RgvViewModel>().FirstOrDefault(x => x.Id == rgv.Id);
if (exist != null)
{
exist.LayOutX = rgv.LayOutX;
exist.LayOutY = rgv.LayOutY;
exist.StationName = rgv.StationName;
exist.StationCode = rgv.StationCode;
2026-03-02 10:56:30 +08:00
exist.FromStationId1 = rgv.FromStationId1;
exist.ToStationId1 = rgv.ToStationId1;
exist.FromStationId2 = rgv.FromStationId2;
exist.ToStationId2 = rgv.ToStationId2;
exist.FromStationPoint1 = rgv.FromStationPoint1;
exist.ToStationPoint1 = rgv.ToStationPoint1;
2026-03-02 09:13:29 +08:00
}
else
{
LayoutList.Add(rgv);
}
}
// 移除已不存在的RGV
for (int i = LayoutList.Count - 1; i >= 0; i--)
{
if (LayoutList[i] is RgvViewModel rgvVm && !rgvs.Any(x => x.Id == rgvVm.Id))
{
LayoutList.RemoveAt(i);
}
}
double maxRow = LayoutList
.Select(e => e.LayOutX)
.DefaultIfEmpty(0)
.Max();
2026-03-02 10:56:30 +08:00
Width = (int)(maxRow + 1) * (int)CellWidthSize;
2026-03-02 09:13:29 +08:00
double maxCol = LayoutList
.Select(e => e.LayOutY)
.DefaultIfEmpty(0)
.Max();
2026-03-02 10:56:30 +08:00
Height = (int)(maxCol + 1) * (int)CellHeightSize;
2026-03-02 09:13:29 +08:00
// 示例:随机移动 RGV1
2026-03-02 10:56:30 +08:00
//var rgv1 = LayoutList.OfType<Rgv6180ViewModel>().FirstOrDefault(x => x.Id == 1);
2026-03-02 09:13:29 +08:00
//if (rgv1 != null)
//{
2026-03-02 10:56:30 +08:00
// rgv1.LayOutX = new Random().Next(0, 7);
2026-03-02 09:13:29 +08:00
//}
}
finally
{
_isProcessing = false;
}
}
}