private void RegisterDTU(DTUInfo dtu) { string setOnlineSQL = string.Format(@" update DTUBase set [是否使用] ={0}, [是否在线] ={1}, [登录时间] = '{2}', [最后注册时间] ='{3}', [终端IP地址] ='{4}', [终端端口] ={5}, [网关IP地址] ='{6}', [网关端口] ={7} where [终端登录号码] = '{8}'", dtu.IsUsed, dtu.IsActive, dtu.ActiveTime, dtu.LastRegisterTime, dtu.TerminalIP, dtu.TerminalPort, dtu.GatewayIP, dtu.GatewayPort, dtu.ID); int rows = DBUtil.ExecuteNonQuery(setOnlineSQL, out string err); if (!string.IsNullOrEmpty(err)) { TraceManager.AppendErrMsg(dtu.ID + "数据链路同步到数据库失败"); return; } if (rows == 0) { TraceManager.AppendInfo(dtu.ID + "数据库没有该条链路信息"); return; } if (rows > 0) { string sqlQuery = string.Format(@"SELECT TOP 1 [ID] ,[名称] ,[描述] ,[DTU厂家] ,[DTU型号] ,[DTU工作方式] ,[通信协议] FROM DTUBase where 终端登录号码='{0}'; ", dtu.ID); DataTable dt = DBUtil.ExecuteDataTable(sqlQuery, out err); if (!string.IsNullOrEmpty(err)) { TraceManager.AppendErrMsg(dtu.ID + "数据库同步链路的业务信息失败"); return; } DataRow dr = dt.Rows[0];// 只查询一行,在条件里面可以不用担心出错 dtu.DBID = DataUtil.ToInt(dr["ID"]); dtu.Name = DataUtil.ToString(dr["名称"]); dtu.Description = DataUtil.ToString(dr["描述"]); dtu.FactoryName = DataUtil.ToString(dr["DTU厂家"]); dtu.Model = DataUtil.ToString(dr["DTU型号"]); dtu.WorkType = DataUtil.ToString(dr["DTU工作方式"]); dtu.Protocol = DTUInfo.GetProtocol(DataUtil.ToString(dr["通信协议"])); DTUCacheManager.OperDTUCache(DTUCacheManager.OPeratingType.Add, dtu, null); } }
private static void HDAPIRefresh() { uint i, iDtuAmount; string str = ""; StringBuilder mess = new StringBuilder(1000); GPRS_USER_INFO user_info = new GPRS_USER_INFO(); str = str + 0x00 + 0x00 + 0x00; iDtuAmount = HDDTUAPI.get_max_user_amount(); // 取最大数量, 为 3000 for (i = 0; i < iDtuAmount; i++) { HDDTUAPI.get_user_at(i, ref user_info); if (user_info.m_status == 1) // 在线 { // 判断 DTU 最后注册时间与现在时间的差值是否超过设置的超时时间 if ((DateTime.Now - HDDTUAPI.ConvertToDateTime(user_info.m_update_time)) > TimeSpan.FromMinutes(dtuTimeoutNum)) { string dtuID = user_info.m_userid; // 若超时则认为该 DTU 不在线, 调用开发包函数使其下线 HDDTUAPI.do_close_one_user2(dtuID, mess); // 从缓存队列移除 DTUInfo dtuCache = DTUCacheManager.GetDTUInfo(dtuID); if (dtuCache != null) { DTUCacheManager.OperDTUCache(DTUCacheManager.OPeratingType.Delete, dtuID, null);//DTUCacheManager.RemoveDtuCache(dtuID); // 下线状态回填数据库 DBWorker.Append(DBCommand.CreateDtuOffline(dtuID)); } else { TraceManager.AppendWarning("DTU:" + dtuID + "超时未注册,从缓存队列移除未发现该对象!"); } continue; } } } }
private static void Excute(GPRS_DATA_RECORD item) { switch (item.m_data_type) { case 0x01: // 注册包 GPRS_USER_INFO userInfo = new GPRS_USER_INFO(); if (HDDTUAPI.get_user_info(item.m_userid, ref userInfo) == 0) { DTUInfo sourceDTU = DTUInfo.ToDTUInfoForSoc(userInfo); if (sourceDTU == null) { return; } DTUInfo targetDTU = DTUCacheManager.GetDTUInfo(item.m_userid); if (targetDTU == null) { // 首次注册--在DBWorker从数据读取业务信息添加到缓存 DBWorker.Append(DBCommand.CreateDtuRegister(sourceDTU)); } else { // 更新链路 DTUCacheManager.OperDTUCache(DTUCacheManager.OPeratingType.UpdateSocketInfo, targetDTU, sourceDTU); } } break; case 0x02: // 注销包 { DTUCacheManager.OperDTUCache(DTUCacheManager.OPeratingType.Delete, item.m_userid, null); } break; case 0x04: // 无效包 TraceManager.AppendInfo(item.m_userid + "---接收无效的数据包"); break; case 0x05: // DTU已经接收到DSC发送的用户数据包 TraceManager.AppendInfo(item.m_userid + "---已经接收到DSC发送的用户数据包"); break; case 0x09: // 数据包 { //TraceManager.AppendInfo("DTU:" + item.m_userid + "接收了" + // ByteUtil.BytesToText(item.m_data_buf, item.m_data_len) // + "--共计" + item.m_data_len.ToString() + "个字节"); DTUInfo dtu = DTUCacheManager.GetDTUInfo(item.m_userid); if (dtu == null) { return; } dtu.Analysis(item.m_data_buf, item.m_data_len); } break; case 0x0d: TraceManager.AppendInfo(item.m_userid + "---参数设置成功"); break; case 0x0b: TraceManager.AppendInfo(item.m_userid + "---参数查询成功"); // config.readconf(); break; case 0x06: TraceManager.AppendInfo("---断开PPP连接成功"); break; case 0x07: TraceManager.AppendInfo(item.m_userid + "---停止向DSC发送数据"); break; case 0x08: TraceManager.AppendInfo("---允许向DSC发送数据"); break; case 0x0A: TraceManager.AppendInfo("---丢弃用户数据"); break; default: break; } }
private void DtuListUpdate(DTUInfo[] dtuInfos) { if (dtuInfos == null || dtuInfos.Length == 0) { return; } string dtuIDs = "'第一个补个位'"; string setOnlineSQL = ""; foreach (DTUInfo dtu in dtuInfos) { if (dtu.IsActive == 1) { setOnlineSQL += string.Format(@" update DTUBase set [是否使用] ={0}, [是否在线] ={1}, [登录时间] = '{2}', [最后注册时间] ='{3}', [终端IP地址] ='{4}', [终端端口] ={5}, [网关IP地址] ='{6}', [网关端口] ={7} where [终端登录号码] = '{8}'", dtu.IsUsed, dtu.IsActive, dtu.ActiveTime, dtu.LastRegisterTime, dtu.TerminalIP, dtu.TerminalPort, dtu.GatewayIP, dtu.GatewayPort, dtu.ID); dtuIDs += "'" + dtu.ID + "'"; } } string setAllOfflineSQL = string.Format(@"UPDATE DTUBase SET 是否在线 =0;"); // 注意先复位先更新 DBUtil.ExecuteNonQuery(setOnlineSQL, out string errMsg); if (!string.IsNullOrEmpty(errMsg)) { TraceManager.AppendErrMsg("DTU列表更新到数据库失败:" + errMsg); return; } DataTable dt = new DataTable(); string querySQL = @"select * from DTUBase where 终端登录号码 in (" + dtuIDs + ");"; // string querySQL = @"select * from DTUBase;"; dt = DBUtil.ExecuteDataTable(querySQL, out errMsg); if (!string.IsNullOrEmpty(errMsg)) { TraceManager.AppendErrMsg("DTU列表从数据库加载时出错" + errMsg); return; } List <DTUInfo> dbDtus = new List <DTUInfo>(); foreach (DataRow dr in dt.Rows) { DTUInfo dtu = new DTUInfo(); dtu.DBID = DataUtil.ToInt(dr["ID"]); dtu.ID = DataUtil.ToString(dr["终端登录号码"]); dtu.Name = DataUtil.ToString(dr["名称"]); dtu.Description = DataUtil.ToString(dr["描述"]); dtu.FactoryName = DataUtil.ToString(dr["DTU厂家"]); dtu.Model = DataUtil.ToString(dr["DTU型号"]); dtu.WorkType = DataUtil.ToString(dr["DTU工作方式"]); dtu.Protocol = DTUInfo.GetProtocol(DataUtil.ToString(dr["通信协议"])); dtu.IsUsed = DataUtil.ToInt(dr["是否使用"]); dtu.IsActive = DataUtil.ToInt(dr["是否在线"]); dtu.ActiveTime = DataUtil.ToDateString(dr["登录时间"]); dtu.LastRegisterTime = DataUtil.ToDateString(dr["最后注册时间"]); dtu.TerminalIP = DataUtil.ToString(dr["终端IP地址"]); dtu.TerminalPort = DataUtil.ToInt(dr["终端端口"]); dtu.GatewayIP = DataUtil.ToString(dr["网关IP地址"]); dtu.GatewayPort = DataUtil.ToInt(dr["网关端口"]); dbDtus.Add(dtu); } // 同步到内存 if (dbDtus.Count > 0) { DTUCacheManager.OperDTUCache(DTUCacheManager.OPeratingType.UpdateBSInfoMulti, dbDtus.ToArray(), null); } }