//----------------------------------------------------------------------- // // 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 SharedObjects.Misc.Objects { using System; using System.Collections.Generic; using global::SharedObjects.Protocol; /// /// Wrapper class for request data. /// public class RequestData : IRequestData { /// /// Gets Destination address for message. /// /// If 'null' use default address. /// /// public uint? destinationAddress { get; private set; } /// /// Gets or sets Creation timestamp. /// public long timestamp { get; set; } /// /// Gets or sets Gets Number of parts received. /// public int part { get; set; } /// /// Gets Actual mode byte value. /// public byte actualMode { get; private set; } /// /// Gets Message body data. /// public TxMsg message { get; private set; } /// /// Gets PID structure /// public XmlClass.pidgroup.pidlist pid { get; private set; } /// /// Gets Divisor value set by PID. /// public int divisor { get; private set; } /// /// Gets or sets Transmit timestamp. /// /// This is used to determine if it has gone too long without an answer from an ECU. /// /// public long txTimestamp { get; set; } /// /// Gets Received data. /// public IList rxData { get { return this.rxDataList; } } /// /// Received data. /// private IList rxDataList = new List(); /// /// Initializes a new instance of the class. /// /// Destination address for message. (Null for default) /// Actual mode byte value. /// PID structure. /// Message body data. public RequestData( uint? destinationAddress, byte actualMode, XmlClass.pidgroup.pidlist pid, TxMsg message) { this.destinationAddress = destinationAddress; this.actualMode = actualMode; this.pid = pid; this.message = message; this.timestamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; this.part = 0; if (this.pid != null) { string refreshStr = this.pid.refresh; // Set default value if 'null'. refreshStr = refreshStr != null ? refreshStr : "1:1"; int n = refreshStr.IndexOf(':'); if (n >= 0) { string divisorStr = refreshStr.Substring(n + 1); this.divisor = Convert.ToInt32(divisorStr); } else { this.divisor = 1; } } else { this.divisor = 1; } } /// /// Compare to other object. /// /// Object to compare with. /// 'true' if equal. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Reviewed")] public override bool Equals(object obj) { bool equal = false; if (obj is RequestData) { RequestData requestData = (RequestData)obj; equal = requestData.destinationAddress == this.destinationAddress; if (equal) { equal = requestData.actualMode == this.actualMode; if (equal) { if (requestData.pid != null && this.pid != null) { equal = requestData.pid.pid_int == this.pid.pid_int; } else { equal = requestData.pid == null && this.pid == null; } if (equal) { equal = this.message.Equals(requestData.message); } } } } return equal; } /// /// Calculate hashcode for object. /// /// Hashcode for object. public override int GetHashCode() { int hashCode = 0; if (this.destinationAddress != null) { hashCode = this.destinationAddress.GetHashCode(); } hashCode = (int)(hashCode << 7 | hashCode >> 25); hashCode ^= this.actualMode.GetHashCode(); hashCode = (int)(hashCode << 7 | hashCode >> 25); hashCode ^= this.pid.pid_int.GetHashCode(); hashCode = (int)(hashCode << 7 | hashCode >> 25); hashCode ^= this.message.GetHashCode(); return hashCode; } /// /// Get String representation of object. /// /// String representation of object public override string ToString() { if (this.pid != null) { if (this.destinationAddress != null) { return "destinationAddress=0x" + ((uint)this.destinationAddress).ToString("x2") + ", mode=0x" + this.actualMode.ToString("x2") + ", pid=0x" + this.pid.pid_int.ToString("x2") + ", data=" + this.message.ToString(); } return "mode=0x" + this.actualMode.ToString("x2") + ", pid=0x" + this.pid.pid_int.ToString("x2") + ", data=" + this.message.ToString(); } if (this.destinationAddress != null) { return "destinationAddress=0x" + ((uint)this.destinationAddress).ToString("x2") + ", mode=0x" + this.actualMode.ToString("x2") + ", pid=, data=" + this.message.ToString(); } return "mode=0x" + this.actualMode.ToString("x2") + ", pid=, data=" + this.message.ToString(); } } }