示例#1
0
        public async Task RePosition(
            double latitude,
            double longitude,
            [Option(ShortName = "r")] RetryPolicyKey retryPolicyKey = RetryPolicyKey.NoRetry,
            [Option(ShortName = "c")] CachePolicyKey cachePolicyKey = CachePolicyKey.NoCache)
        {
            _battery.RePosition(latitude, longitude);

            var retryPolicy = _policyRegistry.Get <IAsyncPolicy>(retryPolicyKey.ToString());
            var cachePolicy = _policyRegistry.Get <IAsyncPolicy>(cachePolicyKey.ToString());
            var meteoPolicy = Policy.WrapAsync(cachePolicy, retryPolicy);

            var coords            = new Coordinate(latitude, longitude);
            var meteoPolicyResult = await meteoPolicy.ExecuteAndCaptureAsync(
                _ => GetMeteo(coords),
                new Context("Get meteo"));

            if (meteoPolicyResult.Outcome == OutcomeType.Successful)
            {
                _meteo = meteoPolicyResult.Result;
            }
            else
            {
                _console.Out.WriteLine($"Get meteo failed: {meteoPolicyResult.FinalException.Message}");
            }

            var inPositionPolicyResult = await retryPolicy.ExecuteAndCaptureAsync(
                _ => ReportPosition(coords),
                new Context("In position."));

            if (inPositionPolicyResult.Outcome == OutcomeType.Failure)
            {
                _console.Out.WriteLine($"Re-position failed: {inPositionPolicyResult.FinalException.Message}");
            }
        }
示例#2
0
        public async Task ToArms(
            [Option(ShortName = "r")] RetryPolicyKey retryPolicyKey     = RetryPolicyKey.NoRetry,
            [Option(ShortName = "c")] CachePolicyKey cachePolicyKey     = CachePolicyKey.NoCache,
            [Option(ShortName = "t")] TimeoutPolicyKey timeoutPolicyKey = TimeoutPolicyKey.NoTimeout,
            double?latitude = null, double?longitude = null)
        {
            var retryPolicy = _policyRegistry.Get <IAsyncPolicy>(retryPolicyKey.ToString());
            var cachePolicy = _policyRegistry.Get <IAsyncPolicy>(cachePolicyKey.ToString());
            var meteoPolicy = Policy.WrapAsync(cachePolicy, retryPolicy);

            await _battery.ToArms(timeoutPolicyKey);

            var coords = new Coordinate(latitude ?? DefaultLatitude, longitude ?? DefaultLongitude);

            var meteoTask        = meteoPolicy.ExecuteAndCaptureAsync(_ => GetMeteo(coords), new Context("Get meteo"));
            var registrationTask = retryPolicy.ExecuteAndCaptureAsync(_ => RegisterAsync(coords), new Context("Register unit"));
            await Task.WhenAll(registrationTask, meteoTask);

            var registrationPolicyCapture = registrationTask.Result;
            var meteoPolicyCapture        = meteoTask.Result;

            if (registrationPolicyCapture.Outcome == OutcomeType.Failure)
            {
                _console.Out.WriteLine($"Register unit failed: {registrationPolicyCapture.FinalException.Message}");
            }
            else
            {
                _mainFiringDirection = registrationPolicyCapture.Result.MainFiringDirection;
            }

            if (meteoPolicyCapture.Outcome == OutcomeType.Failure)
            {
                _console.Out.WriteLine($"Get meteo failed: {meteoPolicyCapture.FinalException.Message}");
            }
        }