public async Task VehicleNotNull_Test()
        {
            VehiclePushDataPoint stuttgartPoint = new VehiclePushDataPoint()
            {
                Timestamp    = 1559137020,
                Odometer     = 7200,
                FuelLevel    = 52,
                PositionLat  = (float)48.771990,
                PositionLong = (float)9.172787
            };

            VehiclePushDataPoint munchenPoint = new VehiclePushDataPoint()
            {
                Timestamp    = 1559137020,
                Odometer     = 7300,
                FuelLevel    = 54,
                PositionLat  = (float)48.137154,
                PositionLong = (float)11.576124
            };

            VehiclePush vehiclePush = new VehiclePush()
            {
                BreakThreshold = 80,
                GasTankSize    = 1800,
                Vin            = "WDD1671591Z000999",
                Data           = new System.Collections.Generic.List <VehiclePushDataPoint>()
                {
                    stuttgartPoint,
                    munchenPoint
                }
            };

            // Arrange
            var controller = new VehicleController();

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            // Act
            var response = await controller.Trips(vehiclePush);

            // Assert
            VehiclePushAnalysis pushResponse;

            Assert.IsTrue(response.TryGetContentValue(out pushResponse));
            Assert.IsNotNull(pushResponse);
        }
Пример #2
0
        public async Task <HttpResponseMessage> Trips(VehiclePush vehiclePush)
        {
            //string username = Thread.CurrentPrincipal.Identity.Name;
            //if (username.ToLower() != "maleuser" && username.ToLower() != "femaleuser") return Request.CreateResponse(HttpStatusCode.BadRequest);

            if (vehiclePush == null)
            {
                return(null);
            }

            VehiclePushAnalysis vehiclePushAnalysis = new VehiclePushAnalysis()
            {
                Vin = vehiclePush?.Vin
            };
            List <VehiclePushDataPoint> dataPoints = vehiclePush.Data;

            //sollte mindestens <2 Elemente haben, denn es ist eine Reise
            if (dataPoints.Count < 1)
            {
                return(null);
            }

            var firstDataPoint = dataPoints[0];
            var lastDataPoint  = dataPoints[dataPoints.Count - 1];


            Response bingResponse = null;

            using (HttpClient client = new HttpClient())
            {
                try
                {
                    string pathOrigin            = path + firstDataPoint?.PositionLat.ToString(CultureInfo.InvariantCulture) + "," + firstDataPoint?.PositionLong.ToString(CultureInfo.InvariantCulture) + myKey;
                    HttpResponseMessage response = await client.GetAsync(pathOrigin);

                    XmlSerializer serializer = new XmlSerializer(typeof(Response));
                    if (response.IsSuccessStatusCode)
                    {
                        var responseString = await response.Content.ReadAsStringAsync();

                        StringReader rdr = new StringReader(responseString);
                        bingResponse = (Response)serializer.Deserialize(rdr);

                        vehiclePushAnalysis.Departure = bingResponse?.ResourceSets?.ResourceSet?.Resources?.Location?.Address?.Locality;
                    }


                    string pathDestination = path + lastDataPoint.PositionLat.ToString(CultureInfo.InvariantCulture) + "," + lastDataPoint.PositionLong.ToString(CultureInfo.InvariantCulture) + myKey;
                    response = await client.GetAsync(pathDestination);

                    if (response.IsSuccessStatusCode)
                    {
                        var responseString = await response.Content.ReadAsStringAsync();

                        StringReader rdr = new StringReader(responseString);
                        bingResponse = (Response)serializer.Deserialize(rdr);
                        vehiclePushAnalysis.Destination = bingResponse?.ResourceSets?.ResourceSet?.Resources?.Location?.Address?.Locality;
                    }
                }

                catch (System.Exception ex)
                {
                    throw;
                }
            }

            if (lastDataPoint.Odometer > firstDataPoint.Odometer)
            {
                //Per 100 km
                vehiclePushAnalysis.Consumption = System.Math.Abs((firstDataPoint.FuelLevel - lastDataPoint.FuelLevel) * 100 / (lastDataPoint.Odometer - firstDataPoint.Odometer));
            }

            int          previousFuelLevel = firstDataPoint.FuelLevel;
            int          currentFuelLevel;
            List <Break> RefuelStops = new List <Break>();
            List <Break> Breaks      = new List <Break>();

            foreach (var dataPoint in dataPoints)
            {
                currentFuelLevel = dataPoint.FuelLevel;

                Break br = new Break()
                {
                    StartTimestamp = dataPoint.Timestamp,
                    EndTimestamp   = dataPoint.Timestamp,
                    PositionLat    = dataPoint.PositionLat,
                    PositionLong   = dataPoint.PositionLong
                };
                if (currentFuelLevel > previousFuelLevel + 1)
                {
                    RefuelStops.Add(br);
                }
                else
                {
                    Breaks.Add(br);
                }

                previousFuelLevel = currentFuelLevel;
            }

            vehiclePushAnalysis.RefuelStops = RefuelStops;
            vehiclePushAnalysis.Breaks      = Breaks;

            return(Request.CreateResponse(HttpStatusCode.OK, vehiclePushAnalysis));
        }