using System; using System.Collections.Generic; using System.Globalization; using System.Threading; using Xylem.Common.CommonCore.Consts; using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages.EventArguments; using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages.MeasurementRecords; using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisConfig; using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore; namespace GenesisDisplayTool { public class CalibrationRecordWithMeterNumber { public UInt16 MeterNumber; public CalibrationRecord CalibChl; public String RawData { get; internal set; } } public class ZeroFlowGenesisMeter : GenesisMeter { public event EventHandler OnZeroFlowRecordIsDecoded; CultureInfo GlobalCulture { get; set; } public Byte Gp30LastByte { get; set; } private Byte[] GP30LastRegister { get; set; } public void SetGP30LastRegister(Byte[] data) { GP30LastRegister = data; } private Byte GP30LastPayloadLsb { get; set; } public Byte GP30LastCommand { get; set; } private Byte GP30RXPayload { get; set; } private Boolean GP30RequestStarted { get; set; } private Boolean GP30RequestFailed { get; set; } private Boolean GP30RequestGotAnswer { get; set; } private Byte Privilege { get; set; } private Byte FirstHitLevelUpPath0 { get; set; } private Byte FirstHitLevelUpPath1 { get; set; } private Byte FirstHitLevelUpPath2 { get; set; } private Byte FirstHitLevelDownPath0 { get; set; } private Byte FirstHitLevelDownPath1 { get; set; } private Byte FirstHitLevelDownPath2 { get; set; } private Byte PercentPath0 { get; set; } private Byte PercentPath1 { get; set; } private Byte PercentPath2 { get; set; } public Byte Subreason { get; set; } private Byte Starthit { get; set; } private Byte StoreCalibration { get; set; } private Byte Amplitude { get; set; } private Byte Samplerate { get; set; } private UInt16 CalibrationFactorPath0 { get; set; } private UInt16 CalibrationFactorPath1 { get; set; } private UInt16 CalibrationFactorPath2 { get; set; } private Int32 ZeroOffsetPath0 { get; set; } private Int32 ZeroOffsetPath1 { get; set; } private Int32 ZeroOffsetPath2 { get; set; } private Byte LedMode { get; set; } private Byte UpdatePeriode { get; set; } private Byte FirstHitShift { get; set; } private UInt32 ToFTempCalibrate { get; set; } private Int32 ToFTempOffsetPath0 { get; set; } private Int32 ToFTempOffsetPath1 { get; set; } private Int32 ToFTempOffsetPath2 { get; set; } public Int32 MeterNumber { get; set; } private Byte MeterSize { get; set; } public Boolean LogOnEnable; public Boolean AmplitudeEnable; public Boolean ZeroFlowOffsetEnable; public Boolean TempCalEnable; public Boolean CompletionEnable; public Boolean LoginFailed; public Boolean PreparationFailed; public Boolean AmplitudeFailed; public Boolean ZeroFlowOffsetFailed; public Boolean TempCalFailed; public Boolean CompletionFailed; public Boolean Ok; public Boolean EmptyPipeCheckEnable; public Boolean EmptyPipeCheckFailed; public Boolean MeterSelected; public List CurrentRecords = new List(); public ZeroFlowGenesisMeter(int Slot) : base() { base.SetupFromConfigFile(Slot); //base.EnableAutoLogon(); Setup(); } public ZeroFlowGenesisMeter(int Slot, PortConfig request, PortConfig streaming, bool ignoreCurrptData) : base() { base.SetupGenesisMeter(Slot, request, streaming, ignoreCurrptData); // base.EnableAutoLogon(); Setup(); } private void Setup() { base.LogRawData(true); this.GP30LastRegister = new Byte[2] { 0, 0 }; this.GP30LastPayloadLsb = 0; this.GP30LastCommand = 0; this.GP30RXPayload = 0; this.GP30RequestStarted = false; this.GP30RequestFailed = false; this.GP30RequestGotAnswer = false; this.FirstHitLevelUpPath0 = 0; this.FirstHitLevelUpPath1 = 0; this.FirstHitLevelUpPath2 = 0; this.FirstHitLevelDownPath0 = 0; this.FirstHitLevelDownPath1 = 0; this.FirstHitLevelDownPath2 = 0; this.Starthit = 0; this.Amplitude = 0; this.Samplerate = 0; this.CalibrationFactorPath0 = 0; this.CalibrationFactorPath1 = 0; this.CalibrationFactorPath2 = 0; this.ZeroOffsetPath0 = 0; this.ZeroOffsetPath1 = 0; this.ZeroOffsetPath2 = 0; this.LedMode = 0; this.MeterNumber = Slot; this.GlobalCulture = CultureInfo.CreateSpecificCulture("en-US"); this.Gp30LastByte = 0; //on zeroflow every param has to be stored _configuration.UseRegisterWatchService = true; _configuration.RegisterWatchServiceUrl = "http://sla12iis01/MeterProcessState/api/RegisterWatch/"; } public Boolean CheckStreamingPort() { if (!StreamingPort.IsOpen()) { base.StreamingPort.Open(); } return StreamingPort.IsOpen(); } public Boolean CheckRequestPort() { if (!RequestPort.IsOpen()) { base.RequestPort.Open(); } return RequestPort.IsOpen(); } public void StartRecordData() { CurrentRecords.Clear(); base.StreamingProtocol.OnRecordIsDecoded += StreamingProtocol_OnRecordIsDecoded; ; SyncStreamingBuffer(SyncMarkRecord.DecodeEveryPackage); } private void StreamingProtocol_OnRecordIsDecoded(object sender, BaseDataEventArgs e) { if (e is CalibDataEventArgs) { OnZeroFlowRecordIsDecoded?.Invoke(this, new CalibrationRecordWithMeterNumber() { MeterNumber = (ushort)(Slot - 1), CalibChl = ((CalibDataEventArgs)e).CalibChl, RawData = ((CalibDataEventArgs)e).RawData }); } } public void StopRecordData() { StreamingProtocol.OnRecordIsDecoded -= StreamingProtocol_OnRecordIsDecoded; SyncStreamingBuffer(SyncMarkRecord.SkipDecoding); } internal void Flush() { if (StreamingPort == null) { throw new ApplicationException("Synchronization not possible due to undefined streaming port"); } SyncStreamingBuffer(SyncMarkRecord.FlushBuffer); } public new Boolean WriteRegisterUnsafe(string reg, T value) { var RetrysLeft = 4; while (RetrysLeft >= 0) { try { var result = base.WriteRegister(reg, value,true,false); if (result) { return result; } Thread.Sleep(100); } catch (Exception e) { base.WriteLog($"Can not WriteRegisterUnsafe retry left: {RetrysLeft}, message: {e.Message}"); } RetrysLeft = RetrysLeft - 1; } return false; } public new Boolean WriteRegisterSafe(string reg, T value) { var RetrysLeft = 4; while (RetrysLeft >= 0) { try { var result = base.WriteRegister(reg, value,true,true); if (result) { return result; } Thread.Sleep(100); } catch (Exception e) { base.WriteLog($"Can not WriteRegisterUnsafe retry left: {RetrysLeft}, message: {e.Message}"); } RetrysLeft = RetrysLeft - 1; } return false; } public new Byte[] ReadRegister(string reg) { var RetrysLeft = 4; Exception lastEx = null; while (RetrysLeft >= 0) { try { return base.ReadRegister(reg); } catch (Exception e) { lastEx = e; base.WriteLog($"Can not WriteRegisterUnsafe retry left: {RetrysLeft}, message: {e.Message}"); } Thread.Sleep(100); RetrysLeft = RetrysLeft - 1; } if (lastEx == null) { throw new ApplicationException("something went totally wrong on ReadRegister. no Exception found"); } throw new ApplicationException($"something went wron on ReadRegister.Message{lastEx.Message}"); } } }