//----------------------------------------------------------------------- // // Copyright © 2012 Nils Hammar. All rights reserved. // //----------------------------------------------------------------------- /* * Software to access vehicle information via the OBD-II connector. * * Copyright © 2012 Nils Hammar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Alternative licensing is possible, see the licensing document. * * The above text may not be removed or modified. */ namespace SimProtocol.SimData { using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms; using SharedObjects; using SharedObjects.GUI; /// /// Static data dictionary. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "Reviewed, intentional")] public class StaticDataDictionary : DataDictionary { /// /// Initializes a new instance of the class. /// /// Event logging appender. /// File to load dictionary from. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Reviewed.")] public StaticDataDictionary(ILogging iAppender, string file) : base(iAppender) { using (FileStream fs = new FileStream(file, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { int row = 0; string line; while ((line = sr.ReadLine()) != null) { row++; try { if (line.ToUpperInvariant().IndexOf("MODE") < 0) { string[] sa = line.Split(new char[] { '\t' }); if (sa.Length > 3) { uint[] modes = this.fetchModes(row, line, sa); if (modes.Length > 0) { uint echoPid = this.getData(1, row, line, sa); uint pid = this.getData(2, row, line, sa); int n = sa.Length - 3; List dataByteList = new List(); for (int i = 0; i < n; i++) { if (sa[i + 3].Trim().Length == 0) { break; } dataByteList.Add(Convert.ToByte(sa[i + 3], 16)); } byte[] ba = dataByteList.ToArray(); for (int i = 0; i < modes.Length; i++) { uint mode = modes[i]; this.addDictionaryItem(mode, echoPid, pid, ba); } } } } } catch (Exception ex) { this.presentError(row, line, ex); } } } } this.indexMode(0x01); this.indexMode(0x02); this.indexMode(0x22); } /// /// Called when start of data replay shall occur. /// /// Callback interface to call when end of replay occurs. /// Replay speed scale factor. /// Time for start of replay. public override void playData(ISimEndPlay iSimEndPlay, double replaySpeed, uint startTime) { // Do nothing, here due to interface requirement. } /// /// Called when stop of data replay shall occur. /// public override void stopData() { // Do nothing, here due to interface requirement. } /// /// Dispose the instance. /// public sealed override void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// /// Dispose the instance. /// /// Dispose flag. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "disposing", Justification = "Compatibility issue.")] protected virtual void Dispose(bool disposing) { // Do nothing. } /// /// Fetch all mode values from file for the current row. /// /// Row number in data file where the error was found. /// Line with data causing the error. /// Array to get Mode values from /// Array of mode values. private uint[] fetchModes(int row, string line, string[] sa) { string[] modesRaw = sa[0].Split(new char[] { ',' }); uint[] modes = new uint[modesRaw.Length]; try { for (int i = 0; i < modesRaw.Length; i++) { modes[i] = Convert.ToUInt32(modesRaw[i].Trim(), 16); } } catch (Exception) { // this.presentError(row, line, ex); modes = new uint[0]; } return modes; } /// /// Present error info to the user. /// /// Row number in data file where the error was found. /// Line with data causing the error. /// Exception data. private void presentError(int row, string line, Exception ex) { if (this.iAppender != null) { this.iAppender.appendTextLn("Row " + row + ": " + ex.GetType().ToString() + ": " + ex.Message + "\r\n" + line + "\r\n" + ex.StackTrace); } else { MessageBox.Show("Row " + row + ": " + ex.GetType().ToString() + ": " + ex.Message + "\r\n" + line + "\r\n" + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// Get the PID value from string. /// /// Column for data. /// Row number in data file where the error was found. /// Line with data causing the error. /// Array to get PID value from /// Parsed pid value. private uint getData(int column, int row, string line, string[] sa) { uint pid = 0; try { pid = Convert.ToUInt32(sa[column], 16); } catch (Exception ex) { this.presentError(row, line, ex); } return pid; } } }