public ActionResult HandleSpeedingViolation(SpeedingViolationDetected @event)
        {
            decimal fine = _fineCalculator.CalculateFine(@event.ViolationInKmh);

            string fineString = fine == 0 ? "tbd by the prosecutor" : $"{fine} Euro";

            _logger.LogInformation($"CJIB: Sent speeding ticket. Road: {@event.RoadId}, Licensenumber: {@event.VehicleId}, " +
                                   $"Violation: {@event.ViolationInKmh} Km/h, Fine: {fineString}, On: {@event.Timestamp.ToString("dd-MM-yyyy")} " +
                                   $"at {@event.Timestamp.ToString("hh:mm:ss")}.");

            return(Ok());
        }
Esempio n. 2
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 @event = new SpeedingViolationDetected
                    {
                        VehicleId      = msg.LicenseNumber,
                        RoadId         = _roadId,
                        ViolationInKmh = violation,
                        Timestamp      = msg.Timestamp
                    };
                    await daprClient.PublishEventAsync <SpeedingViolationDetected>("pubsub", "cjib.speedingviolation", @event);
                }

                return(Ok());
            }
            catch
            {
                return(Ok(new { status = "RETRY" }));
            }
        }
        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());
        }