/// <summary>
        /// 中转执行指令,返回响应结果
        /// </summary>
        /// <param name="tarsrv">服务器</param>
        /// <param name="trans">传输实例</param>
        /// <param name="proxy">设备代理电脑IP地址</param>
        /// <param name="dest">设备目标</param>
        /// <param name="cmdP">指令实例</param>
        /// <param name="tpl">协议名称</param>
        /// <param name="cmd">指令名称</param>
        /// <param name="tagdata">指令数据 tag格式</param>
        /// <returns>返回执行结果</returns>
        private string getResponse(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, CmdProtocol cmdP, string tpl, string cmd, string tagdata)
        {
            if (null == tarsrv || null == proxy || null == dest || null == trans || null == cmdP)
                return "";
            if (string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd) || string.IsNullOrEmpty(tagdata))
                return "";
            CommiManager mgr = CommiManager.GlobalManager;
            string msg = "";

            bool islocal = false;
            string addr = proxy.ToString();
            IPAddress[] locals = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in locals)
                if (addr == ip.ToString())
                {
                    islocal = true;
                    break;
                }
            if ("127.0.0.1" == addr)
                islocal = true;
            //本机通讯或目标是代理机本机则直接通讯
            string wordcmd = basefun.valtag(tagdata, "{命令字}");
            if (islocal || CommiType.SerialPort != dest.ProtocolType)
            {
                if (null == cmdP.EventWh)
                    cmdP.EventWh = new ManualResetEvent(false);
                for (int i = 0; i < 2; i++)
                {
                    cmdP.setCommand(tpl, cmd, tagdata);
                    msg = "";
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 指令:" + cmd + " " + tagdata);
                    myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 指令:" + cmd + " " + tagdata);
                    mgr.SendCommand(dest, cmdP, true);
                    if (cmdP.EventWh.WaitOne(timeout, false))
                        msg = cmdP.ResponseFormat;
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 响应:" + msg);
                    myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 响应:" + msg);
                    string rtn = basefun.valtag(msg, "Success");
                    string wd2 = basefun.valtag(msg, "{命令字}");
                    if ("true" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if ("false" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if (this.cnnout < 1)
                        break;
                    if (string.IsNullOrEmpty(msg) && CommiType.SerialPort != dest.ProtocolType)
                    {
                        Debug.WriteLine(String.Format("{0:HH:mm:ss.fff} 测试网络连接...", DateTime.Now));
                        myLog.WriteLine(String.Format("{0:HH:mm:ss.fff} 测试网络连接...", DateTime.Now));
                        if (!Ping(dest)) break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < 1; i++)
                {
                    cmdP.setCommand(tpl, cmd, tagdata);
                    byte[] data = cmdP.getCommand();
                    trans.CommiTrans(proxy, dest, tpl, cmd, ref data);
                    mgr.SendCommand(tarsrv, trans, true);
                    //同步通讯等待时间1.5秒
                    if (trans.EventWh.WaitOne(2 * timeout, false))
                        data = trans.FileContext;
                    msg = cmdP.FormatResponse(data);
                    string rtn = basefun.valtag(msg, "Success");
                    string wd2 = basefun.valtag(msg, "{命令字}");
                    if ("true" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if ("false" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                }
            }
            return msg;
        }
Example #2
0
        /// <summary>
        /// ͣ����
        /// </summary>
        /// <param name="tarsrv"></param>
        /// <param name="trans"></param>
        /// <param name="proxy"></param>
        /// <param name="dest"></param>
        /// <param name="cmdP"></param>
        /// <param name="tpl"></param>
        /// <param name="cmd"></param>
        /// <param name="tagdata"></param>
        /// <returns></returns>
        public string getParkResponse(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, CmdProtocol cmdP, string tpl, string cmd, string tagdata)
        {
            if (null == tarsrv || null == proxy || null == dest || null == trans || null == cmdP)
                return "";
            if (string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd) || string.IsNullOrEmpty(tagdata))
                return "";
            CommiManager mgr = CommiManager.GlobalManager;
            string msg = "";

            bool islocal = false;
            string addr = proxy.ToString();
            IPAddress[] locals = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in locals)
                if (addr == ip.ToString())
                {
                    islocal = true;
                    break;
                }
            if ("127.0.0.1" == addr)
                islocal = true;
            //����ͨѶ��Ŀ���Ǵ����������ֱ��ͨѶ
            if (islocal || CommiType.SerialPort != dest.ProtocolType)
            {
                if (null == cmdP.EventWh)
                    cmdP.EventWh = new ManualResetEvent(false);
                for (int i = 0; i < 3; i++)
                {
                    cmdP.setCommand(tpl, cmd, tagdata);
                    cmdP.ResetState();
                    mgr.SendCommand(dest, cmdP);
                    msg = "";
                    if (cmdP.EventWh.WaitOne(800, false))
                        msg = cmdP.ResponseFormat;
                    if ("true" == basefun.valtag(msg, "Success"))
                        break;
                    if (!string.IsNullOrEmpty(basefun.valtag(msg, "{״̬}")))
                        break;
                }
            }
            else
            {
                for (int i = 0; i < 3; i++)
                {
                    cmdP.setCommand(tpl, cmd, tagdata);
                    byte[] data = cmdP.getCommand();
                    trans.CommiTrans(proxy, dest, tpl, cmd, ref data);
                    trans.ResetState();
                    mgr.SendCommand(tarsrv, trans);
                    //ͬ��ͨѶ�ȴ�ʱ��1.5��
                    if (trans.EventWh.WaitOne(1500, false))
                        data = trans.FileContext;
                    msg = cmdP.FormatResponse(data);
                    if ("true" == basefun.valtag(msg, "Success"))
                        break;
                    if (!string.IsNullOrEmpty(basefun.valtag(msg, "{״̬}")))
                        break;
                }
            }
            if ("true" != basefun.valtag(msg, "Success"))
            {
                NameValueCollection attr = new NameValueCollection();
                attr["��"] = tpl;
                attr["����"] = tagdata + "\r\n" + cmd;
                ServiceTool.LogMessage(msg, attr, EventLogEntryType.Error);
                return msg;
            }
            return msg;
        }
        /// <summary>
        /// 中转执行指令
        /// </summary>
        /// <param name="tarsrv">服务器</param>
        /// <param name="trans">传输实例</param>
        /// <param name="proxy">设备代理电脑IP地址</param>
        /// <param name="dest">设备目标</param>
        /// <param name="cmdP">指令实例</param>
        /// <param name="tpl">协议名称</param>
        /// <param name="cmd">指令名称</param>
        /// <param name="tagdata">指令数据 tag格式</param>
        /// <returns>是否执行成功</returns>
        private bool sendCommand(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, CmdProtocol cmdP, string tpl, string cmd, string tagdata)
        {
            if (null == tarsrv || null == proxy || null == dest || null == trans || null == cmdP)
                return false;
            if (string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd) || string.IsNullOrEmpty(tagdata))
                return false;
            CommiManager mgr = CommiManager.GlobalManager;
            string msg = "";

            bool islocal = false;
            string addr = proxy.ToString();
            IPAddress[] locals = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in locals)
                if (addr == ip.ToString())
                {
                    islocal = true;
                    break;
                }
            if ("127.0.0.1" == addr)
                islocal = true;
            int tmout = this.timeout;
            if ("门禁" == tpl && "清空白名单" == cmd)
            {
                tmout = 35 * 1000;
                cmdP.TimeOut = new TimeSpan(0, 0, 35);
                cmdP.TimeLimit = new TimeSpan(0, 0, 2 * 35);
                cmdP.TimeFailLimit = cmdP.TimeLimit;
            }
            //本机通讯或目标是代理机本机则直接通讯
            string wordcmd = basefun.valtag(tagdata, "{命令字}");
            if (islocal || CommiType.SerialPort != dest.ProtocolType)
            {
                if (null == cmdP.EventWh)
                    cmdP.EventWh = new ManualResetEvent(false);
                for (int i = 0; i < 2; i++)
                {

                    cmdP.setCommand(tpl, cmd, tagdata);
                    msg = "";
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 指令:" + cmd + " " + tagdata);
                    myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 指令:" + cmd + " " + tagdata);
                    mgr.SendCommand(dest, cmdP, true);
                    if (cmdP.EventWh.WaitOne(tmout, false))
                        msg = cmdP.ResponseFormat;
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 响应:" + msg);
                    myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 响应:" + msg);
                    string rtn = basefun.valtag(msg, "Success");
                    string wd2 = basefun.valtag(msg, "{命令字}");
                    if ("true" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if ("false" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if (this.cnnout < 1)
                        break;
                    if (string.IsNullOrEmpty(msg) && CommiType.SerialPort != dest.ProtocolType)
                    {
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 测试网络连接...");
                        myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 测试网络连接...");
                        if (!Ping(dest)) break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < 1; i++)
                {
                    cmdP.setCommand(tpl, cmd, tagdata);
                    byte[] data = cmdP.getCommand();
                    trans.CommiTrans(proxy, dest, tpl, cmd, ref data);
                    mgr.SendCommand(tarsrv, trans, true);
                    //同步通讯等待时间1.5秒
                    if (trans.EventWh.WaitOne(2 * tmout, false))
                        data = trans.FileContext;
                    msg = cmdP.FormatResponse(data);
                    string rtn = basefun.valtag(msg, "Success");
                    string wd2 = basefun.valtag(msg, "{命令字}");
                    if ("true" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                    if ("false" == rtn && (wordcmd == wd2 || string.IsNullOrEmpty(wordcmd) || string.IsNullOrEmpty(wd2)))
                        break;
                }
            }
            if ("门禁" == tpl && "清空白名单" == cmd)
                this.setTimeout(cmdP);
            if ("true" != basefun.valtag(msg, "Success"))
            {
                NameValueCollection attr = new NameValueCollection();
                attr["协议"] = tpl;
                attr["操作"] = tagdata + "\r\n" + cmd;
                ServiceTool.LogMessage(msg, attr, EventLogEntryType.Error);
                return false;
            }
            return true;
        }