/// <summary>
        /// Processes the feed results.  First, <see cref="LogRecord"/>s are added to <see cref="TrackedGpsData"/> of <see cref="TrackedVehicle"/>s and <see cref="StatusData"/>/<see cref="FaultData"/> records are added to <see cref="TrackedDiagnostic"/>s of <see cref="TrackedVehicle"/>s.  Then, the newly-received data for the affected <see cref="TrackedVehicle"/>s is written to file(s) (and summary info is written to the console window).
        /// </summary>
        /// <param name="results">The <see cref="FeedResultData"/> containing new data received from the data feeds.</param>
        /// <returns></returns>
        async Task ProcessFeedResultsAsync(FeedResultData results)
        {
            try
            {
                // For each received LogRecord, if the LogRecord is for a vehicle that is being tracked, add the LogRecord to the TrackedVehicle's TrackedGpsData.
                foreach (LogRecord logRecord in results.GpsRecords)
                {
                    TrackedVehicle trackedVehicleToUpdate = GetTrackedVehicle(logRecord.Device);
                    if (trackedVehicleToUpdate != null)
                    {
                        TrackedGpsData trackedGpsData = trackedVehicleToUpdate.TrackedGpsData;
                        trackedGpsData.AddData(logRecord);
                    }
                }

                if (useStatusDataFeed == true)
                {
                    // For each received StatusData, if the StatusData represents a Diagnostic that is being tracked and the StatusData is for a vehicle that is being tracked, add the StatusData to the TrackedVehicle's TrackedDiagnostics.
                    foreach (StatusData statusData in results.StatusData)
                    {
                        if (!DiagnosticsToTrack.Where(diagnostic => diagnostic.Id == statusData.Diagnostic.Id).Any())
                        {
                            continue;
                        }
                        TrackedVehicle trackedVehicleToUpdate = GetTrackedVehicle(statusData.Device);
                        if (trackedVehicleToUpdate != null)
                        {
                            TrackedDiagnostic trackedDiagnosticToUpdate = trackedVehicleToUpdate.TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticId == statusData.Diagnostic.Id).First();
                            trackedDiagnosticToUpdate.AddData(statusData);
                        }
                    }
                }
                if (useFaultDataFeed == true)
                {
                    // For each received FaultData, if the FaultData represents a Diagnostic that is being tracked and the FaultData is for a vehicle that is being tracked, add the FaultData to the TrackedVehicle's TrackedDiagnostics.
                    foreach (FaultData faultData in results.FaultData)
                    {
                        if (!DiagnosticsToTrack.Where(diagnostic => diagnostic.Id == faultData.Diagnostic.Id).Any())
                        {
                            continue;
                        }
                        TrackedVehicle trackedVehicleToUpdate = GetTrackedVehicle(faultData.Device);
                        if (trackedVehicleToUpdate != null)
                        {
                            TrackedDiagnostic trackedDiagnosticToUpdate = trackedVehicleToUpdate.TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticId == faultData.Diagnostic.Id).First();
                            trackedDiagnosticToUpdate.AddData(faultData);
                        }
                    }
                }
                WriteFeedResultStatsToConsole();
                foreach (TrackedVehicle trackedVehicle in TrackedVehicles)
                {
                    await trackedVehicle.WriteDataToFileAsync();
                }
            }
            catch (Exception ex)
            {
                ConsoleUtility.LogError(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a <see cref="TrackedVehicle"/> object to represent and hold feed data for the subject <see cref="Device"/>.
        /// </summary>
        /// <param name="device">The <see cref="Device"/> to be represented.</param>
        /// <returns></returns>
        TrackedVehicle CreateTrackedVehicle(Device device)
        {
            TrackedVehicle trackedVehicle = new TrackedVehicle(device, OutputFolder, MaximumFileSizeInBytes);

            if (DiagnosticsToTrack != null && DiagnosticsToTrack.Count > 0)
            {
                // Add TrackedDiagnostics.
                foreach (Diagnostic diagnosticToTrack in DiagnosticsToTrack)
                {
                    TrackedDiagnostic trackedDiagnostic = new TrackedDiagnostic(trackedVehicle.Device, diagnosticToTrack, trackedVehicle.FaultDataFilePath, trackedVehicle.StatusDataFilePath, MaximumFileSizeInBytes);
                    trackedVehicle.TrackedDiagnostics.Add(trackedDiagnostic);
                }
            }
            TrackedVehicles.Add(trackedVehicle);
            return(trackedVehicle);
        }
Beispiel #3
0
        /// <summary>
        /// Triggers the writing to file of any GPS, fault, or status data received for this <see cref="TrackedVehicle"/> since the last time such records were written to file.  Includes logic to split output files into new files when the <see cref="MaximumFileSizeInBytes"/> has been reached.
        /// </summary>
        /// <returns></returns>
        public async Task WriteDataToFileAsync()
        {
            DateTime startTime         = DateTime.Now;
            string   newFilenameSuffix = $"{startTime.ToString(DateFormatStringForOutputFilenames)}.csv";
            int      filenamePrefixLength;
            string   outputFilePathPrefix;

            // If the GPS data file for the current TrackedVehicle has reached the maximum allowed size, change the TrackedGpsData OutputFilePath so that data will be written to a new file.
            if (TrackedGpsData.ReceivedLogRecords.Any())
            {
                FileInfo gpsDataOutputFileInfo = new(GpsDataFilePath);
                if (gpsDataOutputFileInfo.Exists && gpsDataOutputFileInfo.Length >= MaximumFileSizeInBytes)
                {
                    filenamePrefixLength          = GpsDataFilePath.Length - newFilenameSuffix.Length;
                    outputFilePathPrefix          = GpsDataFilePath.Substring(0, filenamePrefixLength);
                    GpsDataFilePath               = $"{outputFilePathPrefix}{newFilenameSuffix}";
                    TrackedGpsData.OutputFilePath = GpsDataFilePath;
                }
            }

            // If there are any TrackedDiagnostics, execute logic to split files that have exceeded the maximum allowed size and add headers to the new files (headers need to be added here because all FaultData diagnostics are written to a single file and all StatusData diagnostics are written to a single file).
            if (TrackedDiagnostics.Any())
            {
                // FaultData:
                if (TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.FaultData).Any())
                {
                    // If the FaultData file for the current TrackedVehicle does not yet exist, or if it exists and has reached the maximum allowed size, change the FaultDataFilePath and set the OutputFilePath of all TrackedDiagnostics that represent FaultData so that data will be written to a new file. Also, create the new file and write the header if there is any data to write.
                    FileInfo faultDataFileInfo = new(FaultDataFilePath);
                    if (faultDataFileInfo.Exists == false || faultDataFileInfo.Length >= MaximumFileSizeInBytes)
                    {
                        filenamePrefixLength = FaultDataFilePath.Length - newFilenameSuffix.Length;
                        outputFilePathPrefix = FaultDataFilePath.Substring(0, filenamePrefixLength);
                        FaultDataFilePath    = $"{outputFilePathPrefix}{newFilenameSuffix}";
                        bool createFileAndWriteHeader = false;
                        foreach (TrackedDiagnostic trackedDiagnostic in TrackedDiagnostics)
                        {
                            if (trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.FaultData)
                            {
                                trackedDiagnostic.OutputFilePath = FaultDataFilePath;
                            }
                            if (trackedDiagnostic.ReceivedFaultData.Any())
                            {
                                createFileAndWriteHeader = true;
                            }
                        }
                        if (createFileAndWriteHeader == true)
                        {
                            using (TextWriter fileWriter = new StreamWriter(FaultDataFilePath, true))
                            {
                                TrackedDiagnostic trackedFaultDataDiagnostic = TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.FaultData).First();
                                fileWriter.WriteLine(trackedFaultDataDiagnostic.FaultDataHeader);
                            }
                        }
                    }
                }
                // StatusData:
                if (TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.StatusData).Any())
                {
                    // If the StatusData file for the current TrackedVehicle does not yet exist, or if it exists and has reached the maximum allowed size, change the StatusDataFilePath and set the OutputFilePath of all TrackedDiagnostics that represent StatusData so that data will be written to a new file. Also, create the new file and write the header if there is any data to write.
                    FileInfo statusDataFileInfo = new(StatusDataFilePath);
                    if (statusDataFileInfo.Exists == false || statusDataFileInfo.Length >= MaximumFileSizeInBytes)
                    {
                        filenamePrefixLength = StatusDataFilePath.Length - newFilenameSuffix.Length;
                        outputFilePathPrefix = StatusDataFilePath.Substring(0, filenamePrefixLength);
                        StatusDataFilePath   = $"{outputFilePathPrefix}{newFilenameSuffix}";
                        bool createFileAndWriteHeader = false;
                        foreach (TrackedDiagnostic trackedDiagnostic in TrackedDiagnostics)
                        {
                            if (trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.StatusData)
                            {
                                trackedDiagnostic.OutputFilePath = StatusDataFilePath;
                            }
                            if (trackedDiagnostic.ReceivedStatusData.Any())
                            {
                                createFileAndWriteHeader = true;
                            }
                        }
                        if (createFileAndWriteHeader == true)
                        {
                            using (TextWriter fileWriter = new StreamWriter(StatusDataFilePath, true))
                            {
                                TrackedDiagnostic trackedStatusDataDiagnostic = TrackedDiagnostics.Where(trackedDiagnostic => trackedDiagnostic.DiagnosticCategoryType == TrackedDiagnostic.DiagnosticCategory.StatusData).First();
                                fileWriter.WriteLine(trackedStatusDataDiagnostic.StatusDataHeader);
                            }
                        }
                    }
                }

                // Write received data to file.
                await TrackedGpsData.WriteDataToFileAsync();

                foreach (TrackedDiagnostic trackedDiagnostic in TrackedDiagnostics)
                {
                    await trackedDiagnostic.WriteDataToFileAsync();
                }
            }
        }