2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2021-02-25 12:38:24 +00:00
|
|
|
|
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2015-02-16 20:01:37 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Globalization;
|
2021-02-25 12:38:24 +00:00
|
|
|
|
using System.IO.Ports;
|
2018-06-13 05:41:03 +00:00
|
|
|
|
using System.Linq;
|
2015-12-04 16:17:20 +00:00
|
|
|
|
using Config.Entities;
|
2016-12-12 16:25:12 +00:00
|
|
|
|
using Dirichlet.Numerics;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
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) ||
|
2015-04-28 14:17:36 +00:00
|
|
|
|
float.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result) ||
|
|
|
|
|
|
(Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result));
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2015-04-28 14:17:36 +00:00
|
|
|
|
if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint, Program.AltCulture, out result)) return result;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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) ||
|
2015-04-28 14:17:36 +00:00
|
|
|
|
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));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2015-04-28 14:17:36 +00:00
|
|
|
|
if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Program.AltCulture, out result)) return result;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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) ||
|
2015-04-28 14:17:36 +00:00
|
|
|
|
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));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2015-04-28 14:17:36 +00:00
|
|
|
|
if (Program.AltCulture != null && float.TryParse(text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent, Program.AltCulture, out result)) return result;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return dfltVal;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static float ParseEFloat(string text)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ParseEFloat(text, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-24 08:44:04 +00:00
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <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>
|
2021-03-05 13:47:21 +00:00
|
|
|
|
public static IList<Rig.GenericDevices.IValve> ValvesOpen(IHasValves entity)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-03-05 13:47:21 +00:00
|
|
|
|
var valvesOpen = new List<Rig.GenericDevices.IValve>();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (entity != null && entity.ValvesOpen != null && entity.ValvesOpen != string.Empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] vOpen = entity.ValvesOpen.Split(new char[] { ';' });
|
|
|
|
|
|
foreach (var vName in vOpen)
|
|
|
|
|
|
{
|
2021-03-05 13:47:21 +00:00
|
|
|
|
valvesOpen.Add((Rig.GenericDevices.IValve)Rig.TbfComponents.FindComponent(vName));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
2021-03-05 13:47:21 +00:00
|
|
|
|
public static IList<Rig.GenericDevices.IValve> ValvesClose(IHasValves entity)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-03-05 13:47:21 +00:00
|
|
|
|
var valvesClose = new List<Rig.GenericDevices.IValve>();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (entity != null && entity.ValvesClose != null && entity.ValvesClose != string.Empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] vClose = entity.ValvesClose.Split(new char[] { ';' });
|
|
|
|
|
|
foreach (var vName in vClose)
|
|
|
|
|
|
{
|
2021-03-05 13:47:21 +00:00
|
|
|
|
valvesClose.Add((Rig.GenericDevices.IValve)Rig.TbfComponents.FindComponent(vName));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return valvesClose;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-01 12:43:04 +00:00
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>
|
2022-10-26 12:42:00 +00:00
|
|
|
|
/// Update a list of regulation valve position pairs from a string
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// </summary>
|
2022-10-26 12:42:00 +00:00
|
|
|
|
/// <param name="regVPositionsStr">String with reg.valve positions in % separated by ';'</param>
|
|
|
|
|
|
/// <param name="regVPositions">List with reg. valve / position pairs</param>
|
|
|
|
|
|
public static void UpdateRegVPositionsFromStr(ref IList<Rig.RegValvePosition> regVPositions, string regVPositionsStr)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2022-10-26 12:42:00 +00:00
|
|
|
|
string[] rvPosStr = string.IsNullOrEmpty(regVPositionsStr) ? new string[0] : regVPositionsStr.Split(new char[] { ';' });
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2022-10-26 12:42:00 +00:00
|
|
|
|
for (int i = 0; i < Math.Min(rvPosStr.Length, regVPositions.Count); i++)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
float fdummy;
|
2022-10-26 12:42:00 +00:00
|
|
|
|
if (Utils.TryParseUFloat(rvPosStr[i], out fdummy) && fdummy >= 0 && fdummy <= 100.0f)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2022-10-26 12:42:00 +00:00
|
|
|
|
regVPositions[i].Position = fdummy;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
2022-10-26 12:42:00 +00:00
|
|
|
|
else //if (rvPosStr[i].Equals("---"))
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2022-10-26 12:42:00 +00:00
|
|
|
|
regVPositions[i].Position = -1.0f; /// Negative value means no regulation valve position change
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-09-08 11:16:31 +00:00
|
|
|
|
|
2015-02-01 12:43:04 +00:00
|
|
|
|
|
2014-09-08 11:16:31 +00:00
|
|
|
|
/// <summary>
|
2022-11-11 13:14:44 +00:00
|
|
|
|
/// Get an array of Pump-With-FM power percentages from a string
|
2014-09-08 11:16:31 +00:00
|
|
|
|
/// </summary>
|
2022-11-11 13:14:44 +00:00
|
|
|
|
/// <param name="pumpWithFMPcts">String from TransitionStep entity defining all FM pump powers</param>
|
2014-09-08 11:16:31 +00:00
|
|
|
|
/// <returns>float[] of FM-pump power percentages from 0 to 100.0f</returns>
|
2021-03-29 20:42:04 +00:00
|
|
|
|
public static float[] GetPumpWithFMPcts(string pumpWithFMPcts)
|
2014-09-08 11:16:31 +00:00
|
|
|
|
{
|
2021-03-05 13:47:21 +00:00
|
|
|
|
int fmPumpCount = Rig.Sequences.SequenceBase.PumpsWithFM.Count;
|
2014-09-08 11:16:31 +00:00
|
|
|
|
float[] result = new float[fmPumpCount];
|
|
|
|
|
|
|
2022-11-11 13:14:44 +00:00
|
|
|
|
string[] fmPctsStr = string.IsNullOrEmpty(pumpWithFMPcts) ? new string[0] : pumpWithFMPcts.Split(new char[] { ';' });
|
2014-09-08 11:16:31 +00:00
|
|
|
|
for (int i = 0; i < fmPumpCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
float fdummy;
|
2015-02-01 10:04:05 +00:00
|
|
|
|
if ((i < fmPctsStr.Length) && Utils.TryParseUFloat(fmPctsStr[i], out fdummy) &&
|
2014-09-08 11:16:31 +00:00
|
|
|
|
(fdummy >= 0) && (fdummy <= 100.0f))
|
|
|
|
|
|
{
|
|
|
|
|
|
result[i] = fdummy;
|
|
|
|
|
|
}
|
2015-02-01 10:04:05 +00:00
|
|
|
|
else if ((i < fmPctsStr.Length) && fmPctsStr[i].Equals("Dflt"))
|
2014-09-08 11:16:31 +00:00
|
|
|
|
{
|
|
|
|
|
|
result[i] = -2.0f; /// This value means default pump power defined by the Test parameter
|
|
|
|
|
|
}
|
2015-02-01 10:04:05 +00:00
|
|
|
|
else //if ((i < rvPosStr.Length) && rvPosStr[i].Equals("---"))
|
2014-09-08 11:16:31 +00:00
|
|
|
|
{
|
|
|
|
|
|
result[i] = -1.0f; /// This value means no pump power change
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2015-01-13 04:00:59 +00:00
|
|
|
|
|
2015-05-23 13:30:31 +00:00
|
|
|
|
/// <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;
|
|
|
|
|
|
|
2021-09-19 11:59:58 +00:00
|
|
|
|
int wmPartNr = 1 + (wmNr1 - 1) / TBF.Data.LineSize;
|
2019-01-14 13:32:26 +00:00
|
|
|
|
return wmPartNr;
|
2015-05-23 13:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-15 10:58:21 +00:00
|
|
|
|
/// <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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-13 04:00:59 +00:00
|
|
|
|
/// <summary>
|
2016-10-27 12:54:37 +00:00
|
|
|
|
/// Converts number to a string with the specified number of significant digits
|
2015-01-13 04:00:59 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">Float value to be converted to a string</param>
|
2016-10-27 12:54:37 +00:00
|
|
|
|
/// <param name="significantDigits">Number of significant digits: 5, 4, 3, 2 or 1 (otherwise a full precision number is printed)</param>
|
2016-01-03 01:05:35 +00:00
|
|
|
|
/// <returns>String representation of a float number</returns>
|
2016-10-27 12:54:37 +00:00
|
|
|
|
public static string FloatToStr(float value, int significantDigits)
|
2015-01-13 04:00:59 +00:00
|
|
|
|
{
|
2016-10-27 12:54:37 +00:00
|
|
|
|
return DoubleToStr(value, significantDigits);
|
2016-01-03 01:05:35 +00:00
|
|
|
|
}
|
2016-10-27 12:54:37 +00:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2016-01-03 01:05:35 +00:00
|
|
|
|
|
2016-10-27 12:54:37 +00:00
|
|
|
|
/// <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)
|
2016-01-03 01:05:35 +00:00
|
|
|
|
{
|
2017-09-27 12:45:05 +00:00
|
|
|
|
if (-float.Epsilon <= value && value <= float.Epsilon)
|
2016-10-27 12:54:37 +00:00
|
|
|
|
{
|
2017-09-27 12:45:05 +00:00
|
|
|
|
return "0";
|
2016-10-27 12:54:37 +00:00
|
|
|
|
}
|
2017-09-27 12:45:05 +00:00
|
|
|
|
else if (significantDigits <= 5)
|
|
|
|
|
|
{
|
2021-05-27 15:30:04 +00:00
|
|
|
|
string fmt = Common.Utils.SignificantDigitsToFmt(value, significantDigits);
|
2017-09-27 12:45:05 +00:00
|
|
|
|
return altCulture ? value.ToString(fmt, Program.AltCulture) : value.ToString(fmt);
|
2016-10-27 12:54:37 +00:00
|
|
|
|
}
|
2017-09-27 12:45:05 +00:00
|
|
|
|
else
|
2016-10-27 12:54:37 +00:00
|
|
|
|
{
|
2017-09-27 12:45:05 +00:00
|
|
|
|
return altCulture ? value.ToString(Program.AltCulture) : value.ToString();
|
2016-10-27 12:54:37 +00:00
|
|
|
|
}
|
2015-01-13 04:00:59 +00:00
|
|
|
|
}
|
2015-04-18 11:31:31 +00:00
|
|
|
|
|
2017-07-25 13:10:06 +00:00
|
|
|
|
public static string RouteToStr(UInt128 route)
|
2015-04-18 11:31:31 +00:00
|
|
|
|
{
|
2017-07-24 11:19:39 +00:00
|
|
|
|
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
|
2015-04-18 11:31:31 +00:00
|
|
|
|
}
|
2015-08-21 18:52:10 +00:00
|
|
|
|
|
2021-05-27 15:30:04 +00:00
|
|
|
|
public static string ToBin88(UInt128 route)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("{0}-{1}-{2}---{3}-{4}-{5}-{6}---{7}-{8}-{9}-{10}",
|
|
|
|
|
|
ToBin8((route >> 80) & 0xFF), /// 0
|
|
|
|
|
|
ToBin8((route >> 72) & 0xFF), /// 1
|
|
|
|
|
|
ToBin8((route >> 64) & 0xFF), /// 2
|
|
|
|
|
|
ToBin8((route >> 56) & 0xFF), /// 3
|
|
|
|
|
|
ToBin8((route >> 48) & 0xFF), /// 4
|
|
|
|
|
|
ToBin8((route >> 40) & 0xFF), /// 5
|
|
|
|
|
|
ToBin8((route >> 32) & 0xFF), /// 6
|
|
|
|
|
|
ToBin8((route >> 24) & 0xFF), /// 7
|
|
|
|
|
|
ToBin8((route >> 16) & 0xFF), /// 8
|
|
|
|
|
|
ToBin8((route >> 8) & 0xFF), /// 9
|
|
|
|
|
|
ToBin8(route & 0xFF)); /// 10
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-24 14:10:55 +00:00
|
|
|
|
public static string ToBin40(UInt64 val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("{0}-{1}-{2}-{3}-{4}", ToBin8((val >> 32) & 0xFF),
|
|
|
|
|
|
ToBin8((val >> 24) & 0xFF),
|
|
|
|
|
|
ToBin8((val >> 16) & 0xFF),
|
|
|
|
|
|
ToBin8((val >> 8) & 0xFF),
|
|
|
|
|
|
ToBin8(val & 0xFF));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string ToBin8(UInt64 val)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Convert.ToString((byte)val, 2);
|
|
|
|
|
|
switch (str.Length)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 1: return "0000000" + str;
|
|
|
|
|
|
case 2: return "000000" + str;
|
|
|
|
|
|
case 3: return "00000" + str;
|
|
|
|
|
|
case 4: return "0000" + str;
|
|
|
|
|
|
case 5: return "000" + str;
|
|
|
|
|
|
case 6: return "00" + str;
|
|
|
|
|
|
case 7: return "0" + str;
|
|
|
|
|
|
default: return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-21 18:52:10 +00:00
|
|
|
|
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"));
|
|
|
|
|
|
}
|
2018-06-13 05:41:03 +00:00
|
|
|
|
|
|
|
|
|
|
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('|');
|
|
|
|
|
|
}
|
2021-02-25 12:38:24 +00:00
|
|
|
|
|
|
|
|
|
|
public static Parity GetParity(string str, Parity defaultParity = Parity.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (str.Equals(Parity.None.ToString())) return Parity.None;
|
|
|
|
|
|
if (str.Equals(Parity.Even.ToString())) return Parity.Even;
|
|
|
|
|
|
if (str.Equals(Parity.Odd.ToString())) return Parity.Odd;
|
|
|
|
|
|
if (str.Equals(Parity.Mark.ToString())) return Parity.Mark;
|
|
|
|
|
|
if (str.Equals(Parity.Space.ToString())) return Parity.Space;
|
|
|
|
|
|
return defaultParity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static StopBits GetStopBits(string str, StopBits defaultStopBits = StopBits.One)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (str.Equals(StopBits.None.ToString())) return StopBits.None;
|
|
|
|
|
|
if (str.Equals(StopBits.One.ToString())) return StopBits.One;
|
|
|
|
|
|
if (str.Equals(StopBits.OnePointFive.ToString())) return StopBits.OnePointFive;
|
|
|
|
|
|
if (str.Equals(StopBits.Two.ToString())) return StopBits.Two;
|
|
|
|
|
|
return defaultStopBits;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Handshake GetHandshake(string str, Handshake defaultHandshake = Handshake.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (str.Equals(Handshake.None.ToString())) return Handshake.None;
|
|
|
|
|
|
if (str.Equals(Handshake.RequestToSend.ToString())) return Handshake.RequestToSend;
|
|
|
|
|
|
if (str.Equals(Handshake.XOnXOff.ToString())) return Handshake.XOnXOff;
|
|
|
|
|
|
if (str.Equals(Handshake.RequestToSendXOnXOff.ToString())) return Handshake.RequestToSendXOnXOff;
|
|
|
|
|
|
return defaultHandshake;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|