using Ionic.Zip; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Explorer; using Tango.Integration.Operation; using Tango.Logging; using Tango.PMR.MachineStatus; using Tango.PPC.Common; using Tango.PPC.Maintenance.Helpers; using Tango.PPC.Maintenance.Models; using Tango.PPC.Maintenance.Views; using Tango.PPC.Storage; namespace Tango.PPC.Maintenance.ViewModels { public class MaintenanceViewVM : PPCViewModel { public ObservableCollection Guides { get; set; } public RelayCommand OpenGuideCommand { get; set; } private List _midTankLevels; public List MidTankLevels { get { return _midTankLevels; } set { _midTankLevels = value; RaisePropertyChangedAuto(); } } private OverallTemperatureModel _overallTemperature; public OverallTemperatureModel OverallTemperature { get { return _overallTemperature; } set { _overallTemperature = value; RaisePropertyChangedAuto(); } } public RelayCommand OpenDyeingHeadCommand { get; set; } public RelayCommand CloseDyeingHeadCommand { get; set; } public RelayCommand ExportLogsCommand { get; set; } public MaintenanceViewVM() { Guides = new ObservableCollection(GuideHelper.CreateAllGuides()); OverallTemperature = new OverallTemperatureModel(); OpenGuideCommand = new RelayCommand(OpenGuide); OpenDyeingHeadCommand = new RelayCommand(OpenDyeingHead); CloseDyeingHeadCommand = new RelayCommand(CloseDyeingHead); ExportLogsCommand = new RelayCommand(ExportLogsToStorage); } public override void OnApplicationStarted() { } public override void OnApplicationReady() { base.OnApplicationReady(); MidTankLevels = MachineProvider.Machine.Configuration.NoneEmptyIdsPacks.OrderBy(x => x.PackIndex).Select(x => new MidTankLevelModel() { Max = MachineOperator.MAX_MIDTANK_LITERS, IDSPack = x, }).ToList(); MachineProvider.MachineOperator.MachineStatusChanged += MachineOperator_MachineStatusChanged; } private void MachineOperator_MachineStatusChanged(object sender, MachineStatus status) { UpdateMidTankLevels(status); OverallTemperature.Temperature = status.OverallTemperature; } public async void OpenGuide(GuideBase guide) { await NavigationManager.NavigateWithObject(guide); } private void UpdateMidTankLevels(MachineStatus status) { if (IsVisible) { foreach (var item in status.IDSPacksLevels) { var model = MidTankLevels.SingleOrDefault(x => x.IDSPack.PackIndex == item.Index); if (model != null) { model.Level = item.MidTankLevel; } } } } private void OpenDyeingHead() { IsFree = false; try { NotificationProvider.SetGlobalBusyMessage("Opening dyeing head lead..."); MachineProvider.MachineOperator.StartMotorHoming(new PMR.Diagnostics.MotorHomingRequest() { Direction = PMR.Diagnostics.MotorDirection.Backward, MotorType = PMR.Hardware.HardwareMotorType.MotoDhLid, Speed = 400, }).Subscribe((response) => { //Next }, (ex) => { //Error IsFree = true; NotificationProvider.ReleaseGlobalBusyMessage(); LogManager.Log(ex, "Error opening dyeing head lead."); NotificationProvider.ShowError(ex.FlattenMessage()); }, () => { //Complete IsFree = true; NotificationProvider.ReleaseGlobalBusyMessage(); NotificationProvider.ShowSuccess("The dyeing head lead is now opened."); }); } catch (Exception ex) { LogManager.Log(ex, "Error opening dyeing head lead."); NotificationProvider.ReleaseGlobalBusyMessage(); NotificationProvider.ShowError(ex.FlattenMessage()); IsFree = true; } } private void CloseDyeingHead() { IsFree = false; try { NotificationProvider.SetGlobalBusyMessage("Closing dyeing head lead..."); MachineProvider.MachineOperator.StartMotorHoming(new PMR.Diagnostics.MotorHomingRequest() { Direction = PMR.Diagnostics.MotorDirection.Forward, MotorType = PMR.Hardware.HardwareMotorType.MotoDhLid, Speed = 400, }).Subscribe((response) => { //Next }, (ex) => { //Error IsFree = true; NotificationProvider.ReleaseGlobalBusyMessage(); LogManager.Log(ex, "Error closing dyeing head lead."); NotificationProvider.ShowError(ex.FlattenMessage()); }, () => { //Complete IsFree = true; NotificationProvider.ReleaseGlobalBusyMessage(); NotificationProvider.ShowSuccess("The dyeing head lead is now closed."); }); } catch (Exception ex) { LogManager.Log(ex, "Error closing dyeing head lead."); NotificationProvider.ReleaseGlobalBusyMessage(); NotificationProvider.ShowError(ex.FlattenMessage()); IsFree = true; } } private async void ExportLogsToStorage() { var result = await NavigationManager. NavigateForResult( new Storage.Models.StorageNavigationRequest() { Intent = Storage.Models.StorageNavigationIntent.SaveFile, DefaultFileName = $"Tango-Logs-{DateTime.Now.ToFileName()}", Filter = "do not display anything", Title = "Export System Logs", }); if (result != null) { String file = result.Path + ".zip"; IsFree = false; try { NotificationProvider.SetGlobalBusyMessage("Exporting system logs..."); var appFileLogger = LogManager.RegisteredLoggers.FirstOrDefault(x => x is FileLogger) as FileLogger; await Task.Factory.StartNew(() => { using (ZipFile zip = new ZipFile(file)) { zip.Password = "1Creativity"; if (appFileLogger != null) { zip.AddDirectory(appFileLogger.Folder); } zip.ParallelDeflateThreshold = -1; zip.Save(); } }); NotificationProvider.ReleaseGlobalBusyMessage(); await NotificationProvider.ShowSuccess("System logs exported successfully."); } catch (Exception ex) { NotificationProvider.ReleaseGlobalBusyMessage(); LogManager.Log(ex, "Error exporting system logs."); await NotificationProvider.ShowError($"An error occurred while trying to export the system logs.\n{ex.FlattenMessage()}"); } finally { NotificationProvider.ReleaseGlobalBusyMessage(); IsFree = true; } } } } }