using System.Collections.Generic;
using System.Globalization;
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);
}
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;
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);
}
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;
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);
}
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;
return dfltVal;
}
public static float ParseEFloat(string text)
{
return ParseEFloat(text, 0);
}
///
/// Get a list of IValve-s from an entity implementing IHasValves
///
/// Entity implementing IHasValves
/// A list of IValves
public static IList ValvesOpen(Entities.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(Entities.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 TransitionStep entity
///
/// TransitionStep entity
/// float[] of regulation valve positions from 0 to 100.0f
public static float[] GetRegulValvesPositions(Entities.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 (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 TransitionStep entity
///
/// TransitionStep entity
/// float[] of FM-pump power percentages from 0 to 100.0f
public static float[] GetPumpWithFMPcts(Entities.TransitionStep step)
{
int fmPumpCount = BenchControl.Sequences.SequenceBase.PumpsWithFM.Count;
float[] result = new float[fmPumpCount];
string[] rvPosStr = (step.PumpWithFMPcts != null) ? step.PumpWithFMPcts.Split(new char[] { ';' }) : new string[0];
for (int i = 0; i < fmPumpCount; i++)
{
float fdummy;
if ((i < rvPosStr.Length) && Utils.TryParseUFloat(rvPosStr[i], out fdummy) &&
(fdummy >= 0) && (fdummy <= 100.0f))
{
result[i] = fdummy;
}
else if (rvPosStr[i].Equals("Dflt"))
{
result[i] = -2.0f; /// This value means default pump power defined by the Test parameter
}
else //if (rvPosStr[i].Equals("---"))
{
result[i] = -1.0f; /// This value means no pump power change
}
}
return result;
}
}
}