using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;
using System.Reflection;
using DPSDK_RES = System.Int32;

namespace DPSDK_CSharpDemo
{
    public partial class CSharpDemo : Form
    {
        public string TakeDeviceID(string strChannel)
        {
            string res = "";
            int pos = strChannel.IndexOf("$");
            if (pos <= 0)
            {
                return strChannel;
            }
            res = strChannel.Substring(0, pos);
            return res;
        }

        public string GetChannelName(string strChannelID)
        {
            if (m_mapIdName.Count == 0 || !m_mapIdName.ContainsKey(strChannelID))
            {
                return "";
            }

            return m_mapIdName[strChannelID];
        }

        private void btnDeviceQueryOrg_Click(object sender, EventArgs e)
        {
            ClearOrgAndDevData();

            int iChannelTypeCount = 0;
            uint uiQueryLen = (uint)(Marshal.SizeOf(typeof(DPSDK_QUERY_ORG_INFO)) - Marshal.SizeOf(typeof(IntPtr)) + iChannelTypeCount * Marshal.SizeOf(typeof(Int32)));
            IntPtr pQueryOrgInfo = Marshal.AllocHGlobal((int)uiQueryLen);

            DPSDK_QUERY_ORG_INFO struQueryOrgInfo = new DPSDK_QUERY_ORG_INFO();
            struQueryOrgInfo.szOrgCode = textDeviceOrgCode.Text;
            Marshal.StructureToPtr(struQueryOrgInfo, pQueryOrgInfo, true);

            IntPtr pChannelTypeList = Marshal.AllocHGlobal(iChannelTypeCount * Marshal.SizeOf(typeof(Int32)));

            Int32[] arrChannelTypeList = new Int32[iChannelTypeCount];
            for (int i = 0; i < iChannelTypeCount; i++)
            {
                arrChannelTypeList[i] = i + 1;
            }

            Marshal.Copy(arrChannelTypeList, 0, new IntPtr(pQueryOrgInfo.ToInt32() + Marshal.SizeOf(typeof(DPSDK_QUERY_ORG_INFO)) - Marshal.SizeOf(typeof(IntPtr))), iChannelTypeCount);

            DPSDK_RES iRet = DPSDK_GetOrganization(m_iSessionID, pQueryOrgInfo, uiQueryLen, fDataCallback, IntPtr.Zero);
            if (iRet == DPSDK_SUCCESS)
            {
                HandleGetAllDeviceInfo(true);

                string strOrgXml = "";
                PacketOrgXml(ref m_struDepInfoAll, ref strOrgXml);
                textDeviceOrgData.Text = strOrgXml;
            }
            else
            {
                AppData.ShowMsgInfo(AppData._CS("Get Organization failed. error code = %1", iRet.ToString()));
            }
            Marshal.FreeHGlobal(pQueryOrgInfo);
        }

        private void btnDeviceQueryByLayered_Click(object sender, EventArgs e)
        {
            //if (!LocalConfig.IsVSL())
            {
                btnDeviceQueryOrg_Click(sender, e);
                return;
            }
            ClearOrgAndDevData();

            string strOrgCode = textDeviceOrgCode.Text;
            DPSDK_RES iRet = HandleGeDeviceTreeByLayered(strOrgCode, "1");
            if (iRet == DPSDK_SUCCESS)
            {
                HandleGetAllDeviceInfo(false);

                string strOrgXml = "";
                PacketOrgXml(ref m_struDepInfoAll, ref strOrgXml);
                textDeviceOrgData.Text = strOrgXml;
            }
            else
            {
                AppData.ShowMsgInfo(AppData._CS("Get Device ByLayered failed. error code = %1", iRet.ToString()));
            }
        }

        private void checkBoxDeviceCompress_CheckedChanged(object sender, EventArgs e)
        {
            IntPtr iCompressType = (IntPtr)(checkBoxDeviceCompress.CheckState == CheckState.Checked ? DPSDK_COMPRESS_TYPE.COMPRESS_DEFAULT : DPSDK_COMPRESS_TYPE.COMPRESS_DISABLE);
            DPSDK_RES iRet = DPSDK_SetCompressType(iCompressType);
	        if (iRet == DPSDK_SUCCESS)
            {
                AppData.ShowMsgInfo("SetCompressType success.");
	        }
	        else
	        {
                AppData.ShowMsgInfo(AppData._CS("SetCompressType Failed. error code = %1", iRet.ToString()));
	        }
        }

        private void btnDeviceGetUserData_Click(object sender, EventArgs e)
        {
            textDeviceUserDataInfo.Text = "";
            IntPtr pUserData = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetUserData(m_iSessionID, textDeviceUserDataName.Text, ref pUserData);
            if (iRet == DPSDK_SUCCESS)
            {
                if (pUserData != IntPtr.Zero)
                {
                    byte[] strUserData = new byte[10240];
                    Marshal.Copy(pUserData, strUserData, 0, 10240);
                    textDeviceUserDataInfo.Text = Encoding.GetEncoding("utf-8").GetString(strUserData);
                    DPSDK_ReleaseDataBuffer(pUserData);
                }  
                AppData.ShowMsgInfo("Get User Data success.");
            }
            else
            {
                AppData.ShowMsgInfo(AppData._CS("Get User Data Failed. error code = %1", iRet.ToString()));
            }
        }

        private void btnDeviceSaveUserData_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_SaveUserData(m_iSessionID, textDeviceUserDataName.Text, textDeviceUserDataInfo.Text, (uint)textDeviceUserDataInfo.Text.Length);
            if (iRet == DPSDK_SUCCESS)
            {
                AppData.ShowMsgInfo("Save User Data success.");
            }
            else
            {
                AppData.ShowMsgInfo(AppData._CS("Save User Data Failed. error code = %1", iRet.ToString()));
            }
        }

