Exemplo n.º 1
0
        /// <summary>
        /// Indicates whether the <see cref="DbDiagnostic"/> differs from the <see cref="Diagnostic"/>, thereby requiring the <see cref="DbDiagnostic"/> to be updated in the database.
        /// </summary>
        /// <param name="dbDiagnostic">The <see cref="DbDiagnostic"/> to be evaluated.</param>
        /// <param name="diagnostic">The <see cref="Diagnostic"/> to compare against.</param>
        /// <returns></returns>
        public static bool DbDiagnosticRequiresUpdate(DbDiagnostic dbDiagnostic, Diagnostic diagnostic)
        {
            if (dbDiagnostic.GeotabId != diagnostic.Id.ToString())
            {
                throw new ArgumentException($"Cannot compare Diagnostic '{diagnostic.Id}' with DbDiagnostic '{dbDiagnostic.GeotabId}' because the IDs do not match.");
            }

            Source        diagnosticSource        = diagnostic.Source;
            UnitOfMeasure diagnosticUnitOfMeasure = diagnostic.UnitOfMeasure;

            if (dbDiagnostic.DiagnosticCode != diagnostic.Code || dbDiagnostic.DiagnosticName != diagnostic.Name || dbDiagnostic.DiagnosticSourceId != diagnosticSource.Id.ToString() || dbDiagnostic.DiagnosticSourceName != diagnosticSource.Name || dbDiagnostic.DiagnosticUnitOfMeasureId != diagnosticUnitOfMeasure.Id.ToString() || dbDiagnostic.DiagnosticUnitOfMeasureName != diagnosticUnitOfMeasure.Name)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the supplied <see cref="Diagnostic"/> into a <see cref="DbDiagnostic"/>.
        /// </summary>
        /// <param name="diagnostic">The <see cref="Diagnostic"/> to be converted.</param>
        /// <returns></returns>
        public static DbDiagnostic GetDbDiagnostic(Diagnostic diagnostic)
        {
            Source        diagnosticSource        = diagnostic.Source;
            UnitOfMeasure diagnosticUnitOfMeasure = diagnostic.UnitOfMeasure;
            Controller    diagnosticController    = diagnostic.Controller;

            DbDiagnostic dbDiagnostic = new DbDiagnostic
            {
                DiagnosticCode              = diagnostic.Code,
                DiagnosticName              = diagnostic.Name,
                DiagnosticSourceId          = diagnosticSource.Id.ToString(),
                DiagnosticSourceName        = diagnosticSource.Name,
                DiagnosticUnitOfMeasureId   = diagnosticUnitOfMeasure.Id.ToString(),
                DiagnosticUnitOfMeasureName = diagnosticUnitOfMeasure.Name,
                GeotabId = diagnostic.Id.ToString()
            };

            if (diagnosticController != null)
            {
                dbDiagnostic.ControllerId = diagnosticController.Id.ToString();

                // Derive the OBD-II Diagnostic Trouble Code (DTC), if applicable.
                if (dbDiagnostic.DiagnosticSourceId == KnownId.SourceObdId.ToString() || dbDiagnostic.DiagnosticSourceId == KnownId.SourceObdSaId.ToString())
                {
                    int diagnosticCode;
                    if (diagnostic.Code != null)
                    {
                        diagnosticCode = (int)diagnostic.Code;
                        string dtcPrefix = "";
                        switch (diagnosticController.Id.ToString())
                        {
                        case nameof(KnownId.ControllerObdPowertrainId):
                            dtcPrefix = Globals.OBD2DTCPrefixPowertrain;
                            break;

                        case nameof(KnownId.ControllerObdWwhPowertrainId):
                            dtcPrefix = Globals.OBD2DTCPrefixPowertrain;
                            break;

                        case nameof(KnownId.ControllerObdBodyId):
                            dtcPrefix = Globals.OBD2DTCPrefixBody;
                            break;

                        case nameof(KnownId.ControllerObdWwhBodyId):
                            dtcPrefix = Globals.OBD2DTCPrefixBody;
                            break;

                        case nameof(KnownId.ControllerObdChassisId):
                            dtcPrefix = Globals.OBD2DTCPrefixChassis;
                            break;

                        case nameof(KnownId.ControllerObdWwhChassisId):
                            dtcPrefix = Globals.OBD2DTCPrefixChassis;
                            break;

                        case nameof(KnownId.ControllerObdNetworkingId):
                            dtcPrefix = Globals.OBD2DTCPrefixNetworking;
                            break;

                        case nameof(KnownId.ControllerObdWwhNetworkingId):
                            dtcPrefix = Globals.OBD2DTCPrefixNetworking;
                            break;

                        default:
                            break;
                        }
                        if (dtcPrefix.Length > 0)
                        {
                            string dtc = Convert.ToString(diagnosticCode, 16).PadLeft(4, '0');
                            dbDiagnostic.OBD2DTC = $"{dtcPrefix}{dtc.ToUpper()}";
                        }
                    }
                }
            }
            return(dbDiagnostic);
        }