public Task Handle(TemperatureRead message, IMessageHandlerContext context)
        {
            Data.OriginatorId    = message.Id;
            Data.TemperatureRead = true;
            Data.Temp            = message.Value;

            return(CheckComplete(context));
        }
Ejemplo n.º 2
0
        public static Weather GetWeatherData(string zipcode)
        {
            Weather         forcastdata = GetForcastDataExternalService(zipcode);
            TemperatureRead current     = GetCurrentDataExternalService(zipcode);

            Weather output = new Weather();

            output.CrntTemp = current;
            output.Readings = forcastdata.Readings;
            output.City     = forcastdata.City;

            return(output);
        }
Ejemplo n.º 3
0
        public static Weather GetForcastDataExternalService(string zipcode)
        {
            Weather weather       = new Weather();
            string  URLForcast    = "http://api.openweathermap.org/data/2.5/forecast";
            string  urlParameters = "?zip={zip}&APPID=62cbe2991520379376b00aecd963ea3a";

            urlParameters = urlParameters.Replace("{zip}", zipcode);
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(URLForcast);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var     result = response.Content.ReadAsStringAsync().Result;
                dynamic obj    = JsonConvert.DeserializeObject <dynamic>(result);

                List <TemperatureRead> temperatureReadList = new List <TemperatureRead>();
                weather.City = obj["city"]["name"];
                foreach (dynamic reading in obj["list"])
                {
                    // First make a System.DateTime equivalent to the UNIX Epoch.
                    System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
                    // Add the number of seconds in UNIX timestamp to be converted.
                    string utcunixdate = reading["dt"];
                    dateTime = dateTime.AddSeconds(double.Parse(utcunixdate));

                    TemperatureRead temperatureRead = new TemperatureRead();

                    temperatureRead.ReadTime    = dateTime;
                    temperatureRead.Temperature = reading["main"]["temp"];
                    temperatureRead.Details     = reading["weather"][0]["description"];
                    temperatureRead.Icon        = reading["weather"][0]["icon"];
                    temperatureReadList.Add(temperatureRead);
                }
                weather.Readings = temperatureReadList.OrderBy(x => x.ReadTime).ToArray();
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
            return(weather);
        }
Ejemplo n.º 4
0
        private void ReadTemperature(object state)
        {
            try
            {
                Temperature.TemperatureSensorReading temperatureReading = GetTemperatureSensorReadingResult();

                if (temperatureReading.IsValid && TemperatureRead != null)
                {
                    TemperatureRead.Invoke(this, new TemperatureReadingArgs(temperatureReading.Temperature, temperatureReading.Humidity));
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Post([FromBody] TemperatureRead tempRead)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // Because binding doesn't seem to work.
            tempRead.ID = 0;
            // Because NTP is hard.
            tempRead.Date = DateTime.Now;

            _context.Add(tempRead);
            await _context.SaveChangesAsync();

            return(Ok());
        }