static async Task <double> ComputeSpeedAsync(CarScan carScan)
        {
            var distanceInKm = await GetDistanceAsync(carScan.StartScannerId, carScan.EndScannerId);

            var durationInHours = carScan.EndTime.Subtract(carScan.StartTime).TotalHours;

            var speed = distanceInKm / durationInHours;

            return(speed);
        }
        public IEnumerable <CarScan> Get()
        {
            CarScan cs = new CarScan()
            {
                Action         = CarStates.Start,
                StartScannerId = "A",
                StartTime      = DateTime.Now,
                LicensePlate   = "1-ABC-AA"
            };

            return(new CarScan[] { cs });
        }
        private void StoreEvent(CarScan carscan)
        {
            var connectionString = "DefaultEndpointsProtocol=https;AccountName=mindparkwebjobs;AccountKey=D6BZdyaZq+kGMxq5H1oAXy9rMElxmcQFMy581uh8lbtDrt3cDEiiOzLoogQu8y6uO2WWixXQbVojURIJKCi3TA==";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
            CloudTable          table          = tableClient.GetTableReference("CarScans");

            table.CreateIfNotExists();

            CarScanStorable carscanStorable = new CarScanStorable(carscan);

            TableOperation insertOperation = TableOperation.Insert(carscanStorable);

            table.Execute(insertOperation);
        }
        public async static Task <string> SendAsync(CarScan carScan)
        {
            var result = await client.PostAsync(
                requestUri : "/api/Scanner",
                content : new StringContent(
                    content: JsonConvert.SerializeObject(carScan),
                    encoding: Encoding.UTF8,
                    mediaType: "application/json"));

            var response = await result.Content.ReadAsStringAsync();

            Console.WriteLine(response);

            return(response);
        }
        /// <summary>
        /// TODO: Replace with your own actor method.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        async Task ICarActor.SetStateAsync(CarScan carscan)
        {
            try
            {
                CarScan cs;
                var     storedCarScan = await this.StateManager.GetStateAsync <CarScan>("state");

                if (storedCarScan.Action == CarStates.Unknown)
                {
                    cs = carscan;
                }
                else
                {
                    cs = storedCarScan;
                }

                switch (carscan.Action)
                {
                case CarStates.Start:
                    break;

                case CarStates.End:
                    cs.EndScannerId = carscan.EndScannerId;
                    cs.EndTime      = carscan.EndTime;
                    cs.Speed        = await ComputeSpeedAsync(cs);

                    break;
                }
                // Requests are not guaranteed to be processed in order nor at most once.
                // The update function here verifies that the incoming state is greater than the current count to preserve order.
                if (carscan.Action == CarStates.End)
                {
                    StoreEvent(cs);
                }
                await this.StateManager.AddOrUpdateStateAsync("state", cs, (key, value) => cs.Action > value.Action?cs : value);
            }
            catch (Exception ex)
            {
                throw;
            }
        }