/// <summary> /// Gets the <see cref="TrackedVehicle"/> representing the subject <see cref="Device"/> from the <see cref="TrackedVehicles"/> list. If not found, a new <see cref="TrackedVehicle"/> will be created, added to the <see cref="TrackedVehicles"/> list and returned under the following conditions: 1 - if specific vehicles are being tracked and the subject <see cref="Device"/> represents one of those vehicles; 2 - if vehicles are to be added to the <see cref="TrackedVehicles"/> list dynamically as they report data. /// </summary> /// <param name="device">The <see cref="Device"/> for which to get the associated <see cref="TrackedVehicle"/>.</param> /// <returns></returns> TrackedVehicle GetTrackedVehicle(Device device) { TrackedVehicle trackedVehicleToReturn = null; // Try to find the TrackedVehicle. List <TrackedVehicle> listWithTargetTrackedVehicle = TrackedVehicles.Where(trackedVehicle => trackedVehicle.DeviceId == device.Id).ToList(); if (listWithTargetTrackedVehicle.Any()) { trackedVehicleToReturn = listWithTargetTrackedVehicle.First(); } if (trackedVehicleToReturn == null) { if (TrackSpecificVehicles == true) { // If tracking specific vehicles and the subject Device is in the list of DevicesToTrack, create the TrackedVehicle. if (DevicesToTrack.Where(deviceToTrack => deviceToTrack.Id == device.Id).Any()) { trackedVehicleToReturn = CreateTrackedVehicle(device); } } else { // If all vehicles that are reporting data are being tracked, create the TrackedVehicle. trackedVehicleToReturn = CreateTrackedVehicle(device); } } return(trackedVehicleToReturn); }
/// <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"/> 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); }