/// <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="sender">门禁设备</param> /// <param name="e">报警事件</param> void device_AlarmHandle(object sender, DvAlarmEventArgs e) { DeviceDoor door = sender as DeviceDoor; if (null == door || null == e || string.IsNullOrEmpty(e.TagAlarm)) { return; } string fire = basefun.valtag(e.TagAlarm, "火警"); if ("1" != fire) { return; } NameObjectList ps = new NameObjectList(); ps["设备ID"] = door.DevID; DataTable tab = this.query.getTable("同组设备", ps); CommiManager commimgr = CommiManager.GlobalManager; for (int i = 0; i < tab.Rows.Count; i++) { DataRow dr = tab.Rows[i]; string dvid = Convert.ToString(dr["ID"]); string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) { return; } int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); if (null == target) { return; } target.setProtocol(Protocol.PTLDoor); door = new DeviceDoor(); door.SetDevice(commimgr, target, dvid, station); door.FireOpenDoor(); } }
/// <summary> /// 读取记录数据 /// </summary> /// <param name="devices">设备ID,逗号分割</param> /// <returns>转换成表格数据</returns> private void FireOpenDoor(string devices) { if (string.IsNullOrEmpty(devices)) return; string[] devid = devices.ToLower().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); //检查设备,没有的添加入缓存 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) || "门禁" != dvtype || DBNull.Value == dr["站址"]) continue; string ctrltype = Convert.ToString(dr["控制器类型"]); if ("考勤机" == ctrltype) continue; CommiTarget target = this.getTarget(dr); if (null == target) continue; target.setProtocol(Protocol.PTLDoor); int station = Convert.ToInt32(dr["站址"]); if ("进出口" == ctrltype || "出入口" == ctrltype) device = new DeviceChannel(); else device = new DeviceDoor(); device.IsPatrol = false; device.dtBeat = DateTime.Now; device.SetDevice(commimgr, target, devid[i], station); devlist.Add(device); reqs[i] = device; } for (int i = 0; i < reqs.Length; i++) { if (null == reqs[i]) continue; DeviceBase dv = reqs[i]; if (!(reqs[i] is DeviceDoor) && !(reqs[i] is DeviceChannel)) continue; if (dv is DeviceDoor) ((DeviceDoor)dv).FireOpenDoor(); else if (dv is DeviceChannel) ((DeviceChannel)dv).FireOpenDoor(); } }
/// <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 tmWorkGather(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; if ("考勤机" != Convert.ToString(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; DeviceDoor device = new DeviceDoor(); target.setProtocol(Protocol.PTLDoor); 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; } DeviceDoor door = null; Monitor.Enter(dvQueue); try { door = dvQueue.Dequeue(); } catch { } Monitor.PulseAll(dvQueue); Monitor.Exit(dvQueue); if (null == door) { continue; } //采集记录 if (null != door.cmdGather) { door.cmdGather.TimeSendInv = door.tsbusy.Add(door.tsinv); } else { door.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>(door.execResponse); cmdP.IsResposeHandle = isResponse; string tag = "@设备地址=" + Convert.ToString(door.station); NameValueCollection datainfo = new NameValueCollection(); string msgprev = ""; for (int i = 0; i < 200; i++) { tag = basefun.setvaltag(tag, "{记录索引}", Convert.ToString(door.posRecord)); string msg = door.getResponse(door.commimgr, door.target, cmdP, "读取记录", tag, door.waitTime); if (string.IsNullOrEmpty(msg)) { continue; } if (string.IsNullOrEmpty(msgprev)) { msgprev = msg; } string cardnum = basefun.valtag(msg, "{卡号}"); string suc = basefun.valtag(msg, "Success"); if ("true" != suc || "16777215" == cardnum || "0" == cardnum) { //bool isend = door.IsEndReadDevice(-1); //记录错误时记入日志,继续下一条 if ("false" == suc) { door.posRecord++; datainfo["操作"] = "读取记录失败"; datainfo["报文"] = CommandBase.Parse(cmdP.ResponseData, true); ServiceTool.LogMessage(msg, datainfo, EventLogEntryType.FailureAudit); } //if (isend && door.posRecord > 20000) // door.ClearRecord(); //if (isend) break; if (door.posRecord > 20000) { bool isend = door.IsEndReadDevice(-1); if (isend) { door.ClearRecord(); } } break; } msgprev = msg; if ("考勤机" == door.devtype) { door.writeRecord(msg, true); } door.posRecord++; } //门禁设备只处理最后一条记录 if (string.IsNullOrEmpty(msgprev) && "考勤机" != door.devtype) { door.writeRecord(msgprev, false); } door.commimgr.RemoveCommand(door.target, cmdP); if (null != door.cmdGather) { door.cmdGather.TimeSendInv = door.tsinv; } else { door.reChecking(1); } } }
/// <summary> /// 火警联动 /// </summary> /// <param name="sender">门禁设备</param> /// <param name="e">报警事件</param> void device_AlarmHandle(object sender, DvAlarmEventArgs e) { DeviceDoor door = sender as DeviceDoor; if (null == door || null == e || string.IsNullOrEmpty(e.TagAlarm)) return; string fire = basefun.valtag(e.TagAlarm, "火警"); if ("1" != fire) return; NameObjectList ps = new NameObjectList(); ps["设备ID"] = door.DevID; DataTable tab = this.query.getTable("同组设备", ps); CommiManager commimgr = CommiManager.GlobalManager; for (int i = 0; i < tab.Rows.Count; i++) { DataRow dr = tab.Rows[i]; string dvid = Convert.ToString(dr["ID"]); string dvtype = Convert.ToString(dr["通讯协议"]); if (string.IsNullOrEmpty(dvtype) || DBNull.Value == dr["站址"]) return; int station = Convert.ToInt32(dr["站址"]); CommiTarget target = this.getTarget(dr); if (null == target) return; target.setProtocol(Protocol.PTLDoor); door = new DeviceDoor(); door.SetDevice(commimgr, target, dvid, station); door.FireOpenDoor(); } }
/// <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; }