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

333 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Timers;
using System.Windows;
using NLog;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore;
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
using Xylem.Common.Utils.Logging;
namespace ProductionUiCordonelLegacy.Model
{
public abstract class BaseMultiModeChildViewModel : IMultiModeChildViewModel
{
private readonly ILogger _logger;
private Timer tmrAutoDetect;
public EventHandler OnPcbIdDetect;
public List<String> BarcodeTrigger = new List<String>();
private Int32 intervalWithoutDetect = 1000;
private Int32 intervalWithDetect = 10000;
private String _currentPcbID;
private MeterBatch Batch = new MeterBatch();
private Visibility visibilityMarriage = Visibility.Collapsed;
public Visibility VisibilityMarriage
{
get => visibilityMarriage;
set
{
visibilityMarriage = value;
NotifyPropertyChanged();
}
}
private Visibility visibilityPicking = Visibility.Collapsed;
public Visibility VisibilityPicking
{
get => visibilityPicking;
set
{
visibilityPicking = value;
NotifyPropertyChanged();
}
}
private Visibility visibilityPressure = Visibility.Collapsed;
public Visibility VisibilityPressure
{
get => visibilityPressure;
set
{
visibilityPressure = value;
NotifyPropertyChanged();
}
}
public String currentSlotStateText = "none";
public String CurrentSlotStateText
{
get => currentSlotStateText;
set
{
currentSlotStateText = value;
NotifyPropertyChanged();
}
}
private SlotState currentSlotState = SlotState.None;
public SlotState CurrentSlotState
{
get => currentSlotState;
set
{
currentSlotState = value;
switch (currentSlotState)
{
case SlotState.None:
CurrentSlotStateText = "Keiner";
break;
case SlotState.IsDetecting:
CurrentSlotStateText = "Warte auf Zähler Kontakt";
break;
case SlotState.Detected:
CurrentSlotStateText = "Zähler detektiert";
break;
case SlotState.InProgress:
CurrentSlotStateText = "Zähler wird bearbeitet";
break;
case SlotState.Done:
CurrentSlotStateText = "Zähler wurde bearbeitet";
break;
case SlotState.ConnectionLost:
CurrentSlotStateText = "Verbindung getrennt";
break;
case SlotState.Error:
CurrentSlotStateText = "Fehler beim bearbeitet";
break;
case SlotState.Fatal:
CurrentSlotStateText = "Schwerwiegender Fehler";
break;
}
NotifyPropertyChanged();
}
}
private String slotInfo = "-";
public String SlotInfo
{
get => slotInfo;
set
{
slotInfo = value;
NotifyPropertyChanged();
}
}
public enum SlotState
{
None,
IsDetecting,
Detected,
InProgress,
Done,
ConnectionLost,
Error,
Fatal
}
private String childName = "asd";
public String ChildName
{
get => childName;
set
{
childName = value;
NotifyPropertyChanged();
}
}
private String order;
public String Order
{
get => order;
set
{
order = value;
NotifyPropertyChanged();
}
}
public String CurrentPcbID
{
get => _currentPcbID;
set
{
_currentPcbID = value;
NotifyPropertyChanged();
}
}
public GenesisMeter CurrentMeter { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public String GetChildIdent()
{
return ChildName;
}
public Int32 SlotNr;
public BaseMultiModeChildViewModel(String ctrName, Int32 slotNR,List<String> barcodeTrigger, Int32? StationId = null, Boolean useTimer = true)
{
_logger = NLogHelper.CreateOrGetLogger("ProductionUI");
ChildName = ctrName;
SlotNr = slotNR;
SlotInfo = SlotNr.ToString();
if (useTimer)
{
tmrAutoDetect = new Timer(intervalWithoutDetect);
tmrAutoDetect.Elapsed += TmrAutoDetect_Elapsed;
tmrAutoDetect.Enabled = true;
}
BarcodeTrigger = barcodeTrigger;
}
public void TmrAutoDetect_Elapsed(Object sender, EventArgs e)
{
CurrentSlotState = SlotState.IsDetecting;
tmrAutoDetect.Enabled = false;
var hasDetect = Detect(SlotNr);
if (hasDetect)
{
CurrentSlotState = SlotState.Detected;
tmrAutoDetect.Interval = intervalWithDetect;
}
else
{
tmrAutoDetect.Interval = intervalWithoutDetect;
}
tmrAutoDetect.Enabled = true;
}
public void TextForceUpdate()
{
var hasDetect = Detect(SlotNr);
if (hasDetect)
{
CurrentSlotState = SlotState.Detected;
}
}
public virtual void InputText(String text)
{
Order = text;
}
public void Log(String txt)
{
_logger?.Info(txt);
}
public abstract Boolean SlotIsLocked();
public event EventHandler MeterDetectedHandler;
private Boolean Detect(Int32 slot)
{
if (!SlotIsLocked())
{
Log("Start Detect");
Boolean hasDetect = false;
if (string.IsNullOrEmpty(CurrentPcbID) || CurrentMeter == null)
{
Batch = new MeterBatch();
var tryMeter = new GenesisMeter();
try
{
tryMeter.SetupFromConfigFile(slot);
Batch.AddMeter(tryMeter);
tryMeter.Logout();
Log("Try to get PcbID");
CurrentPcbID = tryMeter.GetPcbId();
hasDetect = !string.IsNullOrEmpty(CurrentPcbID);
CurrentMeter = tryMeter;
if (hasDetect)
{
Log($"New PcbID detected {CurrentPcbID}");
MeterDetectedHandler?.Invoke(CurrentMeter, EventArgs.Empty);
}
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;
}
Log("Meter is still connected (locked)");
return true;
}
private void DisposeAllConnections()
{
MeterDetectedHandler?.Invoke(null, EventArgs.Empty);
if (Batch != null)
{
Batch.RemoveAllMeters();
if (CurrentMeter != null)
{
CurrentMeter.Dispose();
}
Batch.Dispose();
}
}
public void Dispose()
{
DisposeAllConnections();
}
}
}