219 lines
7.9 KiB
C#
219 lines
7.9 KiB
C#
using Avalonia.Controls.Notifications;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Cowain.Base.Helpers;
|
|
using Cowain.Base.ViewModels;
|
|
using Ke.Bee.Localization.Localizer.Abstractions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Plugin.Cowain.Driver.Abstractions;
|
|
using Plugin.Cowain.Wcs.IServices;
|
|
using Plugin.Cowain.Wcs.Models.Enum;
|
|
using System.Collections.ObjectModel;
|
|
using Ursa.Controls;
|
|
|
|
namespace Plugin.Cowain.Wcs.ViewModels;
|
|
|
|
public partial class WcsTaskListViewModel : PageViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
private ObservableCollection<TaskViewModel>? _taskList;
|
|
[ObservableProperty]
|
|
private ObservableCollection<StationViewModel>? _stations;
|
|
|
|
[ObservableProperty]
|
|
private WcsParamViewModel? _findTaskOnStartUp;
|
|
|
|
[ObservableProperty]
|
|
private WcsParamViewModel? _findTaskEnable;
|
|
|
|
[ObservableProperty]
|
|
private int _totals;
|
|
[ObservableProperty]
|
|
private int _pageSize;
|
|
[ObservableProperty]
|
|
private int _pageIndex;
|
|
|
|
private readonly ILocalizer _l;
|
|
private IServiceScopeFactory _scopeFactory;
|
|
private IStationService _stationService;
|
|
private IDeviceMonitor _deviceMonitor;
|
|
private IProcessService _processService;
|
|
private IWcsParamService _wcsParamService;
|
|
public WcsTaskListViewModel(ILocalizer localizer, IServiceScopeFactory scopeFactory, IStationService stationService, IDeviceMonitor deviceMonitor, IProcessService processService, IWcsParamService wcsParamService)
|
|
{
|
|
_l = localizer;
|
|
_scopeFactory = scopeFactory;
|
|
_stationService = stationService;
|
|
_deviceMonitor = deviceMonitor;
|
|
_processService = processService;
|
|
_wcsParamService = wcsParamService;
|
|
|
|
PageSize = 10;
|
|
PageIndex = 1;
|
|
TaskList = new();
|
|
RefreshCommand.ExecuteAsync(1);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task ToggleFindTaskAsync()
|
|
{
|
|
var findOnStartUp = await _wcsParamService.UpdateAsync(FindTaskOnStartUp);
|
|
if (findOnStartUp.IsSuccess)
|
|
{
|
|
if (FindTaskOnStartUp?.Param == "True")
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var tempService = scope.ServiceProvider.GetRequiredService<IFindFlowTaskService>();
|
|
await tempService.FindTaskAsync();
|
|
}
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["WcsTaskList.FindTaskToggled.Success"] + ":" + FindTaskOnStartUp!.Param);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["WcsTaskList.FindTaskToggled.Error"] + ":" + findOnStartUp.ErrorMessage);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task ToggleTaskEnableAsync()
|
|
{
|
|
var findTaskEnable = await _wcsParamService.UpdateAsync(FindTaskEnable);
|
|
if (findTaskEnable.IsSuccess)
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["WcsTaskList.FindTaskEnable.Success"] + ":" + FindTaskEnable!.Param);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["WcsTaskList.FindTaskEnable.Error"] + ":" + findTaskEnable.ErrorMessage);
|
|
}
|
|
}
|
|
|
|
|
|
[RelayCommand]
|
|
private async Task RefreshAsync(int pageIndex)
|
|
{
|
|
if (Stations == null)
|
|
{
|
|
var stationList = await _stationService.GetAllAsync();
|
|
Stations = new ObservableCollection<StationViewModel>(stationList);
|
|
}
|
|
if (FindTaskOnStartUp == null)
|
|
{
|
|
var findOnStartUp = await _wcsParamService.GetParamAsync("FindTaskOnStartUp");
|
|
if (findOnStartUp.IsSuccess)
|
|
{
|
|
FindTaskOnStartUp = findOnStartUp.Data;
|
|
}
|
|
}
|
|
|
|
if (FindTaskEnable == null)
|
|
{
|
|
var findOnEnable = await _wcsParamService.GetParamAsync("FindTaskEnable");
|
|
if (findOnEnable.IsSuccess)
|
|
{
|
|
FindTaskEnable = findOnEnable.Data;
|
|
}
|
|
}
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var taskService = scope.ServiceProvider.GetRequiredService<IRgvTaskService>();
|
|
var (data, count) = await taskService.GetAllAsync(pageIndex, PageSize);
|
|
Totals = count;
|
|
if (count > 0)
|
|
{
|
|
TaskList?.Clear();
|
|
foreach (var item in data)
|
|
{
|
|
item.Actions = new(taskService.ActionToList(item.Action) ?? new List<RgvActionViewModel>());
|
|
item.SelectedAction = item.Actions.FirstOrDefault(x => x.Id == item.ExecuteAction);
|
|
TaskList?.Add(item);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private bool CanEdit(TaskViewModel? viewModel) => viewModel is not null && !viewModel.IsFinished;
|
|
|
|
[RelayCommand(CanExecute = nameof(CanEdit))]
|
|
private async Task RetryAsync(TaskViewModel? viewModel)
|
|
{
|
|
if (viewModel == null)
|
|
{
|
|
return;
|
|
}
|
|
if (viewModel.SelectedAction == null)
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["WcsTaskList.Retry.Error.SelectedActionNull"]);
|
|
return;
|
|
}
|
|
string action = $"RgvCommand.{viewModel.SelectedAction?.Name}";
|
|
var result = await MessageBox.ShowOverlayAsync(_l["TaskRetryDialog"], $"{_l["TaskRetryAction"]}{viewModel.SelectedAction?.Id}-{_l[action]}", button: MessageBoxButton.YesNo);
|
|
if (result != MessageBoxResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
var getJson = await _processService.GetJsonData(viewModel.ProcessId);
|
|
if (getJson.IsSuccess)
|
|
{
|
|
var taskJsonParam = _deviceMonitor.GetActionParam(getJson.Data.PlcName, getJson.Data.Address);
|
|
if (taskJsonParam.IsSuccess)
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var taskService = scope.ServiceProvider.GetRequiredService<IRgvTaskService>();
|
|
var execute = await taskService.ExecuteAsync(taskJsonParam.Data, RgvUpdateSourceEnum.User, viewModel.SelectedAction?.Id);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[RelayCommand(CanExecute = nameof(CanEdit))]
|
|
private async Task FinishAsync(TaskViewModel? viewModel)
|
|
{
|
|
if (viewModel == null)
|
|
{
|
|
return;
|
|
}
|
|
var result = await MessageBox.ShowOverlayAsync(_l["TaskFinishDialog"], _l["Message.Info.Title"], button: MessageBoxButton.YesNo);
|
|
if (result != MessageBoxResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var taskService = scope.ServiceProvider.GetRequiredService<IRgvTaskService>();
|
|
var finishTask = await taskService.FinishTaskAsync(viewModel.Id);
|
|
if (finishTask.IsSuccess)
|
|
{
|
|
var processService = scope.ServiceProvider.GetRequiredService<IProcessService>();
|
|
var getJson = await processService.GetJsonData(viewModel.ProcessId);
|
|
string? rgvName = string.Empty;
|
|
if (getJson != null)
|
|
{
|
|
rgvName = getJson.Data!.RgvName;
|
|
}
|
|
var rgvService = scope.ServiceProvider.GetRequiredService<IRgvService>();
|
|
var rgvs = await rgvService.GetAllAsync();
|
|
if (rgvs != null)
|
|
{
|
|
var rgv = rgvs.FirstOrDefault(x => x.StationName == rgvName);
|
|
if (rgv != null)
|
|
{
|
|
rgv.FromStationId1 = 0;
|
|
rgv.ToStationId1 = 0;
|
|
rgv.FromStationId2 = 0;
|
|
rgv.ToStationId2 = 0;
|
|
await rgvService.UpdateAsync(rgv);
|
|
}
|
|
}
|
|
await RefreshCommand.ExecuteAsync(1);
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["WcsTaskList.Finish.Success"]);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["WcsTaskList.Finish.Error"] + ":" + finishTask.ErrorMessage);
|
|
}
|
|
|
|
}
|
|
|
|
}
|