package kr.co.icontrols.ilp300_700.ethernet;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Locale;
import java.util.regex.Pattern;

import javax.net.ssl.SSLServerSocket;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.R;
import kr.co.icontrols.ilp300_700.call.CallManager.CALLTYPE;
import kr.co.icontrols.ilp300_700.call.CallManager.CHATEVENT;
import kr.co.icontrols.ilp300_700.call.CallManager.CHATSTATUS;
import kr.co.icontrols.ilp300_700.ethernet.iMAPCommon.iMAPResult;
import kr.co.icontrols.ilp300_700.global.Configure;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.Configure.SetValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.AccessKeyInfoTable;
import kr.co.icontrols.ilp300_700.global.DataClasses.ConfigValueTable;
import kr.co.icontrols.ilp300_700.global.DataClasses.FTPUploadInfo;
import kr.co.icontrols.ilp300_700.global.DataClasses.LineInfoTable;
import kr.co.icontrols.ilp300_700.global.DataClasses.SystemUpgradeInfo;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Declare.AccessValues;
import kr.co.icontrols.ilp300_700.global.Declare.DeviceInfo;
import kr.co.icontrols.ilp300_700.global.Declare.DeviceType;
import kr.co.icontrols.ilp300_700.global.Enumeration.UPGRADESTATUS;
import kr.co.icontrols.ilp300_700.global.FTPUpload.ProcFTPUploadThread;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.LobbyAccessCtrl;
import kr.co.icontrols.ilp300_700.global.SystemUpgrade;
import kr.co.icontrols.ilp300_700.global.SystemUpgrade.ProcSystemUpgradeThread;
import kr.co.icontrols.ilp300_700.imap.MakeiMAPMSG;
import kr.co.icontrols.ilp300_700.imap.Notices;
import kr.co.icontrols.ilp300_700.imap.Request_propose_upgrade;
import kr.co.icontrols.ilp300_700.imap.iMAPResultCtrl;
import kr.co.icontrols.ilp300_700.rfreader.STDAccessKeyManager;
import kr.co.icontrols.ilp300_700.rfreader.UIDAccessKeyManager;

public class iMAPServer {

    static String TAG = "iMAPServer";

    public static ProcSystemUpgradeThread ProcSysUp;
    public static ProcFTPUploadThread mProcFTPUploadThread;

    static boolean bLOG = true;   // true, false
    static boolean bLOG_DEBUG = true;   // true, false
    static boolean bLOG_INFO = true;   // true, false
    static Context mContext;
    public static Handler ServerHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            LOG("[ServerHandler] what [" + msg.what + "], arg1 [" + msg.arg1 + "], arg2 [" + msg.arg2 + "]");
            switch (msg.what) {
                case HANDLERMSG.SEND_HEARTBEAT_NOTICE:
                    Notices.send_Heartbeat();
                    break;

                case HANDLERMSG.CHECK_REBOOT_REQUIRED:
                    checkRebootRequied();
                    break;

                default:
                    LOG("ServerHandler : " + msg.what);
                    break;
            }
        }
    };
    static int PORT_NUM_iMAP = 10000;
    static int PORT_NUM_iMAP_ENCRYPTED = 11000;
    static int SOCKET_WAIT_TIME = 40;
//    static long lSocketConnectionPeriod = 0;
    static int TIMEOUT_iMAP_WAIT = 4000;   // iMAP 타임아웃은 실제 3초 이지만, 마진을 두어 4초로 한다.
    static int SOCKET_BUFFER_SIZE_READ = 50;
    static String SSLKEY_PW = "icontrolswallpad1845!";

//    static DeviceSettingInfo mDeviceSetSetupInfo = new DeviceSettingInfo();
//    static DeviceSetting_LineInfo mDeviceSetting_LineInfo = new DeviceSetting_LineInfo();
    static ChatEventListener mChatEventListener;
    static boolean bChamLinkSupplementation = false;   // 관리PC에서 hcard/dcard/mcard로 보내줘야하나, hkey/dkey/mkey로 보내주고 있기때문에 두 경우 모두 되도록 임시로 보완수정한다. 시흥배곧 한라비발디 2차용(JEFF, 2018.02.28)
    private static Pattern mPattern = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");

    private static void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private static void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private static void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    public static boolean sendReply(Socket Sock, String strReply) {
        try {
            if (Sock.isClosed()) return false;
            LOG("############### sendReply ###############");
            LOG(strReply);
            LOG("############### sentReply ###############");
            DataOutputStream OutStm = new DataOutputStream(Sock.getOutputStream());
            byte[] OutBuffer = strReply.getBytes("euc-kr");
            OutStm.write(OutBuffer);
            OutStm.flush();
            Thread.sleep(100);
            Sock.close();
            return Sock.isClosed();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobbyOpen()");
            e.printStackTrace();
            try {
                Sock.close();
                return false;
            } catch (IOException e1) {
                Log.e(TAG, "[Exception] procLobbyOpen() : Sock.close()");
                e1.printStackTrace();
                return false;
            }
        }
    }

    private static String getAddressinXML(String strMSG) {
        try {
            String[] Splited_01 = strMSG.toLowerCase().split("address");
            String[] Splited_02 = Splited_01[1].trim().split("=");
            String[] Splited_03 = Splited_02[1].trim().split("\"");
            String strResult = Splited_03[1];
            LOG("[getAddressinXML] strResult = " + strResult);
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAddressinXML(strMSG)");
            e.printStackTrace();
            return "ERROR";
        }
    }

    private static String getSenderinXML(String strMSG) {
        try {
            String[] Splited_01 = strMSG.toLowerCase().split("sender");
            String[] Splited_02 = Splited_01[1].trim().split("=");
            String[] Splited_03 = Splited_02[1].trim().split("\"");
            String strResult = Splited_03[1];
            LOG("[getAddressinXML] strResult = " + strResult);
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getSenderinXML(strMSG)");
            e.printStackTrace();
            return "ERROR";
        }
    }

    public static String getAttributeValue(Node node, String attname) {
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            if (attr.getNodeName().equalsIgnoreCase(attname)) return attr.getNodeValue();
        }
        return null;
    }

    private static String trimQuotation(String strValue) {
        strValue = strValue.replaceAll("\"", "");
        return strValue.trim();
    }

    private static boolean procUpgrade(Socket Sock, Document Doc) {
        try {
            Log.i(TAG, "[procUpgrade] ===== START =====");

            if (Global.getUpgradeStatus() == UPGRADESTATUS.IDLE) {
                Global.setUpgradeStatus(UPGRADESTATUS.REQUESTED);
            }
            else {
                Log.w(TAG, "[procUpgrade] Upgrade status is not idle!!");
                return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
            }

            NodeList ItemGuide = Doc.getElementsByTagName("guidemap");
            Node Item = ItemGuide.item(0);

            SystemUpgrade.FinalSystemUpgradeInfo = new SystemUpgradeInfo();

            if (Item != null) {
                NamedNodeMap Attrs = Item.getAttributes();
                for (int j = 0; j < Attrs.getLength(); j++) {
                    Node Attr = Attrs.item(j);

                    if (Attr.getNodeName().equalsIgnoreCase("type")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.strType = Attr.getNodeValue();
                    } else if (Attr.getNodeName().equalsIgnoreCase("count")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt = Integer.parseInt(Attr.getNodeValue());
                        SystemUpgrade.FinalSystemUpgradeInfo.setArray();
                    } else if (Attr.getNodeName().equalsIgnoreCase("user")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.strUserID = Attr.getNodeValue();
                    } else if (Attr.getNodeName().equalsIgnoreCase("passwd")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.strPassword = Attr.getNodeValue();
                    }
                }
            }

            LOG("[procUpgrade] strType [" + SystemUpgrade.FinalSystemUpgradeInfo.strType + "], nModuleCnt [" + SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt + "], strUserID [" + SystemUpgrade.FinalSystemUpgradeInfo.strUserID + "], strPassword [" + SystemUpgrade.FinalSystemUpgradeInfo.strPassword + "]");

            NodeList ItemImgs = Doc.getElementsByTagName("img");
            LOG("[procUpgrade] ItemImgs Cnt [" + ItemImgs.getLength() + "]");

            for (int i = 0; i < ItemImgs.getLength(); i++) {
                Node Img = ItemImgs.item(i);
                NamedNodeMap Attrs = Img.getAttributes();

                for (int j = 0; j < Attrs.getLength(); j++) {
                    Node Attr = Attrs.item(j);
                    if (Attr.getNodeName().equalsIgnoreCase("module")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i] = Attr.getNodeValue();
                        LOG("[procUpgrade] aModule_Type[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i]);
                    }
                    else if (Attr.getNodeName().equalsIgnoreCase("path")) {
                        SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i] = Attr.getNodeValue();
                        LOG("[procUpgrade] aModule_Path[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i]);
                        String[] Parse = SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i].split("/");
                        SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i] = Parse[Parse.length - 1];
                        LOG("[procUpgrade] aModule_FileName[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i]);
                    }
                }
            }

            if (checkUpgradeFileName()) {
                LOG("[procUpgrade] Upgrade file name is corrrect!!");
            }
            else {
                LOG("[procUpgrade] Upgrade file name is not corrrect!!");
                Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
            }

            // MoIP 모듈 확인 (공용 출입/관리자 출입 비밀번호 설정)
            String strMoIPFileName = "null";
            for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
                if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_MOIP)) {
                    strMoIPFileName = SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i];
                }
            }

            if (!strMoIPFileName.equalsIgnoreCase("null")) {
                // MoIP 모듈 존재
                if (SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt > 1) {
                    // MoIP 모듈외에 다른 업데이트 모듈의 업데이트를 위해 업데이트 정보 갱신 (업데이트 정보에서 MoIP 정보를 삭제한다.)
                    if (!replaceUpgradeInfo()) {
                        Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
                    }

                    if (!setAccessPassCode(strMoIPFileName)) {
                        Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
                    }
                }
                else {
                    // MoIP만 존재하는 경우에는 업데이트를 초기화 한다.
                    Log.i(TAG, "[procUpgrade] ===== END ===== MoIP Ver. upgrade!!");
                    if (setAccessPassCode(strMoIPFileName)) {
                        Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                        sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(true));
                        return Notices.send_Heartbeat();
                    }
                    else {
                        Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
                    }
                }
            }

            // Reply
            if (Global.getUpgradeStatus() == UPGRADESTATUS.REQUESTED) {
                // Upgrade process is not running
                Global.setUpgradeStatus(UPGRADESTATUS.PROPOSE);
                Log.i(TAG, "[procUpgrade] ===== END =====");
                return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(true));
            }
            else {
                // Upgrade process is running
                Log.i(TAG, "[procUpgrade] ===== END =====");
                return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
            }

        } catch (Exception e) {
            Log.e(TAG, "[Exception] procUpgrade(Doc)");
            e.printStackTrace();
            Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
            return sendReply(Sock, MakeiMAPMSG.makeReply_upgrade(false));
        }
    }

    private static void startUpgradeProc() {
        try {
            Log.d(TAG, "[startUpgradeProc] ===== START =====");
            Request_propose_upgrade propose_upgrade = new Request_propose_upgrade(mContext);
            if (propose_upgrade.send_propose_upgrade_request()) {
                propose_upgrade.setResultCtrl(new iMAPResultCtrl() {

                    @Override
                    public void onResult(iMAPResult eResult) {
                        Log.d(TAG, "[startUpgradeProc] (onResult) eResult = " + eResult);
                        if (eResult == iMAPResult.OK) {
                            if (ProcSysUp == null) {
                                if (Global.getUpgradeStatus() == UPGRADESTATUS.PROPOSE) {
                                    Global.setUpgradeStatus(UPGRADESTATUS.PROPOSED);
                                    ProcSysUp = new ProcSystemUpgradeThread(mContext);
                                    ProcSysUp.start();
                                } else {
                                    Log.i(TAG, "[startUpgradeProc] Global.getUpgradeStatus() != UPGRADESTATUS.PROPOSE");
                                    Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                                }
                            } else {
                                Log.i(TAG, "[startUpgradeProc] Already upgrade process is running!!");
                                Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                            }
                        } else {
                            Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
                        }
                    }
                });
            } else {
                Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] startUpgradeProc() : " + e);
            e.printStackTrace();
            ProcSysUp = null;
            Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
        }
    }

    private static boolean checkUpgradeFileName() {
        try {
            LOG("[checkUpgradeFileName] ===== START =====");
            int nMatchCnt = 0;
            String strKeyword = "";
            for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
                if (DeviceInfo.Type == DeviceType.LOBBY) {
                    if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_BSP)) {
                        if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_300 || ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) {
                            if (Build.MODEL.indexOf("ILP-300FR_6410") == 0)
                                strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_BSP + SystemUpgrade.strUpgradeFileKeyword_ILP300_700FR_6410 + ".*";
                            else
                                strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_BSP + SystemUpgrade.strUpgradeFileKeyword_ILP300_700 + ".*";
                        } else {
                            Log.w(TAG, "[checkUpgradeFileName] MODULE_BSP Not Supported ModelType [" + ConfigValue.DEVICE_MODELTYPE + "]");
                        }
                    } else if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_APP)) {
                        if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_300 || ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) {
                            if (Build.MODEL.indexOf("ILP-300FR_6410") == 0)
                                strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_APP + SystemUpgrade.strUpgradeFileKeyword_ILP300_700FR_6410 + ".*";
                            else
                                strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_APP + SystemUpgrade.strUpgradeFileKeyword_ILP300_700 + ".*";
                        } else {
                            Log.w(TAG, "[checkUpgradeFileName] MODULE_APP Not Supported ModelType [" + ConfigValue.DEVICE_MODELTYPE + "]");
                        }
                    }
                    else if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_ADD)) {
                        if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_300 || ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) {
                            strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_ADD + SystemUpgrade.strUpgradeFileKeyword_ILP300_700 + ".*";
                        }
                        else {
                            Log.w(TAG, "[checkUpgradeFileName] MODULE_ADD Not Supported ModelType [" + ConfigValue.DEVICE_MODELTYPE + "]");
                        }
                    }
                    else if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_MOIP)) {
                        if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_300 || ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) {
                            strKeyword = ".*" + SystemUpgrade.strUpgradeFileType_MOIP + SystemUpgrade.strUpgradeFileKeyword_ILP300_700 + ".*";
                        }
                        else {
                            Log.w(TAG, "[checkUpgradeFileName] MODULE_MOIP Not Supported ModelType [" + ConfigValue.DEVICE_MODELTYPE + "]");
                        }
                    }
                    else {
                        Log.w(TAG, "[checkUpgradeFileName] Not Supported ModuleType [" + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i] + "]");
                        return false;
                    }
                }
                else {
                    Log.w(TAG, "[checkUpgradeFileName] Not Supported DeviceInfo.Type [" + DeviceInfo.Type + "]");
                    return false;
                }

                if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i].matches(strKeyword))
                    nMatchCnt++;
            }

            if (SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt == nMatchCnt) return true;
            else return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobbyOpen()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean procSetServerVersion(Socket Sock, Document Doc) {
        try {
            LOG("[procSetServerVersion] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("ver_server");
            Element mElement = (Element) mNodeList.item(0);
            String strServerVersion = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSetServerVersion] strServerVersion [" + strServerVersion + "]");

            if (sendReply(Sock, MakeiMAPMSG.makeReply_set_server_version(true)))
                return Global.setMainServerType(strServerVersion);
            else return false;

        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSetServerVersion(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_set_server_version(false));
        }
    }

    /**
     * MoIP 모듈 업데이트(공용 출입/관리자 출입 비밀번호 변경)가 있는 경우
     * 수신된 업데이트 정보를 갱신한다. (업데이트 정보에서 MoIP 정보를 삭제한다.)
     * @return
     */
    private static boolean replaceUpgradeInfo() {
        try {
            // MoIP 모듈외에 다른 모듈이 있는지 확인
            if (SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt > 1) {
                // MoIP 모듈외에 다른 모듈도 존재하는 경우, 업데이트 정보를 갱신하여 계속 진행한다.
                SystemUpgradeInfo NewSystemUpgradeInfo = new SystemUpgradeInfo();
                NewSystemUpgradeInfo.strType = SystemUpgrade.FinalSystemUpgradeInfo.strType;
                NewSystemUpgradeInfo.nModuleCnt = SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt - 1;
                NewSystemUpgradeInfo.strUserID = SystemUpgrade.FinalSystemUpgradeInfo.strUserID;
                NewSystemUpgradeInfo.strPassword = SystemUpgrade.FinalSystemUpgradeInfo.strPassword;
                NewSystemUpgradeInfo.setArray();

                // MoIP 모듈을 제외한 업그레이드 모듈을 갱신한다.
                int nIndex = 0;
                for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
                    if (!SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(SystemUpgrade.MODULE_MOIP)) {
                        NewSystemUpgradeInfo.aModule_Type[nIndex] = SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i];
                        NewSystemUpgradeInfo.aModule_FileName[nIndex] = SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i];
                        NewSystemUpgradeInfo.aModule_Path[nIndex] = SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i];
                        NewSystemUpgradeInfo.aModule_FileSize[nIndex] = SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[i];
                        nIndex++;
                    }
                }

                // 업데이트 정보를 다시 생성한다.
                SystemUpgrade.FinalSystemUpgradeInfo = new SystemUpgradeInfo();
                SystemUpgrade.FinalSystemUpgradeInfo.strType = NewSystemUpgradeInfo.strType;
                SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt = NewSystemUpgradeInfo.nModuleCnt;
                SystemUpgrade.FinalSystemUpgradeInfo.strUserID = NewSystemUpgradeInfo.strUserID;
                SystemUpgrade.FinalSystemUpgradeInfo.strPassword = NewSystemUpgradeInfo.strPassword;
                SystemUpgrade.FinalSystemUpgradeInfo.setArray();

                for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
                    SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i] = NewSystemUpgradeInfo.aModule_Type[i];
                    SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i] = NewSystemUpgradeInfo.aModule_FileName[i];
                    SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i] = NewSystemUpgradeInfo.aModule_Path[i];
                    SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[i] = NewSystemUpgradeInfo.aModule_FileSize[i];
                }

