public async Task <bool> UpdateData() { var json = await dataFetcher.UpdateData(); try { telemetry = JsonSerializer.Deserialize <TelemetryStorage>(json, jsonSerializerOptions); await dataSaver.SaveTelemetryToFile(telemetry); } catch (ArgumentNullException ex) { string EventMessage = "An error occurred in TelemetryParser while updating data. An object to populate with values from JSON file was null."; await EventLogger.LogExceptionForUserAndToFile(EventMessage, ex); await EventLogger.LogExceptionToFile(EventMessage, ex.Message, ex.StackTrace); return(false); } catch (JsonException ex) { string EventMessage = "An error occurred in TelemetryParser while updating data. Downloaded JSON file is invalid."; await EventLogger.LogExceptionForUserAndToFile(EventMessage, ex); return(false); } catch (Exception ex) { string EventMessage = "An unknown error occurred in TelemetryParser while updating data. "; await EventLogger.LogExceptionForUserAndToFile(EventMessage, ex); return(false); } return(true); }
public TelemetryParser(IDataFetcher dataFetcher, IDataSaver dataSaver) { this.dataSaver = dataSaver; this.dataFetcher = dataFetcher; telemetry = new TelemetryStorage(); jsonSerializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }; }
public async Task SaveTelemetryToFile(TelemetryStorage telemetry) { string lineToSave = $"{telemetry.Timestamp},{telemetry.Temperature},{telemetry.Humidity},{telemetry.Pressure}"; try { using (StreamWriter writer = new StreamWriter(CSVFilePath, true)) { await writer.WriteLineAsync(lineToSave); } } catch (Exception ex) { await EventLogger.LogExceptionForUserAndToFile( $"Could not write this line: {lineToSave} to CSV file. This data would not be stored.", ex); } }