/// <summary> /// 添加监控管理的设备 /// </summary> /// <param name="devid">请求的客户端</param> /// <param name="ispatrol">是否持续巡检</param> private void addDevice(string devid, bool ispatrol) { //设备已经监控,则不再增加设备信息 DeviceBase[] devices = this.devlist.ToArray(); for (int i = 0; i < devices.Length; i++) { if (devid == devices[i].DevID) { return; } } //增加设备监控 Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList(); ps["设备ID"] = devid; DataTable tab = this.query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) { return; } DataRow dr = tab.Rows[0]; string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) { return; } int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "门禁": device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return; } device.IsPatrol = ispatrol; device.SetDevice(commimgr, target, devid, station); this.devlist.Add(device); device.RecordHandle += new EventHandler <DvRecordEventArgs>(device_RecordHandle); device.SignalHandle += new EventHandler <DvSignalEventArgs>(device_SignalHandle); device.AlarmHandle += new EventHandler <DvAlarmEventArgs>(device_AlarmHandle); device.StartGather(); }
/// <summary> /// 添加监控管理的设备 /// </summary> /// <param name="devid">请求的客户端</param> /// <param name="ispatrol">是否持续巡检</param> private DeviceBase addDevice(string devid, bool ispatrol) { //增加设备监控 Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList(); ps["设备ID"] = devid; DataTable tab = this.query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) { return(null); } DataRow dr = tab.Rows[0]; string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) { return(null); } int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "门禁": device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return(null); } device.IsPatrol = ispatrol; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid, station); this.devlist.Add(device); device.StartGather(); return(device); }
/// <summary> /// 读取记录数据 /// </summary> /// <param name="devices">设备ID,逗号分割</param> /// <returns>转换成表格数据</returns> private string readRecord(string devices) { if (string.IsNullOrEmpty(devices)) return ""; //没有启动巡检的设备,启动巡检 string[] devid = devices.ToLower().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string rdformat = "<Row><Device>{0}</Device><Dt>{1}</Dt><Data>{2}</Data></Row>"; string data = ""; //检查设备,没有的添加入缓存 CommiManager commimgr = CommiManager.GlobalManager; DeviceBase[] devs = devlist.ToArray(); DeviceBase[] reqs = new DeviceBase[devid.Length]; for (int i = 0; i < devid.Length; i++) { DeviceBase device = null; //检查设备已缓存 for (int j = 0; j < devs.Length; j++) { if (devid[i] != devs[j].DevID) continue; device = reqs[i] = devs[j]; break; } //增加设备监控 if (null != device) continue; NameObjectList ps = new NameObjectList(); ps["设备ID"] = devid[i]; DataTable tab = query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) continue; DataRow dr = tab.Rows[0]; string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) continue; int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); if (null == target) continue; switch (dvtype) { case "门禁": string ctrltype = Convert.ToString(dr["控制器类型"]); if ("进出口" == ctrltype || "出入口" == ctrltype) device = new DeviceChannel(); else device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: continue; } device.IsPatrol = false; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid[i], station); devlist.Add(device); reqs[i] = device; } //通讯获取数据 List<ManualResetEvent> ehm = new List<ManualResetEvent>(); for (int i = 0; i < reqs.Length; i++) { DeviceSyn syn = new DeviceSyn(); syn.dev = reqs[i]; syn.eh = new ManualResetEvent(false); ehm.Add(syn.eh); ThreadManager.QueueUserWorkItem(delegate(object obj) { this.readRecordSyn(obj); }, syn); } ManualResetEvent[] ehs = ehm.ToArray(); ManualResetEvent.WaitAll(ehs); myLog.Flush(); return dataResult; }
/// <summary> /// 添加监控管理的设备 /// </summary> /// <param name="devid">请求的客户端</param> /// <param name="ispatrol">是否持续巡检</param> private DeviceBase addDevice(string devid, bool ispatrol) { //增加设备监控 NameObjectList ps = new NameObjectList(); ps["设备ID"] = devid; DataTable tab = this.query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) { return(null); } DataRow dr = tab.Rows[0]; if (!tab.Columns.Contains("通讯协议")) { string msg = ""; foreach (object obj in dr.ItemArray) { msg += Convert.ToString(obj) + " "; } Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 设备通讯参数值:" + msg); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 设备通讯参数值:" + msg); return(null); } string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) { return(null); } int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); if (null == target) { return(null); } CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "门禁": Debug.WriteLine("门禁"); string ctrltype = Convert.ToString(dr["控制器类型"]); if ("进出口" == ctrltype || "出入口" == ctrltype) { device = new DeviceChannel(); } else { device = new DeviceDoor(); } target.setProtocol(Protocol.PTLDoor); device.AlarmHandle += new EventHandler <DvAlarmEventArgs>(device_AlarmHandle); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return(null); } device.RecordHandle += new EventHandler <DvRecordEventArgs>(device_RecordHandle); Monitor.Enter(devlist); this.devlist.Add(device); Monitor.PulseAll(devlist); Monitor.Exit(devlist); device.IsPatrol = ispatrol; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid, station); device.StartGather(); Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 启动:" + Convert.ToString(station) + " 设备ID:" + devid); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 启动:" + Convert.ToString(station) + " 设备ID:" + devid); return(device); }
/// <summary> /// 添加监控管理的设备 /// </summary> /// <param name="devid">请求的客户端</param> /// <param name="ispatrol">是否持续巡检</param> private void addDevice(string devid, bool ispatrol) { //设备已经监控,则不再增加设备信息 DeviceBase[] devices = this.devlist.ToArray(); for (int i = 0; i < devices.Length; i++) if (devid == devices[i].DevID) return; //增加设备监控 Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList(); ps["设备ID"] = devid; DataTable tab = this.query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) return; DataRow dr = tab.Rows[0]; string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) return; int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "门禁": device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return; } device.IsPatrol = ispatrol; device.SetDevice(commimgr, target, devid, station); this.devlist.Add(device); device.RecordHandle += new EventHandler<DvRecordEventArgs>(device_RecordHandle); device.SignalHandle += new EventHandler<DvSignalEventArgs>(device_SignalHandle); device.AlarmHandle += new EventHandler<DvAlarmEventArgs>(device_AlarmHandle); device.StartGather(); }
/// <summary> /// 定时执行消费机业务 /// </summary> /// <param name="obj"></param> void tmEateryGather(object obj) { QueryDataRes query = new QueryDataRes("消费机管理"); NameObjectList ps = new NameObjectList(); DataTable tab = query.getTable("消费机管理", ps); if (null == tab || tab.Rows.Count < 1) return; myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 启动定时执行"); CommiManager commimgr = CommiManager.GlobalManager; for (int i = 0; i < tab.Rows.Count; i++) { DataRow dr = tab.Rows[i]; if (null == dr || DBNull.Value == dr["ID"] || DBNull.Value == dr["站址"]) continue; string devid = Convert.ToString(dr["ID"]); int station = Convert.ToInt32(dr["站址"]); string devname = devname = Convert.ToString(dr["名称"]); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 消费设备:" + Convert.ToString(dr["站址"]) + " " + devname); CommiTarget target = this.getTarget(dr); if (null == target) continue; DeviceEatery device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); device.IsPatrol = false; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid, station); string msg = device.GatherData(true); string cardnum = basefun.valtag(msg, "{卡号}"); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 采集信息 0:" + msg); if ("0" == cardnum || "16777215" == cardnum) { if (device.IsEndReadDevice(-1)) continue; } int index = 1; while (!string.IsNullOrEmpty(cardnum)) { msg = device.GatherData(false); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 采集信息" + Convert.ToString(index++).PadLeft(4, ' ') + ":" + msg); cardnum = basefun.valtag(msg, "{卡号}"); if ("0" != cardnum && "16777215" != cardnum) { if (device.IsEndReadDevice(-1)) break; } } } }
/// <summary> /// ��Ӽ�ع�����豸 /// </summary> /// <param name="devid">����Ŀͻ���</param> /// <param name="ispatrol">�Ƿ����Ѳ��</param> private DeviceBase addDevice(string devid, bool ispatrol) { //�����豸��� Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList(); ps["�豸ID"] = devid; DataTable tab = this.query.getTable("�豸ͨѶ����", ps); if (null == tab || tab.Rows.Count < 1) return null; DataRow dr = tab.Rows[0]; string dvtype = Convert.ToString(dr["ͨѶЭ��"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["վַ"]) return null; int station = Convert.ToInt32(dr["վַ"]); CommiTarget target = this.getTarget(dr); CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "�Ž�": device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); break; case "����": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return null; } device.IsPatrol = ispatrol; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid, station); this.devlist.Add(device); device.StartGather(); return device; }
private static void gatherData(object obj) { dtRunHdl = DateTime.Now; while (true) { hdlWh.Reset(); for (int i = 0; i < 200; i++) { if (dvQueue.Count > 0) { break; } dtRunHdl = DateTime.Now; hdlWh.WaitOne(20); } if (dvQueue.Count < 1) { dtRunHdl = DateTime.MinValue; break; } DeviceEatery eatery = null; Monitor.Enter(dvQueue); try { eatery = dvQueue.Dequeue(); } catch { } Monitor.PulseAll(dvQueue); Monitor.Exit(dvQueue); if (null == eatery) { continue; } //采集记录 eatery.reChecking(0); CmdProtocol cmdP = new CmdProtocol(false); setTimeout(cmdP); cmdP.TimeFailLimit = cmdP.TimeOut.Add(new TimeSpan(-10 * 10000)); cmdP.TimeLimit = TimeSpan.MaxValue; cmdP.TimeSendInv = new TimeSpan(1, 0, 0); cmdP.ResponseHandle += new EventHandler <ResponseEventArgs>(eatery.execResponse); string tag = "@设备地址=" + Convert.ToString(eatery.station); NameValueCollection datainfo = new NameValueCollection(); for (int i = 0; i < 200; i++) { string strcmd = i < 1 ? "取当前消费记录" : "取下一条消费记录"; if ("补助机" == eatery.devName) { strcmd = i < 1 ? "取当前补助记录" : "取下一条补助记录"; } string msg = eatery.getResponse(eatery.commimgr, eatery.target, cmdP, strcmd, tag, eatery.waitTime); if (string.IsNullOrEmpty(msg)) { continue; } string cardnum = basefun.valtag(msg, "{卡号}"); string suc = basefun.valtag(msg, "Success"); if ("true" != suc || "16777215" == cardnum || "0" == cardnum) { eatery.commimgr.RemoveCommand(eatery.target, cmdP); bool isend = basefun.valtag(msg, "{状态}").Contains("无新记录"); if (!isend) { isend = eatery.IsEndReadDevice(-1); } //记录错误时记入日志,继续下一条 if ("false" == suc) { datainfo["操作"] = "读取记录失败"; datainfo["报文"] = CommandBase.Parse(cmdP.ResponseData, true); ServiceTool.LogMessage(msg, datainfo, EventLogEntryType.FailureAudit); } if (isend) { break; } } eatery.writeRecord(msg); } eatery.commimgr.RemoveCommand(eatery.target, cmdP); eatery.reChecking(1); } }
/// <summary> /// 添加监控管理的设备 /// </summary> /// <param name="devid">请求的客户端</param> /// <param name="ispatrol">是否持续巡检</param> private DeviceBase addDevice(string devid, bool ispatrol) { //增加设备监控 NameObjectList ps = new NameObjectList(); ps["设备ID"] = devid; DataTable tab = this.query.getTable("设备通讯参数", ps); if (null == tab || tab.Rows.Count < 1) return null; DataRow dr = tab.Rows[0]; if (!tab.Columns.Contains("通讯协议")) { string msg = ""; foreach (object obj in dr.ItemArray) msg += Convert.ToString(obj) + " "; Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 设备通讯参数值:" + msg); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 设备通讯参数值:" + msg); return null; } string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) return null; int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); if (null == target) return null; CommiManager commimgr = CommiManager.GlobalManager; DeviceBase device = null; switch (dvtype) { case "门禁": Debug.WriteLine("门禁"); string ctrltype = Convert.ToString(dr["控制器类型"]); if ("进出口" == ctrltype || "出入口" == ctrltype) device = new DeviceChannel(); else device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); device.AlarmHandle += new EventHandler<DvAlarmEventArgs>(device_AlarmHandle); break; case "消费": device = new DeviceEatery(); target.setProtocol(Protocol.PTLEatery); break; default: return null; } device.RecordHandle += new EventHandler<DvRecordEventArgs>(device_RecordHandle); Monitor.Enter(devlist); this.devlist.Add(device); Monitor.PulseAll(devlist); Monitor.Exit(devlist); device.IsPatrol = ispatrol; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid, station); device.StartGather(); Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 启动:" + Convert.ToString(station) + " 设备ID:" + devid); myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 启动:" + Convert.ToString(station) + " 设备ID:" + devid); return device; }