Esempio n. 1
0
        public static async Task <string> GetWeather()
        {
            string url         = "http://api.openweathermap.org/data/2.5/weather?id=2158177&appid=df39100f7fe7b297c789818c5f2bb1bd";
            string weatherjson = await GetAsync(url);

            var obj                = Windows.Data.Json.JsonObject.Parse(weatherjson);
            var otemperature       = ((int)obj["main"].GetObject()["temp"].GetNumber()) - 273;
            var opressure          = (int)obj["main"].GetObject()["pressure"].GetNumber();
            var ohumidity          = (int)obj["main"].GetObject()["humidity"].GetNumber();
            var telemetryDataPoint = new TelemetryDataPoint()
            {
                temperature = otemperature,
                pressure    = opressure,
                humidity    = ohumidity
            };
            var stream1 = new MemoryStream();
            var ser     = new DataContractJsonSerializer(typeof(TelemetryDataPoint));

            ser.WriteObject(stream1, telemetryDataPoint);
            stream1.Position = 0;
            var    sr            = new StreamReader(stream1);
            string MessageString = sr.ReadToEnd();

            ;
            return(MessageString);
        }
Esempio n. 2
0
        public override async Task <TelemetryDataPoint> GetWeatherAsync()
        {
            if (CurrentCityIndex == -1)
            {
                ReadCities();
            }
            string url = string.Format("http://api.openweathermap.org/data/2.5/weather?id={0}&appid={1}",
                                       Cities[CurrentCityIndex].id, OpenWeatherAppKey);
            string weatherjson = await GetAsync(url);

            dynamic obj = JsonConvert.DeserializeObject(weatherjson);


            dynamic temp               = obj.main.temp;
            dynamic press              = obj.main.pressure;
            dynamic humid              = obj.main.humidity;
            var     otemperature       = (int)(float.Parse(temp.ToString()));
            var     opressure          = (int)(int.Parse(press.ToString()));
            var     ohumidity          = (int)(int.Parse(humid.ToString()));
            var     telemetryDataPoint = new TelemetryDataPoint()
            {
                city        = Cities[CurrentCityIndex].name,
                temperature = otemperature - 273,
                pressure    = opressure,
                humidity    = ohumidity
            };

            GetNextCity();
            return(telemetryDataPoint);
        }
Esempio n. 3
0
        public static TelemetryDataPoint Deserialize(string msg)
        {
            try
            {
                TelemetryDataPoint iMsg = JsonConvert.DeserializeObject <TelemetryDataPoint>(msg);
                return((TelemetryDataPoint)iMsg);
            }
            catch (Exception)
            {
                JObject obj = (JObject)JsonConvert.DeserializeObject(msg);


                if (obj != null)
                {
                    var to = new TelemetryDataPoint();
                    ////https://stackoverflow.com/questions/4144778/get-properties-and-values-from-unknown-object
                    //Type myType = obj.GetType();
                    //IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
                    ////////////////////
                    foreach (var property in obj.Properties()) //(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
                    {
                        string propertyName = property.Name;
                        var    val          = obj.GetValue(propertyName); // static classes cannot be instanced, so use null...


                        Type type2 = typeof(TelemetryDataPoint);         // IoTHubConnectionDetails is static class with public static properties
                        foreach (var property2 in type2.GetProperties()) //System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)) //(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
                        {
                            string propertyName2 = property2.Name;
                            if (propertyName2.ToLower() == propertyName.ToLower())
                            {
                                var    propertyInfo = type2.GetProperty(propertyName2); //, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                                string strngVal     = val.ToString();

                                //Assume its a string, but try to convert to double and int
                                object obj2 = strngVal;
                                int    i;
                                double d;
                                if (double.TryParse(strngVal, out d))
                                {
                                    obj2 = d;
                                }
                                else if (int.TryParse(strngVal, out i))
                                {
                                    obj2 = i;
                                }


                                //var info = propertyInfo.GetValue(type2, null);
                                propertyInfo.SetValue(to, obj2);
                            }
                        }
                    }
                    return(to);
                }
                return(null);
            }
        }
Esempio n. 4
0
        public override TelemetryDataPoint GetWeather()
        {
            double currentTemperature = minTemperature + rand.NextDouble() * 15;
            double currentHumidity    = minHumidity + rand.NextDouble() * 20;
            double currentPressure    = 0 + rand.NextDouble() * 100;

            var telemetryDataPoint = new TelemetryDataPoint
            {
                city        = "Xanadu",
                pressure    = (int)currentPressure,
                temperature = (int)currentTemperature,
                humidity    = (int)currentHumidity
            };

            return(telemetryDataPoint);
        }
Esempio n. 5
0
        public override TelemetryDataPoint GetWeather()
        {
            double currentTemperature = 27;
            double currentHumidity    = 15;
            double currentPressure    = 1001;

            var telemetryDataPoint = new TelemetryDataPoint
            {
                city        = "Xanadu",
                pressure    = (int)currentPressure,
                temperature = (int)currentTemperature,
                humidity    = (int)currentHumidity
            };

            return(telemetryDataPoint);
        }
Esempio n. 6
0
        public override string getval(string val)
        {
            double currentTemperature = minTemperature + rand.NextDouble() * 15;
            double currentHumidity    = minHumidity + rand.NextDouble() * 20;
            double currentPressure    = 0 + rand.NextDouble() * 100;
            //Create JSON message
            var telemetryDataPoint = new TelemetryDataPoint
            {
                city        = "",
                pressure    = (int)currentPressure,
                temperature = (int)currentTemperature,
                humidity    = (int)currentHumidity
            };

            return(JsonConvert.SerializeObject(telemetryDataPoint));
        }
Esempio n. 7
0
        public static async Task <TelemetryDataPoint> GetWeatherObj()
        {
            string url         = "http://api.openweathermap.org/data/2.5/weather?id=2158177&appid=df39100f7fe7b297c789818c5f2bb1bd";
            string weatherjson = await GetAsync(url);

            var obj                = Windows.Data.Json.JsonObject.Parse(weatherjson);
            var otemperature       = ((int)obj["main"].GetObject()["temp"].GetNumber()) - 273;
            var opressure          = (int)obj["main"].GetObject()["pressure"].GetNumber();
            var ohumidity          = (int)obj["main"].GetObject()["humidity"].GetNumber();
            var telemetryDataPoint = new TelemetryDataPoint()
            {
                temperature = otemperature,
                pressure    = opressure,
                humidity    = ohumidity
            };

            return(telemetryDataPoint);
        }