// this will be called when a potential exposure has been detected public async Task ExposureDetectedAsync(ExposureDetectionSummary summary, Func <Task <IEnumerable <ExposureInfo> > > getExposureInfo) { UserExposureSummary userExposureSummary = new UserExposureSummary(summary.DaysSinceLastExposure, summary.MatchedKeyCount, summary.HighestRiskScore, summary.AttenuationDurations, summary.SummationRiskScore); userData.ExposureSummary = userExposureSummary; var exposureInfo = await getExposureInfo(); // Add these on main thread in case the UI is visible so it can update await Device.InvokeOnMainThreadAsync(() => { foreach (var exposure in exposureInfo) { Console.WriteLine($"COCOA found exposure {exposure.Timestamp}"); UserExposureInfo userExposureInfo = new UserExposureInfo(exposure.Timestamp, exposure.Duration, exposure.AttenuationValue, exposure.TotalRiskScore, (Covid19Radar.Model.UserRiskLevel)exposure.TransmissionRiskLevel); userData.ExposureInformation.Add(userExposureInfo); } }); await userDataService.SetAsync(userData); // If Enabled Local Notifications //if (userData.IsNotificationEnabled) //{ // var notification = new NotificationRequest // { // NotificationId = 100, // Title = AppResources.LocalNotificationTitle, // Description = AppResources.LocalNotificationDescription // }; // NotificationCenter.Current.Show(notification); //} }
// this will be called when a potential exposure has been detected public async Task ExposureDetectedAsync(ExposureDetectionSummary summary, Func <Task <IEnumerable <ExposureInfo> > > getExposureInfo) { loggerService.StartMethod(); UserExposureSummary userExposureSummary = new UserExposureSummary(summary.DaysSinceLastExposure, summary.MatchedKeyCount, summary.HighestRiskScore, summary.AttenuationDurations, summary.SummationRiskScore); userData.ExposureSummary = userExposureSummary; loggerService.Info($"ExposureSummary.MatchedKeyCount: {userExposureSummary.MatchedKeyCount}"); loggerService.Info($"ExposureSummary.DaysSinceLastExposure: {userExposureSummary.DaysSinceLastExposure}"); loggerService.Info($"ExposureSummary.HighestRiskScore: {userExposureSummary.HighestRiskScore}"); loggerService.Info($"ExposureSummary.AttenuationDurations: {string.Join(",", userExposureSummary.AttenuationDurations)}"); loggerService.Info($"ExposureSummary.SummationRiskScore: {userExposureSummary.SummationRiskScore}"); var config = await GetConfigurationAsync(); if (userData.ExposureSummary.HighestRiskScore >= config.MinimumRiskScore) { var exposureInfo = await getExposureInfo(); loggerService.Info($"ExposureInfo: {exposureInfo.Count()}"); // Add these on main thread in case the UI is visible so it can update await Device.InvokeOnMainThreadAsync(() => { foreach (var exposure in exposureInfo) { loggerService.Info($"Exposure.Timestamp: {exposure.Timestamp}"); loggerService.Info($"Exposure.Duration: {exposure.Duration}"); loggerService.Info($"Exposure.AttenuationValue: {exposure.AttenuationValue}"); loggerService.Info($"Exposure.TotalRiskScore: {exposure.TotalRiskScore}"); loggerService.Info($"Exposure.TransmissionRiskLevel: {exposure.TransmissionRiskLevel}"); UserExposureInfo userExposureInfo = new UserExposureInfo(exposure.Timestamp, exposure.Duration, exposure.AttenuationValue, exposure.TotalRiskScore, (Covid19Radar.Model.UserRiskLevel)exposure.TransmissionRiskLevel); userData.ExposureInformation.Add(userExposureInfo); } }); } loggerService.Info($"Save ExposureSummary. MatchedKeyCount: {userData.ExposureSummary.MatchedKeyCount}"); loggerService.Info($"Save ExposureInformation. Count: {userData.ExposureInformation.Count}"); await userDataService.SetAsync(userData); // If Enabled Local Notifications //if (userData.IsNotificationEnabled) //{ // var notification = new NotificationRequest // { // NotificationId = 100, // Title = AppResources.LocalNotificationTitle, // Description = AppResources.LocalNotificationDescription // }; // NotificationCenter.Current.Show(notification); //} loggerService.EndMethod(); }
public void SetExposureInformation(UserExposureSummary summary, List <UserExposureInfo> informationList) { loggerService.StartMethod(); var summaryJson = JsonConvert.SerializeObject(summary); var informationListJson = JsonConvert.SerializeObject(informationList); secureStorageService.SetValue(PreferenceKey.ExposureSummary, summaryJson); secureStorageService.SetValue(PreferenceKey.ExposureInformation, informationListJson); loggerService.EndMethod(); }
// this will be called when a potential exposure has been detected public async Task ExposureDetectedAsync(ExposureDetectionSummary summary, Func <Task <IEnumerable <ExposureInfo> > > getExposureInfo) { var loggerService = LoggerService; loggerService.StartMethod(); var exposureNotificationService = ExposureNotificationService; var exposureInformationList = exposureNotificationService.GetExposureInformationList() ?? new List <UserExposureInfo>(); UserExposureSummary userExposureSummary = new UserExposureSummary(summary.DaysSinceLastExposure, summary.MatchedKeyCount, summary.HighestRiskScore, summary.AttenuationDurations, summary.SummationRiskScore); loggerService.Info($"ExposureSummary.MatchedKeyCount: {userExposureSummary.MatchedKeyCount}"); loggerService.Info($"ExposureSummary.DaysSinceLastExposure: {userExposureSummary.DaysSinceLastExposure}"); loggerService.Info($"ExposureSummary.HighestRiskScore: {userExposureSummary.HighestRiskScore}"); loggerService.Info($"ExposureSummary.AttenuationDurations: {string.Join(",", userExposureSummary.AttenuationDurations)}"); loggerService.Info($"ExposureSummary.SummationRiskScore: {userExposureSummary.SummationRiskScore}"); var config = await GetConfigurationAsync(); if (userExposureSummary.HighestRiskScore >= config.MinimumRiskScore) { var exposureInfo = await getExposureInfo(); loggerService.Info($"ExposureInfo: {exposureInfo.Count()}"); // Add these on main thread in case the UI is visible so it can update await Device.InvokeOnMainThreadAsync(() => { foreach (var exposure in exposureInfo) { loggerService.Info($"Exposure.Timestamp: {exposure.Timestamp}"); loggerService.Info($"Exposure.Duration: {exposure.Duration}"); loggerService.Info($"Exposure.AttenuationValue: {exposure.AttenuationValue}"); loggerService.Info($"Exposure.TotalRiskScore: {exposure.TotalRiskScore}"); loggerService.Info($"Exposure.TransmissionRiskLevel: {exposure.TransmissionRiskLevel}"); if (exposure.TotalRiskScore >= config.MinimumRiskScore) { UserExposureInfo userExposureInfo = new UserExposureInfo(exposure.Timestamp, exposure.Duration, exposure.AttenuationValue, exposure.TotalRiskScore, (Covid19Radar.Model.UserRiskLevel)exposure.TransmissionRiskLevel); exposureInformationList.Add(userExposureInfo); } } }); } exposureInformationList.Sort((a, b) => a.Timestamp.CompareTo(b.Timestamp)); loggerService.Info($"Save ExposureSummary. MatchedKeyCount: {userExposureSummary.MatchedKeyCount}"); loggerService.Info($"Save ExposureInformation. Count: {exposureInformationList.Count}"); exposureNotificationService.SetExposureInformation(userExposureSummary, exposureInformationList); loggerService.EndMethod(); }
public void JsonSerializeTests() { var testDaysSinceLastExposure = 1; var testMatchedKeyCount = 2UL; var testHighestRiskScore = 30; var testAttenuationDurations = new TimeSpan[] { new TimeSpan(1, 2, 3, 4, 5), new TimeSpan(2, 3, 4, 5, 6) }; var testSummationRiskScore = 10; var testExposureSummary = new UserExposureSummary(testDaysSinceLastExposure, testMatchedKeyCount, testHighestRiskScore, testAttenuationDurations, testSummationRiskScore); var serializedJson = JsonConvert.SerializeObject(testExposureSummary); Assert.NotEmpty(serializedJson); Assert.Contains($"\"DaysSinceLastExposure\":{testDaysSinceLastExposure}", serializedJson); Assert.Contains($"\"MatchedKeyCount\":{testMatchedKeyCount}", serializedJson); Assert.Contains($"\"HighestRiskScore\":{testHighestRiskScore}", serializedJson); Assert.Contains("\"AttenuationDurations\":[\"1.02:03:04.0050000\",\"2.03:04:05.0060000\"]", serializedJson); Assert.Contains($"\"SummationRiskScore\":{testSummationRiskScore}", serializedJson); }
public void SetExposureInformationTests_Success() { mockHttpClientService.Setup(x => x.Create()).Returns(new HttpClient(new MockHttpHandler((request, cancellationToken) => { // Make an error not to process code that cannot be Mocked return(new HttpResponseMessage(HttpStatusCode.NotFound)); }))); var unitUnderTest = CreateService(); var actualExposureSummaryJson = ""; mockSecureStorageService.Setup(x => x.SetValue("ExposureSummary", It.IsAny <string>())).Callback <string, string>((k, v) => { actualExposureSummaryJson = v; }); var actualExposureInformaionJson = ""; mockSecureStorageService.Setup(x => x.SetValue("ExposureInformation", It.IsAny <string>())).Callback <string, string>((k, v) => { actualExposureInformaionJson = v; }); var testExposureSummary = new UserExposureSummary(1, 2, 30, new[] { new TimeSpan(0, 5, 0), new TimeSpan(0, 10, 0) }, 40); var testExposureInformation = new List <UserExposureInfo> { new UserExposureInfo(new DateTime(2020, 12, 21), new TimeSpan(0, 10, 0), 20, 30, UserRiskLevel.Lowest), new UserExposureInfo(new DateTime(2020, 12, 22), new TimeSpan(0, 20, 0), 30, 40, UserRiskLevel.Low) }; unitUnderTest.SetExposureInformation(testExposureSummary, testExposureInformation); mockSecureStorageService.Verify(x => x.SetValue("ExposureSummary", It.IsAny <string>()), Times.Once()); mockSecureStorageService.Verify(x => x.SetValue("ExposureInformation", It.IsAny <string>()), Times.Once()); Assert.NotEmpty(actualExposureSummaryJson); Assert.Contains("\"DaysSinceLastExposure\":1", actualExposureSummaryJson); Assert.Contains("\"MatchedKeyCount\":2", actualExposureSummaryJson); Assert.Contains("\"HighestRiskScore\":30", actualExposureSummaryJson); Assert.Contains("\"SummationRiskScore\":40", actualExposureSummaryJson); Assert.Contains("\"AttenuationDurations\":[\"00:05:00\",\"00:10:00\"]", actualExposureSummaryJson); Assert.NotEmpty(actualExposureInformaionJson); var regex = new Regex("^\\[({.+}),({.+})\\]$"); var matches = regex.Matches(actualExposureInformaionJson); Assert.Single(matches); Assert.Equal(3, matches[0].Groups.Count); var actualInfo1 = matches[0].Groups[1].Value; Assert.Contains("\"Timestamp\":\"2020-12-21T00:00:00\"", actualInfo1); Assert.Contains("\"Duration\":\"00:10:00\"", actualInfo1); Assert.Contains("\"AttenuationValue\":20", actualInfo1); Assert.Contains("\"TotalRiskScore\":30", actualInfo1); Assert.Contains("\"TransmissionRiskLevel\":1", actualInfo1); var actualInfo2 = matches[0].Groups[2].Value; Assert.Contains("\"Timestamp\":\"2020-12-22T00:00:00\"", actualInfo2); Assert.Contains("\"Duration\":\"00:20:00\"", actualInfo2); Assert.Contains("\"AttenuationValue\":30", actualInfo2); Assert.Contains("\"TotalRiskScore\":40", actualInfo2); Assert.Contains("\"TransmissionRiskLevel\":2", actualInfo2); }
// this will be called when a potential exposure has been detected public async Task ExposureDetectedAsync(ExposureDetectionSummary summary, Func <Task <IEnumerable <ExposureInfo> > > getExposureInfo) { var loggerService = LoggerService; loggerService.StartMethod(); var exposureNotificationService = ExposureNotificationService; var exposureInformationList = exposureNotificationService.GetExposureInformationList() ?? new List <UserExposureInfo>(); UserExposureSummary userExposureSummary = new UserExposureSummary(summary.DaysSinceLastExposure, summary.MatchedKeyCount, summary.HighestRiskScore, summary.AttenuationDurations, summary.SummationRiskScore); loggerService.Info($"ExposureSummary.MatchedKeyCount: {userExposureSummary.MatchedKeyCount}"); loggerService.Info($"ExposureSummary.DaysSinceLastExposure: {userExposureSummary.DaysSinceLastExposure}"); loggerService.Info($"ExposureSummary.HighestRiskScore: {userExposureSummary.HighestRiskScore}"); loggerService.Info($"ExposureSummary.AttenuationDurations: {string.Join(",", userExposureSummary.AttenuationDurations)}"); loggerService.Info($"ExposureSummary.SummationRiskScore: {userExposureSummary.SummationRiskScore}"); var config = await GetConfigurationAsync(); var isNewExposureDetected = false; if (userExposureSummary.HighestRiskScore >= config.MinimumRiskScore) { var exposureInfo = await getExposureInfo(); loggerService.Info($"ExposureInfo: {exposureInfo.Count()}"); foreach (var exposure in exposureInfo) { loggerService.Info($"Exposure.Timestamp: {exposure.Timestamp}"); loggerService.Info($"Exposure.Duration: {exposure.Duration}"); loggerService.Info($"Exposure.AttenuationValue: {exposure.AttenuationValue}"); loggerService.Info($"Exposure.TotalRiskScore: {exposure.TotalRiskScore}"); loggerService.Info($"Exposure.TransmissionRiskLevel: {exposure.TransmissionRiskLevel}"); if (exposure.TotalRiskScore >= config.MinimumRiskScore) { UserExposureInfo userExposureInfo = new UserExposureInfo(exposure.Timestamp, exposure.Duration, exposure.AttenuationValue, exposure.TotalRiskScore, (Covid19Radar.Model.UserRiskLevel)exposure.TransmissionRiskLevel); exposureInformationList.Add(userExposureInfo); isNewExposureDetected = true; } } } if (isNewExposureDetected) { loggerService.Info($"Save ExposureSummary. MatchedKeyCount: {userExposureSummary.MatchedKeyCount}"); loggerService.Info($"Save ExposureInformation. Count: {exposureInformationList.Count}"); exposureInformationList.Sort((a, b) => a.Timestamp.CompareTo(b.Timestamp)); exposureNotificationService.SetExposureInformation(userExposureSummary, exposureInformationList); await LocalNotificationService.ShowExposureNotificationAsync(); } else { loggerService.Info($"MatchedKeyCount: {userExposureSummary.MatchedKeyCount}, but no new exposure detected"); } loggerService.EndMethod(); }