예제 #1
0
        /// <summary>
        /// 升级过程处理函数
        /// </summary>
        private void UpdateHandler()
        {
            CommStackLog.RecordInf(enLogLayer.eAdapter, "UpdateHandler running");

            SetFwDescReaultArrived += setFwDescSucess;
            SetFwDescFailed        += setFwDescFail;
            SetFwDataReaultArrived += setFwDatReault;

            // 等待WS重新加入网络的忍耐时间,单位是ms
            //int waitWsOnlineTolerance = 600000;
            int updateHandlerSleepTime = 1000;
            int noWsOnlineTime         = 0;

            while (m_bUpdating)
            {
                try
                {
                    lock (lstUpdWs)
                    {
                        // 升级队列中无请求的WS则通知上层升级过程结束
                        if (lstUpdWs.Count <= 0)
                        {
                            dismuteWsUpstream();
                            if (updateFinished != null)
                            {
                                updateFinished();
                            }

                            return;
                        }

                        // 寻找可以升级的WS
                        foreach (tMAC mote in lstUpdWs)
                        {
                            lock (m_dicOnLineWs)
                            {
                                foreach (string onlineWS in m_dicOnLineWs.Keys)
                                {
                                    // 检查已经在线的WS
                                    if (m_dicOnLineWs[onlineWS])
                                    {
                                        tMAC checkWs = new tMAC(onlineWS);
                                        // 当前WS在线,且是上层请求升级中的一员
                                        if (checkWs.isEqual(mote))
                                        {
                                            curUpdatingWs = checkWs;
                                            CommStackLog.RecordInf(enLogLayer.eAdapter, "hit ws");
                                            noWsOnlineTime = 0;
                                            // 寻找到可升级的WS,则结束对在线WS的遍历
                                            break;
                                        }
                                    }
                                }
                            }

                            // 寻找到可升级的WS,则结束对升级队列的遍历
                            if (curUpdatingWs != null)
                            {
                                break;
                            }
                        }
                    }

                    if (curUpdatingWs != null)
                    {
                        CommStackLog.RecordInf(enLogLayer.eAdapter, "WS(" + curUpdatingWs.ToHexString() + ")update!");
                        // 通知上层开始升级新的WS
                        if (updatingWs != null)
                        {
                            updatingWs(curUpdatingWs);
                        }
                        // 下发固件描述信息
                        if (!SendFwDesc(curUpdatingWs))
                        {
                            // 通知上层升级当前WS失败
                            if (updatedWsFailed != null)
                            {
                                updatedWsFailed(curUpdatingWs);
                            }
                            // 升级失败,从请求升级队列中删除当前WS
                            deleteReqUpdatedWs(curUpdatingWs);
                            curUpdatingWs = null;
                            // 升级当前WS失败,则转为升级下一个WS
                            continue;
                        }
                        // 下发固件数据
                        if (!SendFwData(curUpdatingWs))
                        {
                            m_bExitUpdat = false;
                            // 通知上层升级当前WS失败
                            if (updatedWsFailed != null)
                            {
                                updatedWsFailed(curUpdatingWs);
                            }
                            // 升级失败,从请求升级队列中删除当前WS
                            deleteReqUpdatedWs(curUpdatingWs);
                            curUpdatingWs = null;
                            // 升级当前WS失败,则转为升级下一个WS
                            continue;
                        }
                        // 通知上层升级当前WS成功
                        if (updatedWsSucess != null)
                        {
                            updatedWsSucess(curUpdatingWs);
                        }
                        // 升级成功,从请求升级队列中删除当前WS
                        deleteReqUpdatedWs(curUpdatingWs);
                        curUpdatingWs = null;
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "Message:" + ex.Message);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "Source: " + ex.Source);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "StackTrace: " + ex.StackTrace);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "ToString: " + ex.ToString());
                }

                if (noWsOnlineTime >= waitWsOnlineTolerance)
                {
                    lock (lstUpdWs)
                    {
                        // 剩下的WS都是迟迟不能加入网络的WS,通知上层这些WS升级失败
                        foreach (tMAC mote in lstUpdWs)
                        {
                            if (updatedWsFailed != null)
                            {
                                updatedWsFailed(mote);
                            }
                        }

                        // 清空所有未升级完成的WS
                        lstUpdWs.Clear();
                        dismuteWsUpstream();
                        if (updateFinished != null)
                        {
                            updateFinished();
                        }

                        return;
                    }
                }

                Thread.Sleep(updateHandlerSleepTime);
                noWsOnlineTime += updateHandlerSleepTime;
            }
        }
