예제 #1
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var key = bindingContext.ModelName;
            var val = bindingContext.ValueProvider.GetValue(key);

            if (val != null)
            {
                var s = val.AttemptedValue;
                if (s != null)
                {
                    string[] rawData = s.Split(new[] { ',' });
                    bool     validDataIsAvaialble = null != rawData && 3 == rawData.Length;
                    if (validDataIsAvaialble)
                    {
                        GetEmissionRequest model = new GetEmissionRequest()
                        {
                            VehicleSubTypeId = rawData[0],
                            MapXml           = rawData[1]
                        };
                        string[] distanceArrays     = rawData[2].Split('~');
                        bool     validDistancePoint = null != distanceArrays && 0 < distanceArrays.Length;
                        if (validDistancePoint)
                        {
                            List <double> distances = new List <double>();
                            foreach (string points in distanceArrays)
                            {
                                distances.Add(Double.Parse(points));
                            }
                            model.Distance = distances.ToArray <double>();
                        }
                        bindingContext.Model = model;
                    }
                }
                else
                {
                    // change this line to null if you prefer nulls to empty arrays
                    bindingContext.Model = default(GetEmissionRequest);
                }
                return(true);
            }
            return(false);
        }
예제 #2
0
        public double[] GetEmission(GetEmissionRequest request)
        {
            VehicleTypeRepository repo = new VehicleTypeRepository(CONNECTION_STRING);

            /*Dictionary<string, object> routeData = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(routeDetails);
             * Dictionary<string, object> legs = (routeData["routes"] as Dictionary<string, object>)["legs"] as Dictionary<string, object>;
             * ArrayList steps = legs["steps"] as ArrayList;
             * foreach (var step in steps)
             * {
             *  Dictionary<string, object> distanceData = (step as Dictionary<string, object>)["distance"] as Dictionary<string, object>;
             *  decimal distance = Decimal.Parse(distanceData["value"] as string);
             *  Guid subTypeId = Guid.Parse(vehicleSubType);
             *  VehicleSubType details = repo.GetSubTypeDetails(subTypeId);
             *  decimal emissionFactor = details.FuelType.CO2Emissions;
             *  decimal mileage = details.Mileage;
             *  decimal CO2Emissions = emissionFactor * (distance / mileage);
             *  StringBuilder instructions = new StringBuilder((step as Dictionary<string, object>)["instructions"] as string);
             *  instructions.Insert(0, CO2Emissions.ToString());
             *  (step as Dictionary<string, object>)["instructions"] = instructions.ToString();
             * }
             * return (new JavaScriptSerializer()).Serialize(routeData);
             */
            Guid           subTypeId      = Guid.Parse(request.VehicleSubTypeId);
            VehicleSubType details        = repo.GetSubTypeDetails(subTypeId);
            double         emissionFactor = details.FuelType.CO2EmissionFactor;
            double         mileage        = details.Mileage;
            //converting the entire leg distance to an array and then performing actions and conveting to an array and then passing this back to client
            List <double> _legDistance = new List <double>();

            for (int i = 0; i < request.Distance.Length; i++)
            {
                _legDistance.Add((request.Distance[i] / mileage) * emissionFactor);
            }
            return(_legDistance.ToArray());

            //decimal CO2Emissions = emissionFactor * (request.Distance / mileage);
            //return CO2Emissions;
        }