2021-10-01 09:09:20 +00:00
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 ;
2024-02-12 14:18:19 +00:00
using System.Windows ;
2021-10-01 09:09:20 +00:00
using Xylem.Common.CommonCore.Configuration ;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore ;
2022-08-19 15:22:05 +00:00
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers ;
2021-10-01 09:09:20 +00:00
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore ;
2023-06-02 09:32:01 +00:00
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore.Consts ;
2021-10-01 09:09:20 +00:00
using Xylem.Common.Logic.ProductionOrderCore.TestResults ;
using Xylem.Common.Logic.SoftwareAccessHelper ;
2025-02-05 09:10:04 +00:00
using Stichproben.Cordonel ;
2021-10-01 09:09:20 +00:00
namespace CordonelAssemblyLine.Model
{
public class LeakViewModel : INotifyPropertyChanged
{
2024-02-12 14:18:19 +00:00
public event EventHandler < StringPressureArgs > OnStoreDone ;
2021-10-01 09:09:20 +00:00
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 ) ) ;
}
}
2022-10-19 10:58:08 +00:00
private String testerName ;
public String TesterName
2021-10-01 09:09:20 +00:00
{
get { return testerName ; }
set
{
testerName = value ;
NotifyPropertyChanged ( nameof ( TesterName ) ) ;
}
}
2022-10-19 10:58:08 +00:00
private Boolean inputIsEnabled = false ;
public Boolean InputIsEnabled
2021-10-01 09:09:20 +00:00
{
get { return inputIsEnabled ; }
set
{
inputIsEnabled = value ;
NotifyPropertyChanged ( nameof ( InputIsEnabled ) ) ;
}
}
2022-10-19 10:58:08 +00:00
private Int32 intervalWithoutDetect = 1000 ;
private Int32 intervalWithDetect = 10000 ;
private String _currentPcbID ;
2021-10-01 09:09:20 +00:00
private MeterBatch Batch = new MeterBatch ( ) ;
public GenesisMeter CurrentMeter { get ; private set ; }
2022-10-19 10:58:08 +00:00
public Boolean Locked { get ; set ; } = false ;
public Int32 ? StationID { get ; set ; } = null ;
private String toolTip ;
public String ToolTipInput
2021-10-01 09:09:20 +00:00
{
get
{
return toolTip ;
}
set
{
toolTip = value ;
NotifyPropertyChanged ( nameof ( ToolTipInput ) ) ;
}
}
2022-10-19 10:58:08 +00:00
public String CurrentPcbID
2021-10-01 09:09:20 +00:00
{
get
{
return _currentPcbID ;
}
set
{
_currentPcbID = value ;
NotifyPropertyChanged ( nameof ( CurrentPcbID ) ) ;
}
}
2022-10-19 10:58:08 +00:00
private Boolean _currentPcbHasPressure ;
public Boolean CurrentPcbHasPressure
2022-08-19 15:22:05 +00:00
{
get
{
return _currentPcbHasPressure ;
}
set
{
_currentPcbHasPressure = value ;
NotifyPropertyChanged ( nameof ( CurrentPcbHasPressure ) ) ;
}
}
2024-02-12 14:18:19 +00:00
public Visibility _lockedPressure = Visibility . Hidden ;
public Visibility LockedPressure
{
get
{
return _lockedPressure ;
}
set
{
_lockedPressure = value ;
NotifyPropertyChanged ( nameof ( LockedPressure ) ) ;
}
}
2025-02-05 10:02:36 +00:00
private string lockedPressureMessage ;
public string LockedPressureMessage
{
get = > this . lockedPressureMessage ;
set
{
this . lockedPressureMessage = value ;
this . NotifyPropertyChanged ( nameof ( LockedPressureMessage ) ) ;
}
}
2024-02-12 14:18:19 +00:00
2021-10-01 09:09:20 +00:00
private Timer tmrAutoDetect ;
2022-10-19 10:58:08 +00:00
public LeakViewModel ( Int32 slotNR , Int32 ? stationID = null )
2021-10-01 09:09:20 +00:00
{
2023-08-02 10:38:58 +00:00
//_logger = NLogHelper.CreateOrGetLogger("ProductionUI");
2021-10-01 09:09:20 +00:00
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" ;
}
2022-10-19 10:58:08 +00:00
public Boolean isHydraulicStaion ( )
2021-10-01 09:09:20 +00:00
{
if ( ! StationID . HasValue | | StationID . Value = = 1 )
{
return false ;
}
return true ;
}
private readonly ILogger _logger ;
2022-10-19 10:58:08 +00:00
private readonly Int32 SlotNr ;
2021-10-01 09:09:20 +00:00
2022-10-19 10:58:08 +00:00
public void Log ( String txt )
2021-10-01 09:09:20 +00:00
{
_logger ? . Info ( txt ) ;
}
2022-10-19 10:58:08 +00:00
private Boolean Detect ( )
2021-10-01 09:09:20 +00:00
{
Log ( "Start Detect" ) ;
2022-10-19 10:58:08 +00:00
Boolean hasDetect = false ;
2021-10-01 09:09:20 +00:00
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 ( ) ;
2022-08-19 15:22:05 +00:00
CurrentPcbHasPressure = true ;
try
{
tryMeter . Login ( ) ;
2024-04-02 14:02:15 +00:00
var hasPresure = RegisterConverter . ByteArrayToValue < Boolean > ( tryMeter . ReadRegister ( "METROLOGYASST_PressurePresent" ) ) ;
2022-08-19 15:22:05 +00:00
CurrentPcbHasPressure = hasPresure ;
}
catch ( Exception ex )
{
tryMeter . WriteLog ( ex . Message ) ;
}
2024-02-12 14:18:19 +00:00
2022-08-19 15:22:05 +00:00
2021-10-01 09:09:20 +00:00
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 ( ) ;
}
2022-10-19 10:58:08 +00:00
public void TmrAutoDetect_Elapsed ( Object sender , EventArgs e )
2021-10-01 09:09:20 +00:00
{
tmrAutoDetect . Enabled = false ;
var hasDetect = Detect ( ) ;
if ( hasDetect )
{
tmrAutoDetect . Interval = intervalWithDetect ;
}
else
{
tmrAutoDetect . Interval = intervalWithoutDetect ;
}
tmrAutoDetect . Enabled = true ;
}
2022-10-19 10:58:08 +00:00
internal void ReloadData ( Boolean reset )
2021-10-01 09:09:20 +00:00
{
if ( Result ! = null & & Result . PcbId ! = CurrentPcbID )
{
InputIsEnabled = false ;
2022-10-19 10:58:08 +00:00
Result = new PressureResultDataNotifyable ( ) { Remark = "loading" , TestPoints = new ObservableCollection < PressureTestPoints > ( ) , PcbId = "" } ;
2021-10-01 09:09:20 +00:00
Task < PressureResultDataNotifyable > . Run ( ( ) = >
{
try
{
2022-09-06 14:22:09 +00:00
2021-10-01 09:09:20 +00:00
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 ) ;
2022-08-19 15:22:05 +00:00
//add pressures point
PressureResultDataNotifyable PressureResultData ;
2021-10-01 09:09:20 +00:00
if ( ! pressureResultData . Any ( ) )
{
2024-02-12 14:18:19 +00:00
PressureResultData = new PressureResultDataNotifyable ( ) ;
2021-10-01 09:09:20 +00:00
}
2022-08-19 15:22:05 +00:00
2022-09-06 14:22:09 +00:00
PressureResultData = new PressureResultDataNotifyable ( ) ;
2022-08-19 15:22:05 +00:00
if ( ! CurrentPcbHasPressure )
{
2022-10-19 10:58:08 +00:00
PressureResultData . TestPoints . Remove ( PressureResultData . TestPoints . First ( r = > r . Ident = = 10 ) ) ;
2022-08-19 15:22:05 +00:00
}
return PressureResultData ;
2021-10-01 09:09:20 +00:00
}
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 ;
}
}
) ;
}
}
2024-02-12 14:18:19 +00:00
internal bool StoreData ( Boolean successfull )
2021-10-01 09:09:20 +00:00
{
if ( Result ! = null & & Result . PcbId = = CurrentPcbID )
{
InputIsEnabled = false ;
2024-02-12 14:18:19 +00:00
var isStored = "" ;
2021-10-01 09:09:20 +00:00
Result . TesterName = TesterName ;
Task . Run ( ( ) = >
{
var url = ServiceUrls . MeterProcessStateUrl ( ) ;
2023-06-02 09:32:01 +00:00
var status = DisplayCodes . PressureTestedFailed ;
2025-01-31 11:47:19 +00:00
2021-10-01 09:09:20 +00:00
if ( successfull )
{
2023-06-02 09:32:01 +00:00
status = DisplayCodes . PressureTested ;
2021-10-01 09:09:20 +00:00
}
2025-02-05 09:10:04 +00:00
2021-10-01 09:09:20 +00:00
try
{
url = url + $"SetPressureStateDetailed" ;
var mutedResult = Result . ToBase ( ) ;
2022-09-22 14:07:58 +00:00
mutedResult . Passed = successfull ;
2024-02-12 14:18:19 +00:00
isStored = LocalWebRequest . PostRequestAsyncAndGetContent ( url , json : mutedResult ) ;
2025-02-05 11:37:02 +00:00
2024-02-12 14:18:19 +00:00
if ( ! isStored . Contains ( "0" ) & & ! isStored . Contains ( "1" ) )
2021-10-01 09:09:20 +00:00
{
2024-02-12 14:18:19 +00:00
OnStoreDone ? . Invoke ( null , new StringPressureArgs ( "Kann nicht gespeichert werden" ) ) ;
2023-06-02 09:32:01 +00:00
status = DisplayCodes . PressureTestedFailed ;
2021-10-01 09:09:20 +00:00
}
2025-02-05 09:10:04 +00:00
if ( int . TryParse ( $"{this.CurrentPcbID}" , out var pcbId ) )
{
CurrentMeter . Login ( ) ;
2025-02-12 13:58:27 +00:00
// jedes mal neu wegen die ExeConfig.Current.LeakStationId
var result = new StichprobenManager ( ExeConfig . Current . LeakStationId ) . ShouldBePressureTested ( pcbId ) ;
2025-02-05 09:10:04 +00:00
if ( result . ShouldBePressureTested )
{
CurrentMeter . SetProcessState ( DisplayCodes . PressureTestedPending ) ;
2025-02-05 10:02:36 +00:00
OnStoreDone ? . Invoke ( null , new StringPressureArgs ( $"Stichprobenregel: {result.Rule}% von {result.Count} Stk. Dieser Zähler muss zur Druckprüfung!" , true ) ) ;
2025-02-05 09:10:04 +00:00
}
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 ) ) ;
}
2021-10-01 09:09:20 +00:00
2025-02-05 09:10:04 +00:00
return true ;
}
else
{
OnStoreDone ? . Invoke ( null , new StringPressureArgs ( $"Kann nicht {this.CurrentPcbID} in nummer konvertieren." , false ) ) ;
2021-10-01 09:09:20 +00:00
2025-02-05 09:10:04 +00:00
return false ;
}
2021-10-01 09:09:20 +00:00
}
catch ( Exception ex )
{
Log ( ex . Message ) ;
2023-06-02 09:32:01 +00:00
status = DisplayCodes . PressureTestedFailed ;
2024-02-12 14:18:19 +00:00
OnStoreDone ? . Invoke ( null , new StringPressureArgs ( $"Kann nicht gespeichert werden.Fehler: {ex.Message}" , false ) ) ;
2021-10-01 09:09:20 +00:00
}
2024-02-12 14:18:19 +00:00
OnStoreDone ? . Invoke ( null , new StringPressureArgs ( "Bitte erneut testen oder Linienleitung und QS informieren." , true ) ) ;
return false ;
2021-10-01 09:09:20 +00:00
} ) ;
}
2025-02-05 09:10:04 +00:00
2024-02-12 14:18:19 +00:00
return false ;
2021-10-01 09:09:20 +00:00
}
}
}