/// <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); } }
/// <summary> /// Creates a <see cref="TrackedVehicle"/> instance, validates existence of the output folder, determines filenames for output GPS, fault data and status data files, creates a new <see cref="Geotab.DataOnlyPlan.API.Examples.FleetMonitor.TrackedGpsData"/> instance, and creates a new list to store <see cref="Geotab.DataOnlyPlan.API.Examples.FleetMonitor.TrackedDiagnostic"/> objects. /// </summary> /// <param name="device">The <see cref="Geotab.Checkmate.ObjectModel.Device"/> associated with the vehicle to be tracked.</param> /// <param name="outputFolder">The path of the folder into which output data files are to be written.</param> /// <param name="maximumFileSizeInBytes">The maximum size, in bytes, that an output file can reach before a new output file is created.</param> public TrackedVehicle(Device device, string outputFolder, long maximumFileSizeInBytes) { // Validate outputFolder. if (!Directory.Exists(outputFolder)) { throw new ArgumentException($"The specified folder, '{outputFolder}', does not exist."); } Device = device; DeviceId = Device.Id; // Generate names of files to which data associated with the TrackedVehicle will be written. DateTime startTime = DateTime.Now; string gpsDataFilename = $"GPS Data - DeviceID {DeviceId} FileID {startTime.ToString(DateFormatStringForOutputFilenames)}.csv"; GpsDataFilePath = Path.Combine(outputFolder, gpsDataFilename); string faultDataFilename = $"Fault Data - DeviceID {DeviceId} FileID {startTime.ToString(DateFormatStringForOutputFilenames)}.csv"; FaultDataFilePath = Path.Combine(outputFolder, faultDataFilename); string statusDataFilename = $"Status Data - DeviceID {DeviceId} FileID {startTime.ToString(DateFormatStringForOutputFilenames)}.csv"; StatusDataFilePath = Path.Combine(outputFolder, statusDataFilename); MaximumFileSizeInBytes = maximumFileSizeInBytes; TrackedDiagnostics = new List <TrackedDiagnostic>(); TrackedGpsData = new TrackedGpsData(Device, GpsDataFilePath, maximumFileSizeInBytes); }