433 lines
14 KiB
C#
433 lines
14 KiB
C#
using CordonelAssemblyLine.Data;
|
|
using CordonelAssemblyLine.Event;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using System.Timers;
|
|
using System.Windows;
|
|
using Xylem.Common.CommonCore.Configuration;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore.Consts;
|
|
using Xylem.Common.Logic.ProductionOrderCore.TestResults;
|
|
using Xylem.Common.Logic.SoftwareAccessHelper;
|
|
using Stichproben.Cordonel;
|
|
|
|
namespace CordonelAssemblyLine.Model
|
|
{
|
|
public class LeakViewModel : INotifyPropertyChanged
|
|
{
|
|
public event EventHandler<StringPressureArgs> OnStoreDone;
|
|
public event EventHandler MeterDetectedHandler;
|
|
protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
private PressureResultDataNotifyable result = new PressureResultDataNotifyable();
|
|
public PressureResultDataNotifyable Result
|
|
{
|
|
get { return result; }
|
|
set
|
|
{
|
|
result = value;
|
|
NotifyPropertyChanged(nameof(Result));
|
|
}
|
|
}
|
|
private String testerName;
|
|
public String TesterName
|
|
{
|
|
get { return testerName; }
|
|
set
|
|
{
|
|
testerName = value;
|
|
NotifyPropertyChanged(nameof(TesterName));
|
|
}
|
|
}
|
|
|
|
private Boolean inputIsEnabled = false;
|
|
public Boolean InputIsEnabled
|
|
{
|
|
get { return inputIsEnabled; }
|
|
set
|
|
{
|
|
inputIsEnabled = value;
|
|
NotifyPropertyChanged(nameof(InputIsEnabled));
|
|
}
|
|
}
|
|
|
|
private Int32 intervalWithoutDetect = 1000;
|
|
private Int32 intervalWithDetect = 10000;
|
|
private String _currentPcbID;
|
|
private MeterBatch Batch = new MeterBatch();
|
|
public GenesisMeter CurrentMeter { get; private set; }
|
|
|
|
public Boolean Locked { get; set; } = false;
|
|
public Int32? StationID { get; set; } = null;
|
|
private String toolTip;
|
|
public String ToolTipInput
|
|
{
|
|
get
|
|
{
|
|
return toolTip;
|
|
}
|
|
set
|
|
{
|
|
toolTip = value;
|
|
|
|
NotifyPropertyChanged(nameof(ToolTipInput));
|
|
}
|
|
}
|
|
|
|
public String CurrentPcbID
|
|
{
|
|
get
|
|
{
|
|
return _currentPcbID;
|
|
}
|
|
set
|
|
{
|
|
_currentPcbID = value;
|
|
|
|
NotifyPropertyChanged(nameof(CurrentPcbID));
|
|
}
|
|
}
|
|
private Boolean _currentPcbHasPressure;
|
|
public Boolean CurrentPcbHasPressure
|
|
{
|
|
get
|
|
{
|
|
return _currentPcbHasPressure;
|
|
}
|
|
set
|
|
{
|
|
_currentPcbHasPressure = value;
|
|
|
|
NotifyPropertyChanged(nameof(CurrentPcbHasPressure));
|
|
}
|
|
}
|
|
|
|
public Visibility _lockedPressure = Visibility.Hidden;
|
|
public Visibility LockedPressure
|
|
{
|
|
get
|
|
{
|
|
return _lockedPressure;
|
|
}
|
|
set
|
|
{
|
|
_lockedPressure = value;
|
|
|
|
NotifyPropertyChanged(nameof(LockedPressure));
|
|
}
|
|
}
|
|
|
|
private string lockedPressureMessage;
|
|
public string LockedPressureMessage
|
|
{
|
|
get => this.lockedPressureMessage;
|
|
set
|
|
{
|
|
this.lockedPressureMessage = value;
|
|
this.NotifyPropertyChanged(nameof(LockedPressureMessage));
|
|
}
|
|
}
|
|
|
|
|
|
private Timer tmrAutoDetect;
|
|
|
|
public LeakViewModel(Int32 slotNR, Int32? stationID = null)
|
|
{
|
|
//_logger = NLogHelper.CreateOrGetLogger("ProductionUI");
|
|
StationID = stationID;
|
|
SlotNr = slotNR;
|
|
tmrAutoDetect = new Timer(intervalWithoutDetect);
|
|
tmrAutoDetect.Elapsed += TmrAutoDetect_Elapsed;
|
|
tmrAutoDetect.Enabled = true;
|
|
|
|
ToolTipInput = $"Unterstützte Eingabeformate: {System.Environment.NewLine} " +
|
|
$"2,00E-07 { System.Environment.NewLine} " +
|
|
$"2E-07 { System.Environment.NewLine} " +
|
|
$"2x10-7 { System.Environment.NewLine} " +
|
|
$"2*10^-7 { System.Environment.NewLine} " +
|
|
"0,0000002";
|
|
}
|
|
|
|
public Boolean isHydraulicStaion()
|
|
{
|
|
|
|
if (!StationID.HasValue || StationID.Value == 1)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
private readonly ILogger _logger;
|
|
private readonly Int32 SlotNr;
|
|
|
|
public void Log(String txt)
|
|
{
|
|
_logger?.Info(txt);
|
|
}
|
|
private Boolean Detect()
|
|
{
|
|
|
|
Log("Start Detect");
|
|
Boolean hasDetect = false;
|
|
if (string.IsNullOrEmpty(CurrentPcbID) || CurrentMeter == null)
|
|
{
|
|
Batch = new MeterBatch();
|
|
var tryMeter = new GenesisMeter();
|
|
try
|
|
{
|
|
tryMeter.SetupFromConfigFile(SlotNr);
|
|
Batch.AddMeter(tryMeter);
|
|
tryMeter.Logout();
|
|
Log("Try to get PcbID");
|
|
CurrentPcbID = tryMeter.GetPcbId();
|
|
CurrentPcbHasPressure = true;
|
|
try
|
|
{
|
|
tryMeter.Login();
|
|
var hasPresure = RegisterConverter.ByteArrayToValue<Boolean>(tryMeter.ReadRegister("METROLOGYASST_PressurePresent"));
|
|
CurrentPcbHasPressure = hasPresure;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
tryMeter.WriteLog(ex.Message);
|
|
}
|
|
|
|
|
|
|
|
hasDetect = !string.IsNullOrEmpty(CurrentPcbID);
|
|
CurrentMeter = tryMeter;
|
|
if (hasDetect)
|
|
{
|
|
MeterDetectedHandler?.Invoke(CurrentMeter, EventArgs.Empty);
|
|
Log($"New PcbID detected {CurrentPcbID}");
|
|
}
|
|
else
|
|
{
|
|
Log($"No Meter found");
|
|
DisposeAllConnections();
|
|
}
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log($"Error occur.{ Environment.NewLine} {e.Message}");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Log("Check if Meter is still connected");
|
|
var ret = CurrentMeter.GetPcbId();
|
|
if (string.IsNullOrEmpty(ret) || ret != CurrentPcbID)
|
|
{
|
|
Log("Meter connection lost");
|
|
hasDetect = false;
|
|
DisposeAllConnections();
|
|
|
|
CurrentPcbID = "";
|
|
}
|
|
else
|
|
{
|
|
Log("Meter is still connected");
|
|
hasDetect = true;
|
|
}
|
|
}
|
|
return hasDetect;
|
|
}
|
|
|
|
|
|
private void DisposeAllConnections()
|
|
{
|
|
if (Batch != null)
|
|
{
|
|
MeterDetectedHandler?.Invoke(null, EventArgs.Empty);
|
|
Batch.RemoveAllMeters();
|
|
if (CurrentMeter != null)
|
|
{
|
|
CurrentMeter.Dispose();
|
|
}
|
|
Batch.Dispose();
|
|
}
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
DisposeAllConnections();
|
|
}
|
|
|
|
public void TmrAutoDetect_Elapsed(Object sender, EventArgs e)
|
|
{
|
|
tmrAutoDetect.Enabled = false;
|
|
var hasDetect = Detect();
|
|
if (hasDetect)
|
|
{
|
|
tmrAutoDetect.Interval = intervalWithDetect;
|
|
}
|
|
else
|
|
{
|
|
tmrAutoDetect.Interval = intervalWithoutDetect;
|
|
}
|
|
tmrAutoDetect.Enabled = true;
|
|
}
|
|
internal void ReloadData(Boolean reset)
|
|
{
|
|
|
|
if (Result != null && Result.PcbId != CurrentPcbID)
|
|
{
|
|
InputIsEnabled = false;
|
|
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 tmp = 0;
|
|
var retStr = LocalWebRequest.GetRequestWithError(url, out tmp);
|
|
if (tmp != 200)
|
|
{
|
|
throw new ApplicationException("Keinen Auftrag gefunden. Kommissionierung prüfen!");
|
|
}
|
|
var pressureResultData = JsonConvert.DeserializeObject<List<PressureResultData>>(retStr);
|
|
|
|
|
|
//add pressures point
|
|
PressureResultDataNotifyable PressureResultData;
|
|
if (!pressureResultData.Any())
|
|
{
|
|
PressureResultData = new PressureResultDataNotifyable();
|
|
}
|
|
|
|
PressureResultData = new PressureResultDataNotifyable();
|
|
if (!CurrentPcbHasPressure)
|
|
{
|
|
PressureResultData.TestPoints.Remove(PressureResultData.TestPoints.First(r => r.Ident == 10));
|
|
}
|
|
|
|
|
|
|
|
return PressureResultData;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log(ex.Message);
|
|
return new PressureResultDataNotifyable();
|
|
}
|
|
|
|
})).ContinueWith((a) =>
|
|
{
|
|
if (a == null)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
var res = a.Result;
|
|
res.PcbId = CurrentPcbID;
|
|
|
|
Result = res;
|
|
InputIsEnabled = true;
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
internal bool StoreData(Boolean successfull)
|
|
{
|
|
|
|
if (Result != null && Result.PcbId == CurrentPcbID)
|
|
{
|
|
InputIsEnabled = false;
|
|
var isStored = "";
|
|
Result.TesterName = TesterName;
|
|
Task.Run((Func<Boolean>)(() =>
|
|
{
|
|
var url = ServiceUrls.MeterProcessStateUrl();
|
|
var status = DisplayCodes.PressureTestedFailed;
|
|
|
|
if (successfull)
|
|
{
|
|
status = DisplayCodes.PressureTested;
|
|
}
|
|
|
|
try
|
|
{
|
|
url = url + $"SetPressureStateDetailed";
|
|
var mutedResult = Result.ToBase();
|
|
|
|
mutedResult.Passed = successfull;
|
|
isStored = LocalWebRequest.PostRequestAsyncAndGetContent(url, json: mutedResult);
|
|
|
|
if (!isStored.Contains("0") && !isStored.Contains("1"))
|
|
{
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs("Kann nicht gespeichert werden"));
|
|
status = DisplayCodes.PressureTestedFailed;
|
|
}
|
|
|
|
if (int.TryParse($"{this.CurrentPcbID}", out var pcbId))
|
|
{
|
|
CurrentMeter.Login();
|
|
|
|
// jedes mal neu wegen die ExeConfig.Current.LeakStationId
|
|
var result = new StichprobenManager(ExeConfig.Current.LeakStationId).ShouldBePressureTested(pcbId);
|
|
|
|
if (result.ShouldBePressureTested)
|
|
{
|
|
CurrentMeter.SetProcessState(DisplayCodes.PressureTestedPending);
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs($"Stichprobenregel: {result.Rule}% von {result.Count} Stk. Dieser Zähler muss zur Druckprüfung!", true));
|
|
}
|
|
else
|
|
{
|
|
CurrentMeter.SetProcessState(status);
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs("Ergebnis gespeichert der Zähler kann weiterbearbeitet werden. Dieser Zähler muss NICHT zur Druckprüfung", false));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs($"Kann nicht {this.CurrentPcbID} in nummer konvertieren.", false));
|
|
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log(ex.Message);
|
|
status = DisplayCodes.PressureTestedFailed;
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs($"Kann nicht gespeichert werden.Fehler: {ex.Message}",false));
|
|
}
|
|
|
|
OnStoreDone?.Invoke(null, new StringPressureArgs("Bitte erneut testen oder Linienleitung und QS informieren.", true));
|
|
return false;
|
|
}));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|