コード例 #1
0
        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;
        }
コード例 #2
0
ファイル: EventLogService.cs プロジェクト: cocoa-mhlw/cocoa
        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);
        }