using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.IO; using DTS.Common.Utilities.Logging; using DTS.Common.DAS.Concepts; using DTS.Common.Enums; using DTS.Common.Enums.Sensors; namespace DTS.SensorDB { public abstract class SensorDBBase { //protected System.Globalization.CultureInfo InvariantCulture = System.Globalization.CultureInfo.InvariantCulture; //protected string TableName; private static void Error(string tag, string id, string TableName) { if (!string.IsNullOrEmpty(id)) throw new System.Exception(string.Format("{0}: Can't find tag {1} for entry {2}", TableName, tag, id)); else throw new System.Exception(string.Format("{0}: Can't find tag {1} in file", TableName, tag)); } public static XElement GetTagValueSafe(XElement elem, string tag, string id, String TableName) { XElement tmp = null; try { tmp = elem.Element(tag); } catch (System.Exception) { } return tmp; } public static XElement GetTagValue(XElement elem, string tag, string id, String TableName) { XElement tmp = null; try { tmp = elem.Element(tag); } catch (System.Exception) { Error(tag, id, TableName); // will not return } if (tmp == null) { Error(tag, id, TableName); // will not return } return tmp; } public static void GetValue(XElement elem, string tag, out string target, string id, string TableName) { target = (string)GetTagValue(elem, tag, id, TableName); } public static void GetValueSafe(XElement elem, string tag, out string target, string id, string TableName) { try { target = (string)GetTagValueSafe(elem, tag, id, TableName); } catch (System.Exception ex) { APILogger.Log(ex.Message); target = ""; } } public static void GetValue(XElement elem, string tag, out int target, string id, string TableName) { target = (int)GetTagValue(elem, tag, id, TableName); } public static void GetValue(XElement elem, string tag, out SensorStatus target, string id, string TableName) { target = (SensorStatus)Enum.Parse(typeof(SensorStatus), (string)GetTagValue(elem, tag, id, TableName)); } public static void GetValue(XElement elem, string tag, out double target, string id, string TableName) { var element = GetTagValue(elem, tag, id, TableName); if (double.TryParse(element.Value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out var d)) { target = d; } else { target = double.NaN; } } public static void GetValue(XElement elem, string tag, out bool target, string id, string TableName) { target = (bool)GetTagValue(elem, tag, id, TableName); } public static void GetValue(XElement elem, string tag, out SensorConstants.BridgeType target, string id, string TableName) { var myEnum = Enum.Parse(typeof(SensorConstants.BridgeType), (string)GetTagValue(elem, tag, id, TableName)); target = (SensorConstants.BridgeType)myEnum; } public static void GetValue(XElement elem, string tag, out ShuntMode target, string id, string TableName) { target = (ShuntMode)Enum.Parse(typeof(ShuntMode), (string)GetTagValue(elem, tag, id, TableName)); } public static void GetValue(XElement elem, string tag, out BridgeLeg target, string id, string TableName) { target = (BridgeLeg)Enum.Parse(typeof(BridgeLeg), (string)GetTagValue(elem, tag, id, TableName)); } public static void GetValue(XElement elem, string tag, out ExcitationVoltageOptions.ExcitationVoltageOption target, string id, string TableName) { target = ExcitationVoltageOptions.ExcitationVoltageOption.Undefined; try { var excitationStr = (string)GetTagValue(elem, tag, id, TableName); // this excitation string should be in the form Volt2, Volt2_5, Volt5 and so on // to be a little more helpful, we'll try to manage strings in 2, 2.5, 5 format // too. if (!excitationStr.StartsWith("Volt")) { excitationStr = "Volt" + excitationStr.Replace('.', '_'); } target = ExcitationVoltageOptions.ExcitationVoltageOption.Volt5; Enum.TryParse(excitationStr, out target); //(Test.Module.Channel.Sensor.ExcitationVoltageOption)Enum.Parse(typeof(Test.Module.Channel.Sensor.ExcitationVoltageOption), excitationStr); if (target == ExcitationVoltageOptions.ExcitationVoltageOption.Undefined) { target = ExcitationVoltageOptions.ExcitationVoltageOption.Volt5; } //throw new ArgumentException("SensorDB: " + excitationStr + " is not a valid excitation"); } catch (System.Exception ex) { APILogger.Log(ex.Message, ex); } } public static void GetValue(XElement elem, string tag, out DateTime target, string id, string TableName) { target = (DateTime)GetTagValue(elem, tag, id, TableName); } public static void GetValue(XElement elem, string tag, out FilterClassType target, string id, string TableName) { target = (FilterClassType)Enum.Parse(typeof(FilterClassType), (string)GetTagValue(elem, tag, id, TableName)); } public static void GetValue(XElement elem, string tag, out ZeroMethodType target, string id, string TableName) { var myEnum = Enum.Parse(typeof(ZeroMethodType), (string)GetTagValue(elem, tag, id, TableName)); target = (ZeroMethodType)myEnum; } } }