        private void btnDeviceDelUserData_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_DelUserData(m_iSessionID, textDeviceUserDataName.Text);
            if (iRet == DPSDK_SUCCESS)
            {
                textDeviceUserDataName.Text = "";
                textDeviceUserDataInfo.Text = "";
                AppData.ShowMsgInfo("Del User Data success.");
            }
            else
            {
                AppData.ShowMsgInfo(AppData._CS("Del User Data Failed. error code = %1", iRet.ToString()));
            }
        }

        private void radioButtonDeviceOrg_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDeviceOrg.Checked)
            {
                DisplayOrgTree();
            }
        }

        private void radioButtonDeviceDevice_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDeviceDevice.Checked)
            {
                DisplayDeviceTree();
            }
        }

        private void radioButtonDeviceChannel_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDeviceChannel.Checked)
            {
                DisplayChannelTree();
            }
        }

        private void ClearOrgAndDevData()
        {
            m_struDevInfoAll.vecDevInfo.Clear();

            m_struDepInfoAll.vecChnlID.Clear();
            m_struDepInfoAll.vecDevID.Clear();
            m_struDepInfoAll.vecSubDepInfo.Clear();
            m_struDepInfoAll.depInfo.strCoding = "";
            m_struDepInfoAll.depInfo.strDepName = "";
            m_struDepInfoAll.depInfo.strSN = "";
            m_struDepInfoAll.depInfo.nDepSort = 0;
            m_struDepInfoAll.depInfo.nDepType = 0;
            m_struDepInfoAll.depInfo.isParent = 0;

            m_mapIdName.Clear();
            treeViewDeviceOrg.Nodes.Clear();
        }

        private DPSDK_RES HandleGeDeviceTreeByLayered([MarshalAs(UnmanagedType.LPStr)] string pParentId, [MarshalAs(UnmanagedType.LPStr)] string pNodeType)
        {
            uint uiPageNum = 0;
            uint uiPageSize = 100;
            DPSDK_PAGE_INFO pPageInfo = new DPSDK_PAGE_INFO();
            pPageInfo.uiPage = uiPageNum;
            pPageInfo.uiPageSize = uiPageSize;

            IntPtr uiTotal = IntPtr.Zero;
            DPSDK_GET_DEVICE_LAYERED_PARAM pParam = new DPSDK_GET_DEVICE_LAYERED_PARAM();
            //pParam.iOrgType = 1;
            pParam.iShowDev = checkBoxDeviceContainDevice.CheckState == CheckState.Checked ? 1 : 0;
            pParam.iDeep = 2 + pParam.iShowDev;
            pParam.szNodeType = pNodeType;
            pParam.szID = pParentId;
            Dep_Info_All? depChild = FinOrgInfo(ref m_struDepInfoAll, pParentId);
            if (depChild == null)
            {
                depChild = m_struDepInfoAll;
            }

            DPSDK_RES iRet = DPSDK_SUCCESS;
            do
            {
                uiPageNum++;
                pPageInfo.uiPage = uiPageNum;
                iRet = DPSDK_GetDeviceByLayered(m_iSessionID, ref pParam, ref pPageInfo, ref uiTotal, fDataCallback, Marshal.StringToHGlobalAnsi(pParentId));
            } while ((uiPageNum * uiPageSize < (uint)uiTotal.ToInt32()) && iRet == DPSDK_SUCCESS);

            if (iRet == DPSDK_SUCCESS)
            {
                if (pParentId.Length == 0)
                {
                    iRet = HandleGeDeviceTreeByLayered(depChild.Value.depInfo.strCoding, pNodeType);
                }
                else
                {
                    for (int i = 0; i < depChild.Value.vecSubDepInfo.Count; i++)
                    {
                        if (((Dep_Info_All)depChild.Value.vecSubDepInfo[i]).depInfo.isParent == 1)
                        {
                            iRet = HandleGeDeviceTreeByLayered(((Dep_Info_All)depChild.Value.vecSubDepInfo[i]).depInfo.strCoding, pNodeType);
                        }
                    }
                }
            }
            return iRet;
        }

        private void fDataCallback(IntPtr iDataType, IntPtr pDataBuf, IntPtr uiBufSize, IntPtr pUserData)
        {
            if (iDataType.ToInt32() == DPSDK_DATA_ORG_INFO)
            {
                DPSDK_ORG_INFO pOrgInfo = (DPSDK_ORG_INFO)Marshal.PtrToStructure(pDataBuf, typeof(DPSDK_ORG_INFO));
                CopyOrgData(ref pOrgInfo, ref m_struDepInfoAll);
            }
            else if (iDataType.ToInt32() == DPSDK_DATA_ALL_ORG_INFO)
            {

            }
            else if (iDataType.ToInt32() == DPSDK_DATA_DEVICE_INFO)
            {
                DPSDK_DEV_ALL_INFO_LIST pDevAllInfoList = (DPSDK_DEV_ALL_INFO_LIST)Marshal.PtrToStructure(pDataBuf, typeof(DPSDK_DEV_ALL_INFO_LIST));
                CopyDevData(ref pDevAllInfoList, ref m_struDevInfoAll);
            }
            else if (iDataType.ToInt32() == DPSDK_DATA_DEVICE_LAYERED)
            {
                DPSDK_LAYERED_RESULT_LIST pSrcInfo = (DPSDK_LAYERED_RESULT_LIST)Marshal.PtrToStructure(pDataBuf, typeof(DPSDK_LAYERED_RESULT_LIST));
                string pParentId = Marshal.PtrToStringAnsi(pUserData);
                FinOrgAndCopyDevTree(pParentId, ref pSrcInfo, ref m_struDepInfoAll);
            }
        }            

        private Dep_Info_All? FinOrgInfo(ref Dep_Info_All depCur, [MarshalAs(UnmanagedType.LPStr)] string pParentId)
        {
            if (depCur.depInfo.strCoding == pParentId)
            {
                return depCur;
            }

            for (int i = 0; i < depCur.vecSubDepInfo.Count; i++)
            {
                Dep_Info_All dep = (Dep_Info_All)depCur.vecSubDepInfo[i];
                if (dep.depInfo.strCoding == pParentId)
                {
                    return dep;
                }

                Dep_Info_All? depChild = FinOrgInfo(ref dep, pParentId);
                if (depChild != null)
                {
                    return depChild;
                }
            }
            return null;
        }

        private void CopyOrgData(ref DPSDK_ORG_INFO pOrgInfo, ref Dep_Info_All struDep)
        {
            struDep.vecChnlID.Clear();
            struDep.vecDevID.Clear();
            struDep.vecSubDepInfo.Clear();
            struDep.depInfo.strCoding = pOrgInfo.struOrgBaseInfo.szOrgCode;
            struDep.depInfo.strDepName = pOrgInfo.struOrgBaseInfo.szOrgName;
            struDep.depInfo.strSN = pOrgInfo.struOrgBaseInfo.szOrgSN;
            struDep.depInfo.nDepSort = pOrgInfo.struOrgBaseInfo.iOrgSort;
            struDep.depInfo.nDepType = pOrgInfo.struOrgBaseInfo.iOrgType;

            if (pOrgInfo.iDevNum > 0)
            {
                DPSDK_ORG_SUB_DEV_INFO[] pDevList = new DPSDK_ORG_SUB_DEV_INFO[pOrgInfo.iDevNum];
                //pDevList[0] = (DPSDK_ORG_SUB_DEV_INFO)Marshal.PtrToStructure(pOrgInfo.pDevList, typeof(DPSDK_ORG_SUB_DEV_INFO));
                for (int i = 0; i < pOrgInfo.iDevNum; i++)
                {
                    pDevList[i] = (DPSDK_ORG_SUB_DEV_INFO)Marshal.PtrToStructure(new IntPtr(pOrgInfo.pDevList.ToInt32() + i * Marshal.SizeOf(typeof(DPSDK_ORG_SUB_DEV_INFO))), typeof(DPSDK_ORG_SUB_DEV_INFO));
                    struDep.vecDevID.Add(pDevList[i].szDeviceId);
                }
            }

            if (pOrgInfo.iChannelNum > 0)
            {
                DPSDK_ORG_SUB_CHANNEL_INFO[] pChannelList = new DPSDK_ORG_SUB_CHANNEL_INFO[pOrgInfo.iChannelNum];
                //pChannelList[0] = (DPSDK_ORG_SUB_CHANNEL_INFO)Marshal.PtrToStructure(pOrgInfo.pChannelList, typeof(DPSDK_ORG_SUB_CHANNEL_INFO));
                for (int i = 0; i < pOrgInfo.iChannelNum; i++)
                {
                    pChannelList[i] = (DPSDK_ORG_SUB_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pOrgInfo.pChannelList.ToInt32() + i * Marshal.SizeOf(typeof(DPSDK_ORG_SUB_CHANNEL_INFO))), typeof(DPSDK_ORG_SUB_CHANNEL_INFO));
                    struDep.vecChnlID.Add(pChannelList[i].szChannelId);
                }
            }

            if (pOrgInfo.iOrgNum > 0)
            {
                DPSDK_ORG_INFO[] pOrgList = new DPSDK_ORG_INFO[pOrgInfo.iOrgNum];
                //pOrgList[0] = (DPSDK_ORG_INFO)Marshal.PtrToStructure(pOrgInfo.pOrgList, typeof(DPSDK_ORG_INFO));
                for (int i = 0; i < pOrgInfo.iOrgNum; i++)
                {
                    pOrgList[i] = (DPSDK_ORG_INFO)Marshal.PtrToStructure(new IntPtr(pOrgInfo.pOrgList.ToInt32() + i * Marshal.SizeOf(typeof(DPSDK_ORG_INFO))), typeof(DPSDK_ORG_INFO));
                    Dep_Info_All struSubDep = new Dep_Info_All();
                    struSubDep.vecSubDepInfo = new ArrayList();
                    struSubDep.vecDevID = new ArrayList();
                    struSubDep.vecChnlID = new ArrayList();
                    CopyOrgData(ref pOrgList[i], ref struSubDep);
                    struDep.vecSubDepInfo.Add(struSubDep);
                }
            }
           
        }

        private int FinOrgAndCopyDevTree([MarshalAs(UnmanagedType.LPStr)] string pParentId, ref DPSDK_LAYERED_RESULT_LIST pSrcInfo, ref Dep_Info_All pDepInfo)
        {
            if (pDepInfo.depInfo.strCoding == pParentId)
            {
                CopyDevTreeByLayeredData(ref pSrcInfo, ref pDepInfo);
                return DPSDK_SUCCESS;
            }

            for (int i = 0; i < pDepInfo.vecSubDepInfo.Count; i++)
            {
                Dep_Info_All dep = (Dep_Info_All)pDepInfo.vecSubDepInfo[i];
                if (dep.depInfo.strCoding == pParentId)
                {
                    CopyDevTreeByLayeredData(ref pSrcInfo, ref dep);
                    pDepInfo.vecSubDepInfo[i] = dep;
                    return DPSDK_SUCCESS;
                }

                int iRet = FinOrgAndCopyDevTree(pParentId, ref pSrcInfo, ref dep);
                if (iRet == DPSDK_SUCCESS)
                {
                    pDepInfo.vecSubDepInfo[i] = dep;
                    return iRet;
                }
            }
            return -1;
        }

        private void CopyDevTreeByLayeredData(ref DPSDK_LAYERED_RESULT_LIST pSrcInfo, ref Dep_Info_All pDepInfo)
        {
            if (pSrcInfo.iResultNum > 0)
            {
                DPSDK_LAYERED_RESULT[] pResultList = new DPSDK_LAYERED_RESULT[pSrcInfo.iResultNum];
                pResultList[0] = (DPSDK_LAYERED_RESULT)Marshal.PtrToStructure(pSrcInfo.pResultList, typeof(DPSDK_LAYERED_RESULT));
                if (pResultList[0].szParentID.Length == 0 && pResultList[0].iNodeType == 1)
                {
                    pDepInfo.depInfo.strCoding = pResultList[0].szID;
                    pDepInfo.depInfo.strDepName = pResultList[0].szName;
                    pDepInfo.depInfo.isParent = pResultList[0].isParent;
                    pDepInfo.depInfo.nDepSort = pResultList[0].iSort;
                }
                else
                {
                    for (int i = 0; i < pSrcInfo.iResultNum; i++)
                    {
                        pResultList[i] = (DPSDK_LAYERED_RESULT)Marshal.PtrToStructure(new IntPtr(pSrcInfo.pResultList.ToInt32() + i * Marshal.SizeOf(typeof(DPSDK_LAYERED_RESULT))), typeof(DPSDK_LAYERED_RESULT));
                        if (pResultList[i].iNodeType == 1)
                        {
                            Dep_Info_All struDep = new Dep_Info_All();
                            struDep.vecSubDepInfo = new ArrayList();
                            struDep.vecDevID = new ArrayList();
                            struDep.vecChnlID = new ArrayList();
                            struDep.depInfo.strCoding = pResultList[i].szID;
                            struDep.depInfo.strDepName = pResultList[i].szName;
                            struDep.depInfo.nDepSort = pResultList[i].iSort;
                            struDep.depInfo.isParent = pResultList[i].isParent;
                            pDepInfo.vecSubDepInfo.Add(struDep);
                        }
                        else if (pResultList[i].iNodeType == 2)
                        {
                            pDepInfo.vecDevID.Add(pResultList[i].szID);
                        }
                        else if (pResultList[i].iNodeType == 3)
                        {
                            pDepInfo.vecChnlID.Add(pResultList[i].szID);
                        }
                    }
                }
            }
        }

        private void HandleGetAllDeviceInfo(bool bOnce)
        {
            m_struDevInfoAll.vecDevInfo.Clear();

            if (bOnce)
	        {
		        /*DPSDK_QUERY_DEV_INFO struInfo;
		        memset(&struInfo, 0, sizeof(struInfo));
		        DEMO_STRCPY(struInfo.szOrgCode, m_struDepInfoAll.depInfo.strCoding.c_str());*/
		        int iRet = DPSDK_GetDevice(m_iSessionID, IntPtr.Zero, 0, fDataCallback, IntPtr.Zero);
		        if (iRet == DPSDK_SUCCESS)
		        {
		        }
		        else
		        {
			        AppData.ShowMsgInfo(AppData._CS("Get Device Failed. error code = %1", iRet.ToString()));
		        }
	        }
	        else
            {
                HandleGetDeviceInfoByOrg(ref m_struDepInfoAll, true);
            }

            //SortDeviceByOrg();
            MapIdAndName();
            DisplayTree();
        }

        private void HandleGetDeviceInfoByOrg(ref Dep_Info_All depCur, bool bSubChild)
        {
            if (depCur.vecDevID.Count > 0)
            {
                uint uiQueryLen = (uint)(Marshal.SizeOf(typeof(DPSDK_QUERY_DEV_INFO)) - Marshal.SizeOf(typeof(IntPtr)) + depCur.vecDevID.Count * Marshal.SizeOf(typeof(DPSDK_DEVICE_ID)));
                IntPtr pQueryDevInfo = Marshal.AllocHGlobal((int)uiQueryLen);
                DPSDK_QUERY_DEV_INFO struQueryDevInfo = new DPSDK_QUERY_DEV_INFO();
                struQueryDevInfo.szOrgCode = depCur.depInfo.strCoding;
                Marshal.StructureToPtr(struQueryDevInfo, pQueryDevInfo, true);
                for (int i = 0; i < depCur.vecDevID.Count; i++)
                {
                    DPSDK_DEVICE_ID struDeviceID = new DPSDK_DEVICE_ID();
                    struDeviceID.szId = (string)depCur.vecDevID[i];
                    IntPtr ptr = new IntPtr(pQueryDevInfo.ToInt32() + Marshal.SizeOf(typeof(DPSDK_QUERY_DEV_INFO)) - Marshal.SizeOf(typeof(IntPtr)) + i * Marshal.SizeOf(typeof(DPSDK_DEVICE_ID)));
                    Marshal.StructureToPtr(struDeviceID, ptr, true);
                }
                DPSDK_RES iRet = DPSDK_GetDevice(m_iSessionID, pQueryDevInfo, uiQueryLen, fDataCallback, IntPtr.Zero);
                Marshal.FreeHGlobal(pQueryDevInfo);
            }

            if (bSubChild)
            {
                for (int i = 0; i < depCur.vecSubDepInfo.Count; i++)
                {
                    Dep_Info_All depChild = (Dep_Info_All)depCur.vecSubDepInfo[i];
                    HandleGetDeviceInfoByOrg(ref depChild, bSubChild);
                }
            }
        }

        private void CheckMem(Type structType, object structInstance)
        {
            // 确保输入是结构体类型
            if (!structType.IsValueType)
            {
                throw new ArgumentException("参数必须是结构体类型", nameof(structType));
            }

            // 获取结构体的大小
            int size = Marshal.SizeOf(structType);
            Console.WriteLine($"结构体 {structType.Name} 大小: {size} 字节");

            // 获取结构体字段信息
            FieldInfo[] fields = structType.GetFields(BindingFlags.Public | BindingFlags.Instance);

            // 创建一个字节数组来存储结构体数据
            byte[] structData = new byte[size];

            // 将结构体复制到字节数组
            GCHandle handle = GCHandle.Alloc(structData, GCHandleType.Pinned);
            try
            {
                // 将结构体数据复制到字节数组
                Marshal.StructureToPtr(structInstance, handle.AddrOfPinnedObject(), false);

                // 输出字段信息
                Console.WriteLine("字段值和地址:");
                int Stusize = 0;
                int tp = 0;
                foreach (FieldInfo field in fields)
                {
                    // 获取字段在结构体中的偏移量
                    int offset = Marshal.OffsetOf(structType, field.Name).ToInt32();
                    IntPtr fieldAddress = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + offset);
                    // 获取字段值
                    object value = field.GetValue(structInstance);
                    Stusize = offset - tp;
                    Console.WriteLine($"字段名: {field.Name}, 值: {value}, 地址: {fieldAddress.ToString("X")}, 偏移量:{offset}, size:{Stusize}");
                    tp = offset;
                }
            }
            finally
            {
                // 确保释放GCHandle
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
        }

        private void CopyDevData(ref DPSDK_DEV_ALL_INFO_LIST pDevAllInfoList, ref Dev_Info_All pUserDevInfo)
        {
            for (int i = 0; i < pDevAllInfoList.iDevNum; i++)
            {
                Dev_Info devInfo = new Dev_Info();
                
                IntPtr ptr = new IntPtr(pDevAllInfoList.pDevAllInfoList.ToInt32() + i * Marshal.SizeOf(typeof(DPSDK_DEV_ALL_INFO)));
                DPSDK_DEV_ALL_INFO pDevInfo = (DPSDK_DEV_ALL_INFO)Marshal.PtrToStructure(ptr, typeof(DPSDK_DEV_ALL_INFO));
                devInfo.struDevInfo = pDevInfo.struDevInfo;

                devInfo.iEncChnlNum = pDevInfo.iEncChnlNum;
                if ( devInfo.iEncChnlNum > 0)
                {
                    devInfo.vecEncChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iEncChnlNum; j++) 
                    {
                        DPSDK_ENC_CHANNEL_INFO struEncChnlInfo = (DPSDK_ENC_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pEncChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_ENC_CHANNEL_INFO))), typeof(DPSDK_ENC_CHANNEL_INFO));
                        devInfo.vecEncChnlInfo.Add(struEncChnlInfo);
                    }
                }
                
                devInfo.iDecChnlNum = pDevInfo.iDecChnlNum;
                if (devInfo.iDecChnlNum > 0)
                {
                    devInfo.vecDecChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iDecChnlNum; j++)
                    {
                        DPSDK_DEC_CHANNEL_INFO struDecChnlInfo = (DPSDK_DEC_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pDecChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_DEC_CHANNEL_INFO))), typeof(DPSDK_DEC_CHANNEL_INFO));
                        devInfo.vecDecChnlInfo.Add(struDecChnlInfo);
                    }
                }
                
                devInfo.iAlarmInChnlNum = pDevInfo.iAlarmInChnlNum;
                if (devInfo.iAlarmInChnlNum > 0)
                {
                    devInfo.vecAlarmInChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iAlarmInChnlNum; j++)
                    {
                        DPSDK_ALARMIN_CHANNEL_INFO struAlarmInChnlInfo = (DPSDK_ALARMIN_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pAlarmInChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_ALARMIN_CHANNEL_INFO))), typeof(DPSDK_ALARMIN_CHANNEL_INFO));
                        devInfo.vecAlarmInChnlInfo.Add(struAlarmInChnlInfo);
                    }
                }
                
                devInfo.iAlarmOutChnlNum = pDevInfo.iAlarmOutChnlNum;
                if (devInfo.iAlarmOutChnlNum > 0)
                {
                    devInfo.vecAlarmOutChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iAlarmOutChnlNum; j++)
                    {
                        DPSDK_ALARMOUT_CHANNEL_INFO struAlarmOutChnlInfo = (DPSDK_ALARMOUT_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pAlarmOutChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_ALARMOUT_CHANNEL_INFO))), typeof(DPSDK_ALARMOUT_CHANNEL_INFO));
                        devInfo.vecAlarmOutChnlInfo.Add(struAlarmOutChnlInfo);
                    }
                }
                
                devInfo.iTvWallInChnlNum = pDevInfo.iTvWallInChnlNum;
                if (devInfo.iTvWallInChnlNum > 0)
                {
                    devInfo.vecTvWallInChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iTvWallInChnlNum; j++)
                    {
                        DPSDK_TVWALLIN_CHANNEL_INFO struTvWallInChnlInfo = (DPSDK_TVWALLIN_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pTvWallInChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_TVWALLIN_CHANNEL_INFO))), typeof(DPSDK_TVWALLIN_CHANNEL_INFO));
                        devInfo.vecTvWallInChnlInfo.Add(struTvWallInChnlInfo);
                    }
                }
                
                devInfo.iTvWallOutChnlNum = pDevInfo.iTvWallOutChnlNum;
                if (devInfo.iTvWallOutChnlNum > 0)
                {
                    devInfo.vecTvWallOutChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iTvWallOutChnlNum; j++)
                    {
                        DPSDK_TVWALLOUT_CHANNEL_INFO struTvWallOutChnlInfo = (DPSDK_TVWALLOUT_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pTvWallOutChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_TVWALLOUT_CHANNEL_INFO))), typeof(DPSDK_TVWALLOUT_CHANNEL_INFO));
                        devInfo.vecTvWallOutChnlInfo.Add(struTvWallOutChnlInfo);
                    }
                }


                devInfo.iDoorChnlNum = pDevInfo.iDoorChnlNum;
                if (devInfo.iDoorChnlNum > 0)
                {
                    devInfo.vecDoorChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iDoorChnlNum; j++)
                    {
                        DPSDK_DOOR_CHANNEL_INFO struDoorChnlInfo = (DPSDK_DOOR_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pDoorChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_DOOR_CHANNEL_INFO))), typeof(DPSDK_DOOR_CHANNEL_INFO));
                        devInfo.vecDoorChnlInfo.Add(struDoorChnlInfo);
                    }
                }

                devInfo.iGateChnlNum = pDevInfo.iGateChnlNum;
                if (devInfo.iGateChnlNum > 0)
                {
                    devInfo.vecGateChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iGateChnlNum; j++)
                    {
                        DPSDK_GATE_CHANNEL_INFO struGateChnlInfo = (DPSDK_GATE_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pGateChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_GATE_CHANNEL_INFO))), typeof(DPSDK_GATE_CHANNEL_INFO));
                        devInfo.vecGateChnlInfo.Add(struGateChnlInfo);
                    }
                }

                devInfo.iPEChnlNum = pDevInfo.iPEChnlNum;
                if (devInfo.iPEChnlNum > 0)
                {
                    devInfo.vecPEChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iPEChnlNum; j++)
                    {
                        DPSDK_PE_CHANNEL_INFO struPEChnlInfo = (DPSDK_PE_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pPEChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_PE_CHANNEL_INFO))), typeof(DPSDK_PE_CHANNEL_INFO));
                        devInfo.vecPEChnlInfo.Add(struPEChnlInfo);
                    }

                }

                devInfo.iVoiceChnlNum = pDevInfo.iVoiceChnlNum;
                if (devInfo.iVoiceChnlNum > 0)
                {
                    devInfo.vecVoiceChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iVoiceChnlNum; j++)
                    {
                        DPSDK_VOICE_CHANNEL_INFO struVoiceChnlInfo = (DPSDK_VOICE_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pVoiceChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_VOICE_CHANNEL_INFO))), typeof(DPSDK_VOICE_CHANNEL_INFO));
                        devInfo.vecVoiceChnlInfo.Add(struVoiceChnlInfo);
                    }
                }

                devInfo.iRoadGateChnlNum = pDevInfo.iRoadGateChnlNum;
                if (devInfo.iRoadGateChnlNum > 0)
                {
                    devInfo.vecRoadGateChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iRoadGateChnlNum; j++)
                    {
                        DPSDK_ROADGATE_CHANNEL_INFO struRoadGateChnlInfo = (DPSDK_ROADGATE_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pRoadGateChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_ROADGATE_CHANNEL_INFO))), typeof(DPSDK_ROADGATE_CHANNEL_INFO));
                        devInfo.vecRoadGateChnlInfo.Add(struRoadGateChnlInfo);
                    }
                }

                devInfo.iLEDChnlNum = pDevInfo.iLEDChnlNum;
                if (devInfo.iLEDChnlNum > 0)
                {
                    devInfo.vecLEDChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iLEDChnlNum; j++)
                    {
                        DPSDK_LED_CHANNEL_INFO struLEDChnlInfo = (DPSDK_LED_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pLEDChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_LED_CHANNEL_INFO))), typeof(DPSDK_LED_CHANNEL_INFO));
                        devInfo.vecLEDChnlInfo.Add(struLEDChnlInfo);
                    }
                }

                devInfo.iDispatcherChnlNum = pDevInfo.iDispatcherChnlNum;
                if (devInfo.iDispatcherChnlNum > 0)
                {
                    devInfo.vecDispatcherChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iDispatcherChnlNum; j++)
                    {
                        DPSDK_DISPATCHER_CHANNEL_INFO struDispatcherChnlInfo = (DPSDK_DISPATCHER_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pDispatcherChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_DISPATCHER_CHANNEL_INFO))), typeof(DPSDK_DISPATCHER_CHANNEL_INFO));
                        devInfo.vecDispatcherChnlInfo.Add(struDispatcherChnlInfo);
                    }
                }

                devInfo.iPosChnlNum = pDevInfo.iPosChnlNum;
                if (devInfo.iPosChnlNum > 0)
                {
                    devInfo.vecPosChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iPosChnlNum; j++)
                    {
                        DPSDK_POS_CHANNEL_INFO struPosChnlInfo = (DPSDK_POS_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pPosChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_POS_CHANNEL_INFO))), typeof(DPSDK_POS_CHANNEL_INFO));
                        devInfo.vecPosChnlInfo.Add(struPosChnlInfo);
                    }
                }

                devInfo.iVirtualChnlNum = pDevInfo.iVirtualChnlNum;
                if (devInfo.iVirtualChnlNum > 0)
                {
                    devInfo.vecVirtualChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iVirtualChnlNum; j++)
                    {
                        DPSDK_VIRTUAL_CHANNEL_INFO struVirtualChnlInfo = (DPSDK_VIRTUAL_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pVirtualChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_VIRTUAL_CHANNEL_INFO))), typeof(DPSDK_VIRTUAL_CHANNEL_INFO));
                        devInfo.vecVirtualChnlInfo.Add(struVirtualChnlInfo);
                    }
                }


                devInfo.iCVIChnlNum = pDevInfo.iCVIChnlNum;
                if (devInfo.iCVIChnlNum > 0)
                {
                    devInfo.vecCVIChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iCVIChnlNum; j++)
                    {
                        DPSDK_CVI_CHANNEL_INFO struCVIChnlInfo = (DPSDK_CVI_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pCVIChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_CVI_CHANNEL_INFO))), typeof(DPSDK_CVI_CHANNEL_INFO));
                        devInfo.vecCVIChnlInfo.Add(struCVIChnlInfo);
                    }
                }


                devInfo.iALChnlNum = pDevInfo.iALChnlNum;
                if (devInfo.iALChnlNum > 0)
                {
                    devInfo.vecALChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iALChnlNum; j++)
                    {
                        DPSDK_AUDIO_LIGHT_CHANNEL_INFO struALChnlInfo = (DPSDK_AUDIO_LIGHT_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pALChnlInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_AUDIO_LIGHT_CHANNEL_INFO))), typeof(DPSDK_AUDIO_LIGHT_CHANNEL_INFO));
                        devInfo.vecALChnlInfo.Add(struALChnlInfo);
                    }
                }

                for (int j = 0; j < 4; ++j)
                {
                    devInfo.iAlarmBoxChnlNum += pDevInfo.pAlarmBoxUnit[j].iChannelNum;
                }
                if (devInfo.iAlarmBoxChnlNum > 0)
                {
                    devInfo.vecAlarmBoxChnlInfo = new ArrayList();
                    for (int j = 0; j < 4; ++j)
                    {
                        for (int k = 0; k < pDevInfo.pAlarmBoxUnit[j].iChannelNum; ++k)
                        {
                            DPSDK_ALARMBOX_CHANNEL_INFO struAlarmBoxChnlInfo = (DPSDK_ALARMBOX_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pAlarmBoxUnit[j].pAlarmBoxChnList.ToInt64() + k * Marshal.SizeOf(typeof(DPSDK_ALARMBOX_CHANNEL_INFO))), typeof(DPSDK_ALARMBOX_CHANNEL_INFO));
                            DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ struAlarmBoxChnl;
                            struAlarmBoxChnl.struChannelInfo = struAlarmBoxChnlInfo.struChannelInfo;
                            struAlarmBoxChnl.iUnitSeq = k;
                            devInfo.vecAlarmBoxChnlInfo.Add(struAlarmBoxChnl);
                        }
                    }
                }

                devInfo.iElevatorChnlNum = pDevInfo.iElevatorChnNum;
                if (devInfo.iElevatorChnlNum > 0)
                {
                    devInfo.vecElevatorChnlInfo = new ArrayList();
                    for (int j = 0; j < devInfo.iElevatorChnlNum; j++)
                    {
                        DPSDK_ELEVATOR_CHANNEL_INFO struElevatorChnInfo = (DPSDK_ELEVATOR_CHANNEL_INFO)Marshal.PtrToStructure(new IntPtr(pDevInfo.pElevatorChnInfoList.ToInt32() + j * Marshal.SizeOf(typeof(DPSDK_ELEVATOR_CHANNEL_INFO))), typeof(DPSDK_ELEVATOR_CHANNEL_INFO));
                        devInfo.vecElevatorChnlInfo.Add(struElevatorChnInfo);
                    }
                }
                pUserDevInfo.vecDevInfo.Add(devInfo);
            }
        }

        private void SortDeviceByOrg()
        {
            for (int i = 0; i < m_struDepInfoAll.vecDevID.Count; ++i)
            {
                string tmpDeviceID = (string)m_struDepInfoAll.vecDevID[i];

                Dev_Info struDevInfo = new Dev_Info();
                int it = 0;
                for (; it < m_struDevInfoAll.vecDevInfo.Count; ++it)
                {
                    struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[it];
                    if (struDevInfo.struDevInfo.szDeviceID == tmpDeviceID)
                    {
                        break;
                    }
                }
                m_struDevInfoAll.vecDevInfo.RemoveAt(it);
                m_struDevInfoAll.vecDevInfo.Add(struDevInfo);
            }
        }

        private void MapIdAndName()
        {
            m_mapIdName.Clear();
            if (m_struDevInfoAll.vecDevInfo.Count > 0)
            {
                for (int i = 0; i < m_struDevInfoAll.vecDevInfo.Count; i++)
                {
                    Dev_Info struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[i];
                    m_mapIdName.Add(struDevInfo.struDevInfo.szDeviceID, struDevInfo.struDevInfo.szDeviceName);

                    //Encode channel
                    if (struDevInfo.iEncChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecEncChnlInfo.Count; j++)
                        {
                            DPSDK_ENC_CHANNEL_INFO struEncChnlInfo = (DPSDK_ENC_CHANNEL_INFO)struDevInfo.vecEncChnlInfo[j];
                            m_mapIdName.Add(struEncChnlInfo.struChannelInfo.szChannelID, struEncChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Decode channel
                    if (struDevInfo.iDecChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecDecChnlInfo.Count; j++)
                        {
                            DPSDK_DEC_CHANNEL_INFO struDecChnlInfo = (DPSDK_DEC_CHANNEL_INFO)struDevInfo.vecDecChnlInfo[j];
                            m_mapIdName.Add(struDecChnlInfo.struChannelInfo.szChannelID, struDecChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Alarm input channel
                    if (struDevInfo.iAlarmInChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecAlarmInChnlInfo.Count; j++)
                        {
                            DPSDK_ALARMIN_CHANNEL_INFO struAlarmInChnlInfo = (DPSDK_ALARMIN_CHANNEL_INFO)struDevInfo.vecAlarmInChnlInfo[j];
                            m_mapIdName.Add(struAlarmInChnlInfo.struChannelInfo.szChannelID, struAlarmInChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Alarm output channel
                    if (struDevInfo.iAlarmOutChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecAlarmOutChnlInfo.Count; j++)
                        {
                            DPSDK_ALARMOUT_CHANNEL_INFO struAlarmOutChnlInfo = (DPSDK_ALARMOUT_CHANNEL_INFO)struDevInfo.vecAlarmOutChnlInfo[j];
                            m_mapIdName.Add(struAlarmOutChnlInfo.struChannelInfo.szChannelID, struAlarmOutChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Big screen input channel
                    if (struDevInfo.iTvWallInChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecTvWallInChnlInfo.Count; j++)
                        {
                            DPSDK_TVWALLIN_CHANNEL_INFO struTvWallInChnlInfo = (DPSDK_TVWALLIN_CHANNEL_INFO)struDevInfo.vecTvWallInChnlInfo[j];
                            m_mapIdName.Add(struTvWallInChnlInfo.struChannelInfo.szChannelID, struTvWallInChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Big screen output channel
                    if (struDevInfo.iTvWallOutChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecTvWallOutChnlInfo.Count; j++)
                        {
                            DPSDK_TVWALLOUT_CHANNEL_INFO struTvWallOutChnlInfo = (DPSDK_TVWALLOUT_CHANNEL_INFO)struDevInfo.vecTvWallOutChnlInfo[j];
                            m_mapIdName.Add(struTvWallOutChnlInfo.struChannelInfo.szChannelID, struTvWallOutChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    
                    //Acess channel
                    if (struDevInfo.iDoorChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecDoorChnlInfo.Count; j++)
                        {
                            DPSDK_DOOR_CHANNEL_INFO struDoorChnlInfo = (DPSDK_DOOR_CHANNEL_INFO)struDevInfo.vecDoorChnlInfo[j];
                            m_mapIdName.Add(struDoorChnlInfo.struChannelInfo.szChannelID, struDoorChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //PE channel
                    if (struDevInfo.iPEChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecPEChnlInfo.Count; j++)
                        {
                            DPSDK_PE_CHANNEL_INFO struPEChnlInfo = (DPSDK_PE_CHANNEL_INFO)struDevInfo.vecPEChnlInfo[j];
                            m_mapIdName.Add(struPEChnlInfo.struChannelInfo.szChannelID, struPEChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //Audio channel
                    if (struDevInfo.iVoiceChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecVoiceChnlInfo.Count; j++)
                        {
                            DPSDK_VOICE_CHANNEL_INFO struVoiceChnlInfo = (DPSDK_VOICE_CHANNEL_INFO)struDevInfo.vecVoiceChnlInfo[j];
                            m_mapIdName.Add(struVoiceChnlInfo.struChannelInfo.szChannelID, struVoiceChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //Road gate channel
                    if (struDevInfo.iRoadGateChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecRoadGateChnlInfo.Count; j++)
                        {
                            DPSDK_ROADGATE_CHANNEL_INFO struRoadGateChnlInfo = (DPSDK_ROADGATE_CHANNEL_INFO)struDevInfo.vecRoadGateChnlInfo[j];
                            m_mapIdName.Add(struRoadGateChnlInfo.struChannelInfo.szChannelID, struRoadGateChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //LED channel
                    if (struDevInfo.iLEDChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecLEDChnlInfo.Count; j++)
                        {
                            DPSDK_LED_CHANNEL_INFO struLEDChnlInfo = (DPSDK_LED_CHANNEL_INFO)struDevInfo.vecLEDChnlInfo[j];
                            m_mapIdName.Add(struLEDChnlInfo.struChannelInfo.szChannelID, struLEDChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //Dispatcher channel
                    if (struDevInfo.iDispatcherChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecDispatcherChnlInfo.Count; j++)
                        {
                            DPSDK_DISPATCHER_CHANNEL_INFO struDispatcherChnlInfo = (DPSDK_DISPATCHER_CHANNEL_INFO)struDevInfo.vecDispatcherChnlInfo[j];
                            m_mapIdName.Add(struDispatcherChnlInfo.struChannelInfo.szChannelID, struDispatcherChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                    //POS channel
                    if (struDevInfo.iPosChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecPosChnlInfo.Count; j++)
                        {
                            DPSDK_POS_CHANNEL_INFO struPosChnlInfo = (DPSDK_POS_CHANNEL_INFO)struDevInfo.vecPosChnlInfo[j];
                            m_mapIdName.Add(struPosChnlInfo.struChannelInfo.szChannelID, struPosChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //Virtual channel
                    if (struDevInfo.iVirtualChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecVirtualChnlInfo.Count; j++)
                        {
                            DPSDK_VIRTUAL_CHANNEL_INFO struVirtualChnlInfo = (DPSDK_VIRTUAL_CHANNEL_INFO)struDevInfo.vecVirtualChnlInfo[j];
                            m_mapIdName.Add(struVirtualChnlInfo.struChannelInfo.szChannelID, struVirtualChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //CVI channel
                    if (struDevInfo.iCVIChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecCVIChnlInfo.Count; j++)
                        {
                            DPSDK_CVI_CHANNEL_INFO struCVIChnlInfo = (DPSDK_CVI_CHANNEL_INFO)struDevInfo.vecCVIChnlInfo[j];
                            m_mapIdName.Add(struCVIChnlInfo.struChannelInfo.szChannelID, struCVIChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //AL Channel
                    if (struDevInfo.iALChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecALChnlInfo.Count; j++)
                        {
                            DPSDK_AUDIO_LIGHT_CHANNEL_INFO struALChnlInfo = (DPSDK_AUDIO_LIGHT_CHANNEL_INFO)struDevInfo.vecALChnlInfo[j];
                            m_mapIdName.Add(struALChnlInfo.struChannelInfo.szChannelID, struALChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //AlarmBox
                    if (struDevInfo.iAlarmBoxChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecAlarmBoxChnlInfo.Count; j++)
                        {
                            DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ struAlarmBoxChnlInfo = (DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ)struDevInfo.vecAlarmBoxChnlInfo[j];
                            m_mapIdName.Add(struAlarmBoxChnlInfo.struChannelInfo.szChannelID, struAlarmBoxChnlInfo.struChannelInfo.szChannelName);
                        }
                    }

                    //AL Channel
                    if (struDevInfo.iElevatorChnlNum > 0)
                    {
                        for (int j = 0; j < struDevInfo.vecElevatorChnlInfo.Count; j++)
                        {
                            DPSDK_ELEVATOR_CHANNEL_INFO struElevatorChnlInfo = (DPSDK_ELEVATOR_CHANNEL_INFO)struDevInfo.vecElevatorChnlInfo[j];
                            m_mapIdName.Add(struElevatorChnlInfo.struChannelInfo.szChannelID, struElevatorChnlInfo.struChannelInfo.szChannelName);
                        }
                    }
                }
            }
        }

        private string GetMapNameFromID(string strID)
        {
            if (m_mapIdName.ContainsKey(strID))
            {
                return m_mapIdName[strID];
            }

            return "";
        }

        private void DisplayChnlByDevInfo(ref TreeNode devNode, ref Dev_Info struDevInfo)
        {
            //Encode channel
            if (struDevInfo.iEncChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecEncChnlInfo.Count; j++)
                {
                    DPSDK_ENC_CHANNEL_INFO struEncChnlInfo = (DPSDK_ENC_CHANNEL_INFO)struDevInfo.vecEncChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struEncChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Decode channel
            if (struDevInfo.iDecChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecDecChnlInfo.Count; j++)
                {
                    DPSDK_DEC_CHANNEL_INFO struDecChnlInfo = (DPSDK_DEC_CHANNEL_INFO)struDevInfo.vecDecChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struDecChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Alarm input channel
            if (struDevInfo.iAlarmInChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecAlarmInChnlInfo.Count; j++)
                {
                    DPSDK_ALARMIN_CHANNEL_INFO struAlarmInChnlInfo = (DPSDK_ALARMIN_CHANNEL_INFO)struDevInfo.vecAlarmInChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struAlarmInChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Alarm output channel
            if (struDevInfo.iAlarmOutChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecAlarmOutChnlInfo.Count; j++)
                {
                    DPSDK_ALARMOUT_CHANNEL_INFO struAlarmOutChnlInfo = (DPSDK_ALARMOUT_CHANNEL_INFO)struDevInfo.vecAlarmOutChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struAlarmOutChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Big screen input channel
            if (struDevInfo.iTvWallInChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecTvWallInChnlInfo.Count; j++)
                {
                    DPSDK_TVWALLIN_CHANNEL_INFO struTvWallInChnlInfo = (DPSDK_TVWALLIN_CHANNEL_INFO)struDevInfo.vecTvWallInChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struTvWallInChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Big screen output channel
            if (struDevInfo.iTvWallOutChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecTvWallOutChnlInfo.Count; j++)
                {
                    DPSDK_TVWALLOUT_CHANNEL_INFO struTvWallOutChnlInfo = (DPSDK_TVWALLOUT_CHANNEL_INFO)struDevInfo.vecTvWallOutChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struTvWallOutChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Acess channel
            if (struDevInfo.iDoorChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecDoorChnlInfo.Count; j++)
                {
                    DPSDK_DOOR_CHANNEL_INFO struDoorChnlInfo = (DPSDK_DOOR_CHANNEL_INFO)struDevInfo.vecDoorChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struDoorChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //PE Channel
            if (struDevInfo.iPEChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecPEChnlInfo.Count; j++)
                {
                    DPSDK_PE_CHANNEL_INFO struPEChnlInfo = (DPSDK_PE_CHANNEL_INFO)struDevInfo.vecPEChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struPEChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }
            //Audio channel
            if (struDevInfo.iVoiceChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecVoiceChnlInfo.Count; j++)
                {
                    DPSDK_VOICE_CHANNEL_INFO struVoiceChnlInfo = (DPSDK_VOICE_CHANNEL_INFO)struDevInfo.vecVoiceChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struVoiceChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Road gate channel
            if (struDevInfo.iRoadGateChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecRoadGateChnlInfo.Count; j++)
                {
                    DPSDK_ROADGATE_CHANNEL_INFO struRoadGateChnlInfo = (DPSDK_ROADGATE_CHANNEL_INFO)struDevInfo.vecRoadGateChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struRoadGateChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //LED channel
            if (struDevInfo.iLEDChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecLEDChnlInfo.Count; j++)
                {
                    DPSDK_LED_CHANNEL_INFO struLEDChnlInfo = (DPSDK_LED_CHANNEL_INFO)struDevInfo.vecLEDChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struLEDChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Dispatcher channel
            if (struDevInfo.iDispatcherChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecDispatcherChnlInfo.Count; j++)
                {
                    DPSDK_DISPATCHER_CHANNEL_INFO struDispatcherChnlInfo = (DPSDK_DISPATCHER_CHANNEL_INFO)struDevInfo.vecDispatcherChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struDispatcherChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //POS channel
            if (struDevInfo.iPosChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecPosChnlInfo.Count; j++)
                {
                    DPSDK_POS_CHANNEL_INFO struPosChnlInfo = (DPSDK_POS_CHANNEL_INFO)struDevInfo.vecPosChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struPosChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Virtual channel
            if (struDevInfo.iVirtualChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecVirtualChnlInfo.Count; j++)
                {
                    DPSDK_VIRTUAL_CHANNEL_INFO struVirtualChnlInfo = (DPSDK_VIRTUAL_CHANNEL_INFO)struDevInfo.vecVirtualChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struVirtualChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //CVI channel
            if (struDevInfo.iCVIChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecCVIChnlInfo.Count; j++)
                {
                    DPSDK_CVI_CHANNEL_INFO struCVIChnlInfo = (DPSDK_CVI_CHANNEL_INFO)struDevInfo.vecCVIChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struCVIChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //AL channel
            if (struDevInfo.iALChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecALChnlInfo.Count; j++)
                {
                    DPSDK_AUDIO_LIGHT_CHANNEL_INFO struALChnlInfo = (DPSDK_AUDIO_LIGHT_CHANNEL_INFO)struDevInfo.vecALChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struALChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Alarm Box channel
            if (struDevInfo.iAlarmBoxChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecAlarmBoxChnlInfo.Count; j++)
                {
                    DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ struAlarmBoxChnlInfo = (DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ)struDevInfo.vecAlarmBoxChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struAlarmBoxChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }

            //Elevator channel
            if (struDevInfo.iElevatorChnlNum > 0)
            {
                for (int j = 0; j < struDevInfo.vecElevatorChnlInfo.Count; j++)
                {
                    DPSDK_ELEVATOR_CHANNEL_INFO struElevatorChnlInfo = (DPSDK_ELEVATOR_CHANNEL_INFO)struDevInfo.vecElevatorChnlInfo[j];
                    TreeNode chnlNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struElevatorChnlInfo.struChannelInfo.szChannelName)));
                    devNode.Nodes.Add(chnlNode);
                }
            }
        }

        private void DisplayChannelByDevice(ref TreeNode node, string strDeviceId)
        {
            for (int j = 0; j < m_struDevInfoAll.vecDevInfo.Count; j++)
            {
                Dev_Info struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[j];
                if (struDevInfo.struDevInfo.szDeviceID == strDeviceId)
                {
                    DisplayChnlByDevInfo(ref node, ref struDevInfo);
                }
            }
        }

        private void DisplayOrgTree()
        {
            if (m_struDepInfoAll.depInfo.strCoding == "" || m_struDepInfoAll.depInfo.strCoding == null)
            {
                m_struDepInfoAll.depInfo.strCoding = textDeviceOrgCode.Text;
            }
            treeViewDeviceOrg.Nodes.Clear();

            TreeNode orgNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(m_struDepInfoAll.depInfo.strDepName)));
            treeViewDeviceOrg.Nodes.Add(orgNode);
            LoopOrgNode(ref orgNode, ref m_struDepInfoAll);

            //Add device ID
            for (int i = 0; i < m_struDepInfoAll.vecDevID.Count; i++)
            {
                string strInfo = GetMapNameFromID((string)m_struDepInfoAll.vecDevID[i]);
                if (strInfo == "")
                {
                    strInfo = (string)m_struDepInfoAll.vecDevID[i];
                }
                TreeNode devNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(strInfo)));
                orgNode.Nodes.Add(devNode);

                DisplayChannelByDevice(ref devNode, (string)m_struDepInfoAll.vecDevID[i]);
            }
        }

        private void LoopOrgNode(ref TreeNode node, ref Dep_Info_All struDepInfoAll)
        {
            for (int i = 0; i < struDepInfoAll.vecSubDepInfo.Count; i++)
            {
                Dep_Info_All subDepInfo = (Dep_Info_All)struDepInfoAll.vecSubDepInfo[i];
//                 byte[] buffer1 = Encoding.Default.GetBytes(subDepInfo.depInfo.strDepName);
//                 byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
//                 string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
                TreeNode subOrgNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(subDepInfo.depInfo.strDepName))/*strBuffer*/);
                node.Nodes.Add(subOrgNode);
                LoopOrgNode(ref subOrgNode, ref subDepInfo);

                //Add device ID
                for (int j = 0; j < subDepInfo.vecDevID.Count; j++)
                {
                    string strInfo = GetMapNameFromID((string)subDepInfo.vecDevID[j]);
                    if (strInfo == "")
                    {
                        strInfo = (string)subDepInfo.vecDevID[j];
                    }
                    TreeNode devNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(strInfo)));
                    subOrgNode.Nodes.Add(devNode);

                    DisplayChannelByDevice(ref devNode, (string)subDepInfo.vecDevID[j]);
                }
            }
        }

        private void DisplayDeviceTree()
        {
            treeViewDeviceOrg.Nodes.Clear();
            if (m_struDevInfoAll.vecDevInfo.Count == 0)
            {
                return;
            }

            for (int i = 0; i < m_struDevInfoAll.vecDevInfo.Count; i++)
            {
                Dev_Info struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[i];
                TreeNode devNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struDevInfo.struDevInfo.szDeviceName)));
                treeViewDeviceOrg.Nodes.Add(devNode);

                DisplayChnlByDevInfo(ref devNode, ref struDevInfo);
            }
        }

        private void DisplayChannelTree()
        {
            treeViewDeviceOrg.Nodes.Clear();
            if (m_struDevInfoAll.vecDevInfo.Count == 0)
            {
                return;
            }
            TreeNode orgNode = new TreeNode(Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(m_struDepInfoAll.depInfo.strDepName)));
            treeViewDeviceOrg.Nodes.Add(orgNode);

            for (int i = 0; i < m_struDevInfoAll.vecDevInfo.Count; i++)
            {
                Dev_Info struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[i];

                DisplayChnlByDevInfo(ref orgNode, ref struDevInfo);
            }
        }

        private void DisplayTree()
        {
            if (radioButtonDeviceOrg.Checked)
            {
                DisplayOrgTree();
            }
            else if (radioButtonDeviceDevice.Checked)
            {
                DisplayDeviceTree();
            }
            else if (radioButtonDeviceChannel.Checked)
            {
                DisplayChannelTree();
            }
        }

        private void PacketOrgXml(ref Dep_Info_All struDepInfoAll, ref string strXmlData)
        {
            strXmlData += "<Department ";

            strXmlData = strXmlData + XML_CODING + " = " + struDepInfoAll.depInfo.strCoding + " ";
            strXmlData = strXmlData + XML_NAME + " = " + Encoding.GetEncoding("utf-8").GetString(Encoding.Default.GetBytes(struDepInfoAll.depInfo.strDepName)) + " ";
            strXmlData = strXmlData + XML_SN + " = " + struDepInfoAll.depInfo.strSN + " ";

            strXmlData += ">\r\n";

            for (int i = 0; i < struDepInfoAll.vecSubDepInfo.Count; ++i)
            {
                Dep_Info_All struSubDepInfoAll = (Dep_Info_All)struDepInfoAll.vecSubDepInfo[i];
                PacketOrgXml(ref struSubDepInfoAll, ref strXmlData);
            }
            for (int i = 0; i < struDepInfoAll.vecDevID.Count; ++i)
            {
                strXmlData += "<Device>" + (string)struDepInfoAll.vecDevID[i] + "</Device>\r\n";

                for (int j = 0; j < m_struDevInfoAll.vecDevInfo.Count; j++)
                {
                    Dev_Info struDevInfo = (Dev_Info)m_struDevInfoAll.vecDevInfo[j];
                    if (struDevInfo.struDevInfo.szDeviceID == (string)struDepInfoAll.vecDevID[i])
                    {
                        //Encode channel
                        if (struDevInfo.iEncChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecEncChnlInfo.Count; k++)
                            {
                                DPSDK_ENC_CHANNEL_INFO struEncChnlInfo = (DPSDK_ENC_CHANNEL_INFO)struDevInfo.vecEncChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struEncChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }
                        
                        //Decode channel
                        if (struDevInfo.iDecChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecDecChnlInfo.Count; k++)
                            {
                                DPSDK_DEC_CHANNEL_INFO struDecChnlInfo = (DPSDK_DEC_CHANNEL_INFO)struDevInfo.vecDecChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struDecChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Alarm input channel
                        if (struDevInfo.iAlarmInChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecAlarmInChnlInfo.Count; k++)
                            {
                                DPSDK_ALARMIN_CHANNEL_INFO struAlarmInChnlInfo = (DPSDK_ALARMIN_CHANNEL_INFO)struDevInfo.vecAlarmInChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struAlarmInChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Alarm output channel
                        if (struDevInfo.iAlarmOutChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecAlarmOutChnlInfo.Count; k++)
                            {
                                DPSDK_ALARMOUT_CHANNEL_INFO struAlarmOutChnlInfo = (DPSDK_ALARMOUT_CHANNEL_INFO)struDevInfo.vecAlarmOutChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struAlarmOutChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Big screen input channel
                        if (struDevInfo.iTvWallInChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecTvWallInChnlInfo.Count; k++)
                            {
                                DPSDK_TVWALLIN_CHANNEL_INFO struTvWallInChnlInfo = (DPSDK_TVWALLIN_CHANNEL_INFO)struDevInfo.vecTvWallInChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struTvWallInChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Big screen output channel
                        if (struDevInfo.iTvWallOutChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecTvWallOutChnlInfo.Count; k++)
                            {
                                DPSDK_TVWALLOUT_CHANNEL_INFO struTvWallOutChnlInfo = (DPSDK_TVWALLOUT_CHANNEL_INFO)struDevInfo.vecTvWallOutChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struTvWallOutChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Acess channel
                        if (struDevInfo.iDoorChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecDoorChnlInfo.Count; k++)
                            {
                                DPSDK_DOOR_CHANNEL_INFO struDoorChnlInfo = (DPSDK_DOOR_CHANNEL_INFO)struDevInfo.vecDoorChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struDoorChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //PE Channel
                        if (struDevInfo.iPEChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecPEChnlInfo.Count; k++)
                            {
                                DPSDK_PE_CHANNEL_INFO struPEChnlInfo = (DPSDK_PE_CHANNEL_INFO)struDevInfo.vecPEChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struPEChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Audio channel
                        if (struDevInfo.iVoiceChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecVoiceChnlInfo.Count; k++)
                            {
                                DPSDK_VOICE_CHANNEL_INFO struVoiceChnlInfo = (DPSDK_VOICE_CHANNEL_INFO)struDevInfo.vecVoiceChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struVoiceChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Road gate channel
                        if (struDevInfo.iRoadGateChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecRoadGateChnlInfo.Count; k++)
                            {
                                DPSDK_ROADGATE_CHANNEL_INFO struRoadGateChnlInfo = (DPSDK_ROADGATE_CHANNEL_INFO)struDevInfo.vecRoadGateChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struRoadGateChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //LED channel
                        if (struDevInfo.iLEDChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecLEDChnlInfo.Count; k++)
                            {
                                DPSDK_LED_CHANNEL_INFO struLEDChnlInfo = (DPSDK_LED_CHANNEL_INFO)struDevInfo.vecLEDChnlInfo[j];
                                strXmlData += "<Channel>" + (string)struLEDChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Dispatcher channel
                        if (struDevInfo.iDispatcherChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecDispatcherChnlInfo.Count; k++)
                            {
                                DPSDK_DISPATCHER_CHANNEL_INFO struDispatcherChnlInfo = (DPSDK_DISPATCHER_CHANNEL_INFO)struDevInfo.vecDispatcherChnlInfo[j];
                                strXmlData += "<Channel>" + (string)struDispatcherChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //POS channel
                        if (struDevInfo.iPosChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecPosChnlInfo.Count; k++)
                            {
                                DPSDK_POS_CHANNEL_INFO struPosChnlInfo = (DPSDK_POS_CHANNEL_INFO)struDevInfo.vecPosChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struPosChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Virtual channel
                        if (struDevInfo.iVirtualChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecVirtualChnlInfo.Count; k++)
                            {
                                DPSDK_VIRTUAL_CHANNEL_INFO struVirtualChnlInfo = (DPSDK_VIRTUAL_CHANNEL_INFO)struDevInfo.vecVirtualChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struVirtualChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //CVI channel
                        if (struDevInfo.iCVIChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecCVIChnlInfo.Count; k++)
                            {
                                DPSDK_CVI_CHANNEL_INFO struCVIChnlInfo = (DPSDK_CVI_CHANNEL_INFO)struDevInfo.vecCVIChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struCVIChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //AL channel
                        if (struDevInfo.iALChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecALChnlInfo.Count; k++)
                            {
                                DPSDK_AUDIO_LIGHT_CHANNEL_INFO struALChnlInfo = (DPSDK_AUDIO_LIGHT_CHANNEL_INFO)struDevInfo.vecALChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struALChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //AlarmBox channel
                        if (struDevInfo.iAlarmBoxChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecAlarmBoxChnlInfo.Count; k++)
                            {
                                DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ struAlarmBoxChnlInfo = (DPSDK_ALARMBOX_CHANNEL_INCLUDE_UNITSEQ)struDevInfo.vecAlarmBoxChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struAlarmBoxChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }

                        //Elevator channel
                        if (struDevInfo.iElevatorChnlNum > 0)
                        {
                            for (int k = 0; k < struDevInfo.vecElevatorChnlInfo.Count; k++)
                            {
                                DPSDK_ELEVATOR_CHANNEL_INFO struElevatorChnlInfo = (DPSDK_ELEVATOR_CHANNEL_INFO)struDevInfo.vecElevatorChnlInfo[k];
                                strXmlData += "<Channel>" + (string)struElevatorChnlInfo.struChannelInfo.szChannelID + "</Channel>\r\n";
                            }
                        }
                    }      
                }
            }
            strXmlData += "</Department>\r\n";
        }
    }
}