예제 #2
0
        /// <summary>
        /// 系统脉搏定时器超时处理函数
        /// </summary>
        private void heartbeatHandler()
        {
            CommStackLog.RecordInf(enLogLayer.eAdapter, "heartbeatHandler running");

            while (m_bKeepRunning)
            {
                m_msigMainThreadRun.WaitOne();
                m_asigHaultHeartbeat.Reset();
                if (m_bHaultHeartbeat)
                {
                    m_asigHaultHeartbeat.Set();
                    Thread.Sleep(200);
                    continue;
                }

                try
                {
                    if (m_Mesh.IsQueryingAllWs)
                    {
                        tMAC queryIter = new tMAC("0000000000000000");
ITERATOR:
                        m_asigGetMtCfg.Reset();
                        m_bGetMtCfgTimeout = false;
                        if (GetNextMoteConfig(queryIter) != enURErrCode.ERR_NONE)
                        {
                            CommStackLog.RecordInf(enLogLayer.eAdapter, "QueryAllWs unadmissible");
                            m_Mesh.IsQueryingAllWs = false;
                            if (queryAllWsFailed != null)
                            {
                                queryAllWsFailed();
                            }
                            continue;
                        }
                        else if (queryIter.isEqual(new tMAC("0000000000000000")))
                        {
                            CommStackLog.RecordInf(enLogLayer.eAdapter, "QueryAllWs start");
                            lock (m_dicOnLineWs) { m_dicOnLineWs.Clear(); }
                            m_i32MoniterWsIdx = 0;
                        }

                        if (!m_asigGetMtCfg.WaitOne(m_Mesh.AgtReq2MgrRepTimeout))
                        {
                            m_bGetMtCfgTimeout = true;
                        }
                        if (!m_bGetMtCfgTimeout)
                        {
                            if (m_cacheGetMtCfgResult.RC == eRC.RC_OK)
                            {
                                if (!m_cacheGetMtCfgResult.isAP)
                                {
                                    // Kous: 此处决定只要Mote不为Lost状态,则认为为在线
                                    if (m_cacheGetMtCfgResult.u8State != (byte)enMoteState.Lost)
                                    {
                                        lock (m_dicOnLineWs) { m_dicOnLineWs.Add(m_cacheGetMtCfgResult.mac.ToHexString(), true); }
                                    }
                                    else
                                    {
                                        lock (m_dicOnLineWs) { m_dicOnLineWs.Add(m_cacheGetMtCfgResult.mac.ToHexString(), false); }
                                    }
                                }
                                else
                                {
                                    m_macManager = (string)(m_cacheGetMtCfgResult.mac.ToHexString()).Clone();
                                }

                                queryIter.Assign(m_cacheGetMtCfgResult.mac);
                                goto ITERATOR;
                            }
                            else if (m_cacheGetMtCfgResult.RC == eRC.RC_END_OF_LIST)
                            {
                                queryIter.Assign(new tMAC("0000000000000000"));
                                m_Mesh.IsQueryingAllWs = false;
                                lock (m_dicOnLineWs)
                                {
                                    if (QueryAllWsReaultNotify != null)
                                    {
                                        QueryAllWsReaultNotify(m_dicOnLineWs);
                                    }
                                }
                                CommStackLog.RecordInf(enLogLayer.eAdapter, "QueryAllWs end");
                            }
                            else
                            {
                                m_Mesh.IsQueryingAllWs = false;
                                if (queryAllWsFailed != null)
                                {
                                    queryAllWsFailed();
                                }
                            }
                        }
                        else
                        {
                            m_Mesh.IsQueryingAllWs = false;
                            if (queryAllWsFailed != null)
                            {
                                queryAllWsFailed();
                            }
                        }

                        continue;
                    }

                    int iWsCnt1 = 0;
                    lock (m_dicOnLineWs) { iWsCnt1 = m_dicOnLineWs.Count; }
                    if (iWsCnt1 == 0)    // 1 网络中无在线的WS
                    {
                        // 1.1 本次心跳周期内无接收到的报文
                        if (m_Serial.RxFrameCnt <= 0)
                        {
                            if (m_macManager == string.Empty)
                            {
                                Thread.Sleep(200);
                                continue;
                            }
                            // 1.1.0 通过GetMoteConfig(Manager)感知系统脉搏
                            m_asigGetMtCfg.Reset();
                            m_bGetMtCfgTimeout = false;
                            GetMoteConfig(new tMAC(m_macManager));
                            m_asigGetMtCfg.WaitOne();
                            // 1.1.1 GetMoteConfig(Manager)超时
                            if (m_bGetMtCfgTimeout)
                            {
                                if (m_u8HeartbeatRetryCnt++ > HEARTBEET_RETRY_TIMES)
                                {
                                    m_u8HeartbeatRetryCnt = 0;
                                    CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making failed!");
                                    sessnLost();
                                }
                                else
                                {
                                    CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making timeout!");
                                }
                            }
                            // 1.1.2 GetMoteConfig(Manager)正常
                            else
                            {
                                CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making");
                            }
                        }
                        // 1.2 本次心跳周期内有接收到的报文
                        else
                        {
                            CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making(Rx " + m_Serial.RxFrameCnt + ")");
                            // Added in 2016.10.21 by Kous,添加下一条语句
                            m_Serial.RxFrameCnt = 0;
                        }
                    }
                    else   // 2 网络中有在线的WS
                    {
                        // 2.0 通过GetMoteConfig感知系统脉搏
                        m_asigGetMtCfg.Reset();
                        m_bGetMtCfgTimeout = false;
                        lock (m_dicOnLineWs) { GetMoteConfig(new tMAC(m_dicOnLineWs.Keys.ElementAt(m_i32MoniterWsIdx))); }
                        m_asigGetMtCfg.WaitOne();
                        // 2.1 GetMoteConfig超时
                        if (m_bGetMtCfgTimeout)
                        {
                            if (m_u8HeartbeatRetryCnt++ > HEARTBEET_RETRY_TIMES)
                            {
                                m_u8HeartbeatRetryCnt = 0;
                                CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making failed!");
                                sessnLost();
                            }
                            else
                            {
                                CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making timeout!");
                            }
                        }
                        // 2.2 GetMoteConfig正常
                        else
                        {
                            CommStackLog.RecordInf(enLogLayer.eAdapter, "Pace-making");
                            m_u8HeartbeatRetryCnt = 0;
                            int iWsCnt2 = 0;
                            lock (m_dicOnLineWs) { iWsCnt2 = m_dicOnLineWs.Count; }
                            // 轮询完毕,从头开始
                            if (++m_i32MoniterWsIdx >= iWsCnt2)
                            {
                                m_i32MoniterWsIdx = 0;
                            }
                        }
                    }

                    int residueSleepTime = cfgHeartbeatInterval;
                    while (residueSleepTime > 200)
                    {
                        Thread.Sleep(200);
                        if (m_Mesh.IsQueryingAllWs)
                        {
                            break;
                        }
                        residueSleepTime = residueSleepTime - 200;
                    }

                    if (m_Mesh.IsQueryingAllWs)
                    {
                        continue;
                    }

                    if (residueSleepTime > 0)
                    {
                        Thread.Sleep(residueSleepTime);
                    }

                    continue;
                }
                catch (Exception ex)
                {
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "Message:" + ex.Message);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "Source: " + ex.Source);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "StackTrace: " + ex.StackTrace);
                    CommStackLog.RecordErr(enLogLayer.eAdapter, "ToString: " + ex.ToString());
                }
            }
        }