/// <summary> /// 获取mcgstest /// </summary> /// <returns></returns> public List <XML_MCGSTest> GetAllMCGSTest() { List <XML_MCGSTest> _listMcgsTest = new List <XML_MCGSTest>(); try { XmlNodeList nodeList = Utility.XmlHelper.GetXmlNodeListByXpath(Path, "/ClientConfig/MCGS/Test"); foreach (XmlNode item in nodeList) { XmlElement xel = (XmlElement)item; XML_MCGSTest mcgs = new XML_MCGSTest(); mcgs.UniqueId = int.Parse(xel.GetAttribute("UniqueId")); mcgs.StationUniqueId = int.Parse(xel.GetAttribute("StationUniqueId")); mcgs.TestId = int.Parse(xel.GetAttribute("TestId")); mcgs.ColumnName = xel.GetAttribute("ColumnName"); mcgs.TestName = xel.GetAttribute("TestName"); _listMcgsTest.Add(mcgs); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(_listMcgsTest); }
//接收到客户端发来的数据,并向客户端返回消息 private void RecieveCallBack(Socket RSocket, string msg) { try { //Socket RSocket = (Socket)ar.AsyncState; bool flag = this.Save(msg); //序列化json 并保存到数据库 S_To_C_Data <object> s_to_c_data = new S_To_C_Data <object>(); s_to_c_data.Flag = HandleFlag.PMQC; s_to_c_data.Success = flag; string json = Utility.JsonHelper.JsonSerializer <S_To_C_Data <object> >(s_to_c_data); RSocket.Send(Encoding.Unicode.GetBytes(json), SocketFlags.None); //保存成功返回1 ,反之则0 //把消息输出到页面上 lb_msg.Items.Add(flag ? "保存成功" : "保存失败"); //同时接收客户端回发的数据,用于回发 //RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket); } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } }
/// <summary> /// 获取所有检测节点 /// </summary> /// <returns></returns> public List <XML_CountryNode> GetAllCountryNode() { List <XML_CountryNode> _listCountryTest = new List <XML_CountryNode>(); try { XmlNodeList nodeList = Utility.XmlHelper.GetXmlNodeListByXpath(Path, "/ClientConfig/CountryTest/Nodes/Node"); foreach (XmlNode item in nodeList) { XmlElement xel = (XmlElement)item; XML_CountryNode node = new XML_CountryNode(); node.NodeId = xel.GetAttribute("NodeId"); node.Remark = xel.GetAttribute("备注"); _listCountryTest.Add(node); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(_listCountryTest); }
/// <summary> /// 返回所有农村项目检测点 /// </summary> /// <returns></returns> public List <XML_CountryTest> GetAllCountryTest() { List <XML_CountryTest> _listCountryTest = new List <XML_CountryTest>(); try { XmlNodeList nodeList = Utility.XmlHelper.GetXmlNodeListByXpath(Path, "/ClientConfig/CountryTest/Test"); foreach (XmlNode item in nodeList) { XmlElement xel = (XmlElement)item; XML_CountryTest countryTest = new XML_CountryTest(); countryTest.UniqueId = int.Parse(xel.GetAttribute("UniqueId")); countryTest.StationUniqueId = int.Parse(xel.GetAttribute("StationUniqueId")); countryTest.NodeId = xel.GetAttribute("NodeId"); countryTest.TestId = int.Parse(xel.GetAttribute("TestId")); countryTest.Multiple = double.Parse(xel.GetAttribute("Multiple")); countryTest.Remark = xel.GetAttribute("备注"); _listCountryTest.Add(countryTest); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(_listCountryTest); }
/// <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> /// 获取所有Test /// </summary> /// <returns></returns> public List <Clazz.Config.XML_Test> GetAllTests() { List <Clazz.Config.XML_Test> ListTest = new List <Clazz.Config.XML_Test>(); try { XmlNodeList nodeList = Utility.XmlHelper.GetXmlNodeListByXpath(PATH, "/Config/Tests/Test"); foreach (XmlNode item in nodeList) { XmlElement xel = (XmlElement)item; Clazz.Config.XML_Test test = new Clazz.Config.XML_Test(); test.StationUnique = int.Parse(xel.GetAttribute("StationUnique")); test.StationId = int.Parse(xel.GetAttribute("StationId")); test.RegisterNo = ushort.Parse(xel.GetAttribute("RegisterNo")); test.TestId = int.Parse(xel.GetAttribute("TestId")); test.Multiple = double.Parse(xel.GetAttribute("Multiple")); test.FunctionCode = Convert.ToInt32(xel.GetAttribute("FunctionCode")); test.ReceiveTimeout = Convert.ToInt32(xel.GetAttribute("ReceiveTimeout")); test.DataType = xel.GetAttribute("DataType"); test.Address = byte.Parse(xel.GetAttribute("Address")); test.DecodeOrder = xel.GetAttribute("DecodeOrder"); test.Min = double.Parse(xel.GetAttribute("Min")); test.Max = double.Parse(xel.GetAttribute("Max")); test.AddNumber = double.Parse(xel.GetAttribute("AddNumber")); ListTest.Add(test); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(ListTest); }
private SqlConnection getConByOrgId(string orgId) { string dbname = ""; try { Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(orgId); dbname = _org.DBName; } catch (Exception ex) { LogMg.AddError("orgId:" + orgId + System.Environment.NewLine + ex.ToString()); DEBUG.ThrowException(ex); } return(Util.ServerSocketHelper.GetConnection(dbname)); //返回分厂数据库连接 }
/// <summary> /// 测试连接并立即使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_test_connection_Click(object sender, EventArgs e) { if (test_connection() == true) { try { save(); } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } MessageBox.Show("连接数据库成功"); } else { MessageBox.Show("连接数据库失败"); } }
//接收到客户端发来的数据,并向客户端返回消息 private void RecieveCallBack(Socket RSocket, string msg) { try { //Socket RSocket = (Socket)ar.AsyncState; bool flag = this.Save(msg, RSocket); S_To_C_Data <object> s_to_c_data = new S_To_C_Data <object>(); s_to_c_data.Flag = HandleFlag.Country; s_to_c_data.Success = flag; string json = Utility.JsonHelper.JsonSerializer <S_To_C_Data <object> >(s_to_c_data); RSocket.Send(Encoding.Unicode.GetBytes(json)); } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } }
public void NW_Init() { try { allOrg = SysConfig.orgConfig.Orgs;; ALLTestList = SysConfig.DBCJconfig.ListTest; } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } NetwaySDK.NW_Init(); globalOperaCallback = DoOnGlobalOperation; realDataCallback = DoOnRealData; alarmDataCallback = DoOnAlarmData; NetwaySDK.NW_SetGlobalOperaCallBack(globalOperaCallback, IntPtr.Zero); //设置全局操作回调 NetwaySDK.NW_SetRealCallBack(realDataCallback, IntPtr.Zero); //设置实时数据回调 NetwaySDK.NW_SetAlarmCallBack(alarmDataCallback, IntPtr.Zero); //设置警报回调 NetwaySDK.NW_StartListen("0.0.0.0", 9000); }
// 接收客户端的连接 private static void RecieveAccept() { while (true) { try { Socket clientSocket = ServerSocket.Accept(); clientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), clientSocket); if (ClientConnectHandler != null) { ClientConnectHandler(); //执行委托 } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } } }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="asyncResult"></param> public static void HandlerData(Socket socket, byte[] content, int conLen, byte type, string tel) { try { if (type == ZG_DTU.HEARTBEAT_DATA) //心跳数据 { HeartbeatDataHandler(socket); } else if (type == ZG_DTU.REGISTER_DATA) //注册数据 貌似这里好像不会出现注册数据了,因为注册数据在第一次注册后就不会再注册了 { DTU_ClientManager.AddClient(tel, socket); } else if (type == ZG_DTU.USER_DATA) { } } catch (Exception ex) { //LogManager.Add("json字符串:" + data + System.Environment.NewLine + ex.ToString()); LogMg.AddError(ex); DEBUG.ThrowException(ex); } }
private List <Clazz.Config.XML_DBCJTest> GetAllTestList() { List <Clazz.Config.XML_DBCJTest> ListTest = new List <XML_DBCJTest>(); try { XmlNodeList listTest = Utility.XmlHelper.GetXmlNodeListByXpath(Path, "/Config/DBCJ/Test"); foreach (XmlNode item in listTest) { XmlElement xel = (XmlElement)item; XML_DBCJTest test = new XML_DBCJTest(); test.OrgId = xel.GetAttribute("OrgId"); test.TestId = xel.GetAttribute("TestId"); test.NodeId = xel.GetAttribute("NodeId"); ListTest.Add(test); } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); } return(ListTest); }
/// <summary> /// 将json字符串转换成对象, 再把对象保存到数据库中 /// </summary> /// <param name="json"></param> /// <returns></returns> private bool Save(string json, Socket socket) { bool isSuccess = true; string stationName = ""; //分厂名称 C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json); Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId); if (_org == null) { //将信息写入到日志文件中 orgid为***的污水厂不存在 LogMg.AddError(string.Format("OrgId:{0}不存在", c_to_s_data.OrgId)); isSuccess = false; } else { try { SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例 //查找站点名称 country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId); //更新站点的IP _station.ip = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString(); if (_station != null) { stationName = _station.name; //站点名称 } //遍历数据 并把数据添加到数据库中 List <realrec> listrealrec = new List <realrec>(); List <testrec> listtestrec = new List <testrec>(); foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data) { test test = SWS.test.SingleOrDefault(c => c.testid == item.TestId); if (test == null) { LogMg.AddError(string.Format("testid为 {0} 的检测点不存在", item.TestId)); //记录日志 isSuccess = false; } else { if (test.means.Trim() == "屏幕取词" || test.means.Trim() == "自动获取") { realrec _realrec = new realrec(); _realrec.testid = item.TestId; _realrec.testtime = item.TestTime; _realrec.value = (decimal)item.Value; listrealrec.Add(_realrec); } if (test.means.Trim() == "检测录入") { testrec _testrec = SWS.testrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime); //判断检测当前数据在数据库中是否已经存在 if (_testrec == null) { _testrec = new testrec(); _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; listtestrec.Add(_testrec); } else { _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; } } } } SWS.realrec.InsertAllOnSubmit(listrealrec); SWS.testrec.InsertAllOnSubmit(listtestrec); SWS.SubmitChanges(); isSuccess = true; } catch (Exception ex) { isSuccess = false; //把错误信息输出到日志文件中 LogMg.AddError(ex.ToString()); DEBUG.ThrowException(ex); } } //lock (lb_msg) //{ // if (lb_msg.Items.Count > 200) // lb_msg.Items.Clear(); // this.lb_msg.Items.Add(string.Format("时间:{0} 客户端:{1} 数据行数{2} 保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败")); // } SaveClientIp(socket, c_to_s_data); return(isSuccess); }
/// <summary> /// 序列化json 并保存到数据库 /// </summary> /// <param name="json"></param> /// <returns></returns> private bool Save(string json) { C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json); lb_msg.Items.Add(string.Format("污水厂编号:{0} 接收数据行数:{1}", c_to_s_data.OrgId, c_to_s_data.Data.Count)); try { SWSDataContext SWS = new SWSDataContext(getConByOrgId(c_to_s_data.OrgId)); //循环从客户端接收到的数据 foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data) { //根据testid找出test记录信息 test _test = SWS.test.SingleOrDefault(c => c.testid == item.TestId); if (_test != null) { //把数据转换成数值型 double top1 = 0, top2 = 0, btm1 = 0, btm2 = 0; try { top1 = Convert.ToDouble(_test.toplimit1); top2 = Convert.ToDouble(_test.toplimit2); btm1 = Convert.ToDouble(_test.btmlimit1); btm2 = Convert.ToDouble(_test.btmlimit2); } catch (Exception ex) { LogMg.AddError(ex); //throw ex; } char status = '1'; //拿检测到的值和 提示上限 提示下限 警报上限 警报下限做比较 if (item.Value >= btm1 && item.Value <= top1) { status = '1'; } else if ((item.Value > top1 && item.Value < top2) || (item.Value > btm2 && item.Value < btm1)) { status = '2'; } else { status = '3'; } //修改test表 SWS.ExecuteCommand(string.Format("update test set testtime='{0}' , [value]={1} , status='{2}' , means='{3}' where testid={4}", item.TestTime, item.Value, status, "屏幕取词", item.TestId)); //插入数据到realrec SWS.ExecuteCommand(string.Format("insert into realrec (testid,testtime,[value],status) values({0},'{1}',{2},'{3}')", item.TestId, item.TestTime, item.Value, status)); } } } catch (Exception ex) { LogMg.AddError(ex); DEBUG.ThrowException(ex); return(false); } return(true); }
//接收到客户端发来的数据,并向客户端返回消息 private static void RecieveCallBack(IAsyncResult ar) { Socket RSocket = null; string json = string.Empty; try { RSocket = (Socket)ar.AsyncState; int REnd = RSocket.EndReceive(ar); json = Encoding.Unicode.GetString(MsgBuffer, 0, REnd); if (string.IsNullOrEmpty(json)) { LogMg.AddError("服务器接收到的数据为空"); return; } C_To_S_Data <object> obj = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <object> >(json); AddClient(obj.OrgId, obj.StationId); //添加或更新客户端信息 if (obj.Flag == HandleFlag.Country) { if (CountryCallBack != null) { CountryCallBack(RSocket, json); } } else if (obj.Flag == HandleFlag.DeviceControl) { if (DeviceControlCallBack != null) { DeviceControlCallBack(RSocket, json); } } else if (obj.Flag == HandleFlag.PMQC) { if (PMQCCallBack != null) { PMQCCallBack(RSocket, json); } } else if (obj.Flag == HandleFlag.MobileDetection) { if (MobileDetectionCallBack != null) { MobileDetectionCallBack(RSocket, json); } } else if (obj.Flag == HandleFlag.ClientPublicIp) { if (ClientPublicIpCallBack != null) { ClientPublicIpCallBack(RSocket, json); } } else if (obj.Flag == HandleFlag.DownLoadConfig) { DownLoadConfig.DownLoad(RSocket, json); } } catch (Exception ex) { LogMg.AddError("json字符串:" + json + System.Environment.NewLine + ex.ToString()); LogMg.AddError(ex.ToString()); DEBUG.ThrowException(ex); } }
private bool Save(string json, Socket RSocket) { bool isSuccess = true; string stationName = ""; //站点名称 C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json); Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId); if (_org == null) { //将信息写入到日志文件中 orgid为***的污水厂不存在 LogMg.AddError("OrgId:\"{0}\"不存在"); isSuccess = false; } else { try { SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例 //查找站点名称 country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId); if (_station != null) { stationName = _station.name; //站点名称 } //遍历数据 并把数据添加到数据库中 List <testrec> list = new List <testrec>(); List <test> listTest = new List <test>(); foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data) { //判断检测当前数据在数据库中是否已经存在 if (SWS.realrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime) == null) { testrec _testrec = new testrec(); _testrec.testid = item.TestId; _testrec.testtime = item.TestTime; _testrec.value = (decimal)item.Value; list.Add(_testrec); } //修改test表 SWS.ExecuteCommand(string.Format("update test set [value]={0} where testid={1}", item.Value, item.TestId)); } SWS.testrec.InsertAllOnSubmit(list); SWS.SubmitChanges(); isSuccess = true; } catch (Exception ex) { isSuccess = false; //把错误信息输出到日志文件中 LogMg.AddError(ex.ToString()); DEBUG.ThrowException(ex); } } lock (lb_msg) { if (lb_msg.Items.Count > 200) { lb_msg.Items.Clear(); } this.lb_msg.Items.Add(string.Format("时间:{0} 客户端:{1} 数据行数{2} 保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败")); } return(isSuccess); }