예제 #1
0
        private HeatingLoad CreateHeatingLoadResponse(double heatingLoadValue, double?surfaceArea)
        {
            HeatingLoad heatingLoadModel = new HeatingLoad();

            heatingLoadModel.ConsumptionPerSquareMeter = heatingLoadValue;
            heatingLoadModel.TotalConsumption          = heatingLoadValue * surfaceArea.Value;
            heatingLoadModel.Efficiency = CalculateEfficiency(heatingLoadValue);

            return(heatingLoadModel);
        }
예제 #2
0
        // POST api/heatingload
        public async Task <IHttpActionResult> Post([FromBody] BuildingParameters buildingParameters)
        {
            // TODO: add DI and extract all code, which gets the assessed heating load from controller to a service
            var url    = ConfigurationManager.AppSettings["aml.url"];
            var apiKey = ConfigurationManager.AppSettings["aml.apiKey"];

            // wraping payload with proper web service input model
            var amlInput = CreateAmlInput(buildingParameters);

            var content = JsonConvert.SerializeObject(amlInput,
                                                      new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            // composing request
            var amlRequest = CreateRequestMessage(url, apiKey, content);

            var result = await _client.SendAsync(amlRequest);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                return(InternalServerError());
            }

            string resultContent = await result.Content.ReadAsStringAsync();

            JObject resultObject = JObject.Parse(resultContent);

            // TODO: ouch!
            // Schema of your Web Service output object might be different, change accordingly
            double heatingLoadValue = resultObject.SelectToken("Results.output1.value.Values[0][6]").Value <double>();

            HeatingLoad heatingLoadModel = CreateHeatingLoadResponse(heatingLoadValue, buildingParameters.SurfaceArea);

            return(Ok(heatingLoadModel));
        }