using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DTS.SensorDB.TSF { /// /// the G5 Digital Input Channels section of the TSF /// public class TSFG5DigitalInputSection { private const string SECTION_START_HEADER = "---- G5 Digital Input Channels Begin ----"; private const string COLUMN_HEADER = "datachan,rack,mod,chan,descrip,ISOCode,scale,invert"; private const string SECTION_END_HEADER = "---- G5 Digital Input Channels End ----"; private List _digitalInputChannels = new List(); public G5DigtalInputChannelTSFEntry[] DigitalInputChannels { get { return _digitalInputChannels.ToArray(); } set { _digitalInputChannels = new List(value); } } public void ReadFrom(List lines, ref int currentLine, ref List errors) { if (currentLine == lines.Count) { //errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } string sectionStart = lines[currentLine++]; if (sectionStart != SECTION_START_HEADER) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.G5DIGITALINPUTSECTION_START_INVALID_HEADER, currentLine, sectionStart)); return; } if (currentLine == lines.Count) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } string columnHeader = lines[currentLine++]; if (columnHeader != COLUMN_HEADER) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.G5DIGITALINPUTSECTION_COLUMN_INVALID_HEADER, currentLine, columnHeader)); return; } List g5DigitalInputChannels = new List(); bool done = false; while (!done) { if (currentLine == lines.Count) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } string line = lines[currentLine++]; if (!line.Contains("G5 Digital Input Channels End")) { G5DigtalInputChannelTSFEntry ichan = new G5DigtalInputChannelTSFEntry(); string[] tokens = line.Split(new char[] { ',' }); for (int iCurField = 0; iCurField < tokens.Length; iCurField++) { string s = tokens[iCurField]; G5DigtalInputChannelTSFEntry.Fields field = (G5DigtalInputChannelTSFEntry.Fields)iCurField; switch (field) { case G5DigtalInputChannelTSFEntry.Fields.chan: try { ichan.Chan = Convert.ToInt32(s); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_CHAN, currentLine, s)); } break; case G5DigtalInputChannelTSFEntry.Fields.datachan: try { ichan.DataChan = Convert.ToInt32(s); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_DATACHAN, currentLine, s)); } break; case G5DigtalInputChannelTSFEntry.Fields.descrip: ichan.Descripton = s; break; case G5DigtalInputChannelTSFEntry.Fields.invert: try { switch (s) { case "0": case "F": case "N": ichan.Invert = false; break; case "1": case "T": case "Y": ichan.Invert = true; break; default: { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_INVERT, currentLine, s)); } break; } } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_INVERT, currentLine, s)); } break; case G5DigtalInputChannelTSFEntry.Fields.ISOCode: ichan.ISOCode = s; break; case G5DigtalInputChannelTSFEntry.Fields.mod: try { ichan.Module = Convert.ToInt32(s); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_MODULE, currentLine, s)); } break; case G5DigtalInputChannelTSFEntry.Fields.rack: try { ichan.Rack = Convert.ToInt32(s); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_RACK, currentLine, s)); } break; case G5DigtalInputChannelTSFEntry.Fields.scale: try { ichan.Scale = Convert.ToDouble(s); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_G5DIGITALCHANNEL_INVALID_SCALE, currentLine, s)); } break; default: throw new NotSupportedException("TSFFile::ReadTSF invalid G5DigitalInput field: " + field.ToString()); } } g5DigitalInputChannels.Add(ichan); } else { done = true; } } DigitalInputChannels = g5DigitalInputChannels.ToArray(); } } }