524 lines
16 KiB
C#
524 lines
16 KiB
C#
using CordonelPreadjustmentUi;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Logic.SoftwareAccessHelper;
|
|
|
|
namespace TempFlansh
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
SetEnable(programmState.Start);
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
var helper = new TempHelper();
|
|
try
|
|
{
|
|
var a = TempHelper.UniversalOpenCom(25, 4800, 8, 0, 0);
|
|
|
|
button1.Text = helper.ReadCurrentDisplayValue().ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("GMH3x32E.dll not found. Install it?");
|
|
//if (true)
|
|
//{
|
|
// ProcessStartInfo procInfo = new ProcessStartInfo
|
|
// {
|
|
// WorkingDirectory = Directory.GetCurrentDirectory(),
|
|
// FileName = Path.Combine(Directory.GetCurrentDirectory(), "RegAsm4.exe"),
|
|
// Arguments = $"{Directory.GetCurrentDirectory()}\\GMH3x32E.dll",
|
|
// UseShellExecute = true,
|
|
// CreateNoWindow = false,
|
|
// WindowStyle = ProcessWindowStyle.Normal,
|
|
// Verb = "runas"
|
|
// };
|
|
|
|
// Process proc = Process.Start(procInfo);
|
|
// proc.WaitForExit();
|
|
//}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class CordonelTempLut
|
|
{
|
|
public int Id { get; set; }
|
|
public int PcbId { get; set; }
|
|
public double RefTempC { get; set; }
|
|
public double AvgTof { get; set; }
|
|
|
|
public bool Active { get; set; }
|
|
|
|
|
|
}
|
|
|
|
public class LutTemp
|
|
{
|
|
public int PcbId { get; set; }
|
|
public int FromTempC { get; set; }
|
|
public int ToTempC { get; set; }
|
|
public double? AvgRefTempC { get; set; }
|
|
public List<double> ListOfRecordedTofs { get; set; }
|
|
public double? AvgTofs { get; set; }
|
|
}
|
|
|
|
|
|
|
|
|
|
public ZeroFlowGenesisMeter _currentGenesis { get; private set; }
|
|
public MeterBatch _meterBatch { get; private set; }
|
|
|
|
|
|
private void btnConnect_Click(Object sender, EventArgs e)
|
|
{
|
|
Connect();
|
|
}
|
|
|
|
private void Connect()
|
|
{
|
|
try
|
|
{
|
|
|
|
Int32 slotNr;
|
|
|
|
if (string.IsNullOrEmpty(cbComSlot.SelectedItem.ToString()) ||
|
|
!int.TryParse(cbComSlot.SelectedItem.ToString(), out slotNr))
|
|
{
|
|
return;
|
|
}
|
|
//var drive = 1;
|
|
_currentGenesis?.DisposeMeter();
|
|
//dispose old meter
|
|
_meterBatch = new MeterBatch();
|
|
_currentGenesis = null;
|
|
Thread.Sleep(200);
|
|
|
|
//assign new meter and assign meter to FW update file if this exists
|
|
_currentGenesis = new ZeroFlowGenesisMeter(slotNr, 3, false);
|
|
|
|
//_currentGenesis.SetupFromConfigFile(slotNr);
|
|
_meterBatch.AddMeter(_currentGenesis);
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
_meterBatch.MetersLogin();
|
|
|
|
if (!_currentGenesis.IsLoggedOn)
|
|
{
|
|
Thread.Sleep(2000);
|
|
_meterBatch.MetersLogin();
|
|
}
|
|
|
|
if (!_currentGenesis.IsLoggedOn)
|
|
{
|
|
SetEnable(programmState.Start);
|
|
return;
|
|
}
|
|
|
|
|
|
var a = GetLutsText(_currentGenesis.PcbId);
|
|
//
|
|
_currentGenesis.InitMeasurement();
|
|
Invoke(new Action(() =>
|
|
{
|
|
|
|
txtLuts.Text = a;
|
|
if (LutsMissing)
|
|
{
|
|
txtLuts.BackColor = Color.IndianRed;
|
|
}
|
|
else
|
|
{
|
|
txtLuts.BackColor = Color.LightGreen;
|
|
}
|
|
lblPcbId.Text = _currentGenesis.PcbId;
|
|
SetEnable(programmState.Connected);
|
|
|
|
}));
|
|
});//.ContinueWith(delegate { ViewProgressPcb(false); });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
txtLuts.Text = "-";
|
|
MessageBox.Show(ex.Message);
|
|
SetEnable(programmState.Start);
|
|
}
|
|
}
|
|
|
|
private void btnRefreshLut_Click(object sender, EventArgs e)
|
|
{
|
|
if (!string.IsNullOrEmpty(_currentGenesis?.PcbId))
|
|
{
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
var a = GetLutsText(_currentGenesis.PcbId);
|
|
|
|
Invoke(new Action(() =>
|
|
{
|
|
txtLuts.Text = a;
|
|
if (LutsMissing)
|
|
{
|
|
txtLuts.BackColor = Color.IndianRed;
|
|
}
|
|
else
|
|
{
|
|
txtLuts.BackColor = Color.LightGreen;
|
|
}
|
|
}));
|
|
});
|
|
}
|
|
else
|
|
{
|
|
txtLuts.Text = "-";
|
|
}
|
|
|
|
}
|
|
public class RefTempRange
|
|
{
|
|
public double Min { get; set; }
|
|
public double Max { get; set; }
|
|
}
|
|
private List<RefTempRange> ReftempRangeReq = new List<RefTempRange>()
|
|
{
|
|
new RefTempRange() { Min = 7, Max= 9 },
|
|
new RefTempRange() { Min = 13,Max = 15 },
|
|
new RefTempRange() { Min = 16,Max = 18 },
|
|
new RefTempRange() { Min = 19,Max = 20 },
|
|
new RefTempRange() { Min = 20,Max = 22 },
|
|
new RefTempRange() { Min = 23,Max = 25 },
|
|
new RefTempRange() { Min = 26,Max = 28 },
|
|
new RefTempRange() { Min = 28,Max = 30 },
|
|
new RefTempRange() { Min = 33,Max = 36 },
|
|
};
|
|
private List<CordonelTempLut> cordonelTempLut = null;
|
|
private bool LutsMissing = false;
|
|
private string GetLutsText(string PcbId)
|
|
{
|
|
var ret = new StringBuilder();
|
|
var err = 0;
|
|
var resp = LocalWebRequest.GetRequest($"http://10.49.40.25/MeterProcessState/api/TempFlange/GetLut?PcbId={PcbId}");
|
|
|
|
ret.AppendLine($"Ref Temp C°\t|\t AvgTof");
|
|
|
|
var CurrentReqRequierd = new List<RefTempRange>();
|
|
CurrentReqRequierd.AddRange(ReftempRangeReq);
|
|
try
|
|
{
|
|
cordonelTempLut = JsonConvert.DeserializeObject<List<CordonelTempLut>>(resp);
|
|
|
|
foreach (var item in cordonelTempLut.OrderBy(a => a.RefTempC))
|
|
{
|
|
CurrentReqRequierd.RemoveAll(rem => item.RefTempC >= rem.Min && item.RefTempC <= rem.Max);
|
|
ret.AppendLine($"{item.RefTempC.ToString("n8")}\t|\t{item.AvgTof}");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
ret.AppendLine($"No entrys");
|
|
}
|
|
|
|
LutsMissing = false;
|
|
foreach (var item in CurrentReqRequierd)
|
|
{
|
|
LutsMissing = true;
|
|
ret.AppendLine($"{item.Min}-{item.Max}\t|\t Missing");
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
|
|
private programmState State = programmState.Start;
|
|
private enum programmState
|
|
{
|
|
Start,
|
|
Connected,
|
|
RecordRuning,
|
|
RecordStoped,
|
|
RecodCalculated,
|
|
}
|
|
|
|
private void SetEnable(programmState state)
|
|
{
|
|
State = state;
|
|
switch (State)
|
|
{
|
|
case programmState.Start:
|
|
btnStartRecord.Enabled = false;
|
|
btnStopRecord.Enabled = false;
|
|
btnAddLut.Enabled = false;
|
|
nudRef.Visible = false;
|
|
btnRefreshLut.Enabled = false;
|
|
break;
|
|
case programmState.Connected:
|
|
btnStartRecord.Enabled = true;
|
|
btnStopRecord.Enabled = false;
|
|
btnAddLut.Enabled = false;
|
|
nudRef.Visible = false;
|
|
btnRefreshLut.Enabled = true;
|
|
break;
|
|
case programmState.RecordRuning:
|
|
btnStartRecord.Enabled = false;
|
|
btnStopRecord.Enabled = true;
|
|
btnAddLut.Enabled = false;
|
|
nudRef.Visible = false;
|
|
btnRefreshLut.Enabled = true;
|
|
break;
|
|
case programmState.RecordStoped:
|
|
btnStartRecord.Enabled = true;
|
|
btnStopRecord.Enabled = false;
|
|
btnAddLut.Enabled = true;
|
|
nudRef.Visible = true;
|
|
btnRefreshLut.Enabled = true;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public void tmp()
|
|
{
|
|
|
|
//var cRecords = _currentGenesis.CurrentRecords.ToList();
|
|
//Log($"cRecords.Count={cRecords.Count}");
|
|
//if (cRecords != null && cRecords.Any())
|
|
//{
|
|
// decimal checkTempMeter = 0;
|
|
// if (MeterTempMode == TempMode.CalcTof)
|
|
// {
|
|
// //
|
|
|
|
// var sumTof = 0.0;
|
|
// var countTof = 0;
|
|
// foreach (var tofItem in cRecords.Where(c => c.Channel == 2))
|
|
// {
|
|
|
|
|
|
// if (tofItem.Channel == 2)
|
|
// {
|
|
// Log($"TotalTimeOfFlightS;{(tofItem.TotalTimeOfFlightS * 1000).ToString()}");
|
|
// Log($"RAWTEMP;{interPol(TempFromTofLUT, (tofItem.TotalTimeOfFlightS * 1000))}");
|
|
// sumTof = (tofItem.TotalTimeOfFlightS * 1000) + sumTof;
|
|
// countTof = countTof + 1;
|
|
// }
|
|
|
|
// }
|
|
|
|
// var x = (sumTof / countTof) / 1000;
|
|
|
|
|
|
// Log($"avgTof;{x}({sumTof}/{countTof})");
|
|
|
|
|
|
// checkTempMeter = interPol(TempFromTofLUT, x);
|
|
// Meter.CurrentRecords.Clear();
|
|
// }
|
|
// else
|
|
// {
|
|
// checkTempMeter = (decimal)cRecords.Last().TemperatureDegC;
|
|
// }
|
|
//}
|
|
}
|
|
|
|
private DateTimeOffset start;
|
|
|
|
|
|
private void BtnStartRecord_Click(object sender, EventArgs e)
|
|
{
|
|
start = DateTimeOffset.Now;
|
|
_currentGenesis.StartRecordData();
|
|
_currentGenesis.WriteRegister<Byte>(Register.Genesisflow.SampleRate, 16, checkRegister: true);
|
|
SetEnable(programmState.RecordRuning);
|
|
timer2.Enabled = true;
|
|
|
|
}
|
|
|
|
private void BtnStopRecord_Click(object sender, EventArgs e)
|
|
{
|
|
timer2.Enabled = false;
|
|
OutputAvgTof(true);
|
|
_currentGenesis.WriteRegister<Byte>(Register.Genesisflow.SampleRate, 0, checkRegister: true);
|
|
SetEnable(programmState.RecordStoped);
|
|
|
|
}
|
|
|
|
private void btnAddLut_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
var req = LocalWebRequest.PostRequestAsync($"http://10.49.40.25/MeterProcessState/api/TempFlange/AddLut?PcbId={_currentGenesis.PcbId}&RefTempC={nudRef.Value.ToString().Replace(",", ".")}&AvgTof={lblAvgDutTof.Text.Replace(",", ".")}");
|
|
|
|
|
|
SetEnable(programmState.Connected);
|
|
}
|
|
|
|
private void btnCheck_Click(object sender, EventArgs e)
|
|
{
|
|
if (btnCheck.Text == "Start Check")
|
|
{
|
|
_currentGenesis.StartRecordData();
|
|
timer1.Enabled = true;
|
|
|
|
btnCheck.Text = "Stop Check";
|
|
}
|
|
else
|
|
{
|
|
|
|
_currentGenesis.StopRecordData();
|
|
timer1.Enabled = false;
|
|
btnCheck.Text = "Start Check";
|
|
}
|
|
}
|
|
|
|
private static decimal interPol(List<CordonelTempLut> points, double x)
|
|
{
|
|
var x1 = 0.000;
|
|
var y1 = 0.000;
|
|
var x2 = 0.000;
|
|
var y2 = 0.000;
|
|
decimal checkTempMeter;
|
|
if ((x <= points[0].AvgTof) && (x >= points.Last().AvgTof))
|
|
{
|
|
var i = 0;
|
|
foreach (var val in points)
|
|
{
|
|
if (points[i].AvgTof <= x)
|
|
{
|
|
x1 = points[i - 1].AvgTof;
|
|
y1 = points[i - 1].RefTempC;
|
|
x2 = points[i].AvgTof;
|
|
y2 = points[i].RefTempC;
|
|
break;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
|
|
checkTempMeter = (decimal)((y2 - y1) / (x2 - x1) * (x - x1) + y1);
|
|
}
|
|
else
|
|
{
|
|
checkTempMeter = 1;
|
|
}
|
|
|
|
return checkTempMeter;
|
|
}
|
|
|
|
private void timer1_Tick(object sender, EventArgs e)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
var cRecords = _currentGenesis.CurrentRecords.ToList();
|
|
|
|
|
|
if (cRecords != null && cRecords.Any())
|
|
{
|
|
decimal checkTempMeter = 0;
|
|
|
|
//
|
|
|
|
var sumTof = 0.0;
|
|
var countTof = 0;
|
|
foreach (var tofItem in cRecords.Where(c => c.Channel == 2))
|
|
{
|
|
|
|
|
|
if (tofItem.Channel == 2)
|
|
{
|
|
sumTof = (tofItem.TotalTimeOfFlightS * 1000) + sumTof;
|
|
countTof = countTof + 1;
|
|
}
|
|
|
|
}
|
|
|
|
var x = (sumTof / countTof) / 1000;
|
|
|
|
checkTempMeter = interPol(cordonelTempLut, x);
|
|
|
|
var a = new Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages.MeasurementRecords.CalibrationRecord();
|
|
while (_currentGenesis.CurrentRecords.TryDequeue(out a))
|
|
{
|
|
// do nothing
|
|
}
|
|
|
|
lblCheck.Text = checkTempMeter.ToString();
|
|
|
|
}
|
|
else
|
|
{
|
|
lblCheck.Text = $"temp NA";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblCheck.Text = $"temp NA - {ex.Message}";
|
|
|
|
}
|
|
}
|
|
|
|
private void timer2_Tick(object sender, EventArgs e)
|
|
{
|
|
if (State == programmState.RecordRuning)
|
|
{
|
|
OutputAvgTof();
|
|
|
|
}
|
|
}
|
|
|
|
private void OutputAvgTof(bool stop = false)
|
|
{
|
|
try
|
|
{
|
|
StringBuilder cTofs = new StringBuilder();
|
|
var listOfRec = _currentGenesis.CurrentRecords.Where(s => s.Channel == 2).ToList();
|
|
if (stop)
|
|
{
|
|
_currentGenesis.StopRecordData();
|
|
}
|
|
|
|
var list = listOfRec.Select(s => new { ToF = s.TotalTimeOfFlightS * 1000, Channel = s.Channel });
|
|
foreach (var item in list.OrderBy(o => o.Channel))
|
|
{
|
|
cTofs.Append(item.Channel);
|
|
cTofs.Append(":");
|
|
cTofs.AppendLine((item.ToF / 1000).ToString());
|
|
}
|
|
lblAvgDutTof.Text = (list.Average(s => s.ToF) / 1000).ToString();
|
|
txtCurrentTofs.Text = cTofs.ToString();
|
|
lblLines.Text = list.Count().ToString(); ;
|
|
lblDuration.Text = $"{(DateTimeOffset.Now - start).TotalSeconds}S";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
txtCurrentTofs.Text = ex.Message;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|