/// <summary> /// 从服务器中获得图片 /// </summary> /// <param name="savePath">图片保存相对路径</param> /// <param name="pl">显示图片的容器</param> public void getPic_Server(string filepath, Panel pl) { if (string.IsNullOrEmpty(filepath)) return; int port = 2010; string ipsrv = "127.0.0.1"; string sport = DataAccRes.AppSettings("Granity文件服务"); string conn = DataAccRes.DefaultDataConnInfo.Value; if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return; } Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); ipsrv = Dns.GetHostAddresses(ipsrv)[0].ToString(); } CommiTarget target = new CommiTarget(ipsrv, port, CommiType.TCP); target.setProtocol(CmdFileTrans.PTL); CmdFileTrans cmd = new CmdFileTrans(false); cmd.GetFile(filepath); CommiManager.GlobalManager.SendCommand(target, cmd); if (cmd.EventWh.WaitOne(new TimeSpan(0, 0, 15), false)) { byte[] data = cmd.FileContext; if (data.Length < 1) return; MemoryStream stream = new MemoryStream(data); pl.BackgroundImage = Image.FromStream(stream); } }
/// <summary> /// ǿ�ƶ��豸��ȫ����ʼ�������ز���,���ºڰ����� /// </summary> /// <param name="tagdevcmds">�豸ID������ָ��,tag��ʽ,����ָ����"|"�ָ�</param> public bool CommiDevice(string tagdevcmds) { string devID = basefun.valtag(tagdevcmds, "�豸ID"); string attrcmd = "|" + basefun.valtag(tagdevcmds, "ָ��") + "|"; if (string.IsNullOrEmpty(tagdevcmds)) return false; //������ת������ CmdFileTrans trans = new CmdFileTrans(false); int port = 2010; string sport = DataAccRes.AppSettings("Granity�ļ�����"); if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return false; } string conn = DataAccRes.DefaultDataConnInfo.Value; Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); string ipsrv = "127.0.0.1"; if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); ipsrv = Dns.GetHostAddresses(ipsrv)[0].ToString(); } CommiTarget tarsrv = new CommiTarget(ipsrv, port, CommiType.TCP); tarsrv.setProtocol(CmdFileTrans.PTL); NameObjectList ps = new NameObjectList(); ps["�豸ID"] = devID; DataTable tab = this.query.getTable("�豸ͨѶ����", ps); if (null == tab || tab.Rows.Count < 1) return true; DataRow dr = tab.Rows[0]; //����IP,�������Ƿ�local,ͨѶĿ�� string addr = Convert.ToString(dr["IP��ַ"]); IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); string myip = IpEntry.AddressList[0].ToString(); if (string.IsNullOrEmpty(addr)) addr = myip; if (string.IsNullOrEmpty(addr)) return true; IPAddress proxy = null; try { proxy = IPAddress.Parse(addr); } catch (Exception ex) { NameValueCollection data = new NameValueCollection(); data["IP��ַ"] = addr; ServiceTool.LogMessage(ex, null, EventLogEntryType.Error); return false; } CommiTarget dest = this.getTarget(dr); if (null == dest) return false; string tpl = Convert.ToString(dr["ͨѶЭ��"]); switch (tpl) { case "ͣ����": dest.setProtocol(Protocol.PTLPark); return this.commiDevicePark(tarsrv, trans, proxy, dest, dr, attrcmd); case "�Ž�": dest.setProtocol(Protocol.PTLDoor); return this.commiDeviceDoor(tarsrv, trans, proxy, dest, dr, attrcmd); case "����": dest.setProtocol(Protocol.PTLEatery); return this.commiDeviceEatery(tarsrv, trans, proxy, dest, dr, attrcmd); } return true; }
/// <summary> /// ͣ�����豸��ʼ�������ز���,���ºڰ����� /// </summary> /// <param name="tarsrv">��ת������</param> /// <param name="trans">����ָ��</param> /// <param name="proxy">�豸ͨѶ�������IP��ַ</param> /// <param name="dest">Ŀ���豸</param> /// <param name="drdevice">�豸��Ϣ��¼��</param> /// <param name="attrcmd">ִ��ָ��</param> /// <returns>����ͨѶ���ز����Ƿ�ɹ�</returns> private bool commiDevicePark(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, DataRow drdevice, string attrcmd) { if (null == tarsrv || null == proxy || null == dest || null == trans || null == drdevice || string.IsNullOrEmpty(attrcmd)) return true; CmdProtocol cmdP = new CmdProtocol(false); string devID = Convert.ToString(drdevice["ID"]); NameObjectList ps = new NameObjectList(); ps["�豸ID"] = devID; bool success = false; //ϵͳʱ�� string tpl = "ͣ����"; string valst = Convert.ToString(drdevice["վַ"]); DateTime dtsystem = Convert.ToDateTime(this.query.ExecuteScalar("ϵͳʱ��", ps)); string cmdstr = ",��ʽ��,��ʼ��ID������,��ʼ��������,����ϵͳʱ��,���ؿ��Ʋ���,��ȡ���Ʋ���,����������Ϣ,"; string[] cmds = attrcmd.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < cmds.Length; i++) { if (cmdstr.IndexOf("," + cmds[i] + ",") < 0) continue; string tagdata = "@�豸��ַ=" + valst; if ("���ؿ��Ʋ���" == cmds[i]) { DataTable tabctrlpm = this.query.getTable("�豸���Ʋ���", ps); if (null == tabctrlpm || tabctrlpm.Rows.Count < 1) continue; tagdata = this.getctrlpm(tabctrlpm.Rows[0], valst); } else if ("����ϵͳʱ��" == cmds[i]) { tagdata = basefun.setvaltag(tagdata, "{ϵͳʱ��}", dtsystem.ToString("yyyy-MM-dd HH:mm:ss")); } else if ("����������Ϣ" == cmds[i]) { DataTable tabScress = this.query.getTable("����������Ϣ", ps); if (null == tabScress || tabScress.Rows.Count < 1) continue; string cmd = ""; string downtype = ""; string datatype = ""; string message = ""; if (string.IsNullOrEmpty(Convert.ToString(tabScress.Rows[0]["������Ϣ"]))) { cmd = "0000" + Internal_code(Convert.ToString(tabScress.Rows[0]["������Ϣ"])); downtype = "00"; datatype = "00"; message = Convert.ToString(tabScress.Rows[0]["������Ϣ"]); } else { cmd = "0100" + Internal_code(Convert.ToString(tabScress.Rows[0]["��ӡ��Ϣ"])); downtype = "01"; datatype = "00"; message = Convert.ToString(tabScress.Rows[0]["��ӡ��Ϣ"]); } tagdata = basefun.setvaltag(tagdata, "{���ط�ʽ}", downtype); tagdata = basefun.setvaltag(tagdata, "��������", datatype); tagdata = basefun.setvaltag(tagdata, "�Զ�����Ϣ", Internal_code(message)); string sLen = (cmd.Length / 2).ToString("X"); tagdata = basefun.setvaltag(tagdata, "{�����}", sLen); } cmdP.setCommand(tpl, cmds[i], tagdata); success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); if (!success) return false; //��ʽ��ʱ�豸��2.5s��Ӧ�� if ("��ʽ��" == cmds[i]) System.Threading.Thread.Sleep(3500); } //��������ָ�����ʵ�� TransArg arg = new TransArg(); arg.trans = trans; arg.target = tarsrv; arg.proxy = proxy; arg.dest = dest; arg.devID = devID; arg.addrst = Convert.ToString(drdevice["վַ"]); arg.attrCmdtag = attrcmd; //���ؼƷѱ� success = this.downPayRule(arg); //if (!success) return false; ////������������ //this.query.ExecuteNonQuery("�豸���ر�־����", ps, ps, ps); //���úڰ����� if (attrcmd.IndexOf("|��ʽ��|") > -1 || attrcmd.IndexOf("|��ʼ��ID������|") > -1 || attrcmd.IndexOf("|��ʼ��������|") > -1) this.query.ExecuteNonQuery("�����豸����", ps, ps, ps); success = this.downparkCardList(arg); //�豸������ʾ string tag = "@�豸��ַ=" + valst; this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, "����", tag); return success; }
/// <summary> /// �����豸��ʼ�������ز���,���ºڰ����� /// </summary> /// <param name="tarsrv">��ת������</param> /// <param name="trans">����ָ��</param> /// <param name="proxy">�豸ͨѶ�������IP��ַ</param> /// <param name="dest">Ŀ���豸</param> /// <param name="drdevice">�豸��Ϣ��¼��</param> /// <param name="attrcmd">ִ��ָ��</param> /// <returns>����ͨѶ���ز����Ƿ�ɹ�</returns> private bool commiDeviceEatery(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, DataRow drdevice, string attrcmd) { if (null == tarsrv || null == proxy || null == dest || null == trans || null == drdevice || string.IsNullOrEmpty(attrcmd)) return true; CmdProtocol cmdP = new CmdProtocol(false); string devID = Convert.ToString(drdevice["ID"]); //��������ָ�����ʵ�� TransArg arg = new TransArg(); arg.trans = trans; arg.target = tarsrv; arg.proxy = proxy; arg.dest = dest; arg.devID = devID; arg.addrst = Convert.ToString(drdevice["վַ"]); arg.attrCmdtag = attrcmd; NameObjectList ps = new NameObjectList(); ps["�豸ID"] = devID; bool success = false; string tpl = "����"; string valst = Convert.ToString(drdevice["վַ"]); //ϵͳʱ�� DateTime dtsystem = Convert.ToDateTime(this.query.ExecuteScalar("ϵͳʱ��", ps)); string cmdstr = ",��ʽ��,��ռ�¼,��ղ�������,��պ�����,���ʱ��,����ʱ��,����ʱ��,���ÿ��Ʋ���,"; string[] cmds = attrcmd.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < cmds.Length; i++) { if (cmdstr.IndexOf("," + cmds[i] + ",") < 0) continue; string tagdata = "@�豸��ַ=" + valst; if ("����ʱ��" == cmds[i]) tagdata = basefun.setvaltag(tagdata, "{����ʱ��}", dtsystem.ToString("yyyy-MM-dd HH:mm:ss")); if ("����ʱ��" == cmds[i]) { success = this.downeaterytime(arg); if (!success) return false; continue; } if ("���ÿ��Ʋ���" == cmds[i]) { success = this.downeateryctrlpm(arg); if (!success) return false; continue; } success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); //��ʽ��ʱ�豸��2.5s��Ӧ�� if ("��ʽ��" == cmds[i]) { System.Threading.Thread.Sleep(3500); continue; } if (!success) return false; } //������������ this.query.ExecuteNonQuery("�豸���ر�־����", ps, ps, ps); //���úڰ����� if (attrcmd.IndexOf("|��ʽ��|") > -1 || attrcmd.IndexOf("|��հ�����|") > -1) this.query.ExecuteNonQuery("�����豸����", ps, ps, ps); success = this.downeateryCardList(arg); if (!success) return false; //�������ѿ�û�н��յ��IJ�����������δ���صIJ��� success = this.downeaterySubsidy(arg); return success; }
/// <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> /// <returns>���ش�����ʾ��������ʾ�ɹ�</returns> public string downCardALL() { NameObjectList ps = new NameObjectList(); DataTable tab = this.query.getTable("�豸�б�", ps); if (null == tab || tab.Rows.Count < 1) return ""; string devid = ""; string attrcmd = "|����ID������|���غ�����|ɾ��������|ɾ��ID������|���ذ�����|"; //������ת������ CmdFileTrans trans = new CmdFileTrans(false); int port = 2010; string sport = DataAccRes.AppSettings("Granity�ļ�����"); if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return ""; } string conn = DataAccRes.DefaultDataConnInfo.Value; Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); string ipsrv = "127.0.0.1"; if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); ipsrv = Dns.GetHostAddresses(ipsrv)[0].ToString(); } CommiTarget tarsrv = new CommiTarget(ipsrv, port, CommiType.TCP); tarsrv.setProtocol(CmdFileTrans.PTL); string msg = ""; foreach (DataRow dr in tab.Rows) { //����IP,�������Ƿ�local,ͨѶĿ�� string addr = Convert.ToString(dr["IP��ַ"]); if (string.IsNullOrEmpty(addr)) continue; IPAddress proxy = null; try { proxy = IPAddress.Parse(addr); } catch (Exception ex) { NameValueCollection data = new NameValueCollection(); data["IP��ַ"] = addr; ServiceTool.LogMessage(ex, data, EventLogEntryType.Error); msg += string.Format("��{0}({1})", dr["����"], dr["IP��ַ"]); continue; } CommiTarget dest = this.getTarget(dr); if (null == dest) { msg += string.Format("��{0}({1})", dr["����"], dr["IP��ַ"]); continue; } string tpl = Convert.ToString(dr["ͨѶЭ��"]); bool rtn = true; switch (tpl) { case "ͣ����": dest.setProtocol(Protocol.PTLPark); rtn = this.commiDevicePark(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "�Ž�": dest.setProtocol(Protocol.PTLDoor); rtn = this.commiDeviceDoor(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "����": dest.setProtocol(Protocol.PTLEatery); rtn = this.commiDeviceEatery(tarsrv, trans, proxy, dest, dr, attrcmd); break; } if (!rtn) msg += string.Format("��{0}({1})", dr["����"], dr["IP��ַ"]); } if (!string.IsNullOrEmpty(msg)) msg = msg.Substring(1); return msg; }
/// <summary> /// 停车场设备初始化和下载参数,更新黑白名单 /// </summary> /// <param name="tarsrv">中转服务器</param> /// <param name="trans">传输指令</param> /// <param name="proxy">设备通讯代理电脑IP地址</param> /// <param name="dest">目标设备</param> /// <param name="drdevice">设备信息记录行</param> /// <param name="attrcmd">执行指令</param> /// <returns>返回通讯下载参数是否成功</returns> private bool commiDevicePark(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, DataRow drdevice, string attrcmd) { if (null == tarsrv || null == proxy || null == dest || null == trans || null == drdevice || string.IsNullOrEmpty(attrcmd)) return true; CmdProtocol cmdP = new CmdProtocol(false); this.setTimeout(cmdP); string devID = Convert.ToString(drdevice["ID"]); NameObjectList ps = new NameObjectList(); ps["设备ID"] = devID; bool success = false; //系统时间 string tpl = "停车场"; string valst = Convert.ToString(drdevice["站址"]); Monitor.Enter(query); DateTime dtsystem = Convert.ToDateTime(this.query.ExecuteScalar("系统时间", ps)); Monitor.PulseAll(query); Monitor.Exit(query); string cmdstr = ",格式化,初始化ID白名单,初始化黑名单,加载系统时间,下载控制参数,"; string[] cmds = attrcmd.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < cmds.Length; i++) { if (cmdstr.IndexOf("," + cmds[i] + ",") < 0) continue; string tagdata = "@设备地址=" + valst; if ("下载控制参数" == cmds[i]) { Monitor.Enter(query); DataTable tabctrlpm = this.query.getTable("设备控制参数", ps); Monitor.PulseAll(query); Monitor.Exit(query); if (null == tabctrlpm || tabctrlpm.Rows.Count < 1) continue; tagdata = this.getctrlpm(tabctrlpm.Rows[0], valst); } else if ("加载系统时间" == cmds[i]) { tagdata = basefun.setvaltag(tagdata, "{系统时间}", dtsystem.ToString("yyyy-MM-dd HH:mm:ss")); } success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); if (!success) return false; //格式化时设备有2.5s不应期 if ("格式化" == cmds[i]) System.Threading.Thread.Sleep(3500); } //建立传输指令参数实例 TransArg arg = new TransArg(); arg.trans = trans; arg.target = tarsrv; arg.proxy = proxy; arg.dest = dest; arg.devID = devID; arg.addrst = Convert.ToString(drdevice["站址"]); arg.attrCmdtag = attrcmd; //下载计费标准 success = this.downPayRule(arg); if (!success) return false; //更新下载日期 Monitor.Enter(query); this.query.ExecuteNonQuery("设备下载标志更新", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); //重置黑白名单 if (attrcmd.IndexOf("|格式化|") > -1 || attrcmd.IndexOf("|初始化ID白名单|") > -1 || attrcmd.IndexOf("|初始化黑名单|") > -1) { Monitor.Enter(query); this.query.ExecuteNonQuery("重置设备名单", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); } success = this.downparkCardList(arg); //设备蜂鸣提示 string tag = "@设备地址=" + valst; this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, "蜂鸣", tag); return success; }
/// <summary>- /// 保存设备 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsbSave_Click(object sender, EventArgs e) { this.validateFeeTab(); DataTable tabpark = this.ds.Tables["停车场管理"]; DataTable tabfee = this.ds.Tables["收费标准"]; DataTable tabdspan = this.ds.Tables["收费标准时段"]; DataTable tabdtime = this.ds.Tables["收费标准时刻"]; DataTable[] tablist ={ tabpark, tabfee, tabdspan, tabdtime }; bool isSuccess = this.Query.BeginTransaction(); string msg = ""; if (!isSuccess) msg = "保存失败,无法打开事务!"; else { foreach (DataTable tab in tablist) { NameObjectList[] psins = ParamManager.createParam(tab, DataRowState.Added); NameObjectList[] psupt = ParamManager.createParam(tab, DataRowState.Modified); NameObjectList[] psdel = ParamManager.createParam(tab, DataRowState.Deleted); isSuccess = this.Query.ExecuteNonQuery(tab.TableName, psins, psupt, psdel); if (!isSuccess) { msg = "保存失败,请检查数据是否合法!"; break; } } } if (!isSuccess) this.Query.RollbackAndClose(); else { this.Query.Commit(); this.Query.Close(); foreach (DataTable tab in tablist) tab.AcceptChanges(); //发出通讯更新设备 int port = 2010; string sport = DataAccRes.AppSettings("Granity文件服务"); if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return; } string conn = DataAccRes.DefaultDataConnInfo.Value; Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); string ipsrv = "127.0.0.1"; if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); ipsrv = Dns.GetHostAddresses(ipsrv)[0].ToString(); } CmdFileTrans cmd = new CmdFileTrans(false); cmd.ExtRefreshDevice(); CommiTarget target = new CommiTarget(ipsrv, port, CommiType.TCP); CommiManager.GlobalManager.SendCommand(target, cmd); } if (!isSuccess) MessageBox.Show(msg, "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); else MessageBox.Show("保存成功!", "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
/// <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; }
/// <summary> /// 强制对设备做全部初始化和下载参数,更新黑白名单 /// </summary> /// <param name="tagdevcmds">设备ID和下载指令,tag格式,其中指令以"|"分割</param> public bool CommiDevice(string tagdevcmds) { string devID = basefun.valtag(tagdevcmds, "设备ID"); string attrcmd = "|" + basefun.valtag(tagdevcmds, "指令") + "|"; if (string.IsNullOrEmpty(tagdevcmds)) return false; //设置中转服务器 CmdFileTrans trans = new CmdFileTrans(false); trans.TimeOut = new TimeSpan(0, 0, 0, 0, 600); trans.TimeFailLimit = new TimeSpan(0, 0, 0, 2); int port = 2010; string sport = DataAccRes.AppSettings("Granity文件服务"); if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return false; } string conn = DataAccRes.DefaultDataConnInfo.Value; Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); string ipsrv = "127.0.0.1"; if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); IPAddress[] ips = new IPAddress[] { }; try { ips = Dns.GetHostAddresses(ipsrv); } catch { ipsrv = Dns.GetHostName(); ips = Dns.GetHostAddresses(ipsrv); } foreach (IPAddress ip in ips) { if (ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) continue; ipsrv = ip.ToString(); } } CommiTarget tarsrv = new CommiTarget(ipsrv, port, CommiType.TCP); tarsrv.setProtocol(CmdFileTrans.PTL); NameObjectList ps = new NameObjectList(); ps["设备ID"] = devID; Monitor.Enter(query); DataTable tab = this.query.getTable("设备通讯参数", ps); Monitor.PulseAll(query); Monitor.Exit(query); if (null == tab || tab.Rows.Count < 1) return true; DataRow dr = tab.Rows[0]; //代理IP,检查代理是否本地local,通讯目的 string addr = Convert.ToString(dr["IP地址"]); if (string.IsNullOrEmpty(addr)) return true; IPAddress proxy = null; try { proxy = IPAddress.Parse(addr); } catch (Exception ex) { NameValueCollection data = new NameValueCollection(); data["IP地址"] = addr; ServiceTool.LogMessage(ex, null, EventLogEntryType.Error); return false; } CommiTarget dest = this.getTarget(dr); if (null == dest) return false; string tpl = Convert.ToString(dr["通讯协议"]); bool rtn = true; switch (tpl) { case "停车场": dest.setProtocol(Protocol.PTLPark); rtn = this.commiDevicePark(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "门禁": string ctrltype = Convert.ToString(dr["控制器类型"]); dest.setProtocol(Protocol.PTLDoor); if ("进出口" == ctrltype || "出入口" == ctrltype) rtn = this.commiDeviceChannel(tarsrv, trans, proxy, dest, dr, attrcmd); else rtn = this.commiDeviceDoor(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "消费": dest.setProtocol(Protocol.PTLEatery); rtn = this.commiDeviceEatery(tarsrv, trans, proxy, dest, dr, attrcmd); break; } myLog.Flush(); return rtn; }
/// <summary> /// 消费设备初始化和下载参数,更新黑白名单 /// </summary> /// <param name="tarsrv">中转服务器</param> /// <param name="trans">传输指令</param> /// <param name="proxy">设备通讯代理电脑IP地址</param> /// <param name="dest">目标设备</param> /// <param name="drdevice">设备信息记录行</param> /// <param name="attrcmd">执行指令</param> /// <returns>返回通讯下载参数是否成功</returns> private bool commiDeviceEatery(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, DataRow drdevice, string attrcmd) { if (null == tarsrv || null == proxy || null == dest || null == trans || null == drdevice || string.IsNullOrEmpty(attrcmd)) return true; CmdProtocol cmdP = new CmdProtocol(false); this.setTimeout(cmdP); string devID = Convert.ToString(drdevice["ID"]); //建立传输指令参数实例 TransArg arg = new TransArg(); arg.trans = trans; arg.target = tarsrv; arg.proxy = proxy; arg.dest = dest; arg.devID = devID; arg.addrst = Convert.ToString(drdevice["站址"]); arg.attrCmdtag = attrcmd; NameObjectList ps = new NameObjectList(); ps["设备ID"] = devID; bool success = false; string tpl = "消费"; string valst = Convert.ToString(drdevice["站址"]); //系统时间 Monitor.Enter(query); DateTime dtsystem = Convert.ToDateTime(this.query.ExecuteScalar("系统时间", ps)); Monitor.PulseAll(query); Monitor.Exit(query); string cmdstr = ",格式化,清空记录,清空补助名单,清空黑名单,清空时段,设置时间,设置时段,设置控制参数,"; string[] cmds = attrcmd.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < cmds.Length; i++) { if (cmdstr.IndexOf("," + cmds[i] + ",") < 0) continue; string tagdata = "@设备地址=" + valst; if ("设置时间" == cmds[i]) tagdata = basefun.setvaltag(tagdata, "{日期时间}", dtsystem.ToString("yyyy-MM-dd HH:mm:ss")); if ("设置时段" == cmds[i]) { success = this.downeaterytime(arg); if (!success) return false; continue; } if ("设置控制参数" == cmds[i]) { success = this.downeateryctrlpm(arg); if (!success) return false; continue; } success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); //格式化时设备有2.5s不应期 if ("格式化" == cmds[i]) { System.Threading.Thread.Sleep(3500); continue; } if (!success) return false; } //更新下载日期 Monitor.Enter(query); this.query.ExecuteNonQuery("设备下载标志更新", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); //重置黑白名单 if (attrcmd.IndexOf("|格式化|") > -1 || attrcmd.IndexOf("|清空白名单|") > -1) { Monitor.Enter(query); this.query.ExecuteNonQuery("重置设备名单", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); } success = this.downeateryCardList(arg); if (!success) return false; //回收消费卡没有接收到的补助,并下载未下载的补助 success = this.downeaterySubsidy(arg); return success; }
/// <summary> /// 更新所有设备黑白名单 /// </summary> /// <returns>返回错误提示,无错误提示成功</returns> public string downCardALL() { NameObjectList ps = new NameObjectList(); Monitor.Enter(this.query); DataTable tab = this.query.getTable("设备列表", ps); Monitor.PulseAll(this.query); Monitor.Exit(this.query); if (null == tab || tab.Rows.Count < 1) return ""; string attrcmd = "|下载ID白名单|下载黑名单|删除白名单|删除ID白名单|下载白名单|"; //设置中转服务器 CmdFileTrans trans = new CmdFileTrans(false); trans.TimeOut = new TimeSpan(0, 0, 0, 0, 600); trans.TimeFailLimit = new TimeSpan(0, 0, 0, 2); int port = 2010; string sport = DataAccRes.AppSettings("Granity文件服务"); if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return ""; } string conn = DataAccRes.DefaultDataConnInfo.Value; Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); string ipsrv = "127.0.0.1"; if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); IPAddress[] ips = Dns.GetHostAddresses(ipsrv); foreach (IPAddress ip in ips) { if (ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) continue; ipsrv = ip.ToString(); } } CommiTarget tarsrv = new CommiTarget(ipsrv, port, CommiType.TCP); tarsrv.setProtocol(CmdFileTrans.PTL); string msg = ""; foreach (DataRow dr in tab.Rows) { //代理IP,检查代理是否本地local,通讯目的 string addr = Convert.ToString(dr["IP地址"]); if (string.IsNullOrEmpty(addr)) continue; IPAddress proxy = null; try { proxy = IPAddress.Parse(addr); } catch (Exception ex) { NameValueCollection data = new NameValueCollection(); data["IP地址"] = addr; ServiceTool.LogMessage(ex, data, EventLogEntryType.Error); msg += string.Format("、{0}({1})", dr["名称"], dr["IP地址"]); continue; } CommiTarget dest = this.getTarget(dr); if (null == dest) { msg += string.Format("、{0}({1})", dr["名称"], dr["IP地址"]); continue; } string tpl = Convert.ToString(dr["通讯协议"]); bool rtn = true; switch (tpl) { case "停车场": dest.setProtocol(Protocol.PTLPark); rtn = this.commiDevicePark(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "门禁": string ctrltype = Convert.ToString(dr["控制器类型"]); dest.setProtocol(Protocol.PTLDoor); if ("进出口" == ctrltype || "出入口" == ctrltype) rtn = this.commiDeviceChannel(tarsrv, trans, proxy, dest, dr, attrcmd); else rtn = this.commiDeviceDoor(tarsrv, trans, proxy, dest, dr, attrcmd); break; case "消费": dest.setProtocol(Protocol.PTLEatery); rtn = this.commiDeviceEatery(tarsrv, trans, proxy, dest, dr, attrcmd); break; } if (!rtn) msg += string.Format("、{0}({1})", dr["名称"], dr["IP地址"]); } if (!string.IsNullOrEmpty(msg)) msg = msg.Substring(1); return msg; }
/// <summary> /// 门禁设备初始化和下载参数,更新黑白名单 /// </summary> /// <param name="tarsrv">中转服务器</param> /// <param name="trans">传输指令</param> /// <param name="proxy">设备通讯代理电脑IP地址</param> /// <param name="dest">目标设备</param> /// <param name="drdevice">设备信息记录行</param> /// <param name="attrcmd">执行指令</param> /// <returns>返回通讯下载参数是否成功</returns> private bool commiDeviceDoor(CommiTarget tarsrv, CmdFileTrans trans, IPAddress proxy, CommiTarget dest, DataRow drdevice, string attrcmd) { string iskq = ""; if (null == tarsrv || null == proxy || null == dest || null == trans || null == drdevice || string.IsNullOrEmpty(attrcmd)) return true; CmdProtocol cmdP = new CmdProtocol(false); this.setTimeout(cmdP); string devID = Convert.ToString(drdevice["ID"]); //建立传输指令参数实例 TransArg arg = new TransArg(); arg.trans = trans; arg.target = tarsrv; arg.proxy = proxy; arg.dest = dest; arg.devID = devID; arg.addrst = Convert.ToString(drdevice["站址"]); arg.attrCmdtag = attrcmd; NameObjectList ps = new NameObjectList(); ps["设备ID"] = devID; bool success = false; string tpl = "门禁"; string valst = Convert.ToString(drdevice["站址"]); //系统时间 Monitor.Enter(query); DateTime dtsystem = Convert.ToDateTime(this.query.ExecuteScalar("系统时间", ps)); Monitor.PulseAll(query); Monitor.Exit(query); string cmdstr = ",格式化,清空记录,清空白名单,清空时段,设置时间,设置时段,设置控制参数,修改名单,"; string[] cmds = attrcmd.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < cmds.Length; i++) { if (cmdstr.IndexOf("," + cmds[i] + ",") < 0) continue; string tagdata = "@设备地址=" + valst; if ("设置时间" == cmds[i]) tagdata = basefun.setvaltag(tagdata, "{日期时间}", dtsystem.ToString("yyyy-MM-dd HH:mm:ss")); if ("设置时段" == cmds[i]) { Monitor.Enter(query); DataTable tabtime = this.query.getTable("门禁时段", ps); Monitor.PulseAll(query); Monitor.Exit(query); if (null == tabtime || tabtime.Rows.Count < 1) continue; foreach (DataRow dr in tabtime.Rows) { tagdata = this.getdoortime(dr); tagdata = basefun.setvaltag(tagdata, "设备地址", valst); success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); if (!success) return false; } continue; } if ("设置控制参数" == cmds[i]) { success = this.downdoorctrlpm(arg); if (!success) return false; continue; } success = this.sendCommand(tarsrv, trans, proxy, dest, cmdP, tpl, cmds[i], tagdata); //格式化时设备有2.5s不应期 if ("格式化" == cmds[i]) { Monitor.Enter(query); this.query.ExecuteNonQuery("门禁记录位置重置", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); System.Threading.Thread.Sleep(3500); continue; } if (!success) return false; if ("清空记录" == cmds[i]) { Monitor.Enter(query); this.query.ExecuteNonQuery("门禁记录位置重置", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); } if ("清空白名单" == cmds[i]) System.Threading.Thread.Sleep(20000); if ("修改名单" == cmds[i]) { iskq = "修改名单"; } myLog.WriteLine("当前指令 11111" + iskq); } //更新下载日期 Monitor.Enter(query); this.query.ExecuteNonQuery("设备下载标志更新", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); //重置黑白名单 if (attrcmd.IndexOf("|格式化|") > -1 || attrcmd.IndexOf("|清空白名单|") > -1) { Monitor.Enter(query); this.query.ExecuteNonQuery("重置设备名单", ps, ps, ps); Monitor.PulseAll(query); Monitor.Exit(query); } success = this.downdoorCardList(arg, iskq); return success; }
public void ProcessRequest(HttpContext context) { //设备ID和下载指令,tag格式,其中多个设备和指令需以"|"分割 StreamReader reader = new StreamReader(context.Request.InputStream); string tag = reader.ReadToEnd(); if (string.IsNullOrEmpty(tag)) return; string cmd = basefun.valtag(tag, "指令"); if ("设备同步黑白名单" == cmd) { SynDeviceParam syn = new SynDeviceParam(); string msg = syn.downCardALL(); bool success = true; if (!string.IsNullOrEmpty(msg)) { success = false; msg = basefun.setvaltag("", "提示", msg); } msg = basefun.setvaltag(msg, "成功", success ? "true" : "false"); context.Response.Write(msg); } Debug.WriteLine(DateTime.Now.ToString("HH:mm.ss.fff") + " 发出请求"); string reqid = basefun.valtag(tag, "reqID"); //已经在处理请求的返回结果信息 if (reqlist.ContainsKey(reqid)) { reqParam reqparam = reqlist[reqid]; reqparam.dtreq = DateTime.Now; string[] devs = new string[0]; if (!string.IsNullOrEmpty(reqparam.reqdev)) devs = reqparam.reqdev.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (reqparam.taglist.Count >= devs.Length) reqlist.Remove(reqid); string msg = ""; foreach (string t in reqparam.taglist) msg += t + ";"; Debug.WriteLine(DateTime.Now.ToString("HH:mm.ss.fff") + " 结果:" + msg); context.Response.Write(msg); return; } List<string> keys = new List<string>(); foreach (string k in reqlist.Keys) keys.Add(k); DateTime dt = DateTime.Now.AddMinutes(-1); foreach (string k in keys) { if (reqlist[k].dtreq < dt) reqlist.Remove(k); } string devid = basefun.valtag(tag, "设备ID"); if (string.IsNullOrEmpty(reqid) || string.IsNullOrEmpty(devid) || string.IsNullOrEmpty(cmd)) return; reqParam param = new reqParam(); param.reqid = reqid; param.reqdev = devid; reqlist.Add(reqid, param); //对设备启动或停止监控, patrol持续巡检 CommiManager mgr = CommiManager.GlobalManager; CommiTarget target = getService(); NameValueCollection info = new NameValueCollection(); string tagdev = basefun.setvaltag("", "reqID", reqid); tagdev = basefun.setvaltag(tagdev, "指令", cmd); string[] devids = devid.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < devids.Length; i++) { tagdev = basefun.setvaltag(tagdev, "设备ID", devids[i]); //暂停巡检 info["device"] = devids[i]; if (null != target) { CmdFileTrans cmdtrans = new CmdFileTrans(false); cmdtrans.ExtService(CmdSrvType.HaltDevice, info); mgr.SendCommand(target, cmdtrans, true); } ThreadManager.QueueUserWorkItem(delegate(object obj) { syncommi(obj); }, tagdev); } }
/// <summary> /// 接收传递通讯 /// 包含参数: len,dir=request/response,cmd=TransFile.trans,source,target, /// CommiType,addr=ipaddr/COM1,port,baudRate,parity,dataBits,stopBits /// </summary> /// <param name="sender">触发事件的实例</param> /// <param name="e">响应参数</param> private static void onCommiTransHandle(object sender, ResponseEventArgs e) { CmdFileTrans trans = sender as CmdFileTrans; if (null == trans || null == e || null == e.Target) { return; } NameValueCollection ps = trans.FileHeader; byte[] data = trans.FileContext; if ("TransFile.trans" != ps["cmd"] || "request" != ps["dir"]) { return; } if (string.IsNullOrEmpty(ps["CommiType"]) || string.IsNullOrEmpty(ps["addr"])) { return; } CommiType commitype = CommiType.SerialPort; string addr = ps["addr"]; int port = -1; //检测地址 try { addr = addr.ToUpper(); if (!addr.StartsWith("COM")) { commitype = (CommiType)Enum.Parse(typeof(CommiType), ps["CommiType"], true); } if (!string.IsNullOrEmpty(ps["port"])) { port = Convert.ToInt32(ps["port"]); } } catch { return; } //检测通讯参数 if (CommiType.SerialPort != commitype && port < 0) { return; } if (CommiType.SerialPort == commitype && (string.IsNullOrEmpty(ps["baudRate"]) || string.IsNullOrEmpty(ps["parity"]) || string.IsNullOrEmpty(ps["dataBits"]) || string.IsNullOrEmpty(ps["stopBits"]))) { return; } //创建通讯目标 CommiTarget target = null; if (CommiType.SerialPort != commitype) { target = new CommiTarget(addr, port, commitype); } else { try { int baudRate = Convert.ToInt32(ps["baudRate"]); Parity parity = (Parity)Enum.Parse(typeof(Parity), ps["parity"], true); int databits = Convert.ToInt16(ps["dataBits"]); StopBits stopBits = (StopBits)Enum.Parse(typeof(StopBits), ps["stopBits"], true); target = new CommiTarget(addr, baudRate, parity, databits, stopBits); } catch { return; } } target.setProtocol(Protocol.PTLCard); switch (ps["dtpl"]) { case "停车场": target.setProtocol(Protocol.PTLPark); break; case "门禁": target.setProtocol(Protocol.PTLDoor); break; case "消费": target.setProtocol(Protocol.PTLEatery); break; } //执行传递的指令 CmdProtocol cmd = new CmdProtocol(false); cmd.setCommand(data); ps["dir"] = "response"; CommiManager.GlobalManager.SendCommand(target, cmd); if (!cmd.EventWh.WaitOne(800, false)) { ps["msg"] = "目标通讯超时失败"; } data = cmd.ResponseData; ps["len"] = data.LongLength.ToString(); byte[] fh = SvrFileTrans.ParseInfo(ps); byte[] response = new byte[fh.LongLength + data.LongLength]; Array.Copy(fh, response, fh.LongLength); if (data.Length > 0) { Array.Copy(data, 0, response, fh.LongLength, data.LongLength); } cmd.setCommand(response); CommiManager.GlobalManager.SendCommand(e.Target, cmd, true); }
/// <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; }
/// <summary> /// ��ͼƬ������������ /// </summary> /// <param name="filepath">�ļ�·��</param> /// <param name="filedest">Ŀ��·��,���·��</param> public void SendImage(string filepath, string filedest) { if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath)) return; int port = 2010; string ipsrv = "127.0.0.1"; string sport = DataAccRes.AppSettings("Granity�ļ�����"); string conn = DataAccRes.DefaultDataConnInfo.Value; if (!string.IsNullOrEmpty(sport)) try { port = Convert.ToInt32(sport); } catch { return; } Regex regIP = new Regex(@"server=([\w.\(\)]*)(;|\\)"); if (regIP.IsMatch(conn)) { Match mt = regIP.Match(conn); if (mt.Groups.Count > 1) ipsrv = mt.Groups[1].Value.ToLower(); if ("(local)" == ipsrv || "127.0.0.1" == ipsrv) ipsrv = Dns.GetHostName(); ipsrv = Dns.GetHostAddresses(ipsrv)[0].ToString(); } byte[] context = File.ReadAllBytes(filepath); CmdFileTrans cmd = new CmdFileTrans(false); cmd.SendFile(filedest, context); CommiTarget target = new CommiTarget(ipsrv, port, CommiType.TCP); CommiManager.GlobalManager.SendCommand(target, cmd); target.setProtocol(CmdFileTrans.PTL); if (!cmd.EventWh.WaitOne(new TimeSpan(0, 0, 15), false)) return; }