common/ProductionUiCordonel/Model/PressureMultiModeViewModel.cs
2026-04-23 17:50:07 +02:00

193 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using ProductionUiCordonelLegacy.Data;
using ProductionUiCordonelLegacy.Event;
using Xylem.Common.CommonCore.Configuration;
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore.Consts;
using Xylem.Common.Logic.ProductionOrderCore.ProcessState;
using Xylem.Common.Logic.ProductionOrderCore.TestResults;
using Xylem.Common.Logic.SoftwareAccessHelper;
namespace ProductionUiCordonelLegacy.Model
{
public class PressureMultiModeViewModel : BaseMultiModeChildViewModel
{
public event EventHandler<StringArgs> OnStoreDone;
private PressureResultDataNotifyable result = new PressureResultDataNotifyable();
public PressureResultDataNotifyable Result
{
get => result;
set
{
result = value;
NotifyPropertyChanged();
}
}
private Boolean inputIsEnabled;
public Boolean InputIsEnabled
{
get => inputIsEnabled;
set
{
inputIsEnabled = value;
NotifyPropertyChanged();
}
}
public Boolean Locked { get; set; } = false;
public Int32? StationID { get; set; }
public PressureMultiModeViewModel(String ctrName, Int32 slotNR, List<String> barcodeTrigger, Int32? stationID = null) : base(ctrName, slotNR, barcodeTrigger, stationID)
{
VisibilityPressure = Visibility.Visible;
StationID = stationID;
}
public Boolean isHydraulicStaion()
{
if (!StationID.HasValue || StationID.Value == 1)
{
return false;
}
return true;
}
public override Boolean SlotIsLocked()
{
return false;
}
internal void ReloadData(Boolean reset)
{
if (Result != null && Result.PcbId != CurrentPcbID)
{
InputIsEnabled = false;
if (true)
{
}
Result = new PressureResultDataNotifyable { Remark = "loading", Testpoints = new ObservableCollection<PressureTestPoints>(), PcbId = "" };
Task<PressureResultDataNotifyable>.Run((Func<PressureResultDataNotifyable>)(() =>
{
try
{
var url = ServiceUrls.MeterProcessStateUrl();
url = url + $"GetPressureStateDetailed?PcbId={CurrentPcbID}&TopCount=1";
var retStr = LocalWebRequest.GetRequest(url);
var pressureResultData = JsonConvert.DeserializeObject<List<PressureResultData>>(retStr);
if (!pressureResultData.Any())
{
return new PressureResultDataNotifyable();
}
return new PressureResultDataNotifyable(pressureResultData.First());
}
catch (Exception ex)
{
Log(ex.Message);
return new PressureResultDataNotifyable();
}
})).ContinueWith(a =>
{
var res = a.Result;
res.PcbId = CurrentPcbID;
Result = res;
InputIsEnabled = true;
}
);
}
}
internal void StoreData(Boolean successfull)
{
if (Result != null && Result.PcbId == CurrentPcbID)
{
InputIsEnabled = false;
var isStored = false;
var status = DisplayCodes.PressureTestedFailed;
Task.Run((Action)(() =>
{
var url = ServiceUrls.MeterProcessStateUrl();
if (successfull)
{
status = DisplayCodes.PressureTested;
}
try
{
if (StationID == 1)
{
var addResult = new PressureTestResult
{
FreeText = Result.Remark,
Passed = successfull,
StationId = StationID
};
url = url + $"SetPressureState?PcbId={CurrentPcbID}&FertigungsauftragsNr=0";
if (!LocalWebRequest.PostRequestAsync(url, json: addResult))
{
throw new ApplicationException("PressureState: NOT successful");
}
}
else
{
url = url + "SetPressureStateDetailed";
var mutedResult = Result.ToBase();
isStored = LocalWebRequest.PostRequestAsync(url, json: mutedResult);
if (!isStored)
{
OnStoreDone?.Invoke(null, new StringArgs("Can not store Result"));
status = DisplayCodes.PressureTestedFailed;
}
}
CurrentMeter.Login();
CurrentMeter.SetProcessState(status);
}
catch (Exception ex)
{
Log(ex.Message);
status = DisplayCodes.PressureTestedFailed;
OnStoreDone?.Invoke(null, new StringArgs("Can not store Result"));
}
if (status == DisplayCodes.PressureTested)
{
OnStoreDone?.Invoke(null, new StringArgs("Pressure test stored successfully. Meter can passed"));
return;
}
OnStoreDone?.Invoke(null, new StringArgs("Pressure test stored successfully. But Meter is marked with a leakage. Please inform QS"));
}));
}
}
}
}