Cgw.Channel.MonitorChannelRelationManager.SyncChannelInfo C# (CSharp) Method

SyncChannelInfo() public method

同步通道信息 将输入的通道信息更新到本地,本地存在但输入中没有的通道需要删除,本地没有而输入中有的通道需要添加 本地存在并且输入中也有的通道,也得判断会议号是否变化,有变化还得更新码流,删除/添加软终端呼叫通道等
public SyncChannelInfo ( List &channelInfoList, int maxChannelNum ) : SmcErr
channelInfoList List
maxChannelNum int
return Cgw.SmcError.SmcErr
        public SmcErr SyncChannelInfo(ref List<ChannelInfo> channelInfoList, int maxChannelNum)
        {
            SmcErr err = new CgwError();
            Dictionary<string, MonitorChannelRelation> temp = null;   // 本地通道号码列表跟输入通道号码列表比较,本地通道号码列表需要删除部分
            List<ChannelInfo> addChannelInfoList = new List<ChannelInfo>();  // 本地通道号码列表跟输入通道号码列表比较,本地通道号码列表需要另外添加部分
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Info("SyncChannelInfo start.");

            if (null == channelInfoList || 0 == channelInfoList.Count || maxChannelNum < 0)
            {
                // 错误码 日志
                logEx.Error("SyncChannelInfo param error");
                err.SetErrorNo(CgwError.ERR_CGW_CHANNEL_INPUT_ERROR);
                return err;
            }

            // 根据license中最大可用通道数量,修改输入通道列表中各通道是否有效
            this.ChangeLicense(ref channelInfoList, maxChannelNum);

            try
            {
                bool successed = this.monitorChannelRelationDicLocker.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME);
                // 申请互斥
                if (successed)
                {
                    try
                    {
                        // 区分需要删除的通道和需要添加的通道
                        temp = new Dictionary<string, MonitorChannelRelation>(this.monitorChannelRelationDic);
                        foreach (ChannelInfo item in channelInfoList)
                        {
                            if (this.monitorChannelRelationDic.ContainsKey(item.ChannelLabel))
                            {
                                // 同步通道会议信息
                                err = this.SyncChannelConfAccessCode(item.ChannelLabel, item.AccessCode, ChannelControlType.None);
                                if (!err.IsSuccess())
                                {
                                    logEx.Trace("SyncChannelInfo modify channelLabel:{0} failed, errNo={1}", item.ChannelLabel, err.ErrNo);
                                    return err;
                                }

                                // 修改license是否有效
                                if (null != this.monitorChannelRelationDic[item.ChannelLabel])
                                {
                                    this.monitorChannelRelationDic[item.ChannelLabel].IsValid = item.IsValid;
                                }

                                temp.Remove(item.ChannelLabel);
                            }
                            else
                            {
                                addChannelInfoList.Add(item);
                            }
                        }

                        // 删除通道
                        foreach (KeyValuePair<string, MonitorChannelRelation> item in temp)
                        {
                            // 设置会议号为空,同时删除通道关系字典中通道
                            err = this.SyncChannelConfAccessCode(item.Key, string.Empty, ChannelControlType.Remove);
                            if (!err.IsSuccess())
                            {
                                logEx.Error("SyncChannelInfo del channelLabel:{0} failed, errNo={1}", item.Key, err.ErrNo);
                                return err;
                            }
                        }

                        // 添加通道
                        foreach (ChannelInfo item in addChannelInfoList)
                        {
                            if (this.monitorChannelRelationDic.ContainsKey(item.ChannelLabel))
                            {
                                continue;
                            }

                            // 添加通道,同时设置通道的会议号
                            err = this.SyncChannelConfAccessCode(item.ChannelLabel, item.AccessCode, ChannelControlType.Add);
                            if (!err.IsSuccess())
                            {
                                logEx.Error("SyncChannelInfo add channelLabel:{0} failed, errNo={1}", item.ChannelLabel, err.ErrNo);
                                return err;
                            }

                            // 修改license是否有效
                            if (null != this.monitorChannelRelationDic[item.ChannelLabel])
                            {
                                this.monitorChannelRelationDic[item.ChannelLabel].IsValid = item.IsValid;
                            }
                        }

                        // 日志
                        string channellLog = "Log SyncChannelInfo result:";
                        foreach (KeyValuePair<string, MonitorChannelRelation> pair in this.monitorChannelRelationDic)
                        {
                            string name = string.Format("  ChannelLabel:{0},confAccessCode:{1}, cameraNo:{2}, isValid:{3};", pair.Value.Label, pair.Value.ConfAccessCode, pair.Value.CameraNo, pair.Value.IsValid);
                            channellLog += name;
                        }
                        logEx.Trace(channellLog);

                    }
                    finally
                    {
                        // 释放互斥量
                        this.monitorChannelRelationDicLocker.ExitWriteLock();
                    }
                }
                else
                {
                    // 日志
                    logEx.Error("SyncChannelInfo: Enert Write Lock Failed.WaitingReadCount:{0};WaitingWriteCount:{1}.", this.monitorChannelRelationDicLocker.WaitingReadCount, this.monitorChannelRelationDicLocker.WaitingWriteCount);
                    err.SetErrorNo(CgwError.ERR_CGW_CHANNEL_GET_LOCK_FAIL);
                }
            }
            catch (Exception ex)
            {
                // 日志
                logEx.Error(ex, "SyncChannelInfo: Enert Write Lock Exception.");
                err.SetErrorNo(CgwError.ERR_CGW_CHANNEL_SERVICE_ADNORMAL);
            }

            // 成功日志
            logEx.Info("SyncChannelInfo successfully");

            return err;
        }