/// /// Copyright (c) 2021-2023 Sensus Slovensko a.s. /// using System; namespace Common { public static class Utils { public static string GetTestName(string name, int repeats, int repetitionNr) { if (name == null) return null; if (repeats == 1) return name; return string.Format("{0} ({1}/{2})", name, repetitionNr, repeats); } public static string ToNiceString(double value, int sigDigits) { string format = SignificantDigitsToFmt(value, sigDigits); return (format == "F0") ? value.ToString("F0") : value.ToString(format).TrimEnd(new char[] { '0' }); } public static FlowNames ProfileType2FlowNames(ProfileType pt, bool isAux = false) { switch (pt) { case ProfileType.Q3R: case ProfileType.CompoundQ3RQ3R: return FlowNames.Q3Q2Q1; case ProfileType.CompoundQ3RQnClass: return isAux ? FlowNames.QnQtQmin : FlowNames.Q3Q2Q1; case ProfileType.QnClassColdWater: case ProfileType.QnClassHotWater: case ProfileType.CompoundQnClassQnClass: return FlowNames.QnQtQmin; case ProfileType.CompoundQnClassQ3R: return isAux ? FlowNames.Q3Q2Q1 : FlowNames.QnQtQmin; case ProfileType.QpQi: case ProfileType.CompoundQpQiQpQi: return FlowNames.QpQi; default: return FlowNames.Q3Q2Q1; } } public static string SignificantDigitsToFmt(double value, int sigDigits) { if (sigDigits == 6) { if (value >= 99999.5 || value < -99999.5) return "F0"; else if (value >= 9999.95 || value < -9999.95) return "F1"; else if (value >= 999.995 || value < -999.995) return "F2"; else if (value >= 99.9995 || value < -99.9995) return "F3"; else if (value >= 9.99995 || value < -9.99995) return "F4"; else if (value >= 0.999995 || value < -0.999995) return "F5"; else if (value >= 0.0999995 || value < -0.0999995) return "F6"; else if (value >= 0.00999995 || value < -0.00999995) return "F7"; else if (value >= 0.000999995 || value < -0.000999995) return "F8"; else if (value >= 0.0000999995 || value < -0.0000999995) return "F9"; else return "F10"; } else if (sigDigits == 5) { if (value >= 9999.5 || value < -9999.5) return "F0"; else if (value >= 999.95 || value < -999.95) return "F1"; else if (value >= 99.995 || value < -99.995) return "F2"; else if (value >= 9.9995 || value < -9.9995) return "F3"; else if (value >= 0.99995 || value < -0.99995) return "F4"; else if (value >= 0.099995 || value < -0.099995) return "F5"; else if (value >= 0.0099995 || value < -0.0099995) return "F6"; else if (value >= 0.00099995 || value < -0.00099995) return "F7"; else if (value >= 0.000099995 || value < -0.000099995) return "F8"; else return "F9"; } else if (sigDigits == 4) { if (value >= 999.5 || value < -999.5) return "F0"; else if (value >= 99.95 || value < -99.95) return "F1"; else if (value >= 9.995 || value < -9.995) return "F2"; else if (value >= 0.9995 || value < -0.9995) return "F3"; else if (value >= 0.09995 || value < -0.09995) return "F4"; else if (value >= 0.009995 || value < -0.009995) return "F5"; else if (value >= 0.0009995 || value < -0.0009995) return "F6"; else if (value >= 0.00009995 || value < -0.00009995) return "F7"; else return "F8"; } else if (sigDigits == 3) { if (value >= 99.5 || value < -99.5) return "F0"; else if (value >= 9.95 || value < -9.95) return "F1"; else if (value >= 0.995 || value < -0.995) return "F2"; else if (value >= 0.0995 || value < -0.0995) return "F3"; else if (value >= 0.00995 || value < -0.00995) return "F4"; else if (value >= 0.000995 || value < -0.000995) return "F5"; else if (value >= 0.0000995 || value < -0.0000995) return "F6"; else return "F7"; } else if (sigDigits == 2) { if (value >= 9.5 || value < -9.5) return "F0"; else if (value >= 0.95 || value < -0.95) return "F1"; else if (value >= 0.095 || value < -0.095) return "F2"; else if (value >= 0.0095 || value < -0.0095) return "F3"; else if (value >= 0.00095 || value < -0.00095) return "F4"; else if (value >= 0.000095 || value < -0.000095) return "F5"; else return "F6"; } else /// if (sigDigits == 1) { if (value >= 0.95 || value < -0.95) return "F0"; else if (value >= 0.095 || value < -0.095) return "F1"; else if (value >= 0.0095 || value < -0.0095) return "F2"; else if (value >= 0.00095 || value < -0.00095) return "F3"; else if (value >= 0.000095 || value < -0.000095) return "F4"; else return "F5"; } } /// /// Get either Q1 or Qmin flow for a given Qn and metrological class /// /// Nominal flow in m3/h /// Metrological class ("A" ,"B", "C" or "R#") /// NotSpecified, ColdWater or HotWater /// Default flow when metrologic does not match pattern /// Q1 or Qmin flow in m3/h public static double GetQ1Qmin(double Qn, string metroClass, Medium medium = Medium.ColdWater, double Qdflt = 0) { if (string.IsNullOrEmpty(metroClass)) return Qdflt; int metroClassRatio; if ((metroClass[0] == 'R') && int.TryParse(metroClass.Substring(1), out metroClassRatio) && (metroClassRatio > 0)) { return Qn / (double)metroClassRatio; } else { if (medium == Medium.HotWater) { switch (metroClass) { case "A": return (Qn < 15) ? (0.04 * Qn) : (0.08 * Qn); case "B": return (Qn < 15) ? (0.02 * Qn) : (0.04 * Qn); case "C": return (Qn < 15) ? (0.01 * Qn) : (0.02 * Qn); case "D": return (Qn < 15) ? (0.01 * Qn) : Qdflt; default: break; } } else { switch (metroClass) { case "A": return (Qn < 15) ? (0.04 * Qn) : (0.08 * Qn); case "B": return (Qn < 15) ? (0.02 * Qn) : (0.03 * Qn); case "C": return (Qn < 15) ? (0.01 * Qn) : (0.006 * Qn); default: break; } } } return Qdflt; } /// /// Get either Q2 or Qt flow for a given Qn and metrological class /// /// Nominal flow in m3/h /// Metrological class ("A" ,"B", "C" or "R#") /// NotSpecified, ColdWater or HotWater /// Default flow when metrologic does not match pattern /// Q2 or Qt flow in m3/h public static double GetQ2Qt(double Qn, string metroClass, Medium medium = Medium.ColdWater, double Qdflt = 0) { if (string.IsNullOrEmpty(metroClass)) return Qdflt; int metroClassRatio; if ((metroClass[0] == 'R') && int.TryParse(metroClass.Substring(1), out metroClassRatio) && (metroClassRatio > 0)) { return 1.6 * Qn / (double)metroClassRatio; } if (medium == Medium.HotWater) { switch (metroClass) { case "A": return (Qn < 15) ? (0.1 * Qn) : (0.2 * Qn); case "B": return (Qn < 15) ? (0.08 * Qn) : (0.15 * Qn); case "C": return (Qn < 15) ? (0.06 * Qn) : (0.1 * Qn); case "D": return (Qn < 15) ? (0.015 * Qn) : Qdflt; default: break; } } else { switch (metroClass) { case "A": return (Qn < 15) ? (0.1 * Qn) : (0.3 * Qn); case "B": return (Qn < 15) ? (0.08 * Qn) : (0.2 * Qn); case "C": return (Qn < 15) ? (0.015 * Qn) : (0.015 * Qn); default: break; } } return Qdflt; } /// /// Get either Q4 or Qmax flow for a given Qn and metrological class /// /// Nominal flow in m3/h /// Metrological class ("A" ,"B", "C" or "R#") /// NotSpecified, ColdWater or HotWater /// Default flow when metrologic does not match pattern /// Q4 or Qmax flow in m3/h public static double GetQ4Qmax(double Qn, string metroClass, Medium medium = Medium.ColdWater, double Qdflt = 0) { if (string.IsNullOrEmpty(metroClass)) return Qdflt; switch (metroClass[0]) { case 'A': case 'B': case 'C': case 'D': return 2 * Qn; case 'R': return 1.25 * Qn; default: return Qdflt; } } /// /// Checks requirements on the password regardless of the password history /// /// Password /// true when the password is OK public static bool IsPasswordMeetsRequirements(string password, out string explanation) { int length = string.IsNullOrEmpty(password) ? 0 : password.Length; /// Password length must be at least 6 if (length < CurrentUser.MinPasswdLength) { explanation = string.Format("Password must have at least {0} characters", CurrentUser.MinPasswdLength); return false; } else { explanation = string.Empty; return true; } } /// /// getHash encrypts a string /// /// the string to encrypt /// public static string GetHash(string text) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text); System.Security.Cryptography.SHA512Managed hashstring = new System.Security.Cryptography.SHA512Managed(); byte[] hash = hashstring.ComputeHash(bytes); string hashString = string.Empty; foreach (byte x in hash) { hashString += String.Format("{0:x2}", x); } return hashString; } public static string EncodedStrToUserName(string encodedStr) { string[] subStrings = encodedStr.Split(new char[] { '~' }); return (subStrings.Length >= 1) ? subStrings[0] : string.Empty; } public static string EncodedStrToFullName(string encodedStr) { string[] subStrings = encodedStr.Split(new char[] { '~' }); return (subStrings.Length >= 2) ? subStrings[1] : string.Empty; } public static string EncodedStrToLegalizator(string encodedStr) { string[] subStrings = encodedStr.Split(new char[] { '~' }); return (subStrings.Length >= 3) ? subStrings[2] : string.Empty; } /// /// Returns 'true' when authentication data are valid for a power user. /// /// User name /// Password /// true = authenticated, false = refused public static bool IsPowerUser(string userName, string password) { DateTime date = DateTime.Now.Date; int number = (date.Year % 10) + 10 * (date.Month - 1) + 120 * date.Day; string passwordOfDay = Convert.ToString(number, 8); return (userName.Equals("milan") && password.Equals("kraken")) || (userName.Equals("igor") && password.Equals("mojronko8")) || (userName.Equals("lubo1212") && password.Equals("Tatry52")) || (userName.Equals("Michal") && password.Equals("Plok789456123")) || (userName.Equals("martin") && password.Equals("vaclavek84")) || (userName.Equals("pakan") && password.Equals("kuriatko")) || (userName.Equals("jan") && password.Equals("jaugust")) || (userName.Equals("JCermak") && password.Equals("Zt6911")) || (userName.Equals("gilles") && password.Equals("alibaba")) || (userName.Equals("michal") && password.Equals("mojnelson8")) || //...MF (userName.Equals("marek") && password.Equals("mareciino")) || //...MF (userName.Equals(passwordOfDay) && password.Equals(passwordOfDay)); } public static void CalculateFeatureVector(Iperl.OptoTelegramRaw[] optoData, int optoDataCount, int startIx, int endIx, ref float[] x) { if (startIx < 0 || endIx >= optoDataCount || startIx >= endIx) return; int n = 0; Int64 sumFlow = 0; Int64 sumFlow2 = 0; Int64 sumEmf = 0; Int64 sumEmf2 = 0; Int64 sumMagF = 0; Int64 sumMagF2 = 0; Int64 sumImp = 0; Int64 sumImp2 = 0; for (int ix = startIx; ix <= endIx; ix++) { if (optoData[ix].Flags == Iperl.OptoTelegramFlags.OK || optoData[ix].Flags == Iperl.OptoTelegramFlags.OK_TestStart || optoData[ix].Flags == Iperl.OptoTelegramFlags.OK_TestEnd) { n++; sumFlow += optoData[ix].FlowRaw; sumFlow2 += ((int)optoData[ix].FlowRaw * (int)optoData[ix].FlowRaw); sumEmf += (int)optoData[ix].EmfRaw; sumEmf2 += ((Int64)optoData[ix].EmfRaw * (Int64)optoData[ix].EmfRaw); sumMagF += optoData[ix].MagneticFieldRaw; sumMagF2 += ((int)optoData[ix].MagneticFieldRaw * (int)optoData[ix].MagneticFieldRaw); sumImp += optoData[ix].Impedance; sumImp2 += ((int)optoData[ix].Impedance * (int)optoData[ix].Impedance); } } if (n >= 2) { if (x.Length > 0) x[0] = ((float)sumFlow2 - (float)sumFlow * (float)sumFlow / n) / (n - 1); if (x.Length > 1) x[1] = ((float)sumEmf2 - (float)sumEmf * (float)sumEmf / n) / (n - 1); if (x.Length > 2) x[2] = ((float)sumMagF2 - (float)sumMagF * (float)sumMagF / n) / (n - 1); if (x.Length > 3) x[3] = ((float)sumImp2 - (float)sumImp * (float)sumImp / n) / (n - 1); } } } }