Exemplo n.º 1
0
 public static DBCommand CreateDtuRegister(DTUInfo info)
 {
     return(new DBCommand()
     {
         Type = DBCommandType.DtuRegister, DTUInfo = info
     });
 }
Exemplo n.º 2
0
 // 添加、更新
 private static void AddOrUpdate(DTUInfo dtuInfo)
 {
     // 需要做事件,放弃使用,尽管好用
     dtusCache.AddOrUpdate(dtuInfo.ID, dtuInfo, (dtuID, exists) =>
     {
         return(dtuInfo);
     });
 }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
 private static void TriggerUpdateSocInfo(DTUInfo dtu)
 {
     if (!eventTriggerFlag)
     {
         return;
     }
     if (evtUpdateSocInfo == null)
     {
         return;
     }
     evtUpdateSocInfo(dtu);
 }
Exemplo n.º 5
0
 private static void TriggerAdd(DTUInfo dtu)
 {
     if (!eventTriggerFlag)
     {
         return;
     }
     if (evtAdd == null)
     {
         return;
     }
     evtAdd(dtu);
 }
Exemplo n.º 6
0
        // 更新
        private static void UpdateSocketInfo(DTUInfo targetDTU, DTUInfo sourceDTU)
        {
            // 更新链路信息
            if (sourceDTU == null)
            {
                return;
            }

            targetDTU.IsUsed           = sourceDTU.IsUsed;
            targetDTU.IsActive         = sourceDTU.IsActive;
            targetDTU.ActiveTime       = sourceDTU.ActiveTime;
            targetDTU.LastRegisterTime = sourceDTU.LastRegisterTime;
            targetDTU.TerminalIP       = sourceDTU.TerminalIP;
            targetDTU.TerminalPort     = sourceDTU.TerminalPort;
            targetDTU.GatewayIP        = sourceDTU.GatewayIP;
            targetDTU.TerminalPort     = sourceDTU.GatewayPort;
        }
Exemplo n.º 7
0
        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;
                    }
                }
            }
        }
Exemplo n.º 8
0
        public static DTUInfo ToDTUInfoForSoc(GPRS_USER_INFO gprsUserInfo)
        {
            try
            {
                DTUInfo dtuInfo = new DTUInfo();
                dtuInfo.ID               = gprsUserInfo.m_userid;
                dtuInfo.IsUsed           = 1;
                dtuInfo.IsActive         = 1;
                dtuInfo.ActiveTime       = DataUtil.ToDateString(gprsUserInfo.m_logon_date);
                dtuInfo.LastRegisterTime = DataUtil.ToDateString(HDDTUAPI.ConvertToDateString(gprsUserInfo.m_update_time));
                dtuInfo.TerminalIP       = HDDTUAPI.inet_ntoa(HDDTUAPI.ntohl(gprsUserInfo.m_sin_addr));
                dtuInfo.TerminalPort     = gprsUserInfo.m_sin_port;
                dtuInfo.GatewayIP        = HDDTUAPI.inet_ntoa(HDDTUAPI.ntohl(gprsUserInfo.m_local_addr));
                dtuInfo.GatewayPort      = gprsUserInfo.m_local_port;

                return(dtuInfo);
            }
            catch
            {
            }
            return(null);
        }
Exemplo n.º 9
0
 private static bool UpdateBSInfo(DTUInfo[] dtus)
 {
     // 更新dtu缓存中的业务信息部分,链路部分不更新
     if (dtus == null)
     {
         return(false);
     }
     foreach (DTUInfo dtu in dtus)
     {
         DTUInfo targetDTU = GetDTUInfo(dtu.ID);
         if (targetDTU == null)
         {
             continue;
         }
         targetDTU.DBID        = dtu.DBID;
         targetDTU.Name        = dtu.Name;
         targetDTU.Description = dtu.Description;
         targetDTU.FactoryName = dtu.FactoryName;
         targetDTU.Model       = dtu.Model;
         targetDTU.WorkType    = dtu.WorkType;
         targetDTU.Protocol    = dtu.Protocol;
     }
     return(true);
 }
Exemplo n.º 10
0
 // 添加
 private static void Add(DTUInfo dtu)
 {
     dtusCache.TryAdd(dtu.ID, dtu);
 }
Exemplo n.º 11
0
        // 增删改调度及事件触发
        public static bool OperDTUCache(OPeratingType type, object target, DTUInfo souceDTU)
        {
            try
            {
                switch (type)
                {
                case OPeratingType.Add:
                {
                    DTUInfo dtu = target as DTUInfo;
                    Add(dtu);
                    TriggerAdd(dtu);
                    TraceManager.AppendInfo(dtu.ID + "上线");
                    return(true);
                }

                case OPeratingType.UpdateSocketInfo:
                {
                    DTUInfo targetDTU = target as DTUInfo;
                    UpdateSocketInfo(targetDTU, souceDTU);
                    TriggerUpdateSocInfo(targetDTU);
                    return(true);
                }

                case OPeratingType.UpdateBSInfoMulti:
                {
                    DTUInfo[] dtus = target as DTUInfo[];
                    if (UpdateBSInfo(dtus))
                    {
                        TriggerUpdateMulti(dtus);
                        return(true);
                    }
                    else
                    {
                        TraceManager.AppendErrMsg("DTU缓存更新业务对象失败");
                        return(false);
                    }
                }

                case OPeratingType.Delete:
                {
                    string dtuID = target as String;
                    if (RemoveDtuCache(dtuID))
                    {
                        TriggerDelOne(dtuID);
                        TraceManager.AppendInfo(dtuID + "下线");
                        return(true);
                    }
                    else
                    {
                        TraceManager.AppendErrMsg("DTU缓存移除对象失败");
                        return(false);
                    }
                }

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                TraceManager.AppendErrMsg("DTU缓存字典操作出错:" + e.Message);
                return(false);
            }
        }
Exemplo n.º 12
0
        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;
            }
        }
Exemplo n.º 13
0
        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);
            }
        }