/// <summary> /// 同步分钟数据 /// </summary> /// <remarks>存储于分时数据表</remarks> private async Task <bool> SyncminuteData() { #if DEBUG var watch = new Stopwatch(); watch.Start(); #endif try { using (var connection = new SqlConnection(_connName)) { connection.Open(); var sqlda = new SqlDataAdapter(_tableNameSqlCmd, _connName); var ds = new DataSet(); sqlda.Fill(ds, "TableNames"); foreach (DataRow dr in ds.Tables[0].Rows) { var tName = dr[0].ToString(); //var cmd = "select * from " + tName; var tableSchema = SqlHelper.GetTableMember(tName); //获取该表的结构 var dataAdapter = new SqlDataAdapter(tableSchema, connection); var set = new DataSet(); dataAdapter.Fill(set, "Schema"); var schemas = set.Tables[0]; //该表字段的集合 //foreach (DataRow row in schemas.Rows) //{ // var t = row[0].ToString(); //} var tableName = tName.Substring(1); var station = await _weatherStationService.GetByIdAsync(tableName); if (station == null) { //添加监测点信息 // station.Serialnum = tableName; //return false; } else { var weatherDevices = await _weatherDeviceService.GetByStationIdAsny(station.Serialnum); //if (weatherDevices.Count()==0 || !weatherDevices.Any()) //{ CreateWeatherDevice(schemas, tableName.ToString()); //} //if (weatherDevices.Count()== 0) ////Debugger.Break(); //{ } //本地库中最后一行createtime var createTime = DateTime.Now; AreaStationDataInfoDto lastEntitys = null; if (weatherDevices.Any()) { lastEntitys = await _areaStationDataInfoService.GetByIdAsny(weatherDevices.ToList()[0].Serialnum); } if (lastEntitys != null) { createTime = Convert.ToDateTime(lastEntitys.CreateTime); } ////每次取100行数据 //var selectSql = "select top 100 * from " + tName + " where TT > '" + createTime.ToString() + "'"; //SqlDataAdapter adapter = new SqlDataAdapter(selectSql, connection); //DataSet dataSet = new DataSet(); //dataAdapter.Fill(dataSet, "Table"); //if (dataSet != null && dataSet.Tables.Count > 0) //{ // //using (var tran = WeatherDeviceDataInfo.Meta.CreateTrans()) // { // if (dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count > 0) // { // var rows = dataSet.Tables[0].Rows; // foreach (DataRow row in rows) // { // try // { // foreach( DataColumn col in schemas.Columns) // { // //排除列 // if ( _weatherDeviceTypeService.GetByIdAsny(col.ColumnName.ToUpper()) == null) // { // //暂不作任何处理 // //ServiceLogger.Current.WriteDebugLog(col.Name.ToUpper()); // } // else // { // } // } // } // catch (Exception ex) // { // return false; // } // } // } // //tran.Commit(); // } //} } } connection.Close(); } return(true); } catch (Exception ex) { return(false); XTrace.WriteException(ex); } #if DEBUG watch.Stop(); LogHelper.Debug("同步农田小气候仪数据耗时:" + watch.ElapsedMilliseconds + "ms"); #endif }
private async void Collect() { try { var list = await _areaStationService.GetAllAsync(); list.ToList().ForEach(async rws => { StringBuilder sb = new StringBuilder(); WebServiceSoapClient client = new WebServiceSoapClient(); XmlDocument doc = new XmlDocument(); var stationId = rws.StationCode; var xml = client.getWeather(stationId); doc.LoadXml(xml); XmlNodeList nodeList = doc.GetElementsByTagName("ds"); if (nodeList != null) { // int wind = 0; DateTime time = DateTime.Now; if (DateTime.TryParse(nodeList[0].ChildNodes[0].InnerText, out time)) { try { string rain = nodeList[0].ChildNodes[1].InnerText; //wind = int.Parse(nodeList[0].ChildNodes[2].InnerText); ////wind = 359; //int direction = wind / 45; //if (wind % 45 != 0 && direction % 2 == 0) //{ // direction += 1; //} //if (wind == 360) //{ // direction = 0; //} //string windDirection = Enum.GetName(typeof(WindDirectionEightEnum), direction); string windDirection = nodeList[0].ChildNodes[2].InnerText; string windSpeed = nodeList[0].ChildNodes[3].InnerText; string temprature = nodeList[0].ChildNodes[4].InnerText; string humidity = nodeList[0].ChildNodes[5].InnerText; string pressure = nodeList[0].ChildNodes[6].InnerText; rws.Temprature = temprature.IsNullOrWhiteSpace() ? 0M : Convert.ToDecimal(temprature); rws.Humidity = humidity.IsNullOrWhiteSpace() ? 0M : Convert.ToDecimal(humidity); rws.Rainfall = rain.IsNullOrWhiteSpace() ? 0M : Convert.ToDecimal(rain); rws.WindSpeed = windSpeed.IsNullOrWhiteSpace() ? 0M : Convert.ToDecimal(windSpeed); rws.WindDirection = 0;//direction; rws.Atmosphere = pressure.IsNullOrWhiteSpace() ? 0M : Convert.ToDecimal(pressure); rws.UpdateTime = time; rws.Remark = windDirection; await _areaStationService.UpdateAsync(rws); AreaStationDataInfoDto di = new AreaStationDataInfoDto(); di.Serialnum = rws.StationCode + "-" + DateTime.Now.ToString("O"); di.AreaStationSerialnum = rws.Serialnum; di.Temprature = Convert.ToDecimal(temprature); di.Humidity = Convert.ToDecimal(humidity); di.Rainfall = Convert.ToDecimal(rain); di.WindSpeed = Convert.ToDecimal(windSpeed); di.WindDirection = 0;//direction; di.Atmosphere = Convert.ToDecimal(pressure); di.CreateTime = time; di.UpdateTime = DateTime.Now; di.Remark = windDirection; di.Sort = 0; await _areaStationDataInfoService.AddAsny(di); } catch (Exception ex) { XTrace.WriteLine(ex.Message); XTrace.WriteLine("接口返回XML为:" + xml); } } } }); //list.Save(true); } catch (Exception ex) { XTrace.WriteException(ex); } }