Exemple #1
0
        static async Task example()
        {
            Console.Title = "国家空气质量获取库的使用演示";

            //创建domain客户端
            EnvCnemcPublishDomainContext publishCtx = new EnvCnemcPublishDomainContext(XAP_URL);

            /*-----获取基础数据 :省,市,检测站,然后才能愉快地玩耍,这些只要获取一次保存下来就不用再次获取 */
            //获取数据涉及到的省份
            IEnumerable <Province> provinces = await publishCtx.Load(publishCtx.GetProvincesQuery()).ResultAsync();

            //获取所有城市,业务上City的ProvinceId属性与省份对应
            IEnumerable <City> cities = await publishCtx.Load(publishCtx.GetCitiesQuery()).ResultAsync();

            //获取所有检测站,业务上通过citycode与城市对应
            IEnumerable <StationConfig> stations = await publishCtx.Load(publishCtx.GetStationConfigsQuery()).ResultAsync();

            /*-----基础数据获取完毕*/

            //预览:获取全国所有城市的实时空气质量,因没加查询条件和数量约束数据量比较大
            IEnumerable <CityAQIPublishLive> citylives = await publishCtx.Load(publishCtx.GetCityAQIPublishLivesQuery()).ResultAsync();

            Console.WriteLine($"获取了{citylives.Count()}个城市的数据,限于篇章问题,黑红榜分别显示前5名");
            Console.WriteLine("{0,10}|{1,5}|{2,8}|{3,14}|{4,12}", "地区", "AQI", "等级", "主要污染物", "更新时间");
            Console.WriteLine("黑榜前3");
            citylives.Where(x => int.TryParse(x.AQI, out var i))
            .OrderByDescending(x => int.Parse(x.AQI))
            .Take(5).ToList()
            .ForEach(showCityLive);
            Console.WriteLine("红榜前3");
            citylives.Where(x => int.TryParse(x.AQI, out var i))
            .OrderBy(x => int.Parse(x.AQI))
            .Take(5).ToList()
            .ForEach(showCityLive);

            Console.Write("\r\n进行下一阶段演示:"); Console.ReadLine(); Console.Clear();


            //以下以浙江省和杭州市为示例
            City     hangzhou = cities.First(x => x.CityName.Contains("杭州"));
            Province zhejiang = provinces.First(x => x.Id.Equals(hangzhou.ProvinceId));

            Console.WriteLine($"获取单个省份{zhejiang.ProvinceName}的所有检测站实时空气状况");
            AQIDataPublishLive[] zhengjiang_stations_air = await publishCtx.GetProvincePublishLives(zhejiang.Id).ResultAsync <AQIDataPublishLive[]>();

            Console.WriteLine($"获取了{zhejiang.ProvinceName}所有检测站数据,限于篇章问题,黑红榜分别显示前5名");
            Console.WriteLine("{0,10}|{1,5}|{2,8}|{3,14}|{4,12}", "检测站", "AQI", "等级", "主要污染物", "更新时间");
            Console.WriteLine("黑榜前3");
            zhengjiang_stations_air.Where(x => int.TryParse(x.AQI, out var i))
            .OrderByDescending(x => int.Parse(x.AQI))
            .Take(5).ToList()
            .ForEach(showStationLive);
            Console.WriteLine("红榜前3");
            zhengjiang_stations_air.Where(x => int.TryParse(x.AQI, out var i))
            .OrderBy(x => int.Parse(x.AQI))
            .Take(5).ToList()
            .ForEach(showStationLive);

            Console.Write("进行下一阶段演示:"); Console.ReadLine(); Console.Clear();

            ////获取所有城市的一天内空气质量状况,这个太暴力,不建议使用,服务器趴了大家都别玩
            //CityAQIModel[] allCityDayAqi = await PublishCtx.GetAllCityDayAQIModels().ResultAsync<CityAQIModel[]>();

            //根据城市code获取所有检测站的空气数据
            Console.WriteLine($"使用API 获取单个城市{hangzhou.CityName}的所有检测站实时空气状况");
            AQIDataPublishLive[] hangzhou_stations1 = await publishCtx.GetAirByCity(hangzhou.CityCode).ResultAsync <AQIDataPublishLive[]>();

            Console.WriteLine("{0,10}|{1,5}|{2,8}|{3,14}|{4,12}", "检测站", "AQI", "等级", "主要污染物", "更新时间");
            hangzhou_stations1.ToList().ForEach(showStationLive);


            Console.Write("\r\n进行下一阶段演示:"); Console.ReadLine(); Console.Clear();


            Console.WriteLine($"获取指定城市{hangzhou.CityName}指定检测站前天的空气数据,并按时间升序显示");
            //创建查询杭州的其中一个检测站
            var station_qiandaohu = stations
                                    .Where(x => hangzhou.CityCode == x.CityCode) //杭州的筛选
                                    .Where(x => x.PositionName == "千岛湖")         //名叫千岛湖
                                    .First();

            DateTime startTime            = DateTime.Today.AddDays(-2);
            DateTime endTime              = DateTime.Today.AddDays(-1);
            var      stationHistory_query = publishCtx.GetIAQIDataPublishHistoriesQuery()
                                            .Where(x => x.StationCode == station_qiandaohu.StationCode)
                                            .Where(x => x.TimePoint >= startTime && x.TimePoint < endTime)
                                            .OrderBy(x => x.TimePoint);
            var station_qiandaohu_history = await publishCtx.Load(stationHistory_query).ResultAsync();

            Console.WriteLine("{0,13}|{1,5}|{2,8}|{3,14}|{4,12}", "检测站", "PM2.5", "PM10", "SO2", "更新时间");
            station_qiandaohu_history.ToList().ForEach(showStationHistory);
            Console.WriteLine("\r\n依赖DomainServer框架的应用查询功能就像使用本地数据库一样,是不是见到了熟悉的Linq to sql,是的使用本库想你所想");
            Console.WriteLine("演示结束");
        }
        public async Task airStationLoadTask()
        {
            try
            {
                load_lb.Visible = true;
                string cityCode = "*";
                string cityName = "全国";
                if (cityComboBox.SelectedItem != null)
                {
                    Area areaCityObj = (Area)(cityComboBox.SelectedItem);
                    cityCode = areaCityObj.AreaCode;
                    cityName = areaCityObj.AreaName;
                }
                if (proviceComboBox.SelectedItem != null && (cityCode == "*" || String.IsNullOrEmpty(cityCode)))
                {
                    Area areaProviceObj = (Area)(proviceComboBox.SelectedItem);
                    cityCode = areaProviceObj.AreaCode.Substring(0, 2);
                    cityName = areaProviceObj.AreaName;
                }

                //创建domain客户端
                EnvCnemcPublishDomainContext publishCtx = new EnvCnemcPublishDomainContext(XAP_URL);

                //获取所有检测站,业务上通过citycode与城市对应
                IEnumerable <StationConfig> stations = await publishCtx.Load(publishCtx.GetStationConfigsQuery()).ResultAsync();

                List <JObject> list = new List <JObject>();
                if (stations != null)
                {
                    for (int i = 0; i < stations.Count(); i++)
                    {
                        StationConfig sc = stations.ElementAt(i);

                        if (cityCode == "*" || sc.CityCode.ToString().StartsWith(cityCode))
                        {
                            JObject jo = new JObject();
                            jo["UniqueCode"]   = sc.UniqueCode;
                            jo["Area"]         = sc.Area;
                            jo["CityCode"]     = sc.CityCode;
                            jo["StationCode"]  = sc.StationCode;
                            jo["Latitude"]     = sc.Latitude;
                            jo["Longitude"]    = sc.Longitude;
                            jo["PositionName"] = sc.PositionName;
                            list.Add(jo);
                        }
                    }
                }
                if (list.Count > 0)
                {
                    if (asr.DeleteAirStationInfo())
                    {
                        _ = Task.Run(() =>
                        {
                            bool result     = asr.AddAirStationInfo(list);
                            load_lb.Visible = false;
                            if (result)
                            {
                                stationQueryBtn_Click(null, null);
                                MessageBox.Show(cityName + list.Count + "个空气质量站点同步成功!", " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show(cityName + "空气质量站点同步失败!", " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        });
                    }
                }
                else
                {
                    load_lb.Visible = false;
                    MessageBox.Show(cityName + "没有空气质量站点要同步", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception e)
            {
                load_lb.Visible = false;
                //日志处理
                Loghelper.WriteErrorLog("同步空气质量站点数据失败", e);
                lr.AddLogInfo(e.ToString(), "同步空气质量站点数据失败", "", "Error");
                //throw e;
            }
        }