public static void Main() { HwDevices=new Data(); //Utility.SetLocalTime(GetNetworkTime()); Program myApplication = new Program(); Thread readValues = new Thread(ValuesReader); readValues.Start(); while (init) { }; Touch.Initialize(myApplication); myApplication.Run(myApplication.CreateWindow()); Thread.Sleep(Timeout.Infinite); }
public Post(double hum, double temp, Data hw) { HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://picktheoutfit.com/api/measurements"); request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST"; string json = "{\"humidity\":" + hw.humidity + "," + "\"temperature\":" + temp + ","+"\"weather_type\":" + hw.getWeatherType()+","+"\"location\": \"" + hw.location + "\"}"; // turn our request string into a byte stream byte[] postBytes = Encoding.UTF8.GetBytes(json); // this is important - make sure you specify type this way request.ContentType = "application/json"; request.Accept = "application/json"; request.ContentLength = postBytes.Length; Stream requestStream = request.GetRequestStream(); // now send it requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); // grab te response and print it out to the console along with the status code HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string result; using (StreamReader rdr = new StreamReader(response.GetResponseStream())) { result = rdr.ReadToEnd(); Debug.Print(result); } request.Dispose(); response.Close(); requestStream.Dispose(); }
public Data AnalyseXMLResults(Data hw) { byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(result); MemoryStream strm = new MemoryStream(data); XmlReaderSettings ss = new XmlReaderSettings(); ss.IgnoreWhitespace = false; ss.IgnoreComments=false; XmlReader xml = XmlReader.Create(strm, ss); while (!xml.EOF) { xml.Read(); if (xml.Name == "yweather:location") { while (xml.MoveToNextAttribute()) { if (xml.Name == "city") { hw.location = xml.Value; } if (xml.Name == "country") { hw.location += ", " + xml.Value; Debug.Print("Location:" + hw.location); } } } if (xml.Name == "yweather:astronomy") { while (xml.MoveToNextAttribute()) { if (xml.Name == "sunrise") { string[] aux = xml.Value.Split(':'); hw.sunrise.hour = int.Parse(aux[0]); aux = aux[1].Split(' '); hw.sunrise.minute = int.Parse(aux[0]); Debug.Print("Sunrise:" + hw.sunrise.hour + ":" + hw.sunrise.minute); } if (xml.Name == "sunset") { string[] aux = xml.Value.Split(':'); hw.sunset.hour = int.Parse(aux[0]); aux = aux[1].Split(' '); hw.sunset.minute = int.Parse(aux[0]); Debug.Print("Sunset:" + hw.sunset.hour + ":" + hw.sunset.minute); } } } if (xml.Name == "yweather:atmosphere") { while (xml.MoveToNextAttribute()) { if (xml.Name == "humidity") { Debug.Print("Humidity:" + xml.Value); hw.humidity = int.Parse(xml.Value); } } } if (xml.Name == "yweather:condition") { while (xml.MoveToNextAttribute()) { if (xml.Name == "code") { hw.setWeatherType(int.Parse(xml.Value)); Debug.Print("Code:" + xml.Value); } } } //Gets the current date from the xml file if (xml.NodeType==XmlNodeType.Comment) { string date = xml.Value; string [] aux=date.Split(' '); aux = aux[5].Split(':'); hw.current.hour = 2+int.Parse(aux[0]); hw.current.minute = int.Parse(aux[1]); Debug.Print("Time: " + hw.current.hour+":"+hw.current.minute); } } strm.Close(); strm.Dispose(); xml.Close(); xml.Dispose(); return hw; }
private static void ValuesReader() { double oldTemp = 0; double oldHum = 0; Get xml = new Get("http://weather.yahooapis.com/forecastrss?w="+HwDevices.WOEID+"&u=c.xml"); HwDevices = xml.AnalyseXMLResults(HwDevices); while (true) { //reads the temperature double temp = HwDevices.ReadValue(); //reads the humidity double hum = 0; if (oldHum - hum <= -5 || oldHum - hum >= 5 || oldTemp - temp <= -2 || oldTemp - temp >= 2 || oldTemp == oldHum) { new Post(hum, temp, HwDevices); } //the current values are stored on the old values for the evaluation in the next 5 minutes oldHum = hum; oldTemp = temp; init = false; Thread.Sleep(HwDevices.getUpdateTime()*60*1000); } }