//                Log.i(TAG, "[replaceUpgradeInfo] strType [" + SystemUpgrade.FinalSystemUpgradeInfo.strType + "], nModuleCnt [" + SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt + "], strUserID [" + SystemUpgrade.FinalSystemUpgradeInfo.strUserID + "], strPassword [" + SystemUpgrade.FinalSystemUpgradeInfo.strPassword + "]");
//                for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
//                    Log.i(TAG, "[replaceUpgradeInfo] SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i]);
//                    Log.i(TAG, "[replaceUpgradeInfo] SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i]);
//                    Log.i(TAG, "[replaceUpgradeInfo] SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i]);
//                    Log.i(TAG, "[replaceUpgradeInfo] SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[i]);
//                }

                return true;
            }
            else {
                return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkLobbyAccessPasscodeSetting()");
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 로비폰 MoIP 모듈 업그레이드 파일에 있는 버전 정보를 읽어서 로비폰 출입비밀번호를 설정한다.
     * 연도정보: 공용 출입 비밀번호, 월/일정보: 관리자 출입 비밀번호
     * 예시) MoIP Ver.: 2020.06.17.01 -> 파일명: MOIP_A40i_ILP300_700_2020061701.zip
     *           > 2020을 공용 출입 비밀번호로 설정, > 0617을 관리자 출입 비밀번호로 설정
     * @param strFileName: MoIP 업그레이드 파일명
     */
    private static boolean setAccessPassCode(String strFileName) {
        try {
            Log.i(TAG, "[setAccessPassCode] strFileName [" + strFileName + "]");
            String[] strParsed_01 = strFileName.split("_");
//            for (int i = 0; i < strParsed_01.length; i++) {
//                Log.i(TAG, "[setAccessPassCode] strParsed_01 [" + i + "]" + strParsed_01[i]);
//            }
            String[] strParsed_02 = strParsed_01[strParsed_01.length - 1].split("\\.");
//            for (int i = 0; i < strParsed_02.length; i++) {
//                Log.i(TAG, "[setAccessPassCode] strParsed_02 [" + i + "]" + strParsed_02[i]);
//            }

            if (strParsed_02 == null) {
                Log.w(TAG, "[setAccessPassCode] strParsed_02 is null!!");
                return false;
            }

            String strFileVer = strParsed_02[0];
            if (strFileVer.length() != 10) {
                Log.w(TAG, "[setAccessPassCode] strFileVer length is wrong!! -> " + strFileVer.length());
                return false;
            }

            String moipver = "0000.00.00.00";
            String publicaccesspw = "";
            String adminaccesspw = "";

            StringBuffer strbMoIPVer = new StringBuffer(strFileVer);
            strbMoIPVer.insert(4, ".");
            strbMoIPVer.insert(7, ".");
            strbMoIPVer.insert(10, ".");

            moipver = strbMoIPVer.toString();
            publicaccesspw = strFileVer.substring(0, 4);
            adminaccesspw = strFileVer.substring(4, 8);

            Log.i(TAG, "[setAccessPassCode] moipver [" + moipver + "], publicaccesspw [" + publicaccesspw + "], adminaccesspw [" + adminaccesspw + "]");

            // 차단된 비밀번호 검사
            for (int i = 0; i < Global.astrBlockedPW.length; i++) {
                if (Global.astrBlockedPW[i].equals(publicaccesspw)) return false;
                if (Global.astrBlockedPW[i].equals(adminaccesspw)) return false;
            }

            ConfigValue.strVer_MOIP = moipver;
            ConfigValue.strPublicAccessPW = publicaccesspw;
            ConfigValue.strAdminAccessPW = adminaccesspw;

            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, Configure.ConfigValKey.PublicAccessPW, ConfigValue.strPublicAccessPW)) {
                Log.w(TAG, "[setAccessPassCode] Setting ConfigValue.strPublicAccessPW fail!!");
                return false;
            }

            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, Configure.ConfigValKey.AdminAccessPW, ConfigValue.strAdminAccessPW)) {
                Log.w(TAG, "[setAccessPassCode] Setting ConfigValue.strAdminAccessPW fail!!");
                return false;
            }

            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, Configure.ConfigValKey.Ver_MOIP, ConfigValue.strVer_MOIP)) {
                Log.w(TAG, "[setAccessPassCode] Setting ConfigValue.strVer_MOIP fail!!");
                return false;
            }

            Log.i(TAG, "[setAccessPassCode] Password changed!! ConfigValue.strPublicAccessPW [" + ConfigValue.strPublicAccessPW + "], ConfigValue.strAdminAccessPW [" + ConfigValue.strAdminAccessPW + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setAccessPassCode(String strFileName)");
            e.printStackTrace();
            return false;
        }
    }

    private static String procLobbyOpen(String strRemoteIP) {
        try {
            Log.i(TAG, "[procLobbyOpen] == START ==");

            String strReply = MakeiMAPMSG.makeReply_lobby_open(true);
            return strReply;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobbyOpen()");
            e.printStackTrace();
            return null;
        }
    }

    private static boolean procLobbyOpen(Socket Sock, String strRemoteSender) {
        try {
            Log.i(TAG, "[procLobbyOpen] == START ==");
            LOG("[procLobbyOpen] Sock = " + Sock + ", strRemoteSender = " + strRemoteSender);
            LobbyAccessCtrl.ctrlAccessbyCall(Sock, strRemoteSender);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobbyOpen(Sock, strRemoteSender)");
            e.printStackTrace();
            try {
                Sock.close();
            } catch (IOException e1) {
                Log.e(TAG, "[Exception] procLobbyOpen() : Sock.close()");
                e1.printStackTrace();
            }
            return false;
        }
    }

    private static boolean procDeviceReset(Socket Sock, Document Doc) {
        try {
            LOG("[procDeviceReset] == START ==");

            NodeList mNodeList = Doc.getElementsByTagName("guidemap");
            Element mElement = (Element) mNodeList.item(0);
            NamedNodeMap mAttrs = mElement.getAttributes();
            int nAttrCnt = mAttrs.getLength();
            LOG_DEBUG("[procDeviceReset] nAttrCnt = " + nAttrCnt);
            String strType = "";
            Node mAttr = mAttrs.item(0);
            if (!mAttr.getNodeName().equalsIgnoreCase("type")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(false));
            }
            strType = mAttr.getNodeValue();
            if (!(strType.equalsIgnoreCase("now") || strType.equalsIgnoreCase("week") || strType.equalsIgnoreCase("month") || strType.equalsIgnoreCase("disable") || strType.equalsIgnoreCase("reset_accesskey") || strType.equalsIgnoreCase("reset_factorymode"))) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(false));
            }

            LOG("[procDeviceReset] strType [" + strType + "]");

            if (strType.equalsIgnoreCase("now")) {
                sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(true));
                ((MainActivity) mContext).startSystemReboot();
            } else if (strType.equalsIgnoreCase("week")) {
            } else if (strType.equalsIgnoreCase("month")) {
            } else if (strType.equalsIgnoreCase("disable")) {
            } else if (strType.equalsIgnoreCase("reset_accesskey")) {
                if (resetAccessKeyInfo()) sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(true));
                else sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(false));
                ((MainActivity) mContext).startSystemReboot();
            } else if (strType.equalsIgnoreCase("reset_factorymode")) {
                if (setFactoryMode()) sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(true));
                else sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(false));
                ((MainActivity) mContext).startSystemReboot();
            }

            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procDeviceReset()");
            e.printStackTrace();
            sendReply(Sock, MakeiMAPMSG.makeReply_device_reset(false));
            return false;
        }
    }

    private static boolean procSetSetupInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procSetSetupInfo] ===== START =====");
            String[] strDataName = {"facerecog_level", "facerecog_ai", "facerecog_mask", "facerecog_thres_level"};
            ConfigValueTable SetSetupInfo = new ConfigValueTable();
            SetSetupInfo.clearTable();
            int nParamCnt = 0;

            NodeList ItemGuide = Doc.getElementsByTagName("guidemap");
            Node Item = ItemGuide.item(0);

            if (Item != null) {
                NamedNodeMap Attrs = Item.getAttributes();
                for (int i = 0; i < Attrs.getLength(); i++) {
                    Node Attr = Attrs.item(i);

                    if (Attr.getNodeName().equalsIgnoreCase("devtype")) {
                        String devtype = Attr.getNodeValue();
                        if (!devtype.equalsIgnoreCase("lobby")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "devtype"));
                        }
                    } else if (Attr.getNodeName().equalsIgnoreCase("count")) {
                        String count = Attr.getNodeValue();
                        LOG("[procSetSetupInfo] count = " + count);
                        nParamCnt = Integer.parseInt(count);
                        LOG("[procSetSetupInfo] nParamCnt = " + nParamCnt);
                        if (nParamCnt < 1 || 99 < nParamCnt) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "count"));
                        }
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "guidemap"));
                    }
                }
            } else {
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, null));
            }

            NodeList ItemSetting = Doc.getElementsByTagName("setting");
            LOG("[procSetSetupInfo] ItemSetting.getLength [" + ItemSetting.getLength() + "], nParamCnt [" + nParamCnt + "]");
            if (nParamCnt != ItemSetting.getLength()) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "count"));
            }

            String Name = "";
            String Count = "";
            String Value = "";
            for (int i = 0; i < ItemSetting.getLength(); i++) {
                Node SettingInfo = ItemSetting.item(i);
                NamedNodeMap Attrs = SettingInfo.getAttributes();

                for (int j = 0; j < Attrs.getLength(); j++) {
                    Node Attr = Attrs.item(j);
                    if (Attr.getNodeName().equalsIgnoreCase("name")) Name = Attr.getNodeValue();
                    else if (Attr.getNodeName().equalsIgnoreCase("count"))
                        Count = Attr.getNodeValue();
                    else if (Attr.getNodeName().equalsIgnoreCase("value"))
                        Value = Attr.getNodeValue();
                    else
                    {
                        //strDataName
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "syntax"));
                    }

                }

                if (Name.equalsIgnoreCase("line_info")) {
                    // 라인정보를 해당 클래스에 정리한다.
                    // 예) 순서:라인번호=저층-고층/순서:라인번호=저층-고층
                    LineInfoTable SetSetupLine = new LineInfoTable();
                    SetSetupLine.clearTable();
                    int nLineCnt = Integer.parseInt(Count);
                    if (0 > nLineCnt)
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "line_info"));

                    Element SubList = (Element) SettingInfo;
                    NodeList SubItem = SubList.getElementsByTagName("list");
                    for (int k = 0; k < nLineCnt; k++) {
                        Node LineInfoUnit = SubItem.item(k);
                        String[] strParsed = getAttributeValue(LineInfoUnit, "value").split("-");
                        SetSetupLine.addLineTable(Integer.parseInt(getAttributeValue(LineInfoUnit, "name")), Integer.parseInt(strParsed[0].trim()), Integer.parseInt(strParsed[1].trim()), null);
                    }
                    SetSetupInfo.addConfigValue(Name, Count, Value, SetSetupLine, null);
                }
                else if(Name.equalsIgnoreCase("facerecog_level"))
                {
                    ((MainActivity) mContext).setDataInfo(1, Value);
                }
                else if(Name.equalsIgnoreCase("facerecog_ai"))
                {
                    ((MainActivity) mContext).setDataInfo(2, Value);
                }
                else if(Name.equalsIgnoreCase("facerecog_mask"))
                {
                    ((MainActivity) mContext).setDataInfo(3, Value);
                }
                else if(Name.equalsIgnoreCase("facerecog_thres_level"))
                {
                    ((MainActivity) mContext).setDataInfo(4, Value);
                }
                else if(Name.equalsIgnoreCase("facerecog_ae_mode"))
                {
                    ((MainActivity) mContext).setDataInfo(5, Value); // 0, 1
                }
                else {

                    SetSetupInfo.addConfigValue(Name, Count, Value, null, null);
                }



            }
//            SetSetupInfo.printValue();
            String strCheckResult = checkRcvdSetupInfo(SetSetupInfo);
            if (strCheckResult.equalsIgnoreCase("OK")) {
                strCheckResult = Configure.setSetSetupInfo(SetSetupInfo);
                if (strCheckResult.equalsIgnoreCase("OK")) {
                    if (Configure.setDBConfigValues()) {
                        Configure.getDBConfigValues();
                        STDAccessKeyManager.procLineInfoChanged();   // 세대카드정보를 갱신된 라인정보에 맞추어 갱신합니다.
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(true, null));
                    } else {
                        Configure.getDBConfigValues();
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "database"));
                    }
                }
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, strCheckResult));
            } else
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, strCheckResult));

        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSetSetupInfo(Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_set_setup_info(false, "Exception"));
        }
    }

    private static boolean procGetSetupInfo(Socket Sock, Document Doc) {

        try {
            LOG("[procGetSetupInfo] ===== START =====");
            int nParamCnt = 0;
            int[] iDataInfo = {0,0,0,0};

            NodeList mNodeList = Doc.getElementsByTagName("guidemap");
            Node mNode = mNodeList.item(0);

            if (mNode != null) {
                NamedNodeMap Attrs = mNode.getAttributes();
                for (int i = 0; i < Attrs.getLength(); i++) {
                    Node Attr = Attrs.item(i);
                    if (Attr.getNodeName().equalsIgnoreCase("devtype")) {
                        String devtype = Attr.getNodeValue();
                        if (DeviceInfo.Type == DeviceType.LOBBY) {
                            if (!devtype.equalsIgnoreCase("lobby")) {
                                //LOG("[procGetSetupInfo] ===== START ===== " + Attr.getChildNodes().getLength());
                                LOG("[procGetSetupInfo] 1");
                                return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "devtype", null, iDataInfo));
                            }
                        } else if (DeviceInfo.Type == DeviceType.GUARD) {
                            if (!devtype.equalsIgnoreCase("guard")) {
                                LOG("[procGetSetupInfo] 2");
                                return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "devtype", null, iDataInfo));
                            }
                        } else {
                            LOG("[procGetSetupInfo] 3");
                            return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "devtype", null, iDataInfo));
                        }
                    } else if (Attr.getNodeName().equalsIgnoreCase("count")) {
                        String strParamCnt = Attr.getNodeValue();
                        LOG("[procGetSetupInfo] strParamCnt = " + strParamCnt);
                        nParamCnt = Integer.parseInt(strParamCnt);
                        if (nParamCnt < 1 || 99 < nParamCnt) {
                            LOG("[procGetSetupInfo] 4");
                            return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "count", null, iDataInfo));
                        }
                    } else {
                        LOG("[procGetSetupInfo] 5");
                        return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "count", null, iDataInfo));
                    }
                }   // end of for

                mNodeList = Doc.getElementsByTagName("variable");
                LOG("[procGetSetupInfo] mNodeList.getLength [" + mNodeList.getLength() + "], nParamCnt [" + nParamCnt + "]");

                iDataInfo = ((MainActivity) mContext).getDataInfo();
                boolean[] bCheckData = {false, false, false, false};



//                for(int i = 0; i < mNodeList.getLength(); i++)
//                {
//                    LOG("[procGetSetupInfo] getNodeName " + mNodeList.item(i).);
//                    if(mNodeList.item(i).getNodeName().equalsIgnoreCase("variable"))
//                    {
//                        if(mNodeList.item(i).getNodeValue().equalsIgnoreCase("facerecog_level"))
//                        {
//                            bCheckData[0] = true;
//                        }
//                        else if(mNodeList.item(i).getNodeValue().equalsIgnoreCase("facerecog_ai"))
//                        {
//                            bCheckData[1] = true;
//                        }
//                        else if(mNodeList.item(i).getNodeValue().equalsIgnoreCase("facerecog_mask"))
//                        {
//                            bCheckData[2] = true;
//                        }
//                        else if(mNodeList.item(i).getNodeValue().equalsIgnoreCase("facerecog_thres_level"))
//                        {
//                            bCheckData[3] = true;
//                        }
//                    }
//
//                }
//
//                for(int i = 0; i < iDataInfo.length; i++)
//                {
//                    if(!bCheckData[i])
//                        iDataInfo[i] = 0;
//                }

                if (nParamCnt != mNodeList.getLength()) {
                    LOG("[procGetSetupInfo] 6");

                    return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, "count", null, iDataInfo));
                }

                ConfigValueTable GetSetupValue = new ConfigValueTable();
                GetSetupValue.clearTable();
                for (int i = 0; i < nParamCnt; i++) {
                    Element mElement = (Element) mNodeList.item(i);
                    GetSetupValue.addConfigValue(trimQuotation(mElement.getTextContent()), "", "", "", "");
//                    LOG_DEBUG("[procGetSetupInfo] item[" + i + "] = " + trimQuotation(mElement.getTextContent()));
                }
//                mConfigValueTable.printValue();
                LOG("[procGetSetupInfo] 7");
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(true, null, setGetSetupInfoValue(GetSetupValue), iDataInfo));
            } else {
                LOG("[procGetSetupInfo] 8");
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, null, null, iDataInfo));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procGetSetupInfo(Sock, Doc)");
            e.printStackTrace();
            int[] iDataInfo = {0,0,0,0};
            return sendReply(Sock, MakeiMAPMSG.makeReply_get_setup_info(false, null, null, iDataInfo));
        }
    }

    private static ConfigValueTable setGetSetupInfoValue(ConfigValueTable GetSetupInfo) {
        try {
            LOG("[setGetSetupInfoValue] ===== START =====");
            ConfigValueTable mConfigValueTable = new ConfigValueTable();
            mConfigValueTable.clearTable();
            int nCnt = GetSetupInfo.getCnt();
            for (int i = 0; i < nCnt; i++) {
                String Name = GetSetupInfo.getConfigValue(i).NAME();
                String Value = "";
                if (Name.equalsIgnoreCase("ver_setting")) {
                    Value = ConfigValue.strVer_SETTING;
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("guard_id")) {
                    LOG_DEBUG("[setGetSetupInfoValue] ConfigValue.strGuardIPAddress [" + ConfigValue.strGuardIPAddress + "]");
                    if (ConfigValue.eIPExpansion == SetValue.TRUE) {
                        Value = ConfigValue.strGuardIPAddress;
                    }
                    else {
                        String[] strParsed = ConfigValue.strGuardIPAddress.split("\\.");
                        Value = strParsed[1] + Global.ZeroPadding_Head(2, strParsed[3]);
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("site_code")) {
                    Value = ConfigValue.strSitecode;
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("use_passwd")) {
                    if (ConfigValue.eAccessPWSelected == SetValue.PublicAccessPW_Selected) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eAccessPWSelected == SetValue.ResiAccessPW_Selected) {
                        Value = "disable";
                    }
                    else if (ConfigValue.eAccessPWSelected == SetValue.PW_Unselected) {
                        Value = "none";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("use_adminpw")) {
                    if (ConfigValue.eAdminAccessPWEnable == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eAdminAccessPWEnable == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("rf_level")) {
                    if (ConfigValue.eRFAccessLevel == SetValue.Level_Line) {
                        Value = "line";
                    }
                    else if (ConfigValue.eRFAccessLevel == SetValue.Level_Dong) {
                        Value = "dong";
                    }
                    else if (ConfigValue.eRFAccessLevel == SetValue.Level_Entire) {
                        Value = "all";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("open_duration")) {
                    Value = Integer.toString(ConfigValue.nDoorOpenDuration);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("guard_bell")) {
                    if (ConfigValue.eCallMelody_Guard == SetValue.Bell_1) {
                        Value = "1";
                    }
                    else if (ConfigValue.eCallMelody_Guard == SetValue.Bell_2) {
                        Value = "2";
                    }
                    else if (ConfigValue.eCallMelody_Guard == SetValue.Bell_3) {
                        Value = "3";
                    }
                    else if (ConfigValue.eCallMelody_Guard == SetValue.Bell_4) {
                        Value = "4";
                    }
                    else if (ConfigValue.eCallMelody_Guard == SetValue.Bell_5) {
                        Value = "5";
                    }
                    else if (ConfigValue.eCallMelody_Guard == SetValue.Bell_6) {
                        Value = "6";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("home_bell")) {
                    if (ConfigValue.eCallMelody_Resi == SetValue.Bell_1) {
                        Value = "1";
                    }
                    else if (ConfigValue.eCallMelody_Resi == SetValue.Bell_2) {
                        Value = "2";
                    }
                    else if (ConfigValue.eCallMelody_Resi == SetValue.Bell_3) {
                        Value = "3";
                    }
                    else if (ConfigValue.eCallMelody_Resi == SetValue.Bell_4) {
                        Value = "4";
                    }
                    else if (ConfigValue.eCallMelody_Resi == SetValue.Bell_5) {
                        Value = "5";
                    }
                    else if (ConfigValue.eCallMelody_Resi == SetValue.Bell_6) {
                        Value = "6";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("line_info")) {
                    LineInfoTable mLineInfoTable = new LineInfoTable();
                    mLineInfoTable.clearTable();
                    int nLineCnt = ConfigValue.LineInfo.getCnt();
                    for (int j = 0; j < nLineCnt; j++) {
                        mLineInfoTable.addLineTable(ConfigValue.LineInfo.getLineInfo(j).NAME(), ConfigValue.LineInfo.getLineInfo(j).LOW(), ConfigValue.LineInfo.getLineInfo(j).HIGH(), null);
                    }
                    mConfigValueTable.addConfigValue(Name, Integer.toString(nLineCnt), "list", mLineInfoTable, null);
                }
                else if (Name.equalsIgnoreCase("ip_table")) {
                    LOG_DEBUG("[setGetSetupInfoValue] Not supported item!! [" + Name + "]");
                }
                else if (Name.equalsIgnoreCase("call_level")) {
                    if (ConfigValue.eCallLevel == SetValue.Level_Line) {
                        Value = "line";
                    }
                    else if (ConfigValue.eCallLevel == SetValue.Level_Dong) {
                        Value = "dong";
                    }
                    else if (ConfigValue.eCallLevel == SetValue.Level_Entire) {
                        Value = "all";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("log_enable")) {
                    if (ConfigValue.eLogSend == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eLogSend == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("datetime")) {
                    Calendar Cal = Calendar.getInstance();

                    int nYear = Cal.get(Calendar.YEAR);   // 년도
                    int nMonth = Cal.get(Calendar.MONTH);   // 월 (1월 ~ 12월 : 0 ~ 11)
                    int nDate = Cal.get(Calendar.DATE);   // 일
                    int nHour = Cal.get(Calendar.HOUR_OF_DAY);   // 시
                    int nMin = Cal.get(Calendar.MINUTE);   // 분
                    Value = Global.ZeroPadding_Head(4, Integer.toString(nYear)) + Global.ZeroPadding_Head(2, Integer.toString(nMonth + 1)) + Global.ZeroPadding_Head(2, Integer.toString(nDate))
                            + Global.ZeroPadding_Head(2, Integer.toString(nHour)) + Global.ZeroPadding_Head(2, Integer.toString(nMin));
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("cam_pwrdur")) {
                    LOG("[setGetSetupInfoValue] ConfigValue.eCamPowerCtrl [" + ConfigValue.eCamPowerCtrl + "]");
                    if (ConfigValue.eCamPowerCtrl == SetValue.CamPower_Calling) {
                        Value = "calling";
                    }
                    else if (ConfigValue.eCamPowerCtrl == SetValue.CamPower_Always) {
                        Value = "always";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("role")) {
                    if (ConfigValue.eLPRole == SetValue.LPRole_Lobby) {
                        Value = "lobby";
                    }
                    else if (ConfigValue.eLPRole == SetValue.LPRole_CarGate) {
                        Value = "cargate";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("beat_time")) {
                    Value = Integer.toString(ConfigValue.nHearBeatTime);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("statusbeat_time")) {
                    Value = Integer.toString(ConfigValue.nStatusBeatTime);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("lcd_eco_time")) {
                    Value = Integer.toString(ConfigValue.nECOTime);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("security_link")) {
                    if (ConfigValue.eACULinkEnable == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eACULinkEnable == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("package_link")) {
                    if (ConfigValue.eParcelLink == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eParcelLink == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("external_onepass_link")) {
                    if (ConfigValue.eExternalOnePassLink == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eExternalOnePassLink == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("mainserver_type")) {
                    if (ConfigValue.eMainServerType == SetValue.Server2dot0_Integrated) {
                        Value = "integrated2";
                    }
                    else if (ConfigValue.eMainServerType == SetValue.Server_Separated) {
                        Value = "separated";
                    }
                    else if (ConfigValue.eMainServerType == SetValue.Server_Integrated) {
                        Value = "integrated";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("cardreader_type")) {
                    if (ConfigValue.eCardReaderType == SetValue.CardReaderType_Standard) {
                        Value = "standard";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("dooropendelay")) {
                    Value = Integer.toString(ConfigValue.nFireSensorDoorOpenDelay);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("keyserver")) {
                    Value = ConfigValue.strKeyServerIPAddress;
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("elevdisabledfloors")) {
                    Value = Configure.makeElevDisableFloorString(ConfigValue.ElevDisable);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("ip_expansion")) {
                    if (ConfigValue.eIPExpansion == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eIPExpansion == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("house_division")) {
                    if (ConfigValue.eHouseDivision == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eHouseDivision == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("smartautodoor_link")) {
                    if (ConfigValue.eSmartAutodoorLink == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eSmartAutodoorLink == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("insidesensor_link")) {
                    if (ConfigValue.eInsideSensorLink == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eInsideSensorLink == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("fingerprint_link")) {
                    if (ConfigValue.eFingerPrintLink == SetValue.TRUE) {
                        Value = "enable";
                    }
                    else if (ConfigValue.eFingerPrintLink == SetValue.FALSE) {
                        Value = "disable";
                    }
                    else {
                        Value = "error";
                    }
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("ble_strength")) {
                    Value = Integer.toString(ConfigValue.nBLEStrength);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("onepass_rssi_index")) {
                    Value = ConfigValue.strOnePassRSSI;
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
                else if (Name.equalsIgnoreCase("onepass_available_time")) {
                    Value = Integer.toString(ConfigValue.nOnePassAvailableTime);
                    LOG_DEBUG("[setGetSetupInfoValue] Name [" + Name + "], Value [" + Value + "]");
                    mConfigValueTable.addConfigValue(Name, null, Value, null, null);
                }
            }
            return mConfigValueTable;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setGetSetupInfoValue(mConfigValueTable)");
            e.printStackTrace();
            return null;
        }
    }

    // set_setup_info.request로 수신한 설정정보의 내용을 검사한다.
    private static String checkRcvdSetupInfo(ConfigValueTable SetupInfo) {
        try {
            if (SetupInfo == null) return "ERROR";
            LOG("[checkRcvdSetupInfo] SetupInfo.getCnt() [" + SetupInfo.getCnt() + "]");
            String strResult = "ERROR";
            String strCheckName = "";
            String strCheckValue = "";
            int nCheckValue = 0;
            int nItemCnt = SetupInfo.getCnt();

            for (int i = 0; i < nItemCnt; i++) {

                strCheckName = SetupInfo.getConfigValue(i).NAME();
                strCheckValue = SetupInfo.getConfigValue(i).VALUE();
                strResult = strCheckName;

                if (strCheckName.equalsIgnoreCase("guard_id")) {
                    try {
                        if (ConfigValue.eIPExpansion == SetValue.TRUE) {
                            if (!validateIPAddress(strCheckValue)) {
                                return strResult;
                            }
                        }
                        else {
                            if (strCheckValue.length() < 3 || 4 < strCheckValue.length()) {
                                return strResult;
                            }
                            nCheckValue = Integer.parseInt(strCheckValue);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("use_passwd")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable") || strCheckValue.equalsIgnoreCase("none"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("use_adminpw")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("rf_level")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("line") || strCheckValue.equalsIgnoreCase("dong") || strCheckValue.equalsIgnoreCase("all"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("open_duration")) {
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 1 || 99 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("guard_bell")) {
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 1 || 6 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("home_bell")) {
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 1 || 6 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("datetime")) {
                    try {
                        if (strCheckValue.length() != 14) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("call_level")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("line") || strCheckValue.equalsIgnoreCase("dong") || strCheckValue.equalsIgnoreCase("all"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("log_enable")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("cam_pwrdur")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("calling") || strCheckValue.equalsIgnoreCase("always"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("role")) {
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("lobby") || strCheckValue.equalsIgnoreCase("cargate"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("site_code")) {
                    try {
                        if (strCheckValue.length() != 8) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("line_info")) {
                    try {
                        int nLineCnt = Integer.parseInt(SetupInfo.getConfigValue(i).CNT());
                        if (!strCheckValue.equalsIgnoreCase("list")) return strResult;
                        if (nLineCnt < 1 || 99 < nLineCnt) return strResult;
                        LineInfoTable mLineInfoTable = new LineInfoTable();
                        mLineInfoTable = (LineInfoTable) SetupInfo.getConfigValue(i).LIST();

                        if (nLineCnt != mLineInfoTable.getCnt()) return strResult;

                        int nLineName;
                        int nLowFloor;
                        int nHighFloor;
                        for (int j = 0; j < nLineCnt; j++) {
                            nLineName = mLineInfoTable.getLineInfo(j).NAME();   // 라인번호 검증
                            nLowFloor = mLineInfoTable.getLineInfo(j).LOW();   // 저층 검증
                            nHighFloor = mLineInfoTable.getLineInfo(j).HIGH();   // 고층 검증
                            if (nLineName < 1 || 99 < nLineName) return strResult;
                            if (nLowFloor < 1 || 99 < nLowFloor) return strResult;
                            if (nHighFloor < 1 || 99 < nHighFloor) return strResult;
                            if (nHighFloor < nLowFloor) return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("ip_table")) {
                    strResult = "ip_table";
                    try {
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("ver_setting")) {
                    strResult = "ver_setting";
                    try {
                        if (strCheckValue.length() != 13) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("beat_time")) {
                    strResult = "beat_time";
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 1 || 99 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("statusbeat_time")) {
                    strResult = "statusbeat_time";
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 1 || 99999999 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("lcd_eco_time")) {
                    strResult = "lcd_eco_time";
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 0 || 99 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("security_link")) {
                    strResult = "security_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("package_link")) {
                    strResult = "package_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("external_onepass_link")) {
                    strResult = "external_onepass_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("mainserver_type")) {
                    strResult = "mainserver_type";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("integrated") || strCheckValue.equalsIgnoreCase("separated") || strCheckValue.equalsIgnoreCase("integrated2"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("cardreader_type")) {
                    strResult = "cardreader_type";
                    try {
                        if (!strCheckValue.equalsIgnoreCase("standard")) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("dooropendelay")) {
                    strResult = "dooropendelay";
                    try {
                        nCheckValue = Integer.parseInt(strCheckValue);
                        if (nCheckValue < 0 || 99 < nCheckValue) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("keyserver")) {
                    strResult = "keyserver";
                    try {
                        String[] strSplited = strCheckValue.split("\\.");
                        if (strSplited.length != 4) {
                            return strResult;
                        }
                        int[] nSplited = new int[4];
                        for (int j = 0; j < nSplited.length; j++) {
                            nSplited[j] = Integer.parseInt(strSplited[j]);
                        }

                        if (nSplited[0] < 0 || 255 < nSplited[0]) return strResult;
                        if (nSplited[1] < 0 || 255 < nSplited[1]) return strResult;
                        if (nSplited[2] < 0 || 255 < nSplited[2]) return strResult;
                        if (nSplited[3] < 0 || 255 < nSplited[3]) return strResult;
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("elevdisabledfloors")) {
                    strResult = "elevdisabledfloors";
                    try {
                        String[] strSplited = strCheckValue.split(":");
                        if (strSplited.length != 2) return strResult;
                        int nFloorCnt = Integer.parseInt(strSplited[0]);
                        String[] str2ndSplited = strSplited[1].split("/");
                        if (nFloorCnt != str2ndSplited.length) return strResult;
                        int[] n2ndSplited = new int[nFloorCnt];
                        for (int j = 0; j < n2ndSplited.length; j++) {
                            n2ndSplited[j] = Integer.parseInt(str2ndSplited[j]);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("ip_expansion")) {
                    strResult = "ip_expansion";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("house_division")) {
                    strResult = "house_division";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("smartautodoor_link")) {
                    strResult = "smartautodoor_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("insidesensor_link")) {
                    strResult = "insidesensor_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("fingerprint_link")) {
                    strResult = "fingerprint_link";
                    try {
                        if (!(strCheckValue.equalsIgnoreCase("enable") || strCheckValue.equalsIgnoreCase("disable"))) {
                            return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("ble_strength")) {
                    strResult = "ble_strength";
                    try {
                        int temp = Integer.parseInt(strCheckValue);
                        if (temp < 0 || 20 < temp) return strResult;
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
                else if (strCheckName.equalsIgnoreCase("onepass_rssi_index")) {
                    strResult = "onepass_rssi_index";
                    try {
                        String[] strParsed = strCheckValue.split("/");
                        if (strParsed.length != 3) return strResult;

                        for (int j = 0; j < strParsed.length; j++) {
                            int temp = Integer.parseInt(strParsed[i]);
                            if (temp < 0 || 20 < temp) return strResult;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo) : " + strResult);
                        e.printStackTrace();
                        return strResult;
                    }
                }
            }
            return "OK";
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkRcvdSetupInfo(SetupInfo)");
            e.printStackTrace();
            return "ERROR";
        }
    }

    private static boolean procSetAccessKeyInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procSetAccessKeyInfo] == START ==");
            Global.setEnableDeviceInput(false);

            String strActoin = "null";
            String strKeyType = "null";
            String strDong = "0";
            String strHo = "0";
            String strCount = "0";

            NodeList mNodeList = Doc.getElementsByTagName("action");
            Element mElement = (Element) mNodeList.item(0);
            strActoin = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSetAccessKeyInfo] strActoin = " + strActoin);
            if (!(strActoin.equalsIgnoreCase("save") || strActoin.equalsIgnoreCase("delete") || strActoin.equalsIgnoreCase("renew"))) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            }

            mNodeList = Doc.getElementsByTagName("key_type");
            mElement = (Element) mNodeList.item(0);
            strKeyType = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSetAccessKeyInfo] strKeyType = " + strKeyType);
            if (!(strKeyType.equalsIgnoreCase("hkey") || strKeyType.equalsIgnoreCase("dkey") || strKeyType.equalsIgnoreCase("mkey"))) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            }

            if (strKeyType.equalsIgnoreCase("hkey")) {
                mNodeList = Doc.getElementsByTagName("hkey_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetAccessKeyInfo] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strDong = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("ho")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strHo = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("dkey")) {
                mNodeList = Doc.getElementsByTagName("dkey_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetAccessKeyInfo] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strDong = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("mkey")) {
                mNodeList = Doc.getElementsByTagName("mkey_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetAccessKeyInfo] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                    }
                }
            } else {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            }
            LOG_DEBUG("[procSetAccessKeyInfo] strDong = " + strDong + ", strHo = " + strHo + ", strCount = " + strCount);

            mNodeList = Doc.getElementsByTagName("key_info");
            int nKeyCnt = mNodeList.getLength();
            LOG_DEBUG("[procSetAccessKeyInfo] nKeyCnt = " + nKeyCnt);
            if (nKeyCnt != Integer.parseInt(strCount)) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            }
            String strIndex = "0", strSN = "null", strUID = "null", strKeyNum = "0", strIssueCnt = "0", strIssuedDate = "null", strMessage = "null";
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();
            for (int i = 0; i < nKeyCnt; i++) {
                mElement = (Element) mNodeList.item(i);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
//                LOG_DEBUG("[procSetAccessKeyInfo] nAttrCnt = " + nAttrCnt);
                for (int j = 0; j < nAttrCnt; j++) {
                    Node mAttr = mAttrs.item(j);
                    if (j == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("index")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strIndex = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strIndex = " + strIndex);
                    } else if (j == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("sn")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strSN = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strSN = " + strSN);
                    } else if (j == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("uid")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strUID = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strUID = " + strUID);
                    } else if (j == 3) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("key_num")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strKeyNum = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strKeyNum = " + strKeyNum);
                    } else if (j == 4) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("issue_cnt")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strIssueCnt = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strIssueCnt = " + strIssueCnt);
                    } else if (j == 5) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("issued_date")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strIssuedDate = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strIssuedDate = " + strIssuedDate);
                    } else if (j == 6) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("message")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                        }
                        strMessage = mAttr.getNodeValue().trim();
//                        LOG_DEBUG("[procSetAccessKeyInfo] strMessage = " + strMessage);
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                    }
                }
                LOG_DEBUG("[procSetAccessKeyInfo] i = " + i + ", strIndex = " + strIndex + ", strSN = " + strSN + ", strUID = " + strUID + ", strKeyNum = " + strKeyNum + ", strIssueCnt = " + strIssueCnt + ", strIssuedDate = " + strIssuedDate + ", strMessage = " + strMessage);

                // 수신된 데이터에 중복된 UID가 있는지 검사한다.
                for (int j = 0; j < KeyInfoTable.getCnt(); j++) {
                    if (KeyInfoTable.getKeyInfo(j).getUID().equalsIgnoreCase(strUID)) {
                        Log.w(TAG, "[procSetAccessKeyInfo] Duplicated UID in the message!! [" + strUID + "]");
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
                    }
                }

                KeyInfoTable.addKeyInfo(strIndex, strSN, strUID, strKeyType, ConfigValue.strSitecode, strDong, strHo, strKeyNum, strIssueCnt, strIssuedDate, strIssuedDate, strMessage);


            }
            if (UIDAccessKeyManager.onSetAccessKeyInfo(strActoin, strKeyType, strDong, strHo, KeyInfoTable)) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(true));
            } else {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSetAccessKeyInfo(Socket Sock, Document Doc)");
            e.printStackTrace();
            boolean bResult = sendReply(Sock, MakeiMAPMSG.makeReply_set_accesskey_info(false));
            Global.setEnableDeviceInput(true);
            return bResult;
        }
    }

    private static boolean procGetAccessKeyInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procGetAccessKeyInfo] == START ==");

            String strInfoType = "null";

            NodeList mNodeList = Doc.getElementsByTagName("info_type");
            Element mElement = (Element) mNodeList.item(0);
            strInfoType = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procGetAccessKeyInfo] strInfoType = " + strInfoType);
            if (!(strInfoType.equalsIgnoreCase("uid") || strInfoType.equalsIgnoreCase("sn") || strInfoType.equalsIgnoreCase("uid_all") ||
                    strInfoType.equalsIgnoreCase("sn_all") || strInfoType.equalsIgnoreCase("hkey") || strInfoType.equalsIgnoreCase("dkey") || strInfoType.equalsIgnoreCase("mkey"))) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(false, null));
            }

            if (strInfoType.equals("uid")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableByUID(Doc)));
            } else if (strInfoType.equals("sn")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableBySN(Doc)));
            } else if (strInfoType.equals("uid_all")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableByUIDALL(Doc)));
            } else if (strInfoType.equals("sn_all")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableBySNALL(Doc)));
            } else if (strInfoType.equals("hkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableByHKEY(Doc)));
            } else if (strInfoType.equals("dkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableByDKEY(Doc)));
            } else if (strInfoType.equals("mkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(true, getKeyTableByMKEY(Doc)));
            } else {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(false, null));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procGetAccessKeyInfo(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_get_accesskey_info(false, null));
        }
    }

    private static AccessKeyInfoTable getKeyTableByUID(Document Doc) {
        try {
            LOG("[getKeyTableByUID] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("uid_info");
            Element mElement = (Element) mNodeList.item(0);
            String strUIDInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableByUID] strUIDInfo = " + strUIDInfo);
            return UIDAccessKeyManager.getKeyInfoByUID(strUIDInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByUID(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableBySN(Document Doc) {
        try {
            LOG("[getKeyTableBySN] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("sn_info");
            Element mElement = (Element) mNodeList.item(0);
            String strSNInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableBySN] strSNInfo = " + strSNInfo);
            return UIDAccessKeyManager.getKeyInfoBySN(strSNInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableBySN(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByUIDALL(Document Doc) {
        try {
            LOG("[getKeyTableByUIDALL] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("uid_info");
            Element mElement = (Element) mNodeList.item(0);
            String strUIDInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableByUIDALL] strUIDInfo = " + strUIDInfo);
            return UIDAccessKeyManager.getKeyInfoByUIDALL(strUIDInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByUIDALL(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableBySNALL(Document Doc) {
        try {
            LOG("[getKeyTableBySNALL] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("sn_info");
            Element mElement = (Element) mNodeList.item(0);
            String strSNInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableBySNALL] strSNInfo = " + strSNInfo);
            return UIDAccessKeyManager.getKeyInfoBySNALL(strSNInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableBySNALL(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByHKEY(Document Doc) {
        try {
            LOG("[getKeyTableByHKEY] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("hkey_info");
            Node mNode = mNodeList.item(0);
            NamedNodeMap Attrs = mNode.getAttributes();
            String strDong = "", strHo = "", strNo = "";
            for (int i = 0; i < Attrs.getLength(); i++) {
                Node Attr = Attrs.item(i);
                if (Attr.getNodeName().equalsIgnoreCase("dong")) {
                    strDong = Attr.getNodeValue();
                } else if (Attr.getNodeName().equalsIgnoreCase("ho")) {
                    strHo = Attr.getNodeValue();
                } else if (Attr.getNodeName().equalsIgnoreCase("num")) {
                    strNo = Attr.getNodeValue();
                }
            }
            LOG_DEBUG("[getKeyTableByHKEY] strDong [" + strDong + "], strHo [" + strHo + "], strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByHKEY(strDong, strHo, strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByHKEY(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByDKEY(Document Doc) {
        try {
            LOG("[getKeyTableByDKEY] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("dkey_info");
            Node mNode = mNodeList.item(0);
            NamedNodeMap Attrs = mNode.getAttributes();
            String strDong = "", strNo = "";
            for (int i = 0; i < Attrs.getLength(); i++) {
                Node Attr = Attrs.item(i);
                if (Attr.getNodeName().equalsIgnoreCase("dong")) {
                    strDong = Attr.getNodeValue();
                } else if (Attr.getNodeName().equalsIgnoreCase("num")) {
                    strNo = Attr.getNodeValue();
                }
            }
            LOG_DEBUG("[getKeyTableByDKEY] strDong [" + strDong + "], strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByDKEY(strDong, strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByDKEY(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByMKEY(Document Doc) {
        try {
            LOG("[getKeyTableByMKEY] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("mkey_info");
            Node mNode = mNodeList.item(0);
            NamedNodeMap Attrs = mNode.getAttributes();
            String strNo = "";
            Node Attr = Attrs.item(0);
            if (Attr.getNodeName().equalsIgnoreCase("num")) {
                strNo = Attr.getNodeValue();
            }
            LOG_DEBUG("[getKeyTableByMKEY] strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByMKEY(strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByMKEY(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static boolean procCheckSIPActivation(Socket Sock, Document Doc) {
        try {
            LOG("[procCheckSIPActivation] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("message");
            Element mElement = (Element) mNodeList.item(0);
            String strMessage = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procCheckSIPActivation] strMessage = " + strMessage);
            if (MainActivity.mCallController.checkSIPActivation())
                return sendReply(Sock, MakeiMAPMSG.makeReply_check_sip_activation(true));
            else return sendReply(Sock, MakeiMAPMSG.makeReply_check_sip_activation(false));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procCheckSIPActivation(Sock, Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_check_sip_activation(false));
        }
    }

    private static boolean procLobby_Sync_CardInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procLobby_Sync_CardInfo] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("home_addr");
            Element mElement = (Element) mNodeList.item(0);
            String strHomeAddr = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_CardInfo] strHomeAddr = " + strHomeAddr);

            mNodeList = Doc.getElementsByTagName("card_gen_cnt");
            mElement = (Element) mNodeList.item(0);
            String strIssueCnt = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_CardInfo] strIssueCnt = " + strIssueCnt);
            int nIssueCnt = Integer.parseInt(strIssueCnt);

            String[] strParse = strHomeAddr.split("\\.");
            if ((Integer.parseInt(ConfigValue.strDevice_Dong) % 100) != Integer.parseInt(strParse[1])) {
                LOG_INFO("[procLobby_Sync_CardInfo] Dong is different!!");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_cardinfo(false, strHomeAddr, nIssueCnt));
            }

            int nDong = Integer.parseInt(ConfigValue.strDevice_Dong);
            String strHo_Tail = Integer.toString(Integer.parseInt(strParse[3]) % 100);
            if (strHo_Tail.length() == 1) strHo_Tail = "0" + strHo_Tail;
            int nHo = (Integer.parseInt(strParse[2]) * 100) + Integer.parseInt(strHo_Tail);
            if (!Global.isExistResi(nHo)) {
                LOG_INFO("[procLobby_Sync_CardInfo] Resi is not exist!! [" + nHo + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_cardinfo(false, strHomeAddr, nIssueCnt));
            }

            if (!STDAccessKeyManager.updateResiAccessKey(nDong, nHo, nIssueCnt)) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_cardinfo(false, strHomeAddr, nIssueCnt));
            }
            sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_cardinfo(true, strHomeAddr, nIssueCnt));

            if (ConfigValue.eMainSub == SetValue.Main) {
                return Global.sendSTDRFKeyInfo(AccessValues.card, nDong, nHo, nIssueCnt);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobby_Sync_CardInfo(Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_cardinfo(false, "null", 0));
        }
    }

    private static boolean procLobby_Sync_DCardInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procLobby_Sync_DCardInfo] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("card_number");
            Element mElement = (Element) mNodeList.item(0);
            String strCardNo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_DCardInfo] strCardNo = " + strCardNo);

            mNodeList = Doc.getElementsByTagName("card_gen_cnt");
            mElement = (Element) mNodeList.item(0);
            String strIssueCnt = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_DCardInfo] strIssueCnt = " + strIssueCnt);

            int nIDNo = Integer.parseInt(strCardNo);
            int nIssueCnt = Integer.parseInt(strIssueCnt);
            if (nIDNo < 1 || 99 < nIDNo) {
                LOG_INFO("[procLobby_Sync_DCardInfo] nIDNo is out of range [" + nIDNo + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_dcardinfo(false, nIDNo, nIssueCnt));
            }
            if (nIssueCnt < 1 || 99 < nIssueCnt) {
                LOG_INFO("[procLobby_Sync_DCardInfo] nIssueCnt is out of range [" + nIssueCnt + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_dcardinfo(false, nIDNo, nIssueCnt));
            }

            if (!STDAccessKeyManager.updateDongAdminAccessKey(nIDNo, nIssueCnt)) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_dcardinfo(false, nIDNo, nIssueCnt));
            }
            sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_dcardinfo(true, nIDNo, nIssueCnt));

            if (ConfigValue.eMainSub == SetValue.Main) {
                return Global.sendSTDRFKeyInfo(AccessValues.dcard, 0, nIDNo, nIssueCnt);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobby_Sync_DCardInfo(Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_dcardinfo(false, 0, 0));
        }
    }

    private static boolean procLobby_Sync_MCardInfo(Socket Sock, Document Doc) {
        try {
            LOG("[procLobby_Sync_MCardInfo] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("card_number");
            Element mElement = (Element) mNodeList.item(0);
            String strCardNo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_MCardInfo] strCardNo = " + strCardNo);

            mNodeList = Doc.getElementsByTagName("card_gen_cnt");
            mElement = (Element) mNodeList.item(0);
            String strIssueCnt = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procLobby_Sync_MCardInfo] strIssueCnt = " + strIssueCnt);

            int nIDNo = Integer.parseInt(strCardNo);
            int nIssueCnt = Integer.parseInt(strIssueCnt);

            if (nIDNo < 1 || 99 < nIDNo) {
                LOG_INFO("[procLobby_Sync_MCardInfo] nIDNo is out of range [" + nIDNo + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_mcardinfo(false, nIDNo, nIssueCnt));
            }
            if (nIssueCnt < 1 || 99 < nIssueCnt) {
                LOG_INFO("[procLobby_Sync_MCardInfo] nIssueCnt is out of range [" + nIssueCnt + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_mcardinfo(false, nIDNo, nIssueCnt));
            }

            if (!STDAccessKeyManager.updateEntireAdminAccessKey(nIDNo, nIssueCnt)) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_mcardinfo(false, nIDNo, nIssueCnt));
            }
            sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_mcardinfo(true, nIDNo, nIssueCnt));

            if (ConfigValue.eMainSub == SetValue.Main) {
                return Global.sendSTDRFKeyInfo(AccessValues.mcard, 0, nIDNo, nIssueCnt);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobby_Sync_MCardInfo(Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_sync_mcardinfo(false, 0, 0));
        }
    }

    private static boolean procLobbyDoorCtrl(Socket Sock, Document Doc) {
        try {
            LOG("[procLobbyDoorCtrl] ===== START =====");
//            Element EleList = Doc.getDocumentElement();
            NodeList mNodeList = Doc.getElementsByTagName("action");
            Element mElement = (Element) mNodeList.item(0);
            String strAction = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("duration");
            mElement = (Element) mNodeList.item(0);
            String strDuration = trimQuotation(mElement.getTextContent());

            LOG("[procLobbyDoorCtrl] strAction [" + strAction + "], strDuration [" + strDuration + "]");
            if (!(strAction.equalsIgnoreCase("open") || strAction.equalsIgnoreCase("close")))
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_door_ctrl(false));

            int nDuration = 0;
            try {
                nDuration = Integer.parseInt(strDuration);
            } catch (Exception e) {
                Log.e(TAG, "[Exception] procLobbyDoorCtrl(Sock, Doc) : strDuration -> nDuration");
                e.printStackTrace();
                return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_door_ctrl(false));
            }

            if (strAction.equalsIgnoreCase("open")) Global.ctrlLobbyDoorNMent(true, false, nDuration, false, true);
            else Global.ctrlLobbyDoorNMent(false, false, nDuration, false, true);

            return sendReply(Sock, MakeiMAPMSG.makeReply_lobby_door_ctrl(true));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSetSetupInfo(Doc)");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean procChatMessage(Socket Sock, Document Doc) {
        try {
            LOG("[procChatMessage] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("chat");
            Element mElement = (Element) mNodeList.item(0);
            String strChat = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("message");
            mElement = (Element) mNodeList.item(0);
            String strMessage = trimQuotation(mElement.getTextContent());

            LOG("[procChatMessage] strAction [" + strChat + "]");
            LOG("[procChatMessage] strMessage [" + strMessage + "]");

            if (mChatEventListener != null) {
                mChatEventListener.onChatMessage(strChat);
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_message(true));
            }
            return sendReply(Sock, MakeiMAPMSG.makeReply_chat_message(false));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procChatMessage(Socket Sock, Document Doc)");
            e.printStackTrace();
            sendReply(Sock, MakeiMAPMSG.makeReply_chat_message(false));
            return false;
        }
    }

    private static boolean procChatSequence(Socket Sock, Document Doc) {
        try {
            LOG("[procChatSequence] ===== START =====");

            String strFrom = "null", strTo = "", strSeq = "null", strContact = "null", strStatus = "null", strMessage = "null";

            NodeList mNodeList = Doc.getElementsByTagName("from");
            Element mElement = (Element) mNodeList.item(0);
            strFrom = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("to");
            mElement = (Element) mNodeList.item(0);
            strTo = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("contact");
            mElement = (Element) mNodeList.item(0);
            strContact = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("sequence");
            mElement = (Element) mNodeList.item(0);
            strSeq = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("status");
            mElement = (Element) mNodeList.item(0);
            strStatus = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("message");
            mElement = (Element) mNodeList.item(0);
            strMessage = trimQuotation(mElement.getTextContent());

            LOG("[procChatSequence] strFrom [" + strFrom + "], strTo [" + strTo + "]");
            LOG("[procChatSequence] strContact [" + strContact + "]");
            LOG("[procChatSequence] strSeq [" + strSeq + "], strStatus [" + strStatus + "]");
            LOG("[procChatSequence] strMessage [" + strMessage + "]");

            if (!Global.bSTTTTS) {
                Log.w(TAG, "[procChatSequence] Global.bSTTTTS [" + Global.bSTTTTS + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(false, strFrom, strTo, strContact, strSeq, strStatus, strMessage));
            }

            CHATEVENT eChatEvent = CHATEVENT.NONE;
            eChatEvent = makeChatEvent(strSeq);
            if (eChatEvent == CHATEVENT.NONE || eChatEvent == CHATEVENT.ERROR) {
                Log.w(TAG, "[procChatSequence] eChatEvent is not acceptable!! [" + eChatEvent + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(false, strFrom, strTo, strContact, strSeq, strStatus, strMessage));
            }

            CHATSTATUS eChatStatus = CHATSTATUS.NONE;
            eChatStatus = makeChatStatus(strStatus);
            if (eChatStatus == CHATSTATUS.NONE || eChatStatus == CHATSTATUS.ERROR) {
                Log.w(TAG, "[procChatSequence] eChatStatus is not acceptable!! [" + eChatStatus + "]");
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(false, strFrom, strTo, strContact, strSeq, strStatus, strMessage));
            }

            String[] strParse = strContact.split("@");
            String strRemoteSIPID = strParse[0];
            String strRemoteIP = strParse[1];

            if (mChatEventListener != null) {
                mChatEventListener.onChatEvent(getCallType(strRemoteSIPID), strRemoteIP, strRemoteSIPID, eChatEvent, eChatStatus, strMessage);
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(true, strFrom, strTo, strContact, strSeq, strStatus, strMessage));
            } else {
                Log.w(TAG, "[procChatSequence] mChatEventListener is null!!");
                return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(false, strFrom, strTo, strContact, strSeq, strStatus, strMessage));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procChatSequence(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_chat_sequence(false, "error_01", "error_01", "error_01", "error_01", "error_01", "error_01"));
        }
    }

    private static boolean procLobbyOpenOnePass(Socket Sock, Document Doc) {
        try {
            LOG("[procLobbyOpenOnePass] ===== START =====");
            String strAction = "fail", strDong = null, strHo = "null", strName = "null";
            String[] strParse = Sock.getRemoteSocketAddress().toString().split("/");
            String strRemoteIP = strParse[0];
            NodeList mNodeList = Doc.getElementsByTagName("action");
            Element mElement = (Element) mNodeList.item(0);
            strAction = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("access_info");
            mElement = (Element) mNodeList.item(0);
            NamedNodeMap mAttrs = mElement.getAttributes();
            int nAttrCnt = mAttrs.getLength();
            LOG_DEBUG("[procLobbyOpenOnePass] nAttrCnt = " + nAttrCnt);
            for (int i = 0; i < nAttrCnt; i++) {
                Node mAttr = mAttrs.item(i);
                if (i == 0) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                        sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
                    }
                    strDong = mAttr.getNodeValue();
                } else if (i == 1) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("ho")) {
                        sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
                    }
                    strHo = mAttr.getNodeValue();
                } else if (i == 2) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("name")) {
                        sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
                    }
                    strName = mAttr.getNodeValue();
                } else {
                    sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
                }
            }

            mNodeList = Doc.getElementsByTagName("message");
            mElement = (Element) mNodeList.item(0);
            String strMessage = trimQuotation(mElement.getTextContent());

            LOG("[procLobbyOpenOnePass] strAction [" + strAction + "]");
            LOG("[procLobbyOpenOnePass] strDong [" + strDong + "], strHo [" + strHo + "], strName [" + strName + "]");
            LOG("[procLobbyOpenOnePass] strMessage [" + strMessage + "]");
            if (strAction.equalsIgnoreCase("ok")) {
                sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(true));
            }
            else {
                sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
            }

            LobbyAccessCtrl.ctrlAccessOnePass(strAction, strRemoteIP, Integer.parseInt(strDong), Integer.parseInt(strHo), strName, "null");
            return true;

        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLobbyOpenOnePass(Socket Sock, Document Doc)");
            e.printStackTrace();
            sendReply(Sock, MakeiMAPMSG.makeReply_lobby_open_onepass(false));
            return false;
        }
    }

    private static boolean procRemoteDebugConnect(Socket Sock, Document Doc) {
        try {
            LOG("[procRemoteDebugConnect] ===== START =====");
            String strAction = "disable", strID = null, strPW = "null", strDuration = "null", strPort = "null";
            String[] strParse = Sock.getRemoteSocketAddress().toString().split("/");
            String strRemoteIP = strParse[0];
            NodeList mNodeList = Doc.getElementsByTagName("action");
            Element mElement = (Element) mNodeList.item(0);
            strAction = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("account");
            mElement = (Element) mNodeList.item(0);
            NamedNodeMap mAttrs = mElement.getAttributes();
            int nAttrCnt = mAttrs.getLength();
            LOG_DEBUG("[procRemoteDebugConnect] nAttrCnt = " + nAttrCnt);
            for (int i = 0; i < nAttrCnt; i++) {
                Node mAttr = mAttrs.item(i);
                if (i == 0) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("id")) {
                        sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                    }
                    strID = mAttr.getNodeValue();
                }
                else if (i == 1) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("pw")) {
                        sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                    }
                    strPW = mAttr.getNodeValue();
                }
                else {
                    sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                }
            }

            mNodeList = Doc.getElementsByTagName("option");
            mElement = (Element) mNodeList.item(0);
            mAttrs = mElement.getAttributes();
            nAttrCnt = mAttrs.getLength();
            LOG_DEBUG("[procRemoteDebugConnect] nAttrCnt = " + nAttrCnt);
            for (int i = 0; i < nAttrCnt; i++) {
                Node mAttr = mAttrs.item(i);
                if (i == 0) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("duration")) {
                        Global.setEnableDeviceInput(true);
                        sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                    }
                    strDuration = mAttr.getNodeValue();
                }
                else if (i == 1) {
                    if (!mAttr.getNodeName().equalsIgnoreCase("port")) {
                        Global.setEnableDeviceInput(true);
                        sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                    }
                    strPort = mAttr.getNodeValue();
                }
                else {
                    Global.setEnableDeviceInput(true);
                    sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
                }
            }

            mNodeList = Doc.getElementsByTagName("message");
            mElement = (Element) mNodeList.item(0);
            String strMessage = trimQuotation(mElement.getTextContent());

            LOG("[procRemoteDebugConnect] strAction [" + strAction + "]");
            LOG("[procRemoteDebugConnect] strID [" + strID + "], strPW [" + strPW + "], strDuration [" + strDuration + "], strPort [" + strPort + "]");
            LOG("[procRemoteDebugConnect] strMessage [" + strMessage + "]");

            if (!Global.checkRemoteDebugAccount(strID, strPW)) sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));

            if (strAction.equalsIgnoreCase("enable")) {
                sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(true));
                Global.ctrlRemoteDebugConnect(true, strPort, Integer.parseInt(strDuration));
            }
            else if (strAction.equalsIgnoreCase("disable")) {
                sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(true));
                Global.ctrlRemoteDebugConnect(false, strPort, Integer.parseInt(strDuration));
            }
            else {
                sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
            }

            return true;

        } catch (Exception e) {
            Log.e(TAG, "[Exception] procRemoteDebugConnect(Socket Sock, Document Doc)");
            e.printStackTrace();
            sendReply(Sock, MakeiMAPMSG.makeReply_remote_debug_connect(false));
            return false;
        }
    }

    private static boolean procDeviceLiveCheck(Socket Sock, Document Doc) {
        try {
            LOG("[procDeviceLiveCheck] ===== START =====");
            return sendReply(Sock, MakeiMAPMSG.makeReply_device_live_check(true));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procDeviceLiveCheck(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_device_live_check(false));
        }
    }

    private static CALLTYPE getCallType(String strSIPID) {
        try {
            if (strSIPID.contains("_Resi_")) {
                return CALLTYPE.RESIDENCE;
            } else if (strSIPID.contains("_Lobby_")) {
                return CALLTYPE.LOBBY;
            } else if (strSIPID.contains("_Guard_")) {
                return CALLTYPE.GUARD;
            } else {
                return CALLTYPE.NONE;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procChatSequence(Socket Sock, Document Doc)");
            e.printStackTrace();
            return CALLTYPE.NONE;
        }
    }

    private static CHATEVENT makeChatEvent(String strSeq) {
        try {
            if (strSeq.equals(CHATEVENT.TRY.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.TRY;
            else if (strSeq.equals(CHATEVENT.START.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.START;
            else if (strSeq.equals(CHATEVENT.QUIT.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.QUIT;
            else if (strSeq.equals(CHATEVENT.CANCEL.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.CANCEL;
            else if (strSeq.equals(CHATEVENT.OK.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.OK;
            else if (strSeq.equals(CHATEVENT.BUSY.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.BUSY;
            else if (strSeq.equals(CHATEVENT.REJECT.toString().toLowerCase(Locale.getDefault())))
                return CHATEVENT.REJECT;
            else return CHATEVENT.NONE;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] makeChatSeq(String strSeq)");
            e.printStackTrace();
            return CHATEVENT.ERROR;
        }
    }

    private static CHATSTATUS makeChatStatus(String strStatus) {
        try {
            if (strStatus.equals(CHATSTATUS.IDLE.toString().toLowerCase(Locale.getDefault())))
                return CHATSTATUS.IDLE;
            else if (strStatus.equals(CHATSTATUS.RINGING.toString().toLowerCase(Locale.getDefault())))
                return CHATSTATUS.RINGING;
            else if (strStatus.equals(CHATSTATUS.TALKING.toString().toLowerCase(Locale.getDefault())))
                return CHATSTATUS.TALKING;
            else return CHATSTATUS.NONE;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] makeChatSeq(String strSeq)");
            e.printStackTrace();
            return CHATSTATUS.ERROR;
        }
    }

    public static void setChatEventListener(ChatEventListener SIPEV) {
        mChatEventListener = SIPEV;
    }

    public static void releaseChatEventListener() {
        mChatEventListener = null;
    }

    private static boolean procSetKeyInfoCham(Socket Sock, Document Doc) {
        try {
            LOG("[procSetKeyInfoCham] == START ==");
            Global.setEnableDeviceInput(false);

            String strActoin = "null";
            String strKeyType = "null";
            String strDong = "0";
            String strHo = "0";
            String strCount = "0";

            NodeList mNodeList = Doc.getElementsByTagName("action");
            Element mElement = (Element) mNodeList.item(0);
            strActoin = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSetKeyInfoCham] strActoin = " + strActoin);
            if (!(strActoin.equalsIgnoreCase("save") || strActoin.equalsIgnoreCase("delete") || strActoin.equalsIgnoreCase("renew"))) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
            }

            mNodeList = Doc.getElementsByTagName("key_type");
            mElement = (Element) mNodeList.item(0);
            strKeyType = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSetKeyInfoCham] strKeyType = " + strKeyType);

            // 관리PC에서 hcard/dcard/mcard로 보내줘야하나, hkey/dkey/mkey로 보내주고 있기때문에 두 경우 모두 되도록 임시로 보완수정한다. 시흥배곧 한라비발디 2차용(JEFF, 2018.02.28)
            if (bChamLinkSupplementation) {
                Log.w(TAG, "[procSetKeyInfoCham] bChamLinkSupplementation [" + bChamLinkSupplementation + "], Temporary function!!! Revice MonitoringPC of Mobiletalk)");
                if (!(strKeyType.equalsIgnoreCase("hcard") || strKeyType.equalsIgnoreCase("dcard") || strKeyType.equalsIgnoreCase("mcard") || strKeyType.equalsIgnoreCase("hkey") || strKeyType.equalsIgnoreCase("dkey") || strKeyType.equalsIgnoreCase("mkey"))) {
                    Global.setEnableDeviceInput(true);
                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                }
            } else {
                if (!(strKeyType.equalsIgnoreCase("hcard") || strKeyType.equalsIgnoreCase("dcard") || strKeyType.equalsIgnoreCase("mcard"))) {
                    Global.setEnableDeviceInput(true);
                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                }
            }

            if (strKeyType.equalsIgnoreCase("hcard")) {
                mNodeList = Doc.getElementsByTagName("hcard_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strDong = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("ho")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strHo = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("dcard")) {
                mNodeList = Doc.getElementsByTagName("dcard_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strDong = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("mcard")) {
                mNodeList = Doc.getElementsByTagName("mcard_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strCount = mAttr.getNodeValue();
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                    }
                }
            } else {
                // 관리PC에서 hcard/dcard/mcard로 보내줘야하나, hkey/dkey/mkey로 보내주고 있기때문에 두 경우 모두 되도록 임시로 보완수정한다. 시흥배곧 한라비발디 2차용(JEFF, 2018.02.28)
                if (bChamLinkSupplementation) {
                    Log.w(TAG, "[procSetKeyInfoCham] bChamLinkSupplementation [" + bChamLinkSupplementation + "], Temporary function!!! Revice MonitoringPC of Mobiletalk)");
                    if (strKeyType.equalsIgnoreCase("hkey")) {
                        mNodeList = Doc.getElementsByTagName("hkey_info");
                        mElement = (Element) mNodeList.item(0);
                        NamedNodeMap mAttrs = mElement.getAttributes();
                        int nAttrCnt = mAttrs.getLength();
                        LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                        for (int i = 0; i < nAttrCnt; i++) {
                            Node mAttr = mAttrs.item(i);
                            if (i == 0) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strDong = mAttr.getNodeValue();
                            } else if (i == 1) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("ho")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strHo = mAttr.getNodeValue();
                            } else if (i == 2) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strCount = mAttr.getNodeValue();
                            } else {
                                Global.setEnableDeviceInput(true);
                                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                            }
                        }
                    } else if (strKeyType.equalsIgnoreCase("dkey")) {
                        mNodeList = Doc.getElementsByTagName("dkey_info");
                        mElement = (Element) mNodeList.item(0);
                        NamedNodeMap mAttrs = mElement.getAttributes();
                        int nAttrCnt = mAttrs.getLength();
                        LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                        for (int i = 0; i < nAttrCnt; i++) {
                            Node mAttr = mAttrs.item(i);
                            if (i == 0) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("dong")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strDong = mAttr.getNodeValue();
                            } else if (i == 1) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strCount = mAttr.getNodeValue();
                            } else {
                                Global.setEnableDeviceInput(true);
                                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                            }
                        }
                    } else if (strKeyType.equalsIgnoreCase("mkey")) {
                        mNodeList = Doc.getElementsByTagName("mkey_info");
                        mElement = (Element) mNodeList.item(0);
                        NamedNodeMap mAttrs = mElement.getAttributes();
                        int nAttrCnt = mAttrs.getLength();
                        LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                        for (int i = 0; i < nAttrCnt; i++) {
                            Node mAttr = mAttrs.item(i);
                            if (i == 0) {
                                if (!mAttr.getNodeName().equalsIgnoreCase("count")) {
                                    Global.setEnableDeviceInput(true);
                                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                                }
                                strCount = mAttr.getNodeValue();
                            } else {
                                Global.setEnableDeviceInput(true);
                                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                            }
                        }
                    }
                } else {
                    Global.setEnableDeviceInput(true);
                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                }
            }
            LOG_DEBUG("[procSetKeyInfoCham] strDong = " + strDong + ", strHo = " + strHo + ", strCount = " + strCount);

            if (strKeyType.equals("hcard")) strKeyType = "hkey";
            else if (strKeyType.equals("dcard")) strKeyType = "dkey";
            else if (strKeyType.equals("mcard")) strKeyType = "mkey";
            else {
                if (bChamLinkSupplementation) {
                    Log.w(TAG, "[procSetKeyInfoCham] bChamLinkSupplementation [" + bChamLinkSupplementation + "], Temporary function!!! Revice MonitoringPC of Mobiletalk)");
                } else {
                    Global.setEnableDeviceInput(true);
                    return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                }
            }

            mNodeList = Doc.getElementsByTagName("key_info");
            int nKeyCnt = mNodeList.getLength();
            LOG_DEBUG("[procSetKeyInfoCham] nKeyCnt = " + nKeyCnt);
            if (nKeyCnt != Integer.parseInt(strCount)) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
            }
            String strIndex = "0", strSN = "null", strUID = "null", strMessage = "null";
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();
            for (int i = 0; i < nKeyCnt; i++) {
                mElement = (Element) mNodeList.item(i);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSetKeyInfoCham] nAttrCnt = " + nAttrCnt);
                for (int j = 0; j < nAttrCnt; j++) {
                    Node mAttr = mAttrs.item(j);
                    if (j == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("index")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strIndex = mAttr.getNodeValue().trim();
                        LOG_DEBUG("[procSetAccessKeyInfo] strIndex = " + strIndex);
                    } else if (j == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("sn")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strSN = mAttr.getNodeValue().trim();
                        LOG_DEBUG("[procSetAccessKeyInfo] strSN = " + strSN);
                    } else if (j == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("uid")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strUID = mAttr.getNodeValue().trim();
                        LOG_DEBUG("[procSetAccessKeyInfo] strUID = " + strUID);
                    } else if (j == 3) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("message")) {
                            Global.setEnableDeviceInput(true);
                            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                        }
                        strMessage = mAttr.getNodeValue().trim();
                        LOG_DEBUG("[procSetAccessKeyInfo] strMessage = " + strMessage);
                    } else {
                        Global.setEnableDeviceInput(true);
                        return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
                    }
                }
                LOG_DEBUG("[procSetKeyInfoCham] i = " + i + ", strIndex [" + strIndex + "] , strSN [" + strSN + "], strUID [" + strUID + "], strMessage [" + strMessage + "]");

                KeyInfoTable.addKeyInfo(strIndex, strSN, strUID, strKeyType, ConfigValue.strSitecode, strDong, strHo, "cham", "cham", "cham", "cham", strMessage);
            }
            KeyInfoTable.printValue();
            if (UIDAccessKeyManager.onSetAccessKeyInfo(strActoin, strKeyType, strDong, strHo, KeyInfoTable)) {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(true));
            } else {
                Global.setEnableDeviceInput(true);
                return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSetKeyInfoCham(Socket Sock, Document Doc)");
            e.printStackTrace();
            Global.setEnableDeviceInput(true);
            return sendReply(Sock, MakeiMAPMSG.makeReply_set_keyinfo_cham(false));
        }
    }

    private static boolean procGetKeyInfoCham(Socket Sock, Document Doc) {
        try {
            LOG("[procGetKeyInfoCham] == START ==");

            String strInfoType = "null";

            NodeList mNodeList = Doc.getElementsByTagName("info_type");
            Element mElement = (Element) mNodeList.item(0);
            strInfoType = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procGetKeyInfoCham] strInfoType = " + strInfoType);

            if (strInfoType.equals("hcard")) strInfoType = "hkey";
            else if (strInfoType.equals("dcard")) strInfoType = "dkey";
            else if (strInfoType.equals("mcard")) strInfoType = "mkey";

            if (!(strInfoType.equalsIgnoreCase("uid") || strInfoType.equalsIgnoreCase("sn") || strInfoType.equalsIgnoreCase("uid_all") ||
                    strInfoType.equalsIgnoreCase("sn_all") || strInfoType.equalsIgnoreCase("hkey") || strInfoType.equalsIgnoreCase("dkey") || strInfoType.equalsIgnoreCase("mkey"))) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(false, null));
            }

            if (strInfoType.equals("uid")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableByUID_CHAM(Doc)));
            } else if (strInfoType.equals("sn")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableBySN_CHAM(Doc)));
            } else if (strInfoType.equals("uid_all")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableByUIDALL_CHAM(Doc)));
            } else if (strInfoType.equals("sn_all")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableBySNALL_CHAM(Doc)));
            } else if (strInfoType.equals("hkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableByHKEY_CHAM(Doc)));
            } else if (strInfoType.equals("dkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableByDKEY_CHAM(Doc)));
            } else if (strInfoType.equals("mkey")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(true, getKeyTableByMKEY_CHAM(Doc)));
            } else {
                return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(false, null));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procGetKeyInfoCham(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_get_keyinfo_cham(false, null));
        }
    }

    private static AccessKeyInfoTable getKeyTableByUID_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableByUID_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("uid_info");
            Element mElement = (Element) mNodeList.item(0);
            String strUIDInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableByUID_CHAM] strUIDInfo = " + strUIDInfo);
            return UIDAccessKeyManager.getKeyInfoByUID(strUIDInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByUID_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableBySN_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableBySN_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("sn_info");
            Element mElement = (Element) mNodeList.item(0);
            String strSNInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableBySN_CHAM] strSNInfo = " + strSNInfo);
            return UIDAccessKeyManager.getKeyInfoBySN(strSNInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableBySN_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByUIDALL_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableByUIDALL_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("uid_info");
            Element mElement = (Element) mNodeList.item(0);
            String strUIDInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableByUIDALL_CHAM] strUIDInfo = " + strUIDInfo);
            return UIDAccessKeyManager.getKeyInfoByUIDALL(strUIDInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByUIDALL_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableBySNALL_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableBySNALL_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("sn_info");
            Element mElement = (Element) mNodeList.item(0);
            String strSNInfo = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[getKeyTableBySNALL_CHAM] strSNInfo = " + strSNInfo);
            return UIDAccessKeyManager.getKeyInfoBySNALL(strSNInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableBySNALL_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByHKEY_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableByHKEY_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("hcard_info");
            Node mNode = mNodeList.item(0);
            NamedNodeMap Attrs = mNode.getAttributes();
            String strDong = "", strHo = "", strNo = "all";
            for (int i = 0; i < Attrs.getLength(); i++) {
                Node Attr = Attrs.item(i);
                if (Attr.getNodeName().equalsIgnoreCase("dong")) {
                    strDong = Attr.getNodeValue();
                } else if (Attr.getNodeName().equalsIgnoreCase("ho")) {
                    strHo = Attr.getNodeValue();
                }
            }
            LOG_DEBUG("[getKeyTableByHKEY_CHAM] strDong [" + strDong + "], strHo [" + strHo + "], strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByHKEY(strDong, strHo, strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByHKEY_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByDKEY_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableByDKEY_CHAM] ===== START =====");
            NodeList mNodeList = Doc.getElementsByTagName("dcard_info");
            Node mNode = mNodeList.item(0);
            NamedNodeMap Attrs = mNode.getAttributes();
            String strDong = "", strNo = "all";
            for (int i = 0; i < Attrs.getLength(); i++) {
                Node Attr = Attrs.item(i);
                if (Attr.getNodeName().equalsIgnoreCase("dong")) {
                    strDong = Attr.getNodeValue();
                }
            }
            LOG_DEBUG("[getKeyTableByDKEY_CHAM] strDong [" + strDong + "], strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByDKEY(strDong, strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByDKEY_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static AccessKeyInfoTable getKeyTableByMKEY_CHAM(Document Doc) {
        try {
            LOG("[getKeyTableByMKEY_CHAM] ===== START =====");
//            NodeList mNodeList = Doc.getElementsByTagName("mcard_info");
//            Node mNode = mNodeList.item(0);
//            NamedNodeMap Attrs = mNode.getAttributes();
            String strNo = "all";
            LOG_DEBUG("[getKeyTableByMKEY_CHAM] strNo [" + strNo + "]");
            return UIDAccessKeyManager.getKeyInfoByMKEY(strNo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyTableByMKEY_CHAM(Document Doc)");
            e.printStackTrace();
            return null;
        }
    }

    private static boolean procSyncCardInfoMPC(Socket Sock, Document Doc) {
        try {
            LOG("[procSyncCardInfoMPC] == START ==");

            String strKeyType = "null";
            String strID_High = "0", strID_Low = "0", strIssueCnt = "0";
            String strName = "", strMessage = "";

            NodeList mNodeList = Doc.getElementsByTagName("card_distinct");
            Element mElement = (Element) mNodeList.item(0);
            strKeyType = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procSyncCardInfoMPC] strKeyType = " + strKeyType);
            if (!(strKeyType.equalsIgnoreCase("hcard") || strKeyType.equalsIgnoreCase("dcard") || strKeyType.equalsIgnoreCase("mcard") ||
                    strKeyType.equalsIgnoreCase("v_hcard") || strKeyType.equalsIgnoreCase("v_dcard") || strKeyType.equalsIgnoreCase("v_mcard"))) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
            }

            if (strKeyType.equalsIgnoreCase("hcard")) {
                mNodeList = Doc.getElementsByTagName("hcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("hcard_id_high")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_High = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("hcard_id_low")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("hcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("dcard")) {
                mNodeList = Doc.getElementsByTagName("dcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dcard_id_high")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_High = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dcard_number")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("dcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("mcard")) {
                mNodeList = Doc.getElementsByTagName("mcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("mcard_number")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("mcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("v_hcard")) {
                mNodeList = Doc.getElementsByTagName("v_hcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_hcard_id_high")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_High = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_hcard_id_low")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_hcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("v_dcard")) {
                mNodeList = Doc.getElementsByTagName("v_dcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_dcard_id_high")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_High = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_dcard_number")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 2) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_dcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else if (strKeyType.equalsIgnoreCase("v_mcard")) {
                mNodeList = Doc.getElementsByTagName("v_mcard_sync_info");
                mElement = (Element) mNodeList.item(0);
                NamedNodeMap mAttrs = mElement.getAttributes();
                int nAttrCnt = mAttrs.getLength();
                LOG_DEBUG("[procSyncCardInfoMPC] nAttrCnt = " + nAttrCnt);
                for (int i = 0; i < nAttrCnt; i++) {
                    Node mAttr = mAttrs.item(i);
                    if (i == 0) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_mcard_number")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strID_Low = mAttr.getNodeValue();
                    } else if (i == 1) {
                        if (!mAttr.getNodeName().equalsIgnoreCase("v_mcard_gen_cnt")) {
                            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                        }
                        strIssueCnt = mAttr.getNodeValue();
                    } else {
                        return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
                    }
                }
            } else {
                return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
            }
            LOG_DEBUG("[procSyncCardInfoMPC] strID_High [" + strID_High + "] , strID_Low [" + strID_Low + "], strIssueCnt [" + strIssueCnt + "]");

            mNodeList = Doc.getElementsByTagName("card_name");
            mElement = (Element) mNodeList.item(0);
            strName = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procAlarmOperation] strName = " + strName);

            mNodeList = Doc.getElementsByTagName("card_message");
            mElement = (Element) mNodeList.item(0);
            strMessage = trimQuotation(mElement.getTextContent());
            LOG_DEBUG("[procAlarmOperation] strMessage = " + strMessage);

            if (updateSTDAccessKey(strKeyType, Integer.parseInt(strID_High), Integer.parseInt(strID_Low), Integer.parseInt(strIssueCnt))) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(true));
            } else {
                return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procSyncCardInfoMPC(Socket Sock, Document Doc)");
            e.printStackTrace();
            return sendReply(Sock, MakeiMAPMSG.makeReply_cardrd_sync_cardinfo(false));
        }
    }

    private static boolean updateSTDAccessKey(String strKeyType, int nID_High, int nID_Low, int nIssueCnt) {
        try {
            LOG_INFO("[updateSTDAccessKey] strKeyType [" + strKeyType + "] , nID_High [" + nID_High + "], nID_Low [" + nID_Low + "], nIssueCnt [" + nIssueCnt + "]");

            if (strKeyType.equals("hcard")) {
                return STDAccessKeyManager.updateResiAccessKey(nID_High, nID_Low, nIssueCnt);
            } else if (strKeyType.equals("dcard")) {
                return STDAccessKeyManager.updateDongAdminAccessKey(nID_Low, nIssueCnt);
            } else if (strKeyType.equals("mcard")) {
                return STDAccessKeyManager.updateEntireAdminAccessKey(nID_Low, nIssueCnt);
            } else if (strKeyType.equals("v_hcard")) {
                Log.w(TAG, "[updateSTDAccessKey] Not yet supported!! [" + strKeyType + "]");
                return false;
            } else if (strKeyType.equals("v_dcard")) {
                Log.w(TAG, "[updateSTDAccessKey] Not yet supported!! [" + strKeyType + "]");
                return false;
            } else if (strKeyType.equals("v_mcard")) {
                Log.w(TAG, "[updateSTDAccessKey] Not yet supported!! [" + strKeyType + "]");
                return false;
            } else {
                Log.w(TAG, "[updateSTDAccessKey] Not yet supported!! [" + strKeyType + "]");
                return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateSTDAccessKey(String strKeyType, int nID_High, int nID_Low, int nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean procFTPUpload(Socket Sock, Document Doc) {
        try {
            LOG("[procFTPUpload] == START ==");

            NodeList mNodeList = Doc.getElementsByTagName("account");
            Element mElement = (Element) mNodeList.item(0);
            NamedNodeMap mAttrs = mElement.getAttributes();
            int nAttrCnt = mAttrs.getLength();
            LOG_DEBUG("[procFTPUpload] nAttrCnt = " + nAttrCnt);
            String strID = "", strPW = "", strFilePath = "", strUploadPath = "", strMessage = "";
            Node mAttr = mAttrs.item(0);
            if (!mAttr.getNodeName().equalsIgnoreCase("id")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_ftp_upload(false, "null"));
            }
            strID = mAttr.getNodeValue();

            mAttr = mAttrs.item(1);
            if (!mAttr.getNodeName().equalsIgnoreCase("pw")) {
                return sendReply(Sock, MakeiMAPMSG.makeReply_ftp_upload(false, "null"));
            }
            strPW = mAttr.getNodeValue();

            LOG_INFO("[procFTPUpload] strID [" + strID + "], strPW [" + strPW + "]");

            mNodeList = Doc.getElementsByTagName("filepath");
            mElement = (Element) mNodeList.item(0);
            strFilePath = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("uploadpath");
            mElement = (Element) mNodeList.item(0);
            strUploadPath = trimQuotation(mElement.getTextContent());

            mNodeList = Doc.getElementsByTagName("message");
            mElement = (Element) mNodeList.item(0);
            strMessage = trimQuotation(mElement.getTextContent());

            LOG_INFO("[procFTPUpload] strFilePath [" + strFilePath + "]");
            LOG_INFO("[procFTPUpload] strUploadPath [" + strUploadPath + "]");
            LOG_INFO("[procFTPUpload] strMessage [" + strMessage + "]");

            FTPUploadInfo mFTPUploadInfo = new FTPUploadInfo();

            mFTPUploadInfo.strUserID = strID;
            mFTPUploadInfo.strPassword = strPW;
            mFTPUploadInfo.strFilePath = strFilePath;
            mFTPUploadInfo.strTargetIP = strMessage;

            String[] strParsed = strUploadPath.split("/");
            for (int i = 3; i < strParsed.length; i++) {
//                LOG_INFO("[procFTPUpload] strParsed [" + i + "] = " + strParsed[i]);
                mFTPUploadInfo.strUploadPath += "/" + strParsed[i];
            }
            mFTPUploadInfo.strTargetIP = strParsed[2];

            mFTPUploadInfo.printValue();
            startFTPUpload(mFTPUploadInfo);
            return sendReply(Sock, MakeiMAPMSG.makeReply_ftp_upload(true, "null"));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procFTPUpload(Socket Sock, Document Doc)");
            e.printStackTrace();
            sendReply(Sock, MakeiMAPMSG.makeReply_ftp_upload(false, "null"));
            return false;
        }
    }

    private static void startFTPUpload(FTPUploadInfo mFTPUploadInfo) {
        try {
            mProcFTPUploadThread = new ProcFTPUploadThread(mContext, mFTPUploadInfo);
            mProcFTPUploadThread.start();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] startFTPUpload(FTPUploadInfo mFTPUploadInfo)");
            e.printStackTrace();
        }
    }

    public static boolean resetAccessKeyInfo() {
        try {
            // 출입카드 DB를 삭제한다.
            if (MainActivity.RFKeySTD_RESI_DBManager != null) {
                if (!MainActivity.RFKeySTD_RESI_DBManager.closeDatabase()) return false;
                if (!MainActivity.RFKeySTD_RESI_DBManager.deleteDBFile()) return false;
            }

            if (MainActivity.RFKeySTD_ADMINDONG_DBManager != null) {
                if (!MainActivity.RFKeySTD_ADMINDONG_DBManager.closeDatabase()) return false;
                if (!MainActivity.RFKeySTD_ADMINDONG_DBManager.deleteDBFile()) return false;
            }

            if (MainActivity.RFKeySTD_ADMINENTIRE_DBManager != null) {
                if (!MainActivity.RFKeySTD_ADMINENTIRE_DBManager.closeDatabase()) return false;
                if (!MainActivity.RFKeySTD_ADMINENTIRE_DBManager.deleteDBFile()) return false;
            }

            if (MainActivity.RFKeyDBManager != null) {
                if (!MainActivity.RFKeyDBManager.closeDatabase()) return false;
                if (!MainActivity.RFKeyDBManager.deleteDBFile()) return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] resetAccessKeyInfo()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setFactoryMode() {
        try {
            String DATABASE_LOCATION = "/sdcard/BESTIN/DATABASE";
            return deleteDirectory(DATABASE_LOCATION);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setFactoryMode()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean copyFile(String strScrFile, String strDstFile) {
        try {
            LOG_INFO("[copyFile] strScrFile [" + strScrFile + "], strDstFile [" + strDstFile + "]");
            File srcFile = new File(strScrFile);
            if (!srcFile.exists()) {
                LOG_INFO("[copyFile] Source File is not exist!! strScrFile [" + strScrFile + "]");
                return false;
            }
            FileInputStream mFileInputStream = new FileInputStream(srcFile);
            FileOutputStream mFileOutputStream = new FileOutputStream(strDstFile);

            int nReadCnt = 0;
            byte[] buffer = new byte[1024];
            while ((nReadCnt = mFileInputStream.read(buffer, 0, 1024)) != -1) {
                mFileOutputStream.write(buffer, 0, nReadCnt);
            }
            mFileOutputStream.close();
            mFileInputStream.close();
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] copyFile(File mFile, String strSaveFile)");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean deleteDirectory(String strTarget) {
        try {
            LOG_INFO("[deleteDirectory] strTarget [" + strTarget + "]");
            File mFile = new File(strTarget);
            if (!mFile.exists()) {
                LOG_INFO("[deleteDirectory] Directory is not exist!! strTarget = " + strTarget);
                return false;
            }
            if (!mFile.isDirectory()) {
                LOG_INFO("[deleteDirectory] Given path is not directory!! strTarget = " + strTarget);
                return false;
            }

            File[] mFileList = new File(strTarget).listFiles();
            if (1 > mFileList.length) {
                LOG_INFO("[deleteDirectory] There is no files!!");
                return false;
            }

            for (int i = 0; i < mFileList.length; i++) {
                LOG_INFO("[deleteDirectory] Filename to delete -> " + mFileList[i].getName());
                if (!mFileList[i].delete()) {
                    LOG_INFO("[deleteDirectory] Fail to delete a file!! -> " + mFileList[i].getName());
                    return false;
                }
            }
            LOG_INFO("[deleteDirectory] ===== OK ======");
            return true;

        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteDirectory(String strTarget)");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean makeDirectory(String strTarget) {
        try {
            LOG_INFO("[makeDirectory] strTarget [" + strTarget + "]");
            File Directory = new File(strTarget);
            if (Directory.isDirectory()) {
                Log.d(TAG, "[makeDirectory] Directory is already exist!!");
                return true;
            } else {
                Log.d(TAG, "[makeDirectory] Directory is not exist!!");
                return Directory.mkdir();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initBestinDirectory() : " + e);
            e.printStackTrace();
            return false;
        }
    }

    private static boolean deleteFile(String strTargetFile) {
        try {
            LOG_INFO("[deleteFile] strTargetFile [" + strTargetFile + "]");
            File mFile = new File(strTargetFile);
            if (mFile.exists()) {
                return mFile.delete();
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteFile(String strTargetFile) : " + e);
            e.printStackTrace();
            return false;
        }
    }

    private static void checkRebootRequied() {
        try {
            LOG_INFO("[checkRebootRequied] Configure.bRebootRequied [" + Configure.bRebootRequied + "]");
            checkIPExpansion();
            if (Configure.bRebootRequied)
                ((MainActivity) mContext).sendHandlerMsg(MainActivity.HANDLERMSG.SYSTEM_REBOOT, 0, 0);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkRebootRequied()");
            e.printStackTrace();
        }
    }

    private static void checkIPExpansion() {
        try {
            LOG_INFO("[checkIPExpansion] ========== START ==========");
            int nDong = Integer.parseInt(ConfigValue.strDevice_Dong);
            int nDong_Head = nDong / 100;
            String[] astrIPAddress = ConfigValue.strDeviceIPAddress.split("\\.");

            if (ConfigValue.eIPExpansion == SetValue.TRUE) {
                if (nDong_Head != Integer.parseInt(astrIPAddress[0]) && !(astrIPAddress[1].equals("0"))) {
                    EthernetCtrl.setDeviceIPAddressByBasicInfo(mContext, ConfigValue.strSitecode, ConfigValue.strDevice_Dong, ConfigValue.strDevice_ID);
                    ((MainActivity) mContext).sendHandlerMsg(MainActivity.HANDLERMSG.SYSTEM_REBOOT, 0, 0);
                }
            } else {
                if (10 != Integer.parseInt(astrIPAddress[0])) {
                    EthernetCtrl.setDeviceIPAddressByBasicInfo(mContext, ConfigValue.strSitecode, ConfigValue.strDevice_Dong, ConfigValue.strDevice_ID);
                    ((MainActivity) mContext).sendHandlerMsg(MainActivity.HANDLERMSG.SYSTEM_REBOOT, 0, 0);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkIPExpansion()");
            e.printStackTrace();
        }
    }

    public static boolean validateIPAddress(String IPAddress) {
        Log.i(TAG, "[validateIPAddress] IPAddress [" + IPAddress + "]");
        if (IPAddress == null) return false;
        return mPattern.matcher(IPAddress).matches();
    }

    public static void sendHandlerMsg(int what, int arg1, int arg2) {
        try {
            LOG("[sendHandlerMsg] what [" + what + "], arg1 [" + arg1 + "], arg2 [" + arg2 + "]");
            Message HandleMsg = ServerHandler.obtainMessage();
            HandleMsg.what = what;
            HandleMsg.arg1 = arg1;
            HandleMsg.arg2 = arg2;
            ServerHandler.sendMessage(HandleMsg);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendHandlerMsg(msg)");
            e.printStackTrace();
        }
    }

    public static void sendHandlerMsgDelayed(int what, int arg1, int arg2, long delay) {
        try {
            LOG("[sendHandlerMsgDelayed] what [" + what + "], arg1 [" + arg1 + "], arg2 [" + arg2 + "], delay [" + delay + "]");
            Message HandleMsg = ServerHandler.obtainMessage();
            HandleMsg.what = what;
            HandleMsg.arg1 = arg1;
            HandleMsg.arg2 = arg2;
            ServerHandler.sendMessageDelayed(HandleMsg, delay);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendHandlerMsgDelayed(msg)");
            e.printStackTrace();
        }
    }

    public static interface ChatEventListener {
        public void onChatEvent(CALLTYPE eCallType, String strRemoteIP, String strRemoteSIPID, CHATEVENT eChatEvent, CHATSTATUS eChatStatus, String strMessage);

        public void onChatMessage(String strMessage);
    }

    public static class runiMAPsvr extends Thread {
        // 비암호화 iMAP 통신 처리
        ServerSocket mServerSocket;
        prociMAPsvr mprociMAPsvr;
        boolean bSvrRunning = true;

        public runiMAPsvr(Context context, boolean bRun) {
            try {
                Log.w(TAG, "[runiMAPsvr] bRun [" + bRun + "]");
                mContext = context;
                bSvrRunning = bRun;
                mServerSocket = new ServerSocket(PORT_NUM_iMAP);
            } catch (IOException e) {
                Log.e(TAG, "[IOException] runiMAPsvr()");
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            try {
                while (bSvrRunning) {
                    try {
                        LOG_INFO("[runiMAPsvr().run()] prociMAPsvr OPEN!! : NonEncrypted");
                        Socket Sock = null;
                        if (!mServerSocket.isClosed()) Sock = mServerSocket.accept();
                        mprociMAPsvr = new prociMAPsvr(Sock);
                        mprociMAPsvr.start();
                    } catch (IOException e) {
                        Log.e(TAG, "[IOException] runiMAPsvr().run() : NonEncrypted");
                        e.printStackTrace();
                    }
                }

                try {
                    Log.w(TAG, "[runiMAPsvr().run()] ========== Server sockets close!! ==========");
                    if (mServerSocket != null) mServerSocket.close();
                } catch (Exception e) {
                    Log.e(TAG, "[Exception] stopiMAPsvr.run() : Socket close!!");
                    e.printStackTrace();
                }
            } catch (Exception e) {
                Log.e(TAG, "[Exception] runiMAPsvr().run()");
                e.printStackTrace();
            }
        }

        public void stopiMAPsvr() {
            try {
                Log.i(TAG, "[stopiMAPsvr] ========== START ==========");
                bSvrRunning = false;
                if (mServerSocket != null) {
                    mServerSocket.close();
                    mServerSocket = null;
                }
            } catch (Exception e) {
                Log.e(TAG, "[Exception] stopiMAPsvr()");
                e.printStackTrace();
            }
        }
    }

    public static class prociMAPsvr extends Thread {
        Socket mSocket;
        String strRcvMSG;

        public prociMAPsvr(Socket Sock) {
            mSocket = Sock;
        }

        @Override
        public void run() {
            try {
                LOG_INFO("[prociMAPsvr().run()] Received something!!");
                if (!Global.getEnableDeviceInput()) {
                    Log.e(TAG, "[prociMAPsvr().run()] Global.getEnableDeviceInput() = " + Global.getEnableDeviceInput());
                    return;
                }
                InputStream InStm = mSocket.getInputStream();
                mSocket.setSoLinger(true, 3000);
                BufferedReader MsgReader = new BufferedReader(new InputStreamReader(InStm, "euc-kr"));
                strRcvMSG = "";
                String InBuffer;
                int i = 0;
                boolean bRcvOK = false;
                long lSocketConnectionPeriod = System.currentTimeMillis() + TIMEOUT_iMAP_WAIT;   // 타임아웃시간을 설정한다.

                // 비암호화 iMAP 통신 처리
                while (System.currentTimeMillis() < lSocketConnectionPeriod) {
                    try {
                        if (MsgReader.ready()) {
                            try {
                                char[] ReadData = new char[SOCKET_BUFFER_SIZE_READ * 1024];
                                InBuffer = "";
                                while (MsgReader.ready()) {
                                    long charCnt = MsgReader.read(ReadData);
                                    InBuffer += new String(ReadData, 0, (int) charCnt);
                                }

                                if (InBuffer != null) strRcvMSG += InBuffer;

                                if (0 < InBuffer.indexOf("</imap>")) {
                                    bRcvOK = true;
                                    LOG_INFO("[prociMAPsvr().run()] Received DONE!!");
                                    break;
                                }
                            } catch (Exception e) {
                                Log.e(TAG, "[Exception] prociMAPsvr.run() ===== 1 =====");
                                e.printStackTrace();
                                return;
                            }
                        } else {
                            Thread.sleep(100);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] prociMAPsvr.run() ===== 2 =====");
                        mSocket.close();
                        e.printStackTrace();
                        return;
                    }
                }

                try {
                    strRcvMSG = strRcvMSG.substring(strRcvMSG.indexOf("<"));
                    LOG("=== Received iMAP ===\n\r" + strRcvMSG);
                    LOG("=== End iMAP ===");
                } catch (Exception e) {
                    Log.e(TAG, "[Exception] prociMAPsvr.run() ===== 3 =====");
                    e.printStackTrace();
                }

                if (!bRcvOK) {
                    Log.i(TAG, "[prociMAPsvr().run()] iMAP message is unacceptable!! : " + strRcvMSG);
                    mSocket.close();
                    return;
                }

                // 메시지를 처리하는 함수
                prociMAPMessage(mSocket, strRcvMSG);

                if (!mSocket.isClosed()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.e(TAG, "[Exception] mSocket.isBound()");
                        e.printStackTrace();
                    }
                    Log.e(TAG, "[Exception] mSocket still opened!!");
                    mSocket.close();
                }

                if (Global.getUpgradeStatus() == UPGRADESTATUS.PROPOSE) {
                    startUpgradeProc();
                }

            } catch (IOException e) {
                Log.e(TAG, "[Exception] prociMAPsvr.run() ===== 5 =====");
                e.printStackTrace();
            }
        }
    }

    public static class runiMAPSSLsvr extends Thread {
        // 암호화 iMAP 통신 처리
        prociMAPSSLsvr mprociMAPSSLsvr;
        SSLServerSocket mSSLServerSocket;
        SSLServer mSSLServer;
        boolean bServerRun = true;

        public runiMAPSSLsvr(Context context, boolean bRun) {
            try {
                Log.w(TAG, "[runiMAPSSLsvr] bRun [" + bRun + "]");
                mContext = context;
                bServerRun = bRun;
                mSSLServer = new SSLServer(PORT_NUM_iMAP_ENCRYPTED, mContext.getResources().openRawResource(R.raw.wallpad_key), SSLKEY_PW);
                mSSLServerSocket = mSSLServer.getSSLServerSocket();
            } catch (Exception e) {
                Log.e(TAG, "[Exception] runiMAPSSLsvr()");
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            try {
                // 암호화
                while (bServerRun) {
                    try {
                        LOG_INFO("[runiMAPSSLsvr().run()] prociMAPSSLsvr OPEN!! : Encrypted");
                        Socket SSLSock = null;
                        if (!mSSLServerSocket.isClosed()) SSLSock = mSSLServerSocket.accept();
                        mprociMAPSSLsvr = new prociMAPSSLsvr(SSLSock);
                        mprociMAPSSLsvr.start();
                    } catch (IOException e) {
                        Log.e(TAG, "[IOException] runiMAPsvr().run() : Encrypted");
                        e.printStackTrace();
                    }
                }

                try {
                    Log.w(TAG, "[runiMAPSSLsvr().run()] ========== Server sockets close!! ==========");
                    if (mSSLServerSocket != null) mSSLServerSocket.close();
                } catch (Exception e) {
                    Log.e(TAG, "[Exception] runiMAPSSLsvr.run() : Socket close!!");
                    e.printStackTrace();
                }
            } catch (Exception e) {
                Log.e(TAG, "[Exception] runiMAPSSLsvr().run()");
                e.printStackTrace();
            }
        }

        public void stopiMAPsvr() {
            try {
                Log.i(TAG, "[stopiMAPsvr] ========== START ==========");
                bServerRun = false;
                if (mSSLServerSocket != null) {
                    mSSLServerSocket.close();
                    mSSLServerSocket = null;
                }
            } catch (Exception e) {
                Log.e(TAG, "[Exception] stopiMAPsvr()");
                e.printStackTrace();
            }
        }
    }

    public static class prociMAPSSLsvr extends Thread {
        Socket mSocket;
        String strRcvMSG;

        public prociMAPSSLsvr(Socket Sock) {
            mSocket = Sock;
        }

        @Override
        public void run() {
            try {
                LOG_INFO("[prociMAPSSLsvr().run()] Received something!!");
                if (!Global.getEnableDeviceInput()) {
                    Log.e(TAG, "[prociMAPSSLsvr().run()] Global.getEnableDeviceInput() = " + Global.getEnableDeviceInput());
                    return;
                }
                InputStream InStm = mSocket.getInputStream();
                mSocket.setSoLinger(true, 3000);
                BufferedReader MsgReader = new BufferedReader(new InputStreamReader(InStm, "euc-kr"));
                strRcvMSG = "";
                String InBuffer;
                int i = 0;
                boolean bRcvOK = false;
                long lSocketConnectionPeriod = System.currentTimeMillis() + TIMEOUT_iMAP_WAIT;   // 타임아웃시간을 설정한다.

                // 암호화 통신
                char[] ReadData = new char[SOCKET_BUFFER_SIZE_READ * 1024];

                while (System.currentTimeMillis() < lSocketConnectionPeriod) {
                    try {
                        InBuffer = "";
                        long charCnt = MsgReader.read(ReadData);
                        if (charCnt > 0) {
                            InBuffer += new String(ReadData, 0, (int) charCnt);
                            strRcvMSG += InBuffer;
                            if (-1 < InBuffer.indexOf("</imap>")) {
                                bRcvOK = true;
                                Log.i(TAG, "[prociMAPSSLsvr().run()] Received DONE!! (SSL)");
                                break;
                            }
                        } else {
                            Thread.sleep(100);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "[Exception] prociMAPSSLsvr.run() ===== 1 ===== (SSL)");
                        mSocket.close();
                        e.printStackTrace();
                        return;
                    }
                }

                try {
                    strRcvMSG = strRcvMSG.substring(strRcvMSG.indexOf("<"));
                    LOG("=== Received iMAP ===\n\r" + strRcvMSG);
                    LOG("=== End iMAP ===");
                } catch (Exception e) {
                    Log.e(TAG, "[Exception] prociMAPSSLsvr.run() ===== 2 =====");
                    e.printStackTrace();
                }

                if (!bRcvOK) {
                    Log.i(TAG, "[prociMAPsvr().run()] iMAP message is unacceptable!! : " + strRcvMSG);
                    mSocket.close();
                    return;
                }

                prociMAPMessage(mSocket, strRcvMSG);

                if (!mSocket.isClosed()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.e(TAG, "[Exception] mSocket.isBound()");
                        e.printStackTrace();
                    }
                    Log.e(TAG, "[Exception] mSocket still opened!!");
                    mSocket.close();
                }

                if (Global.getUpgradeStatus() == UPGRADESTATUS.PROPOSE) {
                    startUpgradeProc();
                }

            } catch (IOException e) {
                Log.e(TAG, "[Exception] prociMAPsvr.run() ===== 3 =====");
                e.printStackTrace();
            }
        }
    }

    public static void prociMAPMessage(Socket mSocket, String strRcvMSG) {
        try {
            LOG("[prociMAPMessage] ========== START ==========)");
            DocumentBuilderFactory DocBdFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder DocBuilder = DocBdFactory.newDocumentBuilder();
            InputStream InStmXML = new ByteArrayInputStream(strRcvMSG.getBytes());
            Document Doc = DocBuilder.parse(InStmXML);
            Element EleList = Doc.getDocumentElement();

            // 메시지에서 IP Address와 Sender 정보를 추출합니다.
            String strAddress = EleList.getAttribute("address");   // 메시지에서 IP Address를 추출합니다.
            String strSender = EleList.getAttribute("sender");   // 메시지에서 Sender 정보를 추출합니다.
            LOG("[prociMAPMessage] strAddress = " + strAddress + ", strSender = " + strSender);
            NodeList Items = EleList.getElementsByTagName("service");
            Node Item = Items.item(0);
            NamedNodeMap Attrs = Item.getAttributes();
            Log.i(TAG, "[prociMAPMessage] Attrs.getLength() = " + Attrs.getLength());
            for (int j = 0; j < Attrs.getLength(); j++) {
                Node Attr = Attrs.item(j);
                String strCMD;
                if (Attr.getNodeName().equalsIgnoreCase("type")) {
                    strCMD = Attr.getNodeValue();
                    Log.i(TAG, "[prociMAPMessage] Message Type : " + strCMD);

                    if (strCMD.equalsIgnoreCase("request")) {

                        for (int k = j + 1; k < Attrs.getLength(); k++) {
                            Attr = Attrs.item(k);
                            strCMD = Attr.getNodeValue();
                            Log.i(TAG, "[prociMAPMessage] Element Name : " + strCMD);

                            if (strCMD.equalsIgnoreCase("upgrade")) {
                                LOG_DEBUG("=== upgrade ===");
                                procUpgrade(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("set_server_version")) {
                                LOG_DEBUG("=== set_server_version ===");
                                procSetServerVersion(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("set_setup_info")) {
                                LOG_DEBUG("=== set_setup_info ===");
                                procSetSetupInfo(mSocket, Doc);
                                sendHandlerMsgDelayed(HANDLERMSG.SEND_HEARTBEAT_NOTICE, 0, 0, 1000);
                                sendHandlerMsgDelayed(HANDLERMSG.CHECK_REBOOT_REQUIRED, 0, 0, 2000);
                            } else if (strCMD.equalsIgnoreCase("get_setup_info")) {
                                LOG_DEBUG("=== get_setup_info ===");
                                procGetSetupInfo(mSocket, Doc);
                                sendHandlerMsgDelayed(HANDLERMSG.SEND_HEARTBEAT_NOTICE, 0, 0, 1000);
                            } else if (strCMD.equalsIgnoreCase("lobby_open")) {
                                LOG_DEBUG("=== lobby_open ===");
                                procLobbyOpen(mSocket, strSender);
                            } else if (strCMD.equalsIgnoreCase("lobby_door_ctrl")) {
                                LOG_DEBUG("=== lobby_door_ctrl ===");
                                procLobbyDoorCtrl(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("check_sip_activation")) {
                                LOG_DEBUG("=== check_sip_activation ===");
                                procCheckSIPActivation(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("device_reset")) {
                                LOG_DEBUG("=== device_reset ===");
                                procDeviceReset(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("set_accesskey_info")) {
                                LOG_DEBUG("=== set_accesskey_info ===");
                                procSetAccessKeyInfo(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("get_accesskey_info")) {
                                LOG_DEBUG("=== get_accesskey_info ===");
                                procGetAccessKeyInfo(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("lobby_sync_cardinfo")) {
                                LOG_DEBUG("=== lobby_sync_cardinfo ===");
                                procLobby_Sync_CardInfo(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("lobby_sync_dcardinfo")) {
                                LOG_DEBUG("=== lobby_sync_dcardinfo ===");
                                procLobby_Sync_DCardInfo(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("lobby_sync_mcardinfo")) {
                                LOG_DEBUG("=== lobby_sync_mcardinfo ===");
                                procLobby_Sync_MCardInfo(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("set_keyinfo_cham")) {
                                LOG_DEBUG("=== set_keyinfo_cham ===");
                                procSetKeyInfoCham(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("get_keyinfo_cham")) {
                                LOG_DEBUG("=== get_keyinfo_cham ===");
                                procGetKeyInfoCham(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("cardrd_sync_cardinfo")) {
                                LOG_DEBUG("=== cardrd_sync_cardinfo ===");
                                procSyncCardInfoMPC(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("ftp_upload")) {
                                LOG_DEBUG("=== ftp_upload ===");
                                procFTPUpload(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("chat_message")) {
                                LOG_DEBUG("=== chat_message ===");
                                procChatMessage(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("chat_sequence")) {
                                LOG_DEBUG("=== chat_sequence ===");
                                procChatSequence(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("lobby_open_onepass")) {
                                LOG_DEBUG("=== lobby_open_onepass ===");
                                procLobbyOpenOnePass(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("remote_debug_connect")) {
                                LOG_DEBUG("=== remote_debug_connect ===");
                                procRemoteDebugConnect(mSocket, Doc);
                            } else if (strCMD.equalsIgnoreCase("device_live_check")) {
                                LOG_DEBUG("=== device_live_check ===");
                                procDeviceLiveCheck(mSocket, Doc);
                            }
                            //jslee start
                            else if (strCMD.equalsIgnoreCase("upgrade_facepic")) {
                                LOG_DEBUG("=== upgrade_facepic ===");
                                String user = "";
                                String password = "";
                                String path = "";
                                String filename = "";

                                NodeList homeItems = EleList.getElementsByTagName("homeaddr");
                                Node homeItem = homeItems.item(0);
                                NamedNodeMap homeAttrs = homeItem.getAttributes();


                                for (int h = 0; h < homeAttrs.getLength(); h++) {
                                    Node homeAttr = homeAttrs.item(h);
                                    if (homeAttr.getNodeName().equalsIgnoreCase("user")) {
                                        user = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("passwd")) {
                                        password = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("path")) {
                                        path = homeAttr.getNodeValue();
                                    }
                                }
                                NodeList fileItems = EleList.getElementsByTagName("filename");
                                Node fileItem = fileItems.item(0);
                                NodeList fileItemChildNodes = fileItem.getChildNodes();
                                filename = fileItemChildNodes.item(0).getNodeValue();
                                filename = filename.replaceAll("\"", "");

                                ((MainActivity) mContext).setFirmwareUpgrade(mSocket, strAddress + ";;" + user + ";;" + password + ";;" + path + ";;" + filename);

                            }
                            else if (strCMD.equalsIgnoreCase("add_facepic")) {
                                LOG_DEBUG("=== add_facepic ===");
                                String dong = "";
                                String ho = "";
                                String alias = "";
                                String user = "";
                                String password = "";
                                String path = "";
                                String filename = "";

                                NodeList homeItems = EleList.getElementsByTagName("homeaddr");
                                Node homeItem = homeItems.item(0);
                                NamedNodeMap homeAttrs = homeItem.getAttributes();


                                for (int h = 0; h < homeAttrs.getLength(); h++) {
                                    Node homeAttr = homeAttrs.item(h);
                                    if (homeAttr.getNodeName().equalsIgnoreCase("dong")) {
                                        dong = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("ho")) {
                                        ho = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("alias")) {
                                        alias = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("user")) {
                                        user = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("passwd")) {
                                        password = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("path")) {
                                        path = homeAttr.getNodeValue();
                                    }
                                }
                                NodeList fileItems = EleList.getElementsByTagName("filename");
                                Node fileItem = fileItems.item(0);
                                NodeList fileItemChildNodes = fileItem.getChildNodes();
                                filename = fileItemChildNodes.item(0).getNodeValue();
                                filename = filename.replaceAll("\"", "");

                                ((MainActivity) mContext).setData(mSocket, strAddress + ";;" + dong + ";;" + ho + ";;" + alias + ";;" + user + ";;" + password + ";;" + path + ";;" + filename,1);

                            }
                            //NYW
                            else if (strCMD.equalsIgnoreCase("del_facepic")) {
                                LOG_DEBUG("=== del_facepic ===");
                                String dong = "";
                                String ho = "";
                                String alias = "";

                                NodeList homeItems = EleList.getElementsByTagName("homeaddr");
                                Node homeItem = homeItems.item(0);
                                NamedNodeMap homeAttrs = homeItem.getAttributes();


                                for (int h = 0; h < homeAttrs.getLength(); h++) {
                                    Node homeAttr = homeAttrs.item(h);
                                    if (homeAttr.getNodeName().equalsIgnoreCase("dong")) {
                                        dong = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("ho")) {
                                        ho = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("alias")) {
                                        alias = homeAttr.getNodeValue();
                                    }
                                }

                                ((MainActivity) mContext).delData(mSocket, strAddress + ";;" + dong + ";;" + ho + ";;" + alias + ";;", 2);
                            }
                            else if (strCMD.equalsIgnoreCase("download_facepic")) {
                                LOG_DEBUG("=== download_facepic ===");

                                String user = "";
                                String password = "";
                                String path = "";
                                String filename = "";

                                NodeList homeItems = EleList.getElementsByTagName("pic_data");
                                Node homeItem = homeItems.item(0);
                                NamedNodeMap homeAttrs = homeItem.getAttributes();


                                for (int h = 0; h < homeAttrs.getLength(); h++) {
                                    Node homeAttr = homeAttrs.item(h);
                                    if (homeAttr.getNodeName().equalsIgnoreCase("user")) {
                                        user = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("passwd")) {
                                        password = homeAttr.getNodeValue();
                                    } else if (homeAttr.getNodeName().equalsIgnoreCase("path")) {
                                        path = homeAttr.getNodeValue();
                                    }
                                }
                                NodeList fileItems = EleList.getElementsByTagName("filename");
                                Node fileItem = fileItems.item(0);
                                NodeList fileItemChildNodes = fileItem.getChildNodes();
                                filename = fileItemChildNodes.item(0).getNodeValue();
                                filename = filename.replaceAll("\"", "");

                                ((MainActivity) mContext).setdownload(mSocket, strAddress + ";;" + user + ";;" + password + ";;" + path + ";;" + filename,1);

                            }
                            else if (strCMD.equalsIgnoreCase("notice")) {
                                Log.i(TAG, "[prociMAPMessage] Message Type : " + strCMD);
                            }
                        }
                    }
                }
            }
            
        } catch (Exception e) {
            Log.e(TAG, "[Exception] prociMAPMessage(Socket mSocket, String strMsg)");
            e.printStackTrace();
        }
        
    }

    // MainActivity Handler
    public final class HANDLERMSG {
        public final static int SEND_HEARTBEAT_NOTICE = 110;
        public final static int CHECK_REBOOT_REQUIRED = 120;
    }


}