/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using Config.Entities; using Dirichlet.Numerics; namespace TBF { public class Utils { /// /// Parse unsigned float /// public static bool TryParseUFloat(string text, out float result) { return float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result) || float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result)); } public static float ParseUFloat(string text, float dfltVal) { float result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result)) return result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result)) return result; return dfltVal; } public static float ParseUFloat(string text) { return ParseUFloat(text, 0); } /// /// Parse float, sign allowed /// public static bool TryParseSFloat(string text, out float result) { return float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result) || float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Program.AltCulture, out result)); } public static float ParseSFloat(string text, float dfltVal) { float result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result)) return result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Program.AltCulture, out result)) return result; return dfltVal; } public static float ParseSFloat(string text) { return ParseSFloat(text, 0); } /// /// Parse float, sign allowed, exponent allowed /// public static bool TryParseEFloat(string text, out float result) { return float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result) || float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, Program.AltCulture, out result)); } public static float ParseEFloat(string text, float dfltVal) { float result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result)) return result; if (float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, Program.AltCulture, out result)) return result; return dfltVal; } public static float ParseEFloat(string text) { return ParseEFloat(text, 0); } /// /// Parse unsigned double /// public static bool TryParseUDouble(string text, out double result) { return double.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result) || double.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result)); } public static double ParseUDouble(string text, double dfltVal) { double result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out result)) return result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result)) return result; return dfltVal; } public static double ParseUDouble(string text) { return ParseUDouble(text, 0); } /// /// Parse double, sign allowed /// public static bool TryParseSDouble(string text, out double result) { return double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result) || double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Program.AltCulture, out result)); } public static double ParseSDouble(string text, double dfltVal) { double result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out result)) return result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Program.AltCulture, out result)) return result; return dfltVal; } public static double ParseSDouble(string text) { return ParseSDouble(text, 0); } /// /// Parse double, sign allowed, exponent allowed /// public static bool TryParseEDouble(string text, out double result) { return double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result) || double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result) || (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, Program.AltCulture, out result)); } public static double ParseEDouble(string text, double dfltVal) { double result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.CurrentCulture, out result)) return result; if (double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result)) return result; if (Program.AltCulture != null && double.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, Program.AltCulture, out result)) return result; return dfltVal; } public static double ParseEDouble(string text) { return ParseEDouble(text, 0); } /// /// Get a list of IValve-s from an entity implementing IHasValves /// /// Entity implementing IHasValves /// A list of IValves public static IList ValvesOpen(IHasValves entity) { var valvesOpen = new List(); if (entity != null && entity.ValvesOpen != null && entity.ValvesOpen != string.Empty) { string[] vOpen = entity.ValvesOpen.Split(new char[] { ';' }); foreach (var vName in vOpen) { valvesOpen.Add((BenchControl.GenericDevices.IValve)BenchControl.TbfComponents.FindComponent(vName)); } } return valvesOpen; } /// /// Get a list of IValve-s from an entity implementing IHasValves /// /// Entity implementing IHasValves /// A list of IValves public static IList ValvesClose(IHasValves entity) { var valvesClose = new List(); if (entity != null && entity.ValvesClose != null && entity.ValvesClose != string.Empty) { string[] vClose = entity.ValvesClose.Split(new char[] { ';' }); foreach (var vName in vClose) { valvesClose.Add((BenchControl.GenericDevices.IValve)BenchControl.TbfComponents.FindComponent(vName)); } } return valvesClose; } /// /// Get an array of RegulValve positions from a string /// /// String from TransitionStep entity defining all RegulValve positions /// float[] of regulation valve positions from 0 to 100.0f public static float[] GetRegulValvesPositions(string regulValvesPct) { int regvCount = BenchControl.Sequences.SequenceBase.RegulValves.Count; float[] result = new float[regvCount]; string[] rvPosStr = string.IsNullOrEmpty(regulValvesPct) ? new string[0] : regulValvesPct.Split(new char[] { ';' }); for (int i = 0; i < regvCount; i++) { float fdummy; if ((i < rvPosStr.Length) && Utils.TryParseUFloat(rvPosStr[i], out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f)) { result[i] = fdummy; } else // if ((i < rvPosStr.Length) && rvPosStr[i].Equals("---")) { result[i] = -1.0f; /// Negative value means no regulation valve position change } } return result; } /// /// Get an array of Pump-With-FM power percentages from a string /// /// String from TransitionStep entity defining all FM pump powers /// float[] of FM-pump power percentages from 0 to 100.0f public static float[] GetPumpWithFMPcts(string pumpWithFMPcts) { int fmPumpCount = BenchControl.Sequences.SequenceBase.PumpsWithFM.Count; float[] result = new float[fmPumpCount]; string[] fmPctsStr = string.IsNullOrEmpty(pumpWithFMPcts) ? new string[0] : pumpWithFMPcts.Split(new char[] { ';' }); for (int i = 0; i < fmPumpCount; i++) { float fdummy; if ((i < fmPctsStr.Length) && Utils.TryParseUFloat(fmPctsStr[i], out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f)) { result[i] = fdummy; } else if ((i < fmPctsStr.Length) && fmPctsStr[i].Equals("Dflt")) { result[i] = -2.0f; /// This value means default pump power defined by the Test parameter } else //if ((i < rvPosStr.Length) && rvPosStr[i].Equals("---")) { result[i] = -1.0f; /// This value means no pump power change } } return result; } /// /// To prepare the last argument of GetSortedTestResults() and GetDecoratedTestNames() functions /// /// Watermeter nr. (1-based) /// Meter: false=standard, true=combined /// Part nr. of the meter public static int PartNr(int wmNr1, bool combined) { if (wmNr1 == 0) return 0; if (combined) return wmNr1; int wmPartNr = 1 + (wmNr1 - 1) / Config.Data.LineSize; return wmPartNr; } /// /// Return test title for a given test data and a repetition number /// /// TestData entity /// 1 .. Nr. repetitions /// Test title (string) public static string TestTitle(Results.Entities.TestData testData, int repetitonNr) { if (testData.Repeats == 1) { return testData.Name; /// Single test } else { return string.Format("{0} ({1}/{2})", testData.Name, repetitonNr, testData.Repeats); /// More test repetitions } } /// /// Converts number to a string with the specified number of significant digits /// /// Float value to be converted to a string /// Number of significant digits: 5, 4, 3, 2 or 1 (otherwise a full precision number is printed) /// String representation of a float number public static string FloatToStr(float value, int significantDigits) { return DoubleToStr(value, significantDigits); } public static string FloatToStr(float value, int significantDigits, bool altCulture) { return DoubleToStr(value, significantDigits, altCulture); } public static string DoubleToStr(double value, int significantDigits) { return DoubleToStr(value, significantDigits, false); } /// /// Converts a double number to a string with the specified number of significant digits /// /// Double value to be converted to a string /// Number of significant digits: 5, 4, 3, 2 or 1 (otherwise a full precision number is printed) /// true = use alternative culture (use decimal comma) /// String representation of a double number public static string DoubleToStr(double value, int significantDigits, bool altCulture) { if (-float.Epsilon <= value && value <= float.Epsilon) { return "0"; } else if (significantDigits <= 5) { string fmt = Config.Utils.SignificantDigitsToFmt(value, significantDigits); return altCulture ? value.ToString(fmt, Program.AltCulture) : value.ToString(fmt); } else { return altCulture ? value.ToString(Program.AltCulture) : value.ToString(); } } public static string RouteToStr(UInt128 route) { return string.Format("{0}-{1} {2}-{3}-{4}-{5}", ((route >> 80) & 0xFFFF).ToString("X4"), /// 0 ((route >> 64) & 0xFFFF).ToString("X4"), /// 1 ((route >> 48) & 0xFFFF).ToString("X4"), /// 2 ((route >> 32) & 0xFFFF).ToString("X4"), /// 3 ((route >> 16) & 0xFFFF).ToString("X4"), /// 4 (route & 0xFFFF).ToString("X4")); /// 5 } public static string ToMyString(DateTime dateTime) { return string.Format("{0}{1}{2}_{3}{4}", dateTime.Year, dateTime.Month.ToString("D2"), dateTime.Day.ToString("D2"), dateTime.Hour.ToString("D2"), dateTime.Minute.ToString("D2")); } public static bool IsGoodFName(string fname) { return !fname.Contains('\\') && !fname.Contains('/') && !fname.Contains(':') && !fname.Contains('*') && !fname.Contains('?') && !fname.Contains('"') && !fname.Contains('<') && !fname.Contains('>') && !fname.Contains('|'); } } }