public async Task <ActionResult> VehicleExit(VehicleRegistered msg, [FromServices] DaprClient daprClient)
        {
            try
            {
                // get vehicle state
                var vehicleState = await _vehicleStateRepository.GetVehicleStateAsync(msg.LicenseNumber);

                if (vehicleState == null)
                {
                    return(NotFound());
                }

                // log exit
                _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")} " +
                                       $"of vehicle with license-number {msg.LicenseNumber}.");

                // update state
                vehicleState.ExitTimestamp = msg.Timestamp;
                await _vehicleStateRepository.SaveVehicleStateAsync(vehicleState);

                // handle possible speeding violation
                int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(
                    vehicleState.EntryTimestamp, vehicleState.ExitTimestamp);
                if (violation > 0)
                {
                    _logger.LogInformation($"Speeding violation detected ({violation} KMh) of vehicle" +
                                           $"with license-number {vehicleState.LicenseNumber}.");

                    var speedingViolation = new SpeedingViolation
                    {
                        VehicleId      = msg.LicenseNumber,
                        RoadId         = _roadId,
                        ViolationInKmh = violation,
                        Timestamp      = msg.Timestamp
                    };

                    // In last half of Assignment 3, update pub/sub to use Dapr ASP.NET Core client
                    //   argument #1: Name of pub/sub component
                    //   argument #2: Name of topic to which to publish
                    //   argument #3: The message payload
                    // publish speedingviolation
                    await daprClient.PublishEventAsync("pubsub", "collectfine", speedingViolation);

                    // pub/sub code from first-half of Assignment 3
                    // var message = JsonContent.Create<SpeedingViolation>(speedingViolation);
                    // // Replace the hardcoded API code with a call to the Dapr pub/sub side car
                    // await _httpClient.PostAsync("http://localhost:3600/v1.0/publish/pubsub/collectfine", message);
                    // //await _httpClient.PostAsync("http://localhost:6001/collectfine", message);
                }

                return(Ok());
            }
            catch
            {
                return(StatusCode(500));
            }
        }
    public async Task <ActionResult> VehicleExitAsync(VehicleRegistered msg, [FromServices] DaprClient daprClient)
    {
        try
        {
            // get vehicle state
            var state = await _vehicleStateRepository.GetVehicleStateAsync(msg.LicenseNumber);

            if (state == default(VehicleState))
            {
                return(NotFound());
            }

            // log exit
            _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")} " +
                                   $"of vehicle with license-number {msg.LicenseNumber}.");

            // update state
            var exitState = state.Value with {
                ExitTimestamp = msg.Timestamp
            };
            await _vehicleStateRepository.SaveVehicleStateAsync(exitState);

            // handle possible speeding violation
            int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(exitState.EntryTimestamp, exitState.ExitTimestamp.Value);
            if (violation > 0)
            {
                _logger.LogInformation($"Speeding violation detected ({violation} KMh) of vehicle" +
                                       $"with license-number {state.Value.LicenseNumber}.");

                var speedingViolation = new SpeedingViolation
                {
                    VehicleId      = msg.LicenseNumber,
                    RoadId         = _roadId,
                    ViolationInKmh = violation,
                    Timestamp      = msg.Timestamp
                };

                // publish speedingviolation (Dapr publish / subscribe)
                await daprClient.PublishEventAsync("pubsub", "speedingviolations", speedingViolation);
            }

            return(Ok());
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error occurred while processing EXIT");
            return(StatusCode(500));
        }
    }
        public async Task <ActionResult> VehicleExit(VehicleRegistered msg)
        {
            try
            {
                // get vehicle state
                var vehicleState = await _vehicleStateRepository.GetVehicleStateAsync(msg.LicenseNumber);

                if (vehicleState == null)
                {
                    return(NotFound());
                }

                // log exit
                _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")} " +
                                       $"of vehicle with license-number {msg.LicenseNumber}.");

                // update state
                vehicleState.ExitTimestamp = msg.Timestamp;
                await _vehicleStateRepository.SaveVehicleStateAsync(vehicleState);

                // handle possible speeding violation
                int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(
                    vehicleState.EntryTimestamp, vehicleState.ExitTimestamp);
                if (violation > 0)
                {
                    _logger.LogInformation($"Speeding violation detected ({violation} KMh) of vehicle" +
                                           $"with license-number {vehicleState.LicenseNumber}.");

                    var speedingViolation = new SpeedingViolation
                    {
                        VehicleId      = msg.LicenseNumber,
                        RoadId         = _roadId,
                        ViolationInKmh = violation,
                        Timestamp      = msg.Timestamp
                    };

                    // publish speedingviolation
                    var message = JsonContent.Create <SpeedingViolation>(speedingViolation);
                    await _httpClient.PostAsync("http://localhost:5001/collectfine", message);
                }

                return(Ok());
            }
            catch
            {
                return(StatusCode(500));
            }
        }
示例#4
0
        public async Task <ActionResult> VehicleExit(
            VehicleRegistered msg,
            //[FromState(DAPR_STORE_NAME)]StateEntry<VehicleInfo> state,
            [FromServices] DaprClient daprClient)
        {
            try
            {
                // get vehicle state
                var state = await daprClient.GetStateEntryAsync <VehicleState>(DAPR_STORE_NAME, msg.LicenseNumber);

                if (state.Value == null)
                {
                    return(NotFound());
                }

                // log exit
                _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")}: " +
                                       $"{state.Value.Brand} {state.Value.Model} with license-number {msg.LicenseNumber}.");

                // update state
                state.Value.ExitTimestamp = msg.Timestamp;
                await state.SaveAsync();

                // handle possible speeding violation
                int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(state.Value.EntryTimestamp, state.Value.ExitTimestamp);
                if (violation > 0)
                {
                    _logger.LogInformation($"Speeding violation detected ({violation} KMh) of {state.Value.Brand} {state.Value.Model} " +
                                           $"with license-number {state.Value.LicenseNumber}.");

                    var speedingViolation = new SpeedingViolation
                    {
                        VehicleId      = msg.LicenseNumber,
                        RoadId         = _roadId,
                        ViolationInKmh = violation,
                        Timestamp      = msg.Timestamp
                    };
                    await _governmentService.SendFine(speedingViolation);
                }

                return(Ok());
            }
            catch
            {
                return(Ok(new { status = "RETRY" }));
            }
        }
示例#5
0
    public async Task RegisterExitAsync(VehicleRegistered msg)
    {
        try
        {
            Logger.LogInformation($"EXIT detected in lane {msg.Lane} at " +
                                  $"{msg.Timestamp.ToString("hh:mm:ss")} " +
                                  $"of vehicle with license-number {msg.LicenseNumber}.");

            // remove lost vehicle timer
            await UnregisterReminderAsync("VehicleLost");

            // get vehicle state
            var vehicleState = await this.StateManager.GetStateAsync <VehicleState>("VehicleState");

            vehicleState = vehicleState with {
                ExitTimestamp = msg.Timestamp
            };
            await this.StateManager.SetStateAsync("VehicleState", vehicleState);

            // handle possible speeding violation
            int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(
                vehicleState.EntryTimestamp, vehicleState.ExitTimestamp.Value);
            if (violation > 0)
            {
                Logger.LogInformation($"Speeding violation detected ({violation} KMh) of vehicle " +
                                      $"with license-number {vehicleState.LicenseNumber}.");

                var speedingViolation = new SpeedingViolation
                {
                    VehicleId      = msg.LicenseNumber,
                    RoadId         = _roadId,
                    ViolationInKmh = violation,
                    Timestamp      = msg.Timestamp
                };

                // publish speedingviolation (Dapr publish / subscribe)
                await _daprClient.PublishEventAsync("pubsub", "speedingviolations", speedingViolation);
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error in RegisterExit");
        }
    }
        public async Task <ActionResult> VehicleExit(VehicleRegistered msg, [FromServices] IHttpClientFactory httpClientFactory)
        {
            // get vehicle state
            var state = _repo.GetVehicleState(msg.LicenseNumber);

            if (state == null)
            {
                return(NotFound());
            }

            // log exit
            _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")}: " +
                                   $"{state.Brand} {state.Model} with license-number {state.LicenseNumber}.");

            // update state
            state.ExitTimestamp = msg.Timestamp;
            _repo.StoreVehicleState(state);

            // handle possible speeding violation
            int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(state.EntryTimestamp, state.ExitTimestamp);

            if (violation > 0)
            {
                _logger.LogInformation($"Speeding violation detected ({violation} KMh) of {state.Brand} {state.Model} " +
                                       $"with license-number {state.LicenseNumber}.");

                var @event = new SpeedingViolationDetected
                {
                    VehicleId      = msg.LicenseNumber,
                    RoadId         = _roadId,
                    ViolationInKmh = violation,
                    Timestamp      = msg.Timestamp
                };

                var @eventJson = new StringContent(JsonSerializer.Serialize(@event, _jsonSerializerOptions), Encoding.UTF8, "application/json");
                var httpClient = httpClientFactory.CreateClient();
                var response   = await httpClient.PostAsync("http://localhost:6000/cjib/speedingviolation", @eventJson);
            }

            return(Ok());
        }
        public async Task <ActionResult> VehicleExit(VehicleRegistered msg, [FromServices] IHttpClientFactory httpClientFactory, [FromServices] DaprClient daprClient)
        {
            // get vehicle state
            var state = await daprClient.GetStateEntryAsync <VehicleState>(DAPR_STORE_NAME, msg.LicenseNumber);

            if (state.Value == null)
            {
                return(NotFound());
            }

            // log exit
            _logger.LogInformation($"EXIT detected in lane {msg.Lane} at {msg.Timestamp.ToString("hh:mm:ss")}: " +
                                   $"{state.Value.Brand} {state.Value.Model} with license-number {state.Value.LicenseNumber}.");

            // update state
            state.Value.ExitTimestamp = msg.Timestamp;
            await state.SaveAsync();

            // handle possible speeding violation
            int violation = _speedingViolationCalculator.DetermineSpeedingViolationInKmh(state.Value.EntryTimestamp, state.Value.ExitTimestamp);

            if (violation > 0)
            {
                _logger.LogInformation($"Speeding violation detected ({violation} KMh) of {state.Value.Brand} {state.Value.Model} " +
                                       $"with license-number {state.Value.LicenseNumber}.");

                var @event = new SpeedingViolationDetected
                {
                    VehicleId      = msg.LicenseNumber,
                    RoadId         = _roadId,
                    ViolationInKmh = violation,
                    Timestamp      = msg.Timestamp
                };

                await daprClient.PublishEventAsync <SpeedingViolationDetected>("pubsub", "cjib.speedingviolation", @event);
            }

            return(Ok());
        }