using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DTS.SensorDB.TSF { /// /// handles the CalculateChannelInformation section of the TSF /// public class TSFCalculatedChannelInformationSection { private const string SECTION_START_HEADER = "---- Start Calculated Channel Information ----"; private const string COLUMN_HEADER = "chan,descrip,processtype,1stchan,2ndchan,3rdchan,value,EU,expmaxrange"; private const string SECTION_END_HEADER = "---- End Calculated Channel Information ----"; private List _calculatedChannels = new List(); public TSFCalculatedChannelEntry[] CalculatedChannels { get { return _calculatedChannels.ToArray(); } set { _calculatedChannels = new List(value); } } /// /// reads section from TSF /// assumes currentline is the start of the section /// /// /// /// public void ReadFrom(List lines, ref int currentLine, ref List errors) { _calculatedChannels.Clear(); if (currentLine == lines.Count) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } string sectionHeader = lines[currentLine++]; if (sectionHeader != SECTION_START_HEADER) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.CALCULATEDCHANNELSSECTION_INVALIDSECTIONHEADER, currentLine, sectionHeader)); return; } if (currentLine == lines.Count) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } string columnsHeader = lines[currentLine++]; if (columnsHeader != COLUMN_HEADER) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.CALCULATEDCHANNELSECTION_INVALIDCOLUMNHEADER, currentLine, columnsHeader)); return; } var invariant = System.Globalization.CultureInfo.InvariantCulture; bool done = false; List calculatedChannels = new List(); 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("End Calculated Channel Information")) { string[] tokens = line.Split(new char[] { ',' }); TSFCalculatedChannelEntry cc = new TSFCalculatedChannelEntry(); for (int iCurField = 0; iCurField < tokens.Length; iCurField++) { string s = tokens[iCurField]; TSFCalculatedChannelEntry.Fields field = (TSFCalculatedChannelEntry.Fields)iCurField; switch (field) { case TSFCalculatedChannelEntry.Fields.chan: try { cc.Chan = Convert.ToInt32(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_CHAN_FIELD, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.descrip: cc.Description = s; break; case TSFCalculatedChannelEntry.Fields.EU: cc.EU = s; break; case TSFCalculatedChannelEntry.Fields.expmaxrange: try { cc.ExpMaxRange = Convert.ToDouble(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_EXPMAXRANGE, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.firstchan: try { cc.FirstChan = Convert.ToInt32(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_FIRSTCHAN, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.processtype: try { cc.ProcessType = Convert.ToInt32(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_PROCESSTYPE, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.progress2: try { cc.Progress2 = Convert.ToDouble(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_PROGRESS2, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.progress3: try { cc.Progress3 = Convert.ToDouble(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_PROGRESS3, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.secondchan: try { cc.SecondChan = Convert.ToInt32(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_SECONDCHAN, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.thirdchan: try { cc.ThirdChan = Convert.ToInt32(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_THIRDCHAN, currentLine, s)); } break; case TSFCalculatedChannelEntry.Fields.value: try { cc.Value = Convert.ToDouble(s, invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_CALCULATED_CHANNEL_INVALID_VALUE, currentLine, s)); } break; } } calculatedChannels.Add(cc); } else { done = true; } CalculatedChannels = calculatedChannels.ToArray(); } } } }