borepin/Borepin/Borepin/PageModel/MachinePageModel.cs
2022-06-25 15:32:30 +02:00

155 lines
4.7 KiB
C#

using Borepin.Base;
using Prism.Commands;
using Prism.Navigation;
using System.Threading.Tasks;
using System.Windows.Input;
using FabAccessAPI.Schema;
using Borepin.Model;
using Prism.Services;
using Borepin.Service;
using Borepin.Base.Exceptions;
using Capnp.Rpc;
using System;
namespace Borepin.PageModel
{
public class MachinePageModel : ConnectionModelBase
{
#region Private Fields
private string _ID;
private Machine _Machine;
#endregion
#region Contructors
public MachinePageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService)
{
UseMachineCommand = new DelegateCommand(UseMachineCommandExecute);
GiveBackMachineCommand = new DelegateCommand(GiveBackMachineCommandExecute);
ForceFreeMachineCommand = new DelegateCommand(ForceFreeMachineCommandExecute);
}
#endregion
#region Data
public override Task LoadInstance(object instance)
{
if(instance is string)
{
_ID = instance as string;
}
else
{
throw new InstanceIncorrectException();
}
return Task.CompletedTask;
}
public override async Task LoadAPIData()
{
_Machine = (await _API.Session.MachineSystem.Info.GetMachine(_ID).ConfigureAwait(false)).Just;
MachineItem = new MachineVisualize(_Machine);
MachineItem.LoadData();
}
#endregion
#region Fields
private MachineVisualize _MachineItem;
public MachineVisualize MachineItem
{
get => _MachineItem;
set => SetProperty(ref _MachineItem, value);
}
#endregion
#region Commands
private ICommand _UseMachineCommand;
public ICommand UseMachineCommand
{
get => _UseMachineCommand;
set => SetProperty(ref _UseMachineCommand, value);
}
public async void UseMachineCommandExecute()
{
IsBusy = true;
if(_API.IsConnected)
{
try
{
Machine.IUseInterface useInterface = _Machine.Use;
await useInterface.Use().ConfigureAwait(false);
await LoadAPIData().ConfigureAwait(false);
}
catch (RpcException exception) when (string.Equals(exception.Message, "RPC connection is broken. Task would never return.", StringComparison.Ordinal))
{
Log.Debug("RPC Connection Loss");
}
}
IsBusy = false;
}
private ICommand _GiveBackMachineCommand;
public ICommand GiveBackMachineCommand
{
get => _GiveBackMachineCommand;
set => SetProperty(ref _GiveBackMachineCommand, value);
}
public async void GiveBackMachineCommandExecute()
{
IsBusy = true;
if (_API.IsConnected)
{
try
{
Machine.IInUseInterface inUseInterface = _Machine.Inuse;
await inUseInterface.GiveBack().ConfigureAwait(false);
await LoadAPIData().ConfigureAwait(false);
}
catch(RpcException exception) when(string.Equals(exception.Message, "RPC connection is broken. Task would never return.", StringComparison.Ordinal))
{
Log.Debug("RPC Connection Loss");
}
}
IsBusy = false;
}
private ICommand _ForceFreeMachineCommand;
public ICommand ForceFreeMachineCommand
{
get => _ForceFreeMachineCommand;
set => SetProperty(ref _ForceFreeMachineCommand, value);
}
public async void ForceFreeMachineCommandExecute()
{
IsBusy = true;
if (_API.IsConnected)
{
try
{
Machine.IManageInterface manageInterface = _Machine.Manage;
await manageInterface.ForceFree().ConfigureAwait(false);
await LoadAPIData().ConfigureAwait(false);
}
catch (RpcException exception) when (string.Equals(exception.Message, "RPC connection is broken. Task would never return.", StringComparison.Ordinal))
{
Log.Debug("RPC Connection Loss");
}
}
IsBusy = false;
}
#endregion
}
}