479 lines
20 KiB
C#
479 lines
20 KiB
C#
///
|
|
/// Copyright (c) 2016-2018 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Reflection;
|
|
using Config.Entities;
|
|
|
|
namespace Config
|
|
{
|
|
public enum Unit
|
|
{
|
|
[Description("---")] None,
|
|
|
|
[Description("imp.")] pulse,
|
|
#if LANG_PL
|
|
[Description("stopień")] degree,
|
|
#else
|
|
[Description("degree")] degree,
|
|
#endif
|
|
[Description("l")] l, /// * 1 liter
|
|
[Description("dm3")] dm3, /// 1 dm3 = 1 liter
|
|
[Description("m3")] m3, /// 1000 l
|
|
[Description("gal(UK)")] gal_UK, /// 1 gal(UK) = 4.54609 l
|
|
[Description("gal(US)")] gal_US, /// 1 gal(US) = 3.78541178 l
|
|
|
|
[Description("l/h")] lph, /// 1 liter/h
|
|
[Description("m3/h")] m3ph, /// * 1 m3/h = 1000 l/h
|
|
|
|
[Description("g")] g, /// 0.001 kg
|
|
[Description("kg")] kg, /// * 1 kilogram
|
|
[Description("t")] t, /// 1000 kg
|
|
[Description("lb")] lb, /// 0.45359237 kg
|
|
|
|
[Description("s")] s, /// * 1 second
|
|
[Description("min")] min, /// 1 min = 60 s
|
|
[Description("hr")] hour, /// 1 hour = 60 min = 3600 s
|
|
[Description("ms")] ms, /// 1 ms = 0.001 s
|
|
|
|
[Description("°C")] C, /// * degree Celsius (°C)
|
|
[Description("°F")] F, /// degree Fahrenheit
|
|
[Description("K")] K, /// degree Kelvin
|
|
|
|
[Description("Pa")] Pa, /// 1 Pa = 0.01 hPa
|
|
[Description("hPa")] hPa, /// 1 hPa = 100 Pa
|
|
[Description("kPa")] kPa, /// 1 kPa = 10 HPa
|
|
[Description("MPa")] MPa, /// 1 MPa = 10000 hPa
|
|
[Description("mbar")] mbar, /// 1 mbar = 1 hPa = 100 Pa
|
|
[Description("bar")] bar, /// * 1 Bar = 1000 mbar = 0.1 MPa = 100000 Pa
|
|
[Description("inHg")] inHg, /// 1 inHg = 33.864 hPa
|
|
|
|
[Description("R%")] RPct, /// * 1 R%
|
|
|
|
[Description("%")] Pct, /// * 1 %
|
|
[Description("promile")] Promile, /// 1 promile = 0.1 %
|
|
|
|
[Description("mm")] mm, /// * 1 millimeter
|
|
[Description("cm")] cm, /// 1 cm = 10 mm
|
|
[Description("m")] m, /// 1 m = 1000 mm
|
|
[Description("in")] inch, /// 1 inch = 25.4 mm
|
|
|
|
[Description("kg/m3")] kgpm3, /// * 1 kg/m3 = 0.001 kg/l
|
|
[Description("kg/l")] kgpl, /// 1 kg/l = 1000 kg/m3
|
|
|
|
[Description("J")] J, /// * 1 Joul
|
|
[Description("kJ")] kJ, /// 1 kJ = 1000 J
|
|
[Description("MJ")] MJ, /// 1 MJ = 1000000 J
|
|
[Description("Wh")] Wh, /// 1 Wh = 3600 J
|
|
[Description("kWh")] kWh, /// 1 kWh = 3600000 J
|
|
[Description("MWh")] MWh, /// 1 MWh = 3600000000 J
|
|
|
|
#if LANG_PL
|
|
[Description("imp/l")] ppl, /// * 1 pulse/l
|
|
[Description("imp/dm3")] ppdm3, /// * 1 pulse/dm3
|
|
[Description("stopień/l")] degpl, /// * 1 degree/l
|
|
[Description("stopień/dm3")] degpdm3, /// * 1 degree/dm3
|
|
[Description("l/imp")] lpp, /// * 1 l/pulse
|
|
[Description("dm3/imp")] dm3pp, /// * 1 dm3/pulse
|
|
[Description("l/stopień")] lpdeg, /// * 1 l/degree
|
|
[Description("dm3/stopień")] dm3pdeg, /// * 1 dm3/degree
|
|
#else
|
|
[Description("pulse/l")] ppl, /// * 1 pulse/l
|
|
[Description("pulse/dm3")] ppdm3, /// * 1 pulse/dm3
|
|
[Description("degree/l")] degpl, /// * 1 degree/l
|
|
[Description("degree/dm3")] degpdm3, /// * 1 degree/dm3
|
|
[Description("l/pulse")] lpp, /// * 1 l/pulse
|
|
[Description("dm3/pulse")] dm3pp, /// * 1 dm3/pulse
|
|
[Description("l/degree")] lpdeg, /// * 1 l/degree
|
|
[Description("dm3/degree")] dm3pdeg, /// * 1 dm3/degree
|
|
#endif
|
|
///
|
|
[Description("pulse/kWh")] ppkWh, /// * 1 pulse/kWh
|
|
[Description("kWh/pulse")] kWhpp, /// * 1 kWh/pulse
|
|
|
|
Count
|
|
}
|
|
|
|
public enum Quantity
|
|
{
|
|
/// Quantities with units and conversions (double -> double)
|
|
#if LANG_DE
|
|
[Description("Volumen")] Volume,
|
|
[Description("Durchfluss")] Flow,
|
|
[Description("Masse")] Mass,
|
|
[Description("Zeit")] Time,
|
|
[Description("Temperatur")] Temperature,
|
|
[Description("Druck")] Pressure,
|
|
[Description("Feuchtigkeit")] Humidity,
|
|
[Description("Fehler")] Error,
|
|
[Description("Länge")] Length,
|
|
[Description("Dichte")] Density,
|
|
[Description("Energie")] Energy,
|
|
[Description("Impulse")] Pulses,
|
|
[Description("Impulse/l")] PulsePerLtr,
|
|
[Description("Impulse/kWh")] PulsePerKWh,
|
|
|
|
/// Quantities without units and conversions
|
|
[Description("Nummer")] Number,
|
|
[Description("Text")] String,
|
|
[Description("Boolean")] Boolean,
|
|
[Description("Datum und Uhrzeit")] DateTime,
|
|
#elif LANG_PL
|
|
[Description("Objętość")] Volume,
|
|
[Description("Przepływ")] Flow,
|
|
[Description("Masa")] Mass,
|
|
[Description("Czas")] Time,
|
|
[Description("Temperatura")] Temperature,
|
|
[Description("Ciśnienie")] Pressure,
|
|
[Description("Wilgotność")] Humidity,
|
|
[Description("Błąd")] Error,
|
|
[Description("Długość")] Length,
|
|
[Description("Gęstość")] Density,
|
|
[Description("Energia")] Energy,
|
|
[Description("Impulsy")] Pulses,
|
|
[Description("Impulsy/litr")] PulsePerLtr,
|
|
[Description("Impulsy/kWh")] PulsePerKWh,
|
|
|
|
/// Quantities without units and conversions
|
|
[Description("Numer")] Number,
|
|
[Description("Tekst")] String,
|
|
[Description("Boolean")] Boolean,
|
|
[Description("Data i czas")] DateTime,
|
|
#elif LANG_CS
|
|
[Description("Objem")] Volume,
|
|
[Description("Průtok")] Flow,
|
|
[Description("Hmotnost")] Mass,
|
|
[Description("Čas")] Time,
|
|
[Description("Teplota")] Temperature,
|
|
[Description("Tlak")] Pressure,
|
|
[Description("Vlhkost")] Humidity,
|
|
[Description("Chyba")] Error,
|
|
[Description("Délka")] Length,
|
|
[Description("Hustota")] Density,
|
|
[Description("Energie")] Energy,
|
|
[Description("Pulzy")] Pulses,
|
|
[Description("Pulzy/litr")] PulsePerLtr,
|
|
[Description("Pulzy/kWh")] PulsePerKWh,
|
|
|
|
/// Quantities without units and conversions
|
|
[Description("Počet")] Number,
|
|
[Description("Text")] String,
|
|
[Description("Boolean")] Boolean,
|
|
[Description("Datum a čas")] DateTime,
|
|
#elif LANG_IT
|
|
[Description("Volume")] Volume,
|
|
[Description("Flusso")] Flow,
|
|
[Description("Massa")] Mass,
|
|
[Description("Tempo")] Time,
|
|
[Description("Temperatura")] Temperature,
|
|
[Description("Pressione")] Pressure,
|
|
[Description("Umidità")] Humidity,
|
|
[Description("Errore")] Error,
|
|
[Description("Lungezza")] Length,
|
|
[Description("Densità")] Density,
|
|
[Description("Energia")] Energy,
|
|
[Description("Impulso")] Pulses,
|
|
[Description("Impulso/litro")] PulsePerLtr,
|
|
[Description("Impulso/kWh")] PulsePerKWh,
|
|
|
|
/// Quantities without units and conversions
|
|
[Description("Numero")] Number,
|
|
[Description("Stringa")] String,
|
|
[Description("Boolean")] Boolean,
|
|
[Description("Data e ora")] DateTime,
|
|
#else
|
|
[Description("Volume")] Volume,
|
|
[Description("Flow")] Flow,
|
|
[Description("Mass")] Mass,
|
|
[Description("Time")] Time,
|
|
[Description("Temperature")] Temperature,
|
|
[Description("Pressure")] Pressure,
|
|
[Description("Humidity")] Humidity,
|
|
[Description("Error")] Error,
|
|
[Description("Length")] Length,
|
|
[Description("Density")] Density,
|
|
[Description("Energy")] Energy,
|
|
[Description("Pulses")] Pulses,
|
|
[Description("Pulses/liter")] PulsePerLtr,
|
|
[Description("Pulses/kWh")] PulsePerKWh,
|
|
|
|
/// Quantities without units and conversions
|
|
[Description("Number")] Number,
|
|
[Description("String")] String,
|
|
[Description("Boolean")] Boolean,
|
|
[Description("Date and time")] DateTime,
|
|
#endif
|
|
Count,
|
|
}
|
|
|
|
public static class Units
|
|
{
|
|
public static bool IsUnitless(Quantity quantity)
|
|
{
|
|
return quantity >= Quantity.Number;
|
|
}
|
|
|
|
public static bool IsDefaultUnit(Unit unit)
|
|
{
|
|
return unit == Unit.l || unit == Unit.m3ph || unit == Unit.kg || unit == Unit.s || unit == Unit.C || unit == Unit.bar || unit == Unit.RPct ||
|
|
unit == Unit.Pct || unit == Unit.mm || unit == Unit.kgpm3 || unit == Unit.J || unit == Unit.ppl || unit == Unit.ppkWh;
|
|
}
|
|
|
|
public static bool IsQuantity(Unit units, Quantity quantity)
|
|
{
|
|
return (quantity == GetQuantity(units));
|
|
}
|
|
|
|
public static Quantity GetQuantity(Unit units)
|
|
{
|
|
switch (units)
|
|
{
|
|
case Unit.pulse:
|
|
case Unit.degree:
|
|
return Config.Quantity.Pulses;
|
|
|
|
case Unit.l:
|
|
case Unit.dm3:
|
|
case Unit.m3:
|
|
case Unit.gal_UK:
|
|
case Unit.gal_US:
|
|
return Config.Quantity.Volume;
|
|
|
|
case Unit.lph:
|
|
case Unit.m3ph:
|
|
return Config.Quantity.Flow;
|
|
|
|
case Unit.g:
|
|
case Unit.kg:
|
|
case Unit.t:
|
|
case Unit.lb:
|
|
return Config.Quantity.Mass;
|
|
|
|
case Unit.s:
|
|
case Unit.min:
|
|
case Unit.hour:
|
|
case Unit.ms:
|
|
return Config.Quantity.Time;
|
|
|
|
case Unit.C:
|
|
case Unit.F:
|
|
case Unit.K:
|
|
return Config.Quantity.Temperature;
|
|
|
|
case Unit.Pa:
|
|
case Unit.hPa:
|
|
case Unit.mbar:
|
|
case Unit.kPa:
|
|
case Unit.bar:
|
|
case Unit.MPa:
|
|
case Unit.inHg:
|
|
return Config.Quantity.Pressure;
|
|
|
|
case Unit.RPct:
|
|
return Config.Quantity.Humidity;
|
|
|
|
case Unit.Pct:
|
|
case Unit.Promile:
|
|
return Config.Quantity.Error;
|
|
|
|
case Unit.mm:
|
|
case Unit.cm:
|
|
case Unit.m:
|
|
case Unit.inch:
|
|
return Config.Quantity.Length;
|
|
|
|
case Unit.kgpm3:
|
|
case Unit.kgpl:
|
|
return Config.Quantity.Density;
|
|
|
|
case Unit.J:
|
|
case Unit.kJ:
|
|
case Unit.MJ:
|
|
case Unit.Wh:
|
|
case Unit.kWh:
|
|
case Unit.MWh:
|
|
return Config.Quantity.Energy;
|
|
|
|
case Unit.ppl:
|
|
case Unit.ppdm3:
|
|
case Unit.degpl:
|
|
case Unit.degpdm3:
|
|
case Unit.lpp:
|
|
case Unit.dm3pp:
|
|
case Unit.lpdeg:
|
|
case Unit.dm3pdeg:
|
|
return Config.Quantity.PulsePerLtr;
|
|
|
|
case Unit.ppkWh:
|
|
case Unit.kWhpp:
|
|
return Config.Quantity.PulsePerKWh;
|
|
|
|
default:
|
|
return Config.Quantity.Number;
|
|
}
|
|
}
|
|
|
|
public static bool IsVolume(Unit units) { return IsQuantity(units, Config.Quantity.Volume); }
|
|
public static bool IsFlow(Unit units) { return IsQuantity(units, Config.Quantity.Flow); }
|
|
public static bool IsMass(Unit units) { return IsQuantity(units, Config.Quantity.Mass); }
|
|
public static bool IsTime(Unit units) { return IsQuantity(units, Config.Quantity.Time); }
|
|
public static bool IsTemperature(Unit units) { return IsQuantity(units, Config.Quantity.Temperature); }
|
|
public static bool IsPressure(Unit units) { return IsQuantity(units, Config.Quantity.Pressure); }
|
|
public static bool IsHumidity(Unit units) { return IsQuantity(units, Config.Quantity.Humidity); }
|
|
public static bool IsError(Unit units) { return IsQuantity(units, Config.Quantity.Error); }
|
|
public static bool IsLength(Unit units) { return IsQuantity(units, Config.Quantity.Length); }
|
|
public static bool IsDensity(Unit units) { return IsQuantity(units, Config.Quantity.Density); }
|
|
public static bool IsEnergy(Unit units) { return IsQuantity(units, Config.Quantity.Energy); }
|
|
public static bool IsPulses(Unit units) { return IsQuantity(units, Config.Quantity.Pulses); }
|
|
public static bool IsPulsePerLtr(Unit units) { return IsQuantity(units, Config.Quantity.PulsePerLtr); }
|
|
public static bool IsPulsePerKWh(Unit units) { return IsQuantity(units, Config.Quantity.PulsePerKWh); }
|
|
|
|
|
|
public static double ConvertTo(Unit units, double v)
|
|
{
|
|
switch (units)
|
|
{
|
|
/// Volume: internal representation in l
|
|
case Unit.dm3:
|
|
case Unit.l: return v; /// 1 liter
|
|
case Unit.m3: return 0.001 * v; /// 1 m3 = 1000 l
|
|
case Unit.gal_UK: return 0.21996925 * v; /// 1 gal(UK) = 4.54609 l
|
|
case Unit.gal_US: return 0.26417205 * v; /// 1 gal(US) = 3.78541178 l
|
|
|
|
/// Flow: internal representation in m3/h
|
|
case Unit.lph: return 1000 * v; /// 1 liter/h
|
|
case Unit.m3ph: return v; /// 1 m3/h = 1000 l/h
|
|
|
|
/// Mass: internal representation in kg
|
|
case Unit.g: return 1000 * v; /// 1 g = 0.001 kg
|
|
case Unit.kg: return v; /// 1 kilogram
|
|
case Unit.t: return 0.001 * v; /// 1 t = 1000 kg
|
|
case Unit.lb: return 2.2046226 * v; /// 1 lb = 0.45359237 kg
|
|
|
|
/// Time or duration: internal representation in seconds [s]
|
|
case Unit.s: return v;
|
|
case Unit.min: return v / 60;
|
|
case Unit.hour: return v / 3600;
|
|
case Unit.ms: return 1000 * v;
|
|
|
|
/// Temperature: internal representation in °C
|
|
case Unit.C: return v; /// degree Celsius (°C)
|
|
case Unit.F: return 1.8 * v + 32; /// degree Fahrenheit
|
|
case Unit.K: return v + 273.15; /// degree Kelvin
|
|
|
|
/// Pressure: internal representation in bar = 0.1 MPa
|
|
case Unit.Pa: return 100000 * v; /// 1 hPa = 100 Pa
|
|
case Unit.hPa: return 1000 * v; /// 1 hPa = 100 Pa
|
|
case Unit.mbar: return 1000 * v; /// 1 mbar = 1 hPa = 100 Pa
|
|
case Unit.kPa: return 100 * v; /// 1 kPa = 10 HPa
|
|
case Unit.bar: return v; /// 1 bar = 1000 mbar = 100000 Pa
|
|
case Unit.MPa: return 0.1 * v; /// 1 MPa = 10000 hPa
|
|
case Unit.inHg: return 29.53 * v; /// 1 inHg = 33.864 hPa
|
|
|
|
/// Error: internal representation in %
|
|
case Unit.Pct: return v; /// 1 %
|
|
case Unit.Promile: return 10 * v; /// 1 promile = 0.1 %
|
|
|
|
/// Diameter, length: internal representation in mm
|
|
case Unit.mm: return v; /// 1 mm
|
|
case Unit.cm: return 0.1 * v; /// 1 cm = 10 mm
|
|
case Unit.m: return 0.001 * v; /// 1 m = 1000 mm
|
|
case Unit.inch: return v / 25.4; /// 1 inch = 25.4 mm
|
|
|
|
/// Density: internal representation in kg/m3
|
|
case Unit.kgpm3: return v;
|
|
case Unit.kgpl: return 0.001 * v;
|
|
|
|
/// Energy, internal representation in Joul
|
|
case Unit.J: return v; /// 1 Joul
|
|
case Unit.kJ: return 0.001 * v; /// 1 kJ = 1000 J
|
|
case Unit.MJ: return 0.000001 * v; /// 1 kJ = 1000000 J
|
|
case Unit.Wh: return v / 3600.0; /// 1 Wh = 3600 J
|
|
case Unit.kWh: return v / 3600000.0; /// 1 kWh = 3600000 J
|
|
case Unit.MWh: return v / 3600000000.0;/// 1 MWh = 3600000000 J
|
|
|
|
case Unit.kWhpp:
|
|
case Unit.lpp:
|
|
case Unit.dm3pp:
|
|
case Unit.lpdeg:
|
|
case Unit.dm3pdeg: return ((v <= float.Epsilon) ? 0 : 1 / v);
|
|
|
|
default: return v;
|
|
}
|
|
}
|
|
|
|
public static double ConvertFrom(Unit units, double v)
|
|
{
|
|
switch (units)
|
|
{
|
|
/// Volume: internal representation in l
|
|
case Unit.dm3:
|
|
case Unit.l: return v; /// 1 liter
|
|
case Unit.m3: return 1000 * v; /// 1000 l
|
|
case Unit.gal_UK: return 4.54609 * v; /// 1 gal(UK) = 4.54609 l
|
|
case Unit.gal_US: return 3.78541178 * v; /// 1 gal(US) = 3.78541178 l
|
|
|
|
/// Flow: internal representation in m3/h
|
|
case Unit.lph: return 0.001 * v; /// 1 liter/h
|
|
case Unit.m3ph: return v; /// 1 m3/h = 1000 l/h
|
|
|
|
/// Mass: internal representation in kg
|
|
case Unit.g: return 0.001 * v; /// 1 g = 0.001 kg
|
|
case Unit.kg: return v; /// 1 kilogram
|
|
case Unit.t: return 1000 * v; /// 1 t = 1000 kg
|
|
case Unit.lb: return 0.45359237 * v; /// 1 lb = 0.45359237 kg
|
|
|
|
/// Time or duration: internal representation in seconds [s]
|
|
case Unit.s: return v;
|
|
case Unit.min: return 60 * v;
|
|
case Unit.hour: return 3600 * v;
|
|
case Unit.ms: return v / 1000;
|
|
|
|
/// Temperature: internal representation in °C
|
|
case Unit.C: return v; /// degree Celsius (°C)
|
|
case Unit.F: return 5 * (v-32) / 9; /// degree Fahrenheit
|
|
case Unit.K: return v - 273.15; /// degree Kelvin
|
|
|
|
/// Pressure: internal representation in bar = 0.1 MPa
|
|
case Unit.Pa: return 0.00001 * v; /// 1 hPa = 100 Pa
|
|
case Unit.hPa: return 0.001 * v; /// 1 hPa = 100 Pa
|
|
case Unit.mbar: return 0.001 * v; /// 1 mbar = 1 hPa = 100 Pa
|
|
case Unit.kPa: return 0.01 * v; /// 1 kPa = 10 HPa
|
|
case Unit.bar: return v; /// 1 bar = 1000 mbar = 100000 Pa
|
|
case Unit.MPa: return 10 * v; /// 1 MPa = 10000 hPa
|
|
case Unit.inHg: return 0.033864 * v; /// 1 inHg = 33.864 hPa
|
|
|
|
/// Error: internal representation in %
|
|
case Unit.Pct: return v; /// 1 %
|
|
case Unit.Promile: return 0.1 * v; /// 1 promile = 0.1 %
|
|
|
|
/// Diameter, length: internal representation in mm
|
|
case Unit.mm: return v; /// 1 mm
|
|
case Unit.cm: return 10 * v; /// 1 cm = 10 mm
|
|
case Unit.m: return 1000 * v; /// 1 m = 1000 mm
|
|
case Unit.inch: return 25.4 * v; /// 1 inch = 25.4 mm
|
|
|
|
/// Density: internal representation in kg/m3
|
|
case Unit.kgpm3: return v;
|
|
case Unit.kgpl: return 1000 * v;
|
|
|
|
/// Energy, internal representation in Joul
|
|
case Unit.J: return v; /// 1 Joul
|
|
case Unit.kJ: return 1000 * v; /// 1 kJ = 1000 J
|
|
case Unit.MJ: return 1000000 * v; /// 1 kJ = 1000000 J
|
|
case Unit.Wh: return 3600 * v; /// 1 Wh = 3600 J
|
|
case Unit.kWh: return 3600000 * v; /// 1 kWh = 3600000 J
|
|
case Unit.MWh: return 3600000000 * v; /// 1 kWh = 3600000000 J
|
|
|
|
case Unit.kWhpp:
|
|
case Unit.lpp:
|
|
case Unit.dm3pp:
|
|
case Unit.lpdeg:
|
|
case Unit.dm3pdeg: return ((v == 0) ? 0 : 1 / v);
|
|
|
|
default: return v;
|
|
}
|
|
}
|
|
}
|
|
}
|