tbf/TBF/Utils.cs

363 lines
16 KiB
C#

///
/// 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
{
/// <summary>
/// Parse unsigned float
/// </summary>
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);
}
/// <summary>
/// Parse float, sign allowed
/// </summary>
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);
}
/// <summary>
/// Parse float, sign allowed, exponent allowed
/// </summary>
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);
}
/// <summary>
/// Parse unsigned double
/// </summary>
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);
}
/// <summary>
/// Parse double, sign allowed
/// </summary>
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);
}
/// <summary>
/// Parse double, sign allowed, exponent allowed
/// </summary>
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);
}
/// <summary>
/// Get a list of IValve-s from an entity implementing IHasValves
/// </summary>
/// <param name="entity">Entity implementing IHasValves</param>
/// <returns>A list of IValves</returns>
public static IList<BenchControl.GenericDevices.IValve> ValvesOpen(IHasValves entity)
{
var valvesOpen = new List<BenchControl.GenericDevices.IValve>();
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;
}
/// <summary>
/// Get a list of IValve-s from an entity implementing IHasValves
/// </summary>
/// <param name="entity">Entity implementing IHasValves</param>
/// <returns>A list of IValves</returns>
public static IList<BenchControl.GenericDevices.IValve> ValvesClose(IHasValves entity)
{
var valvesClose = new List<BenchControl.GenericDevices.IValve>();
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;
}
/// <summary>
/// Get an array of RegulValve positions from a TransitionStep entity
/// </summary>
/// <param name="entity">TransitionStep entity</param>
/// <returns>float[] of regulation valve positions from 0 to 100.0f</returns>
public static float[] GetRegulValvesPositions(TransitionStep step)
{
int regvCount = BenchControl.Sequences.SequenceBase.RegulValves.Count;
float[] result = new float[regvCount];
string[] rvPosStr = (step.RegulValvesPct != null) ? step.RegulValvesPct.Split(new char[] { ';' }) : new string[0];
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;
}
/// <summary>
/// Get an array of Pump-With-FM power percentages from a TransitionStep entity
/// </summary>
/// <param name="entity">TransitionStep entity</param>
/// <returns>float[] of FM-pump power percentages from 0 to 100.0f</returns>
public static float[] GetPumpWithFMPcts(TransitionStep step)
{
int fmPumpCount = BenchControl.Sequences.SequenceBase.PumpsWithFM.Count;
float[] result = new float[fmPumpCount];
string[] fmPctsStr = (step.PumpWithFMPcts != null) ? step.PumpWithFMPcts.Split(new char[] { ';' }) : new string[0];
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;
}
/// <summary>
/// To prepare the last argument of GetSortedTestResults() and GetDecoratedTestNames() functions
/// </summary>
/// <param name="wmNr1">Watermeter nr. (1-based)</param>
/// <param name="combined">Meter: false=standard, true=combined</param>
/// <returns>Part nr. of the meter</returns>
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;
}
/// <summary>
/// Return test title for a given test data and a repetition number
/// </summary>
/// <param name="testData">TestData entity</param>
/// <param name="repetitonNr">1 .. Nr. repetitions</param>
/// <returns>Test title (string)</returns>
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
}
}
/// <summary>
/// Converts number to a string with the specified number of significant digits
/// </summary>
/// <param name="value">Float value to be converted to a string</param>
/// <param name="significantDigits">Number of significant digits: 5, 4, 3, 2 or 1 (otherwise a full precision number is printed)</param>
/// <returns>String representation of a float number</returns>
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);
}
/// <summary>
/// Converts a double number to a string with the specified number of significant digits
/// </summary>
/// <param name="value">Double value to be converted to a string</param>
/// <param name="significantDigits">Number of significant digits: 5, 4, 3, 2 or 1 (otherwise a full precision number is printed)</param>
/// <param name="altCulture">true = use alternative culture (use decimal comma)</param>
/// <returns>String representation of a double number</returns>
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('|');
}
}
}