DB.SensusOracle : Automatic WMTypeId assignment in progress.
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using log4net;
|
||||
#if MANAGED
|
||||
using Oracle.ManagedDataAccess.Client;
|
||||
#else
|
||||
using Oracle.DataAccess.Client;
|
||||
#endif
|
||||
using Common;
|
||||
|
||||
namespace TBF.Rig.Output.DB.SensusOracle
|
||||
{
|
||||
public class VtWztypSd
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(VtWztypSd));
|
||||
|
||||
public int IdWztyp;
|
||||
public int RevWztyp;
|
||||
public string Wztyp;
|
||||
public string Art;
|
||||
public string Metrolklasse;
|
||||
public string Zulasszeichen;
|
||||
public double Impulsrate;
|
||||
public double Kontaktwert;
|
||||
public bool Warmpruefen;
|
||||
public bool Optikaufsetzen;
|
||||
public string Formatausdruck;
|
||||
public string Formatetikett;
|
||||
public string Formatnetzwerk;
|
||||
public string Formatzaehlernr;
|
||||
public int IdWzgruppe;
|
||||
public int Anzahlpruefungen;
|
||||
public string Materialnr;
|
||||
public float Qmax;
|
||||
public byte Pruefart;
|
||||
public byte Zaehlerflag;
|
||||
public double Korrekturwert;
|
||||
public int IdBenutzer;
|
||||
public DateTime Aenderungsdatum;
|
||||
public string Bemerkung;
|
||||
public bool Verwendet;
|
||||
public IList<VtPruefpunktSollSd> ListOfTests; /// null = To be loaded
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
public VtWztypSd()
|
||||
{
|
||||
ListOfTests = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create VtWztypSd object from a batch.
|
||||
/// </summary>
|
||||
/// <param name="batch"></param>
|
||||
/// <returns></returns>
|
||||
public static VtWztypSd FromBatch(Results.Entities.Batch batch, int idBenutzer)
|
||||
{
|
||||
var sampleWM = batch.WaterMeters.FirstOrDefault(x => (x != null && !x.Disabled && x.WaterMeterData != null));
|
||||
if (sampleWM == null) return null;
|
||||
var wmData = sampleWM.WaterMeterData;
|
||||
if (wmData == null) return null;
|
||||
|
||||
int adjTestsCount = 0; /// Negative number
|
||||
int finalTestsCount = 0; /// Positive number
|
||||
foreach (var tr in batch.TestRslts)
|
||||
{
|
||||
if (tr.TestData.Publish == (sbyte)Publish.OnScreen)
|
||||
adjTestsCount--;
|
||||
if (tr.TestData.Publish == (sbyte)Publish.Always && tr.TestData.Evaluate)
|
||||
finalTestsCount++;
|
||||
}
|
||||
|
||||
var listOfTests = new List<VtPruefpunktSollSd>();
|
||||
foreach (var tr in batch.TestRslts)
|
||||
{
|
||||
if (tr.TestData.Publish == (sbyte)Publish.OnScreen)
|
||||
listOfTests.Add(VtPruefpunktSollSd.FromTestRslt(tr, adjTestsCount++, (float)wmData.Q3_Qn));
|
||||
if (tr.TestData.Publish == (sbyte)Publish.Always && tr.TestData.Evaluate)
|
||||
listOfTests.Add(VtPruefpunktSollSd.FromTestRslt(tr, finalTestsCount--, (float)wmData.Q3_Qn));
|
||||
}
|
||||
|
||||
var rslt = new VtWztypSd()
|
||||
{
|
||||
IdWztyp = 0,
|
||||
RevWztyp = 0,
|
||||
Wztyp = batch.ProcedureName,
|
||||
Art = "VT",
|
||||
Metrolklasse = wmData.MetrologicalClass,
|
||||
Zulasszeichen = wmData.ApprovalInfo,
|
||||
Impulsrate = 0, // TODO
|
||||
Kontaktwert = 0, // TODO
|
||||
Warmpruefen = false,
|
||||
Optikaufsetzen = true,
|
||||
Formatausdruck = string.Empty, // TODO
|
||||
Formatetikett = string.Empty, // TODO
|
||||
Formatnetzwerk = string.Empty, // TODO
|
||||
Formatzaehlernr = string.Empty, // TODO
|
||||
IdWzgruppe = 0,
|
||||
Anzahlpruefungen = listOfTests.Count,
|
||||
Materialnr = string.Empty, // TODO
|
||||
Qmax = (float)wmData.Q3_Qn,
|
||||
Pruefart = 0,
|
||||
Zaehlerflag = 0,
|
||||
Korrekturwert = 0,
|
||||
IdBenutzer = idBenutzer,
|
||||
Aenderungsdatum = DateTime.Now,
|
||||
Bemerkung = string.Empty, // TODO
|
||||
Verwendet = true,
|
||||
ListOfTests = listOfTests,
|
||||
};
|
||||
|
||||
log.DebugFormat("FromProcedure: [{0}] created from proc. {1}", rslt, batch.ProcedureName);
|
||||
|
||||
return rslt;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("Id={0} Rev={1} Q3={2} metro={3}", IdWztyp, RevWztyp, Qmax, Metrolklasse);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two VtWztypSd objects except of IdWztyp, RevWztyp,
|
||||
/// IdBenutzer, Aenderungsdatum, Bemerkung and Verwendet members.
|
||||
/// Tollerance for Impulsrate and Kontaktwert is 1/100000.
|
||||
/// Tolleranve for Qmax and Korrekturwert is 1/1000 (0.1%).
|
||||
/// </summary>
|
||||
/// <param name="p1">VtWztypSd object 1/param>
|
||||
/// <param name="p2">VtWztypSd object 2</param>
|
||||
/// <returns>true when equal</returns>
|
||||
public static bool AreEqual(VtWztypSd p1, VtWztypSd p2, Database db = null, OracleConnection conn = null)
|
||||
{
|
||||
if (p1 == null && p2 == null) return true; /// Both are null --> are equal
|
||||
if (p1 == null || p2 == null) return false; /// One is null --> not equal
|
||||
|
||||
bool equal = p1.Wztyp == p2.Wztyp &&
|
||||
p1.Art == p2.Art &&
|
||||
p1.Metrolklasse == p2.Metrolklasse &&
|
||||
p1.Zulasszeichen == p2.Zulasszeichen &&
|
||||
((p1.Impulsrate == 0 && p2.Impulsrate == 0)
|
||||
|| 2 * Math.Abs(p1.Impulsrate - p2.Impulsrate) / (p1.Impulsrate + p2.Impulsrate) < 0.00001) &&
|
||||
((p1.Kontaktwert == 0 && p2.Kontaktwert == 0)
|
||||
|| 2 * Math.Abs(p1.Kontaktwert - p2.Kontaktwert) / (p1.Kontaktwert + p2.Kontaktwert) < 0.00001) &&
|
||||
p1.Warmpruefen == p2.Warmpruefen &&
|
||||
p1.Optikaufsetzen == p2.Optikaufsetzen &&
|
||||
p1.Formatausdruck == p2.Formatausdruck &&
|
||||
p1.Formatetikett == p2.Formatetikett &&
|
||||
p1.Formatnetzwerk == p2.Formatnetzwerk &&
|
||||
p1.Formatzaehlernr == p2.Formatzaehlernr &&
|
||||
p1.IdWzgruppe == p2.IdWzgruppe &&
|
||||
p1.Anzahlpruefungen == p2.Anzahlpruefungen &&
|
||||
p1.Materialnr == p2.Materialnr &&
|
||||
((p1.Qmax == 0 && p2.Qmax == 0)
|
||||
|| 2 * Math.Abs(p1.Qmax - p2.Qmax) / (p1.Qmax + p2.Qmax) < 0.001) &&
|
||||
p1.Pruefart == p2.Pruefart &&
|
||||
p1.Zaehlerflag == p2.Zaehlerflag &&
|
||||
((p1.Korrekturwert == 0 && p2.Korrekturwert == 0)
|
||||
|| 2 * Math.Abs(p1.Korrekturwert - p2.Korrekturwert) / (p1.Korrekturwert + p2.Korrekturwert) < 0.001);
|
||||
|
||||
if (!equal)
|
||||
{
|
||||
return false; /// Two procedures are different regardless of their tests
|
||||
}
|
||||
|
||||
/// Load information about test form Oracle if it is missing
|
||||
if (((p1.ListOfTests == null && p1.IdWztyp > 0 && p1.RevWztyp > 0) ||
|
||||
(p2.ListOfTests == null && p2.IdWztyp > 0 && p2.RevWztyp > 0)) && db != null && conn != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
if (p1.ListOfTests == null && p1.IdWztyp > 0 && p1.RevWztyp > 0)
|
||||
{
|
||||
p1.ListOfTests = db.ReadVtPruefpunktSollSd(conn, p1.IdWztyp, p1.RevWztyp);
|
||||
}
|
||||
|
||||
if (p2.ListOfTests == null && p2.IdWztyp > 0 && p2.RevWztyp > 0)
|
||||
{
|
||||
p2.ListOfTests = db.ReadVtPruefpunktSollSd(conn, p2.IdWztyp, p2.RevWztyp);
|
||||
}
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
log.ErrorFormat("Failed to read from VT_PRUEFPUNKT_SOLL_SD: {0}", exc.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (p1.ListOfTests == null || p2.ListOfTests == null)
|
||||
{
|
||||
log.ErrorFormat("Information about tests is missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p1.ListOfTests.Count != p2.ListOfTests.Count)
|
||||
{
|
||||
return false; /// Tests counts of two procedures are different
|
||||
}
|
||||
|
||||
for (int i = 0; i < p1.ListOfTests.Count; i++)
|
||||
{
|
||||
if (!VtPruefpunktSollSd.AreEqual(p1.ListOfTests[i], p2.ListOfTests[i]))
|
||||
{
|
||||
return false; /// One pair of tests is different, tests are not equal
|
||||
}
|
||||
}
|
||||
|
||||
log.InfoFormat("Procedures are equal: {0}/{1}({2}) and {3}/{4}({5})",
|
||||
p1.IdWztyp, p1.RevWztyp, p1.Wztyp, p2.IdWztyp, p2.RevWztyp, p2.Wztyp);
|
||||
|
||||
return true; /// Two procedures are equal
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user