private async Task <bool> SendAsync(string idempotencyKey, List <EventLog> eventLogList) { _loggerService.StartMethod(); try { var request = new V1EventLogRequest() { IdempotencyKey = idempotencyKey, Platform = _essentialsService.Platform, AppPackageName = _essentialsService.AppPackageName, EventLogs = eventLogList, }; // Create device verification payload PolicyResult <string> policyResult = await Policy .HandleResult <string>(result => _deviceVerifier.IsErrorPayload(result)) .WaitAndRetryAsync(3, retryAttempt => { double delay = Math.Pow(2, retryAttempt + 1); _loggerService.Warning($"Payload creation failed. retryAttempt:{retryAttempt} delay:{delay}sec"); return(TimeSpan.FromSeconds(delay)); }) .ExecuteAndCaptureAsync(() => _deviceVerifier.VerifyAsync(request)); if (policyResult.Outcome == OutcomeType.Failure) { _loggerService.Error("Payload creation failed all."); return(false); } _loggerService.Info("Payload creation successful."); request.DeviceVerificationPayload = policyResult.Result; ApiResponse <string> response = await _httpDataService.PutEventLog(request); _loggerService.Info($"PutEventLog() StatusCode:{response.StatusCode}"); if (response.StatusCode == (int)HttpStatusCode.Created) { _loggerService.Info("Send event log succeeded"); return(true); } } catch (Exception ex) { _loggerService.Exception("Exception occurred, SendAsync", ex); } finally { _loggerService.EndMethod(); } _loggerService.Error("Send event log failure"); return(false); }
private async Task <DiagnosisSubmissionParameter> CreateSubmissionAsync(IEnumerable <TemporaryExposureKey> temporaryExposureKeys, string positiveDiagnosis) { var loggerService = LoggerService; loggerService.StartMethod(); // Filter Temporary exposure keys var filteredTemporaryExposureKeys = ExposureNotificationService.FliterTemporaryExposureKeys(temporaryExposureKeys); // Create the network keys var keys = filteredTemporaryExposureKeys.Select(k => new DiagnosisSubmissionParameter.Key { KeyData = Convert.ToBase64String(k.Key), RollingStartNumber = (uint)(k.RollingStart - DateTime.UnixEpoch).TotalMinutes / 10, RollingPeriod = (uint)(k.RollingDuration.TotalMinutes / 10), }); var beforeKey = JsonConvert.SerializeObject(temporaryExposureKeys.ToList()); var afterKey = JsonConvert.SerializeObject(keys.ToList()); Debug.WriteLine($"C19R {beforeKey}"); Debug.WriteLine($"C19R {afterKey}"); if (keys.Count() == 0) { loggerService.Error($"Temporary exposure keys is empty."); loggerService.EndMethod(); throw new InvalidDataException(); } // Generate Padding var padding = GetPadding(); // Create the submission var submission = new DiagnosisSubmissionParameter() { Keys = keys.ToArray(), Regions = AppSettings.Instance.SupportedRegions, Platform = DeviceInfo.Platform.ToString().ToLowerInvariant(), DeviceVerificationPayload = null, AppPackageName = AppInfo.PackageName, VerificationPayload = positiveDiagnosis, Padding = padding }; submission.DeviceVerificationPayload = await DeviceVerifier.VerifyAsync(submission); loggerService.Info($"DeviceVerificationPayload is {(string.IsNullOrEmpty(submission.DeviceVerificationPayload) ? "null or empty" : "set")}."); loggerService.Info($"VerificationPayload is {(string.IsNullOrEmpty(submission.VerificationPayload) ? "null or empty" : "set")}."); loggerService.EndMethod(); return(submission); }
private async Task <DiagnosisSubmissionParameter> CreateSubmissionAsync( DateTime symptomOnsetDate, IList <TemporaryExposureKey> temporaryExposureKeys, string processNumber, string idempotencyKey ) { _loggerService.StartMethod(); // Create the network keys var keys = temporaryExposureKeys.Select(k => new DiagnosisSubmissionParameter.Key { KeyData = Convert.ToBase64String(k.KeyData), RollingStartNumber = (uint)k.RollingStartIntervalNumber, RollingPeriod = (uint)k.RollingPeriod, ReportType = (uint)k.ReportType, }); // Generate Padding var padding = GetPadding(); // Create the submission var submission = new DiagnosisSubmissionParameter() { SymptomOnsetDate = symptomOnsetDate.ToString(AppConstants.FORMAT_TIMESTAMP), Keys = keys.ToArray(), Regions = AppSettings.Instance.SupportedRegions, Platform = DeviceInfo.Platform.ToString().ToLowerInvariant(), DeviceVerificationPayload = null, AppPackageName = AppInfo.PackageName, VerificationPayload = processNumber, IdempotencyKey = idempotencyKey, Padding = padding }; submission.DeviceVerificationPayload = await _deviceVerifier.VerifyAsync(submission); _loggerService.Info($"DeviceVerificationPayload is {(string.IsNullOrEmpty(submission.DeviceVerificationPayload) ? "null or empty" : "set")}."); _loggerService.Info($"VerificationPayload is {(string.IsNullOrEmpty(submission.VerificationPayload) ? "null or empty" : "set")}."); _loggerService.EndMethod(); return(submission); }
private async Task SendExposureDataAsync( string idempotencyKey, ExposureData exposureData ) { _loggerService.StartMethod(); SendEventLogState sendEventLogState = _userDataRepository.GetSendEventLogState(); bool isEnabled = sendEventLogState == SendEventLogState.Enable; if (!isEnabled) { _loggerService.Debug($"Send event-log function is not enabled."); _loggerService.EndMethod(); return; } await _serverConfigurationRepository.LoadAsync(); string exposureDataCollectServerEndpoint = _serverConfigurationRepository.EventLogApiEndpoint; _loggerService.Debug($"exposureDataCollectServerEndpoint: {exposureDataCollectServerEndpoint}"); try { var contentJson = exposureData.ToJsonString(); var eventLog = new V1EventLogRequest.EventLog() { HasConsent = isEnabled, Epoch = _dateTimeUtility.UtcNow.ToUnixEpoch(), Type = "ExposureData", Subtype = "Debug", Content = contentJson, }; var eventLogs = new[] { eventLog }; var request = new V1EventLogRequest() { IdempotencyKey = idempotencyKey, Platform = _essentialsService.Platform, AppPackageName = _essentialsService.AppPackageName, EventLogs = eventLogs, }; request.DeviceVerificationPayload = await _deviceVerifier.VerifyAsync(request); var requestJson = request.ToJsonString(); var httpContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); Uri uri = new Uri(exposureDataCollectServerEndpoint); HttpResponseMessage response = await _httpClient.PutAsync(uri, httpContent); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); _loggerService.Debug($"{responseJson}"); } else { _loggerService.Info($"UploadExposureDataAsync {response.StatusCode}"); } } finally { _loggerService.EndMethod(); } }
private async Task<DiagnosisSubmissionParameter> CreateSubmissionAsync( bool hasSymptom, DateTime symptomOnsetDate, IList<TemporaryExposureKey> temporaryExposureKeys, string processNumber, string idempotencyKey ) { _loggerService.StartMethod(); // Create the network keys var keys = temporaryExposureKeys.Select(k => new DiagnosisSubmissionParameter.Key { KeyData = Convert.ToBase64String(k.KeyData), RollingStartNumber = (uint)k.RollingStartIntervalNumber, RollingPeriod = (uint)k.RollingPeriod, ReportType = (uint)k.ReportType, }); // Generate Padding var padding = GetPadding(); // Create the submission var submission = new DiagnosisSubmissionParameter() { HasSymptom = hasSymptom, OnsetOfSymptomOrTestDate = symptomOnsetDate.ToString(AppConstants.FORMAT_TIMESTAMP), Keys = keys.ToArray(), Regions = AppSettings.Instance.SupportedRegions, Platform = DeviceInfo.Platform.ToString().ToLowerInvariant(), DeviceVerificationPayload = null, AppPackageName = AppInfo.PackageName, VerificationPayload = processNumber, IdempotencyKey = idempotencyKey, Padding = padding }; // Create device verification payload var tries = 0; var delay = 4 * 1000; while (true) { var deviceVerificationPayload = await _deviceVerifier.VerifyAsync(submission); if (!_deviceVerifier.IsErrorPayload(deviceVerificationPayload)) { _loggerService.Info("Payload creation successful."); submission.DeviceVerificationPayload = deviceVerificationPayload; break; } else if (tries >= 3) { _loggerService.Error("Payload creation failed all."); throw new DiagnosisKeyRegisterException(DiagnosisKeyRegisterException.Codes.FailedCreatePayload); } _loggerService.Warning($"Payload creation failed. {tries + 1} time(s)."); _loggerService.Info($"delay {delay} msec"); await Task.Delay(delay); delay *= 2; tries++; } _loggerService.Info($"DeviceVerificationPayload is {(string.IsNullOrEmpty(submission.DeviceVerificationPayload) ? "null or empty" : "set")}."); _loggerService.Info($"VerificationPayload is {(string.IsNullOrEmpty(submission.VerificationPayload) ? "null or empty" : "set")}."); _loggerService.EndMethod(); return submission; }