コード例 #1
0
ファイル: airDB.cs プロジェクト: rssktt/FairAir
        public static async Task<XDocument> getAQI()
        {
            XDocument xd = null;

            string url = "http://data.cabq.gov/airquality/aqindex/daily/";
            string day = Convert.ToString(System.DateTime.Now.Day);
            string year = Convert.ToString(System.DateTime.Now.Year);
            string month = Convert.ToString(System.DateTime.Now.Month);
            string hour = Convert.ToString(System.DateTime.Now.Hour);
            year = year.Remove(0, 2);

            string urlEnd = year + month + day;
            //tx1.Text = hour;
            string result = null;
            bool except = false;
            string furl = null;
            string date = null;
            //gets data with furl as the target url
            for (int i = System.DateTime.Now.Hour; i >= 0; i--)
            {
                if (i < 10)
                {
                    furl = url + urlEnd + "0" + Convert.ToString(i) + ".NM2";
                    date = urlEnd;
                }
                else
                {
                    furl = url + urlEnd + Convert.ToString(i) + ".NM2";
                    date = urlEnd;
                }
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(furl);
                    req.Method = "GET";
                    WebResponse res = await req.GetResponseAsync();
                    StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
                    result = sr.ReadToEnd();
                    sr.Dispose();
                    res.Dispose();
                }
                catch (Exception ex)
                {
                    // handle error
                    except = true;
                }
                if (except == false)
                    break;
            }
            if (except == true)// nothing was recieved, return old file or create one if it doesnt exist
            {
                StorageFolder sfProject = Package.Current.InstalledLocation;
                StorageFolder sfLocal = ApplicationData.Current.LocalFolder;
                Stream sr, sw;

                bool bFileExists;

                try
                {
                    sw = await sfLocal.OpenStreamForWriteAsync(filename, CreationCollisionOption.FailIfExists);
                    sw.Dispose();
                    bFileExists = false;

                }
                catch
                {
                    bFileExists = true;
                }
                if (bFileExists == false)
                {
                    sr = await sfProject.OpenStreamForReadAsync(filename);
                    sw = await sfLocal.OpenStreamForWriteAsync(filename, CreationCollisionOption.ReplaceExisting);
                    await sr.CopyToAsync(sw);
                    sr.Dispose();
                    sw.Dispose();
                }

                sr = await sfLocal.OpenStreamForReadAsync(filename);
                xd = XDocument.Load(sr);
                sr.Dispose();
                return xd;
            }
            string[] param = result.Split('\n');

            int ind = 0;
            int ine = 0;
            List<string[]> data = new List<string[]>();
            while (ind != -1)
            {
                ind = Array.IndexOf(param, "BEGIN_GROUP");
                ine = Array.IndexOf(param, "END_GROUP");
                data.Add(param.Skip(ind).Take(ine - ind + 1).ToArray());
                param = param.Skip(ine + 1).Take(param.Length).ToArray();
            }
            data.RemoveAt(data.Count - 1);
            string[] final = new string[19];
            int offset = 0;

            for (int i = 0; i < data.Count; i++)// goes through all types, ozone, co2,ect..
            {
                string[] tempArray = data.ElementAt(i);
                string type = tempArray[1].Remove(0, 9);
                string[] tempVal = new string[50];
                string[] tempVal2 = new string[50];

                int count = 0;
                double totalSum = 0;
                for (int h = 14; h < tempArray.Length; h = h + 2)//goes through all locations, odd check ignores good/bad location values
                {
                    if (tempArray[h].Equals("END_DATA"))
                        break;

                    tempVal = tempArray[h].Split(',').Skip(2).ToArray();
                    tempVal2 = tempArray[h + 1].Split(',').Skip(2).ToArray();

                    double sum = 0;
                    double cur;
                    int goodCount = 0;
                    for (int f = 0; f < tempVal.Length; f++)
                    {
                        if (tempVal2[f].Trim().Equals("G"))
                        {
                            if (double.TryParse(tempVal[f], out cur))
                            {
                                sum = sum + cur;
                                goodCount++;
                            }
                        }
                    }
                    totalSum = totalSum + (sum / goodCount);
                    count++;


                }
                totalSum = totalSum / count;
                final[i + offset] = type;
                final[i + 1 + offset] = Convert.ToString(totalSum);
                final[i + 2 + offset] = null;
                offset = offset + 2;

            }
            final[18] = date;
            xd = await update(final);
            return xd;
        }
コード例 #2
0
 public async Task OpenFile(Stream stream)
 {
   var rope = new Rope<char>();
   using (var reader = new StreamReader(stream))
   using (var writer = new Editor.RopeWriter(rope))
   {
     await reader.CopyToAsync(writer);
   }
   Document.Replace(0, Document.TextLength, new RopeTextSource(rope));
 }