/// <summary> /// 返回所有的Station /// </summary> /// <returns></returns> private List <Clazz.Config.XML_Station> GetAllStation() { List <Clazz.Config.XML_Station> ListStation = new List <Clazz.Config.XML_Station>(); try { XmlNodeList nodeList = Utility.XmlHelper.GetXmlNodeListByXpath(PATH, "/Config/Stations/Station"); foreach (XmlNode item in nodeList) { XmlElement xel = (XmlElement)item; Clazz.Config.XML_Station station = new Clazz.Config.XML_Station(); station.Unique = int.Parse(xel.GetAttribute("Unique")); //主键 station.StationId = int.Parse(xel.GetAttribute("StationId")); //站点Id station.Name = xel.GetAttribute("Name"); //站点名称 station.OrgId = xel.GetAttribute("OrgId"); station.Tel = xel.GetAttribute("Tel"); //手机号码 station.Protocol = xel.GetAttribute("Protocol"); //手机号码 ListStation.Add(station); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(ListStation); }
/// <summary> /// /// </summary> /// <param name="clientInfo"></param> /// <param name="datetime"></param> public static void Update() { int length = Clients.Count; try { for (int i = 0; i < length; i++) { Clazz.Config.XML_Station station = SysConfig.DTU_StationConfig.GetStationByTel(Clients[i].TelOrGprsId); if (station != null) { Clients[i].Protocol = station.Protocol; Clients[i].StationId = station.StationId; } else { Clients[i].Protocol = ""; Clients[i].StationId = 0; } } } catch (Exception) { } if (ClientChangeHander != null) { ClientChangeHander(); } }
public Clazz.Config.XML_Station GetStationByTel(string tel) { Clazz.Config.XML_Station station = null; station = Stations.SingleOrDefault(c => c.Tel == tel); return(station); }
/// <summary> /// 根据Tel(dtu编号,设备唯一编号)获取数据库连接 /// </summary> /// <returns></returns> private static SqlConnection GetDbConnectionByTel(String tel) { try { //先根据站点id找到orgid ,再根据orgid找出dbname Clazz.Config.XML_Station xml_station = SysConfig.DTU_StationConfig.Stations.SingleOrDefault(c => c.Tel == tel); if (xml_station != null && xml_station.Org != null) { return(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, xml_station.Org.DBName, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword)); } else { return(null); } } catch (Exception) { return(null); } }
/// <summary> /// 根据电话号码找出站点名称 /// </summary> /// <param name="tel"></param> /// <returns></returns> public static string GetStationNameByTel(string tel) { string stationName = string.Empty; Clazz.Config.XML_Station station = SysConfig.DTU_StationConfig.GetStationByTel(tel); if (station != null) { stationName = station.Name; } else { bool ExistStation = false; foreach (XML_Org item in SysConfig.orgConfig.Orgs) { try { SWSDataContext db = new SWSDataContext(ServerSocketHelper.GetConnection(item.DBName)); country_station s = db.country_station.SingleOrDefault(c => c.jiankongyitiji_version == 2 && c.transfer_code == tel); if (s != null) { ExistStation = true; stationName = s.name; } } catch (Exception ex) { LogMg.AddError(ex); } } if (ExistStation == false) { stationName = "未知的客户端"; } } return(stationName); }
private static void ExecuteOrder(object state) { ///第一步:找出需要采集的指标 ///第二步:循环采集的指标,取出一个指标, ///第三步:锁住客户端 ///第四步:采集 ///第五步:释放客户端 ///第六步:回到第二步 ///结束 DTUClientInfo client = (DTUClientInfo)state; Clazz.Config.XML_Station station = SysConfig.DTU_StationConfig.GetStationByTel(client.TelOrGprsId); if (station == null) { return; } List <Clazz.Config.XML_Test> listTest = station.ListTest; if (station != null && listTest != null) { foreach (Clazz.Config.XML_Test test in listTest) { using (MyLock mylock = new MyLock(client, 3000, false)) // 在循环内锁 { if (mylock.IsTimeout == false) { //LogManager.AddDebug("AutoCollectionThread 线程:" + Thread.CurrentThread.ManagedThreadId + " 开始锁了 是否超时" + mylock.IsTimeout); try { ModbusReturn modbusReturn = new ModbusReturn(); int count = 0; //执行错误的次数 while (modbusReturn.success == false && count < SysConfig.userProfile.ExecuteFailureCount) { modbusReturn.clear(); LogMg.AddDebug(string.Format("开始接收 时间:{0},站点:{1} tel:{2},testid:{3}", DateTime.Now.ToString(), client.Name, client.TelOrGprsId.ToString(), test.TestId)); modbus.readdata(client.Protocol, client.socket, client.TelOrGprsId, test.Address, test.RegisterNo, test.FunctionCode, test.DataType, test.DecodeOrder, test.ReceiveTimeout, modbusReturn); count++; } if (modbusReturn.success) //接收数据成功 { DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now); if (Between(modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max)) //如果值不在取值范围内,则不要 { SWSDataContext db = new SWSDataContext(Util.ServerSocketHelper.GetConnection(station.Org.DBName)); SaveToDatabase(db, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber); //保存数据 LogMg.AddDebug(string.Format("接收时间:{0}, tel:{1},value:{2},testid:{3}", DateTime.Now.ToString(), client.TelOrGprsId.ToString(), modbusReturn.value * test.Multiple + test.AddNumber, test.TestId)); // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2},值:{3}", DateTime.Now.ToString(), station.Name, test.TestId, value * test.Multiple + test.AddNumber)); } else { LogMg.AddDebug(string.Format("接收时间:【{0}】,站点:{1},testid:{2},乘以倍率之后的值:{3} 由于值不在范围内[{4},{5}],丢弃", DateTime.Now.ToString(), station.Name, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max)); } } else { //接收数据失败 LogMg.AddDebug(string.Format("接收数据失败")); // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2}, 接收数据失败", DateTime.Now.ToString(), station.Name, test.TestId)); if (modbusReturn.ErrorMsg.Contains("设备未在线")) { return; } } } catch (SocketException) { } catch (Exception ex) { LogMg.AddError(ex); DEBUG.MsgBox(ex.ToString()); } //LogManager.AddDebug("AutoCollectionThread 线程:" + Thread.CurrentThread.ManagedThreadId + " 释放锁了 "); } else { LogMg.AddDebug("AutoCollectionThread 锁失败了"); } } } } }
public bool GenerateFromDbToXML() { List <Clazz.Config.XML_Station> XML_stations = new List <Clazz.Config.XML_Station>(); //从数据库中找出来的站点信息列表 List <Clazz.Config.XML_Test> XML_Tests = new List <Clazz.Config.XML_Test>(); //从数据库中找出来的test信息 SWS_DBDataContext SWS_DB = new SWS_DBDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, SysConfig.userProfile.DbName, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword)); List <SWS_DB.org> db_orgs = SWS_DB.org.Where(c => c.org_type == 2 || c.org_type == 3).ToList(); foreach (SWS_DB.org _org in db_orgs) { SWSDataContext country_db = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, _org.dbname, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword)); try { //List<country_station> db_stations = country_db.country_station.Where(c => c.transfer_code != "" && c.transfer_code != "NULL" && c.Protocol != "" && c.deleteflag == false).ToList(); List <country_station> db_stations = country_db.ExecuteQuery <country_station>("select * from country_station where transfer_code <> '' and transfer_code <> 'NULL' and Protocol <> '' and deleteflag=0").ToList(); List <syscode> syscodes = country_db.syscode.Where(c => c.type == "0060").ToList(); foreach (country_station station in db_stations) { try { //将站点信息添加到列表 Clazz.Config.XML_Station xml_sta = new Clazz.Config.XML_Station(); xml_sta.Unique = XML_stations.Count; xml_sta.StationId = station.id; xml_sta.Name = station.name; xml_sta.OrgId = _org.orgid.ToString(); xml_sta.Tel = station.transfer_code; xml_sta.Protocol = syscodes.Single(c => c.code == station.Protocol).name; XML_stations.Add(xml_sta); //找出站点的检测指标 List <test> tests = country_db.test.Where(c => c.station_id == station.id && c.means.Contains("自动获取") && c.delete_flag == false).ToList(); List <gong_kuang_config> gkcs = country_db.gong_kuang_config.Where(c => c.testid != 0 && c.config_type != "03" && c.config_type != "04").ToList(); foreach (test _test in tests) { try { List <gong_kuang_config> gkc = gkcs.Where(c => c.testid == _test.testid).ToList(); //这个一般只有一行 但配置的时候可能会出现两行, if (gkc.Count > 0) { Clazz.Config.XML_Test xml_t = new Clazz.Config.XML_Test(); xml_t.StationUnique = xml_sta.Unique; xml_t.StationId = xml_sta.StationId; xml_t.RegisterNo = ushort.Parse(gkc[0].read_register); xml_t.TestId = _test.testid; xml_t.Multiple = gkc[0].Multiple == null ? 1 : (double)gkc[0].Multiple; //如果没填,则默认为0 xml_t.FunctionCode = int.Parse(gkc[0].function_code); xml_t.ReceiveTimeout = (int)gkc[0].receive_timeout; xml_t.DataType = gkc[0].data_type; xml_t.Address = byte.Parse(gkc[0].address.ToString()); if (!string.IsNullOrEmpty(gkc[0].decode_order)) { xml_t.DecodeOrder = gkc[0].decode_order; } xml_t.Min = gkc[0].dmin == null ? 0 : (double)gkc[0].dmin; //如果没填,则默认为0 xml_t.Max = gkc[0].dmax == null ? 4294967296 : (double)gkc[0].dmax; //如果没填,则默认为0 xml_t.AddNumber = gkc[0].AddNumber == null ? 0 : (double)gkc[0].AddNumber; //如果没填,则默认为0 xml_t.备注 = _test.name; XML_Tests.Add(xml_t); } } catch (Exception ex) { LogMg.AddError(ex); } } } catch (Exception ex) { LogMg.AddError(ex); } } } catch (Exception ex) { LogMg.AddError(ex); } } SaveConfigToXML(XML_stations, XML_Tests); return(true); }