//----------------------------------------------------------------------- // // 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 DataSource.FileAccess { using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Xml.Serialization; using global::SharedObjects; using global::SharedObjects.Misc; /// /// Class for file access. /// public class VehiclesFileAccess : XmlParserHelper { /// /// Name of file used. /// private string fileName; /// /// Gets List of vehicles. /// private List vehiclesInt = new List(); /// /// Gets List of vehicles. /// public IList vehicles { get { return this.vehiclesInt; } } /// /// Initializes a new instance of the class. /// /// Logging interface. /// Name of data file. /// Data file directory path. public VehiclesFileAccess(ILogging iLogging, string fileName, string dataFileDir) : base(iLogging, dataFileDir) { this.fileName = fileName; } /// /// Make a copy of an existing object. /// /// Source object. /// New object [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Reviewed.")] public static XmlClass.vehicle duplicate(XmlClass.vehicle oldVehicle) { XmlClass.vehicle newVehicle = null; if (oldVehicle != null) { XmlSerializer xms = new XmlSerializer(oldVehicle.GetType()); using (Stream stream = new MemoryStream()) { try { xms.Serialize(stream, oldVehicle); stream.Seek(0, SeekOrigin.Begin); newVehicle = (XmlClass.vehicle)xms.Deserialize(stream); } finally { stream.Close(); } } } return newVehicle; } /// /// Get the highest vehicle ID. /// /// List to search in. /// Highest vehicle ID. public static int getHighestVehicleId(IList vehicles) { int n = 1; if (vehicles != null) { foreach (XmlClass.vehicle vehicle in vehicles) { if (n < vehicle.id) { n = vehicle.id; } } } return n; } /// /// Get message flags for vehicle. /// /// Vehicle item to get flags from. /// Message flags. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed.")] public static uint getMsgFlags(XmlClass.vehicle vehicle) { uint flagbits = 0; string unsupportedFlags = string.Empty; foreach (XmlClass.vehicle.flags flag in vehicle.msgflags) { switch (flag.flag) { case "START_OF_MESSAGE": flagbits |= 0x00000002; break; case "ISO15765_FRAME_PAD": flagbits |= 0x00000040; break; case "ISO15765_ADDR_TYPE": flagbits |= 0x00000080; break; case "CAN_29BIT_ID": flagbits |= 0x00000100; break; case "WAIT_P3_MIN_ONLY": flagbits |= 0x00000200; break; case "SCI_MODE": flagbits |= 0x00400000; break; case "SCI_TX_VOLTAGE": flagbits |= 0x00800000; break; default: unsupportedFlags += flag.flag + "\r\n"; break; } } if (unsupportedFlags.Length > 0) { MessageBox.Show( vehicle.name + "\r\n\r\nUnsupported Msg Flags:\r\n" + unsupportedFlags, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return flagbits; } /// /// Save the current list of vehicles. /// public void save() { this.save(this.vehiclesInt); } /// /// Load vehicle list from file. /// public void load() { this.iLogging.appendText("Loading vehicles." + "\r\n"); this.vehiclesInt = new List(); XmlSerializer xms = new XmlSerializer(this.vehiclesInt.GetType()); xms.UnknownNode += new XmlNodeEventHandler(xms_UnknownNode); xms.UnknownAttribute += new XmlAttributeEventHandler(xms_UnknownAttribute); xms.UnknownElement += new XmlElementEventHandler(xms_UnknownElement); xms.UnreferencedObject += new UnreferencedObjectEventHandler(xms_UnreferencedObject); StreamReader sr = null; try { sr = new StreamReader(this.dataFileDir + this.fileName); this.vehiclesInt = (List)xms.Deserialize(sr); this.iLogging.appendText(this.vehiclesInt.Count + " vehicles Loaded from XML file." + "\r\n"); for (int i = 0; i < this.vehiclesInt.Count; i++) { string name = this.vehiclesInt[i].name; int bits = this.vehiclesInt[i].addressbits; int numEcus = 0; if (this.vehiclesInt[i].ecus != null) { numEcus = this.vehiclesInt[i].ecus.Count; } this.iLogging.appendText("Vehicle " + i + ": " + name + ", bits=" + bits + ", numEcus=" + numEcus + "\r\n"); } } finally { if (sr != null) { sr.Close(); } } this.vehiclesInt.Sort(Utils.vehicle_sorter); // Get highest ID of vehicle. int n = VehiclesFileAccess.getHighestVehicleId(this.vehiclesInt); // Set ID on all vehicles not having an ID. n++; foreach (XmlClass.vehicle vehicle in this.vehiclesInt) { if (vehicle.id == 0) { vehicle.id = n++; } } } /// /// Save the vehicles. /// /// List of vehicles to save. private void save(List vehicles) { // Get highest ID of vehicle. int n = getHighestVehicleId(vehicles); // Set ID on all vehicles not having an ID. n++; foreach (XmlClass.vehicle vehicle in vehicles) { if (vehicle.id == 0) { vehicle.id = n++; } } XmlSerializer xms = new XmlSerializer(vehicles.GetType()); TextWriter writer = null; try { writer = new StreamWriter(this.dataFileDir + this.fileName); xms.Serialize(writer, vehicles); } finally { if (writer != null) { writer.Close(); } } } } }