Пример #1
0
        /// <summary>
        /// 直接采集一条数据,并自动存入数据库,返回获取数据的记录
        /// 没有记录时恢复巡检
        /// </summary>
        /// <param name="isfirst">是否首次提取,首次会补充采集当前记录以防漏采</param>
        public override string GatherData(bool isfirst)
        {
            if (null == this.commimgr || null == this.target || string.IsNullOrEmpty(this.devid) || this.station < 1)
            {
                return("");
            }
            CmdProtocol cmdP = this.cmdtemp;

            if (null == cmdP)
            {
                cmdP = new CmdProtocol(false);
                setTimeout(cmdP);
                cmdP.ResponseHandle += new EventHandler <ResponseEventArgs>(this.execResponse);
                cmdP.IsResposeHandle = isResponse;
                this.cmdtemp         = cmdP;
            }
            this.reChecking(0);
            cmdP.TimeFailLimit = cmdP.TimeOut.Add(new TimeSpan(-10 * 10000));
            string tag = "@设备地址=" + Convert.ToString(this.station);
            string msg = getResponse(this.commimgr, this.target, cmdP, "读取记录", tag, this.waitTime);

            if (string.IsNullOrEmpty(msg))
            {
                return(msg);
            }
            string cardnum = basefun.valtag(msg, "{卡号}");
            string suc     = basefun.valtag(msg, "Success");

            if ("true" != suc || "16777215" == cardnum || "0" == cardnum)
            {
                //记录错误时记入日志,继续下一条
                if ("false" == suc)
                {
                    NameValueCollection datainfo = new NameValueCollection();
                    datainfo["操作"] = "读取记录";
                    datainfo["报文"] = CommandBase.Parse(cmdP.ResponseData, true);
                    ServiceTool.LogMessage(msg, datainfo, EventLogEntryType.FailureAudit);
                    return(msg);
                }
                if (!dvQueue.Contains(this))
                {
                    this.reChecking(1);
                }
                return(msg);
            }
            this.writeRecord(msg);
            if (!dvQueue.Contains(this))
            {
                this.reChecking(1);
            }
            return(msg);
        }
Пример #2
0
        /// <summary>
        /// 向客户端发送数据:记录/信号/事件
        /// </summary>
        /// <param name="device">设备ID</param>
        /// <param name="datatype">数据类型:record,signal,alarm</param>
        /// <param name="data">发送的数据字节</param>
        private void sendclientdata(string device, string datatype, byte[] data)
        {
            if (string.IsNullOrEmpty(device) || string.IsNullOrEmpty(datatype) || null == data || data.Length < 1)
            {
                return;
            }
            TimeSpan ts    = new TimeSpan(0, 3, 0);
            DateTime dtNow = DateTime.Now;

            string dvid = "," + device + ",";
            NameValueCollection info = new NameValueCollection();

            info.Add("cmd", "TransFile.extend");
            info.Add("service", "monitor");
            info.Add("len", Convert.ToString(data.Length));
            info.Add("deviceid", device);
            info.Add("datatype", datatype);
            ServiceTool.LogMessage("发送数据(设备):" + device + "\r\n" + Encoding.GetEncoding("GB2312").GetString(data), null, EventLogEntryType.Information);

            //对关联连接响应数据
            ClientMap[] cms = this.clientlist.ToArray();
            for (int i = 0; i < cms.Length; i++)
            {
                if (null == cms[i].client || null == cms[i].client.Client || cms[i].cmds.Count < 1 || !cms[i].client.Client.Connected)
                {
                    continue;
                }
                ClientInfo       cf   = cms[i].client;
                MonitorCommand[] cmds = cms[i].cmds.ToArray();
                for (int m = 0; m < cmds.Length; m++)
                {
                    if (!cmds[m].devices.Contains(device))
                    {
                        continue;
                    }
                    info["id"] = cmds[m].cmdid;
                    byte[] context = SvrFileTrans.ParseInfo(info);
                    int    len     = context.Length;
                    Array.Resize <byte>(ref context, context.Length + data.Length);
                    Array.Copy(data, 0, context, len, data.Length);
                    Monitor.Enter(cf);
                    cf.BufferResponse.Add(context);
                    Monitor.PulseAll(cf);
                    Monitor.Exit(cf);
                }
            }
        }
Пример #3
0
 /// <summary>
 /// 定时器执行,定时检查无效设备
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void tmService_Elapsed(object sender, ElapsedEventArgs e)
 {
     if (isRuning)
     {
         return;
     }
     try
     {
         isRuning = true;
         Haltdev(null, null);
     }
     catch (Exception ex)
     {
         ServiceTool.LogMessage(ex, null, EventLogEntryType.Error);
     }
     isRuning = false;
 }
Пример #4
0
        /// <summary>
        /// 监控设备,巡检指令心跳,增加巡检设备
        /// 间隔超过5分钟自动认为该指令退出
        /// </summary>
        /// <param name="info">指令内容:service='monitor',device(逗号分割多个设备ID),id,patrol='true'(patrol持续执行)</param>
        /// <param name="client">客户端连接信息</param>
        public void Monitordev(NameValueCollection info, ClientInfo client)
        {
            if (null == info || null == client || null == client.Client || !client.Client.Connected)
            {
                return;
            }
            string devid = info["device"];

            if (string.IsNullOrEmpty(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            ServiceTool.LogMessage("启动巡检(设备):" + devid, null, EventLogEntryType.Warning);
            //加入设备列表,并增加巡检设备
            string[]     devices = devid.ToLower().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            DeviceBase[] devs    = devlist.ToArray();
            for (int i = 0; i < devices.Length; i++)
            {
                string id    = devices[i];
                bool   exist = false;
                for (int j = 0; j < devs.Length; j++)
                {
                    if (id != devs[i].DevID.ToLower())
                    {
                        continue;
                    }
                    devs[j].dtBeat = DateTime.Now;
                    exist          = true;
                    break;
                }
                if (exist)
                {
                    continue;
                }
                this.addDevice(devid, true);
            }
            this.returnInfo(info, client, true);
        }
Пример #5
0
        /// <summary>
        /// 获取数据行的设备目标位置参数
        /// 记录包含字段【访问方式】(TCP/UDP/SerialPort)、【端口】(60000/COM1)、【地址】(192.168.1.146)
        /// </summary>
        /// <param name="dr">数据记录</param>
        /// <returns></returns>
        private CommiTarget getTarget(DataRow dr)
        {
            if (null == dr || (DBNull.Value == dr["串口"] && DBNull.Value == dr["IP地址"]))
            {
                return(null);
            }
            CommiTarget target    = new CommiTarget();
            CommiType   commiType = CommiType.UDP;
            string      stype     = Convert.ToString(dr["通讯类别"]);

            switch (stype)
            {
            case "TCP/IP(局域网)":
                commiType = CommiType.TCP; break;

            case "UDP/IP(局域网)":
                commiType = CommiType.UDP; break;

            default:
                commiType = CommiType.SerialPort; break;
            }
            try
            {
                if (CommiType.SerialPort == commiType)
                {
                    string   portname = Convert.ToString(dr["串口"]);
                    int      baudRate = Convert.ToInt16(dr["波特率"]);
                    int      dataBits = Convert.ToInt16(dr["数据位"]);
                    decimal  s        = Convert.ToDecimal(dr["停止位"]);
                    StopBits sb       = StopBits.None;
                    if (1 == s)
                    {
                        sb = StopBits.One;
                    }
                    else if (2 == s)
                    {
                        sb = StopBits.Two;
                    }
                    else if (1 < s && s < 2)
                    {
                        sb = StopBits.OnePointFive;
                    }

                    target.SetProtocolParam(portname, baudRate, Parity.None, dataBits, sb);
                }
                else
                {
                    string addr = Convert.ToString(dr["IP地址"]);
                    if (string.IsNullOrEmpty(addr) || DBNull.Value == dr["端口"])
                    {
                        return(null);
                    }
                    int port = Convert.ToInt32(dr["端口"]);
                    target.SetProtocolParam(addr, port, commiType);
                }
            }
            catch (Exception ex)
            {
                NameValueCollection data = new NameValueCollection();
                data["操作"]   = "创建通讯目标";
                data["设备ID"] = Convert.ToString(dr["ID"]);
                ServiceTool.LogMessage(ex, data, EventLogEntryType.Error);
                return(null);
            }
            return(target);
        }
Пример #6
0
        /// <summary>
        /// 移除监控的设备或没有指定设备就移除整个指令,或对无效(心跳超时)指令移除
        /// </summary>
        /// <param name="info">指令内容:service='halt',device,id,all='true'</param>
        /// <param name="client">客户端连接信息</param>
        public void Haltdev(NameValueCollection info, ClientInfo client)
        {
            if (null == info)
            {
                info = new NameValueCollection();
            }
            string cmdid = info["id"];
            string devid = info["device"];
            string all   = info["all"];

            if (!string.IsNullOrEmpty(devid))
            {
                devid += "," + devid + ",";
                devid  = devid.Replace(",,", ",");
            }
            if (!string.IsNullOrEmpty(all))
            {
                all = all.ToLower();
            }
            ServiceTool.LogMessage("停止巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
            //移除指定的设备监控,或移除指令,或移除无效指令
            TimeSpan ts    = new TimeSpan(0, 5, 0);
            DateTime dtNow = DateTime.Now;

            ClientMap[] maps = this.clientlist.ToArray();
            for (int i = maps.Length - 1; i > -1; i--)
            {
                if (null == maps[i] || null == maps[i].client.Client || !maps[i].client.Client.Connected)
                {
                    MonitorCommand[] cmdsd = new MonitorCommand[0];
                    if (null != maps[i])
                    {
                        maps[i].cmds.Clear();
                    }
                    this.clientlist.Remove(maps[i]);
                    ServiceTool.LogMessage("清除指令" + maps[i].client.IPEndPoint, null, EventLogEntryType.Warning);
                    continue;
                }
                MonitorCommand[] cmds = maps[i].cmds.ToArray();
                for (int m = 0; m < cmds.Length; m++)
                {
                    MonitorCommand cmd = cmds[m];
                    if (cmdid != cmd.cmdid && dtNow - cmd.dtbeat > ts)
                    {
                        maps[i].cmds.Remove(cmd);
                        continue;
                    }
                    if (cmdid == cmd.cmdid && string.IsNullOrEmpty(devid))
                    {
                        maps[i].cmds.Remove(cmd);
                    }
                    else if ((cmdid == cmd.cmdid || "true" == all) && cmd.devices.Contains(devid))
                    {
                        cmd.devices = cmd.devices.Replace(devid, ",");
                    }
                }
                if (maps[i].cmds.Count < 1)
                {
                    this.clientlist.Remove(maps[i]);
                }
                //移除巡检设备
                if ("true" == all && !string.IsNullOrEmpty(devid))
                {
                    devid = devid.Replace(",", "");
                    DeviceBase[] devices = this.devlist.ToArray();
                    for (int m = 0; m < devices.Length; m++)
                    {
                        if (devid != devices[m].DevID)
                        {
                            continue;
                        }
                        this.devlist.Remove(devices[m]);
                        devices[m].StopGather();
                        ServiceTool.LogMessage("清除设备:" + devices[m].DevID, null, EventLogEntryType.Warning);
                        break;
                    }
                }
            }
            //不是持续巡检设备,在没有接收指令时停止巡检
            DeviceBase[] devs = this.devlist.ToArray();
            for (int i = 0; i < devs.Length; i++)
            {
                if (null == devs[i])
                {
                    this.devlist.Remove(devs[i]);
                    continue;
                }
                if (devs[i].IsPatrol)
                {
                    continue;
                }
                devid = "," + devs[i].DevID + ",";
                bool isfind = false;
                maps = this.clientlist.ToArray();
                for (int m = 0; m < maps.Length; m++)
                {
                    MonitorCommand[] cmds = maps[m].cmds.ToArray();
                    for (int k = 0; k < cmds.Length; k++)
                    {
                        if (cmds[k].devices.Contains(devid))
                        {
                            isfind = true;
                        }
                        if (isfind)
                        {
                            break;
                        }
                    }
                    if (isfind)
                    {
                        break;
                    }
                }
                if (isfind)
                {
                    break;
                }
                this.devlist.Remove(devs[i]);
                devs[i].StopGather();
                ServiceTool.LogMessage("清除设备:" + devs[i].DevID, null, EventLogEntryType.Warning);
            }
            this.returnInfo(info, client, true);
        }
Пример #7
0
        /// <summary>
        /// 监控设备,巡检指令心跳,增加巡检设备
        /// 间隔超过5分钟自动认为该指令退出
        /// </summary>
        /// <param name="info">指令内容:service='monitor',device,id,patrol='true'(patrol持续执行)</param>
        /// <param name="client">客户端连接信息</param>
        public void Monitordev(NameValueCollection info, ClientInfo client)
        {
            if (null == info || null == client || null == client.Client || !client.Client.Connected)
            {
                return;
            }
            string cmdid = info["id"];
            string devid = info["device"];

            if (string.IsNullOrEmpty(cmdid))
            {
                return;
            }
            ServiceTool.LogMessage("启动巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
            //增加客户端连接信息
            ClientMap[] cms = this.clientlist.ToArray();
            ClientMap   cm  = null;

            for (int i = cms.Length - 1; i > -1; i--)
            {
                if (null == cms[i] || null == cms[i].client.Client || !cms[i].client.Client.Connected)
                {
                    continue;
                }
                if (client != cms[i].client)
                {
                    continue;
                }
                cm = cms[i];
                break;
            }
            if (null == cm)
            {
                cm        = new ClientMap();
                cm.client = client;
                this.clientlist.Add(cm);
            }
            //添加监听指令
            MonitorCommand cmd = null;

            foreach (MonitorCommand mcd in cm.cmds)
            {
                if (cmdid == mcd.cmdid)
                {
                    cmd = mcd;
                    break;
                }
            }
            if (null == cmd)
            {
                cmd       = new MonitorCommand();
                cmd.cmdid = cmdid;
                cm.cmds.Add(cmd);
            }
            cmd.dtbeat = DateTime.Now;

            //增加监听巡检设备
            if (string.IsNullOrEmpty(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            devid = "," + devid + ",";
            devid = devid.Replace(",,", ",");
            if (cmd.devices.Contains(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            //加入设备列表,并增加巡检设备
            if (string.IsNullOrEmpty(cmd.devices))
            {
                cmd.devices = devid;
            }
            else
            {
                cmd.devices += devid.Substring(1);
            }
            bool ispatrol = false;

            if ("true" == info["patrol"])
            {
                ispatrol = true;
            }
            devid = devid.Replace(",", "");
            this.addDevice(devid, ispatrol);
            this.returnInfo(info, client, true);
        }
Пример #8
0
        /// <summary>
        /// 写入记录
        /// </summary>
        /// <param name="tagmsg">tag格式信息:{卡号}、状态编号,读卡器号,{刷卡时间}</param>
        /// <param name="isevent">是否触发事件</param>
        private string writeRecord(string tagmsg, bool isevent)
        {
            string cardnum = basefun.valtag(tagmsg, "{卡号}");
            string suc     = basefun.valtag(tagmsg, "Success");

            // if ("true" != suc || "16777215" == cardnum || "0" == cardnum || string.IsNullOrEmpty(cardnum))
            if ("16777215" == cardnum || "0" == cardnum || string.IsNullOrEmpty(cardnum))
            {
                return(tagmsg);
            }
            NameObjectList ps = new NameObjectList();

            ps["卡号"] = cardnum;
            DataTable tabuser = null;

            try
            {
                Monitor.Enter(objquery);
                tabuser = this.qyemp.getTable("卡用户信息", ps);
            }
            finally
            {
                Monitor.PulseAll(objquery);
                Monitor.Exit(objquery);
            }
            if (null != tabuser && tabuser.Rows.Count > 0)
            {
                DataRow dr = tabuser.Rows[0];
                tagmsg = basefun.setvaltag(tagmsg, "{部门ID}", Convert.ToString(dr["部门ID"]));
                tagmsg = basefun.setvaltag(tagmsg, "{部门代码}", Convert.ToString(dr["部门代码"]));
                tagmsg = basefun.setvaltag(tagmsg, "{部门}", Convert.ToString(dr["部门"]));
                tagmsg = basefun.setvaltag(tagmsg, "{姓名}", Convert.ToString(dr["姓名"]));
            }
            string info = "";

            string[] cols = { "{卡号}", "{部门}", "{部门ID}", "{部门代码}", "{姓名}", "状态编号", "读卡器", "{刷卡时间}" };
            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(tagmsg, cols[c]));
            }
            string strstate = this.getStateDoorCard(cardnum, basefun.valtag(info, "状态编号"), basefun.valtag(info, "读卡器"));

            tagmsg    = basefun.setvaltag(tagmsg, "状态", strstate);
            info      = basefun.setvaltag(info, "状态", strstate);
            ps        = ParamManager.createParam(info);
            ps["控制器"] = this.devid;
            ps["时间"]  = ps["刷卡时间"] = DateTime.MinValue;
            DateTime dt = DateTime.MinValue;

            try
            {
                string dtstr = basefun.valtag(tagmsg, "{刷卡时间}");
                if (!string.IsNullOrEmpty(dtstr) && "0001-1-1 0:00:00" != dtstr)
                {
                    ps["时间"] = ps["刷卡时间"] = dt = Convert.ToDateTime(dtstr);
                }
            }
            catch { dt = DateTime.MinValue; }
            if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
            {
                if (null == dtobj)
                {
                    return(tagmsg);
                }
                try { dt = Convert.ToDateTime(dtobj); }
                catch { ServiceTool.LogMessage(info, null, EventLogEntryType.Warning); return(tagmsg); }
                if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
                {
                    ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
                    return(tagmsg);
                }
                ps["时间"] = ps["刷卡时间"] = dtobj;
            }
            else
            {
                dtobj = ps["时间"];
            }
            bool success = false;

            try
            {
                Monitor.Enter(objquery);
                success = qydata.ExecuteNonQuery("采集门禁数据", ps, ps, ps);
            }
            finally
            {
                Monitor.PulseAll(objquery);
                Monitor.Exit(objquery);
            }
            if (!success)
            {
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
            if (isevent)
            {
                this.AddRecord(info);
                DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);
                this.RaiseRecord(arg);
            }
            return(tagmsg);
        }
Пример #9
0
        /// <summary>
        /// 验证状态信息,对状态对比变化时生成事件记录
        /// </summary>
        /// <param name="item">巡检项</param>
        /// <param name="devid">控制器ID</param>
        /// <param name="taginfo">状态信息tag标记值</param>
        private string validateSate(string taginfo)
        {
            if (string.IsNullOrEmpty(taginfo))
            {
                return(taginfo);
            }
            Dictionary <string, string[]> dictstate = new Dictionary <string, string[]>();

            string[] alarm = { "4号门报警", "3号门报警", "2号门报警", "1号门报警" };
            dictstate.Add("报警", alarm);
            alarm = new string[] { "火警", "无效刷卡", "联动报警", "非法闯入", "超时", "胁迫" };
            dictstate.Add("警报", alarm);
            alarm = new string[] { "芯片故障", "系统故障4", "时钟故障", "系统故障2", "系统故障1" };
            dictstate.Add("故障", alarm);
            alarm = new string[] { "按钮4状态", "按钮3状态", "按钮2状态", "按钮1状态" };
            dictstate.Add("按钮", alarm);
            alarm = new string[] { "门磁4状态", "门磁3状态", "门磁2状态", "门磁1状态" };
            dictstate.Add("门磁", alarm);
            alarm = new string[] { "继4状态", "继3状态", "继2状态", "继1状态" };
            dictstate.Add("继电器", alarm);
            //火警报警,WG板子使用火警事件记录来反映的
            string strstate = this.getStateDoorCard(basefun.valtag(taginfo, "{刷卡卡号}"),
                                                    basefun.valtag(taginfo, "状态编号"), basefun.valtag(taginfo, "读卡器"));

            if ("火警报警" == strstate)
            {
                taginfo = basefun.setvaltag(taginfo, "火警", "1");
                foreach (string k in dictstate["报警"])
                {
                    taginfo = basefun.setvaltag(taginfo, k, "1");
                }
            }
            //检查前后状态改变;alarmmsg报警信息,isalarm当前是否报警改变
            string         msg = "", alarmmsg = "", msgsigal = "";
            bool           isalarm = false;
            NameObjectList psevent = new NameObjectList();

            psevent["控制器"] = this.devid;
            DateTime dt = DateTime.MinValue;

            try
            {
                psevent["时间"] = dt = Convert.ToDateTime(basefun.valtag(taginfo, "{当前时间}"));
            }
            catch { dt = DateTime.MinValue; }
            if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
            {
                NameValueCollection datainfo = new NameValueCollection();
                datainfo["操作"] = "巡检状态";
                ServiceTool.LogMessage(taginfo, datainfo, EventLogEntryType.FailureAudit);
                if (null == dtobj)
                {
                    return(taginfo);
                }
                try { dt = Convert.ToDateTime(dtobj); }
                catch { return(taginfo); }
                if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
                {
                    return(taginfo);
                }
                psevent["时间"] = dtobj;
            }
            else
            {
                dtobj = psevent["时间"];
            }
            alarm = new string[] { "警报", "报警", "故障", "按钮", "门磁", "继电器" };
            foreach (string state in alarm)
            {
                //对比状态生成事件
                psevent["事件"] = state;
                string tagorgi   = this.tagInfos[state];
                string tagnews   = "";
                bool   ischanged = false;
                int    inum      = 5;
                foreach (string st in dictstate[state])
                {
                    inum--;
                    string valorg = basefun.valtag(tagorgi, st);
                    if (string.IsNullOrEmpty(valorg))
                    {
                        valorg = "0";
                    }
                    string valnew = basefun.valtag(taginfo, st);
                    tagnews = basefun.setvaltag(tagnews, st, valnew);
                    if ("警报" == state && "1" == valnew)
                    {
                        if (string.IsNullOrEmpty(alarmmsg))
                        {
                            alarmmsg = st;
                        }
                        else
                        {
                            alarmmsg += "," + st;
                        }
                    }
                    if (valorg == valnew && ("报警" != state || !isalarm))
                    {
                        continue;
                    }
                    ischanged = true;
                    if ("警报" == state)
                    {
                        continue;
                    }
                    //处理事件
                    if ("报警" == state || "故障" == state)
                    {
                        if ("报警" == state)
                        {
                            psevent["编号"] = inum.ToString();
                            psevent["内容"] = st + "(" + alarmmsg + ")";
                        }
                        else
                        {
                            psevent.Remove("编号");
                            psevent["内容"] = st;
                        }
                        msg = basefun.setvaltag(msg, st, valnew);
                        try
                        {
                            Monitor.Enter(objquery);
                            if ("1" == valnew)
                            {
                                this.query.ExecuteNonQuery("发生道闸事件", psevent, psevent, psevent);
                            }
                            else
                            {
                                this.query.ExecuteNonQuery("结束道闸事件", psevent, psevent, psevent);
                            }
                        }
                        finally
                        {
                            Monitor.PulseAll(objquery);
                            Monitor.Exit(objquery);
                        }
                    }
                    else
                    {
                        psevent["编号"] = inum.ToString();
                        psevent["内容"] = st + "变位 " + valnew;
                        msgsigal      = basefun.setvaltag(msgsigal, st, valnew);
                        try
                        {
                            Monitor.Enter(objquery);
                            this.query.ExecuteNonQuery("道闸变位事件", psevent, psevent, psevent);
                        }
                        finally
                        {
                            Monitor.PulseAll(objquery);
                            Monitor.Exit(objquery);
                        }
                    }
                }//foreach (string st in dictstate[state])
                this.tagInfos[state] = tagnews;
                if (!ischanged)
                {
                    continue;
                }
                psevent["类别"] = state;
                psevent["内容"] = tagnews;
                try
                {
                    Monitor.Enter(objquery);
                    this.query.ExecuteNonQuery("道闸巡检状态", psevent, psevent, psevent);
                }
                finally
                {
                    Monitor.PulseAll(objquery);
                    Monitor.Exit(objquery);
                }
                //报警内容变化时,先结束原警报事件
                if ("警报" == state)
                {
                    isalarm = !string.IsNullOrEmpty(alarmmsg);
                    if (!isalarm)
                    {
                        foreach (string a in dictstate[state])
                        {
                            msg = basefun.setvaltag(msg, a, "0");
                        }
                    }
                    psevent.Remove("编号");
                    try
                    {
                        Monitor.Enter(objquery);
                        this.query.ExecuteNonQuery("结束道闸事件", psevent, psevent, psevent);
                    }
                    finally
                    {
                        Monitor.PulseAll(objquery);
                        Monitor.Exit(objquery);
                    }
                }
            }//foreach (string state in alarm)
            if (string.IsNullOrEmpty(this.Alarm.tag))
            {
                this.Alarm.tag       = taginfo;
                this.Alarm.dtReceive = DateTime.Now;
            }
            if (string.IsNullOrEmpty(this.Signal.tag))
            {
                this.Signal.tag       = taginfo;
                this.Signal.dtReceive = DateTime.Now;
            }
            if (string.IsNullOrEmpty(msg) && string.IsNullOrEmpty(msgsigal))
            {
                return(taginfo);
            }
            if (!string.IsNullOrEmpty(msg))
            {
                alarm = alarmmsg.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string a in alarm)
                {
                    msg = basefun.setvaltag(msg, a, "1");
                }
                this.Alarm.dtReceive = DateTime.Now;
                this.Alarm.tag       = taginfo;
                this.Signal.tag      = taginfo;
                DvAlarmEventArgs arg = new DvAlarmEventArgs(this.devid, this.station, dt, taginfo);
                arg.TagAlarm = msg;
                this.RaiseAlarm(arg);
            }
            if (!string.IsNullOrEmpty(msgsigal))
            {
                this.Signal.dtReceive = DateTime.Now;
                this.Signal.tag       = taginfo;
                this.Alarm.tag        = taginfo;
                DvSignalEventArgs arg = new DvSignalEventArgs(this.devid, this.station, dt, taginfo);
                arg.TagSignal = msgsigal;
                this.RaiseSignal(arg);
            }
            return(taginfo);
        }
Пример #10
0
 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;
         }
         DeviceChannel channel = null;
         Monitor.Enter(dvQueue);
         try { channel = dvQueue.Dequeue(); }
         catch { }
         Monitor.PulseAll(dvQueue);
         Monitor.Exit(dvQueue);
         if (null == channel)
         {
             continue;
         }
         if (channel.sumRecord > 500)
         {
             channel.ClearRecord();
             continue;
         }
         //采集记录
         if (null != channel.cmdGather)
         {
             channel.cmdGather.TimeSendInv = channel.tsbusy.Add(channel.tsinv);
         }
         else
         {
             channel.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>(channel.execResponse);
         cmdP.IsResposeHandle = isResponse;
         cmdP.DeviceType      = "通道闸.采集";
         string tag = "@设备地址=" + Convert.ToString(channel.station);
         NameValueCollection datainfo = new NameValueCollection();
         for (int i = 0; i < 200; i++)
         {
             string msg = channel.getResponse(channel.commimgr, channel.target, cmdP, "读取记录", tag, channel.waitTime);
             if (string.IsNullOrEmpty(msg))
             {
                 continue;
             }
             string cardnum = basefun.valtag(msg, "{卡号}");
             string suc     = basefun.valtag(msg, "Success");
             if ("true" != suc || "16777215" == cardnum || "0" == cardnum)
             {
                 //bool isend = channel.IsEndReadDevice(-1);
                 //记录错误时记入日志,继续下一条
                 if ("false" == suc)
                 {
                     datainfo["操作"] = "读取记录失败";
                     datainfo["报文"] = CommandBase.Parse(cmdP.ResponseData, true);
                     ServiceTool.LogMessage(msg, datainfo, EventLogEntryType.FailureAudit);
                 }
                 break;
                 //if (isend)  break;
             }
             channel.writeRecord(msg);
         }
         channel.commimgr.RemoveCommand(channel.target, cmdP);
         //channel.reChecking(1);
         if (null != channel.cmdGather)
         {
             channel.cmdGather.TimeSendInv = channel.tsinv;
         }
         else
         {
             channel.reChecking(1);
         }
     }
 }
Пример #11
0
        /// <summary>
        /// 在当前没有执行指令响应时利用响应事件保存数据
        /// </summary>
        /// <param name="sender">事件指令实例</param>
        /// <param name="e"></param>
        private void execResponse(object sender, ResponseEventArgs e)
        {
            if (null == sender || null == e || !e.Success)
            {
                return;
            }
            CmdProtocol cmdP = sender as CmdProtocol;

            if (null == cmdP || 0 == this.stateChecking || this.isexecuting)
            {
                return;
            }
            string msg     = cmdP.ResponseFormat;
            string suc     = basefun.valtag(msg, "Success");
            string cardnum = basefun.valtag(msg, "{卡号}");
            string st      = basefun.valtag(msg, "{状态}");

            if ("true" != suc || string.IsNullOrEmpty(cardnum) || "16777215" == cardnum || "0" == cardnum)
            {
                return;
            }
            string info = "";

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 执行:" + msg);
            myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 执行:" + msg);
            string[] cols = { "{卡号}", "{卡类}", "{消费时间}", "{消费金额}", "{卡余额}", "{累计补助金额}", "{消费机号}", "{操作员号}" };
            if ("补助机" == this.devName)
            {
                cols = new string[] { "{卡号}", "{卡类}", "{充值时间}", "{本次补助金额}", "{补助后总额}" }
            }
            ;
            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
            }
            NameObjectList ps = ParamManager.createParam(info);

            ps["消费机"] = this.devid;
            DateTime dt  = DateTime.MinValue;
            string   fld = "补助机" == this.devName ? "充值时间" : "消费时间";

            try
            {
                dt = Convert.ToDateTime(ps[fld]);
            }
            catch { }
            if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
            {
                NameValueCollection datainfo = new NameValueCollection();
                datainfo["操作"] = "提取消费记录";
                datainfo["报文"] = CommandBase.Parse(cmdP.ResponseData, true);
                ServiceTool.LogMessage(msg, datainfo, EventLogEntryType.FailureAudit);
                if (null == dtobj)
                {
                    return;
                }
                try { dt = Convert.ToDateTime(dtobj); }
                catch { return; }
                ps[fld] = dtobj;
            }
            else
            {
                dtobj = ps[fld];
            }
            bool success = false;

            if ("补助机" == this.devName)
            {
                success = this.query.ExecuteNonQuery("消费接收补助", ps, ps, ps);
            }
            else
            {
                success = this.query.ExecuteNonQuery("采集数据", ps, ps, ps);
            }

            if (!success)
            {
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
        }
Пример #12
0
        /// <summary>
        /// 写入记录,分补助或消费分别保存
        /// </summary>
        /// <param name="msg">tag格式信息:{卡号}、状态编号,读卡器号,{刷卡时间}</param>
        private string writeRecord(string msg)
        {
            string cardnum = basefun.valtag(msg, "{卡号}");
            string suc     = basefun.valtag(msg, "Success");

            if ("true" != suc || "16777215" == cardnum || "0" == cardnum || string.IsNullOrEmpty(cardnum))
            {
                return(msg);
            }
            string info = "";

            string[] cols = { "{卡号}", "{卡类}", "{消费时间}", "{消费金额}", "{卡余额}", "{累计补助金额}", "{消费机号}", "{操作员号}" };
            if ("补助机" == this.devName)
            {
                cols = new string[] { "{卡号}", "{卡类}", "{充值时间}", "{本次补助金额}", "{补助后总额}" }
            }
            ;
            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
            }
            NameObjectList ps = ParamManager.createParam(info);

            ps["消费机"] = this.devid;

            //msg = "@{帧头}=C0,@{设备地址}=17,@{控制场}=01,@{长度场}=25,@Success=true,@{状态}=操作成功!,@{命令长度}=23,@{消费机号}=17,@{操作员号}=255,@{卡号}=16777215,@{卡类}=255,@{消费时间}=0001/1/1 0:00:00,@{卡余额}=1677721.5,@{累计补助金额}=6553.5,@{消费金额}=6553.5,@{记录指针}=11297,@{校验字}=32,@{帧尾}=C0";
            ///*************************查看时间格式是否正确 2012-06-20
            string dtTime = Convert.ToString(basefun.valtag(msg, "{消费时间}"));

            try
            {
                DateTime s = Convert.ToDateTime(dtTime);
                if (dtTime.ToString() == "0001/1/1 0:00:00" || Convert.ToDateTime(dtTime) < Convert.ToDateTime("2000-01-01 00:00:00"))
                {
                    ps["设备ID"] = this.devid;
                    ps["卡号"]   = cardnum;
                    ps["日期"]   = dtTime;
                    ps["金额"]   = basefun.valtag(msg, "{卡余额}");
                    this.query.ExecuteNonQuery("错误记录", ps, ps, ps);
                }
            }
            catch
            {
                ps["设备ID"] = this.devid;
                ps["卡号"]   = cardnum;
                ps["日期"]   = dtTime;
                ps["金额"]   = basefun.valtag(msg, "{卡余额}");
                this.query.ExecuteNonQuery("错误记录", ps, ps, ps);
            }
            ///*************************查看时间格式是否正确 2012-06-20



            DateTime dt  = DateTime.MinValue;
            string   fld = "补助机" == this.devName ? "充值时间" : "消费时间";

            try
            {
                dt = Convert.ToDateTime(ps[fld]);
            }
            catch { }
            if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
            {
                if (null == dtobj)
                {
                    return(msg);
                }
                try { dt = Convert.ToDateTime(dtobj); }
                catch { return(msg); }
                ps[fld] = dtobj;
            }
            else
            {
                dtobj = ps[fld];
            }
            bool success = false;

            if ("补助机" == this.devName)
            {
                success = this.query.ExecuteNonQuery("消费接收补助", ps, ps, ps);
            }
            else
            {
                success = this.query.ExecuteNonQuery("采集数据", ps, ps, ps);
            }
            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 保存:" + this.devName + " " + this.station.ToString());
            myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 保存:" + this.devName + " " + this.station.ToString());
            if (!success)
            {
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
            if (recordpos.ContainsKey(this.devid))
            {
                recordpos[this.devid]++;
                ps.Clear();
                ps["消费机"]  = this.devid;
                ps["记录位置"] = recordpos[this.devid];
                query.ExecuteNonQuery("记录指针", ps, ps, ps);
            }
            this.AddRecord(info);
            DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);

            this.RaiseRecord(arg);
            return(msg);
        }

        object dtobj = null;
Пример #13
0
 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);
     }
 }
Пример #14
0
        /// <summary>
        /// 直接采集一条数据,并自动存入数据库,返回获取数据的记录
        /// 没有记录时恢复巡检
        /// </summary>
        /// <param name="isfirst">是否首次提取,首次会补充采集当前记录以防漏采</param>
        public override string GatherData(bool isfirst)
        {
            if (null == this.commimgr || null == this.target || string.IsNullOrEmpty(this.devid) || this.station < 1)
            {
                return("");
            }
            CmdProtocol cmdP = this.cmdGather;

            if (null == cmdP)
            {
                cmdP = new CmdProtocol(false);
            }
            string[] cols = { "{卡号}", "状态编号", "读卡器", "{刷卡时间}" };
            string   tag  = "@设备地址=" + Convert.ToString(this.station);

            if ("考勤机" == this.devtype)
            {
                tag = basefun.setvaltag(tag, "{记录索引}", Convert.ToString(this.posRecord));
            }
            this.reChecking(0);
            cmdP.setCommand("门禁", "读取记录", tag);
            string msg     = getResponse(this.commimgr, this.target, cmdP, this.waitTime);
            string cardnum = basefun.valtag(msg, "{卡号}");
            string suc     = basefun.valtag(msg, "Success");

            if ("true" == suc)
            {
                this.posRecord++;
            }
            if ("true" != suc || string.IsNullOrEmpty(cardnum) || "16777215" == cardnum || "0" == cardnum)
            {
                if (this.posRecord > 10000 && ("false" == suc || "16777215" == cardnum || "0" == cardnum))
                {
                    //清空记录
                    cmdP.setCommand("门禁", "清空记录", tag);
                    cmdP.ResetState();
                    this.commimgr.SendCommand(this.target, cmdP);
                    if (cmdP.EventWh.WaitOne(this.waitTime, false))
                    {
                        suc = basefun.valtag(cmdP.ResponseFormat, "Success");
                        if ("true" == suc)
                        {
                            this.posRecord = 0;
                            NameObjectList pspos = new NameObjectList();
                            pspos["控制器"] = this.devid;
                            this.query.ExecuteNonQuery("重置记录位置", pspos, pspos, pspos);
                        }
                    }
                }
                this.reChecking(1);
                return(msg);
            }
            string info = "";

            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
            }
            info = basefun.setvaltag(info, "状态", this.getStateDoorCard(basefun.valtag(info, "状态编号")));
            NameObjectList ps = ParamManager.createParam(info);

            ps["控制器"] = this.devid;
            ps["时间"]  = ps["刷卡时间"];
            DateTime dt = DateTime.MinValue;

            try
            {
                dt = Convert.ToDateTime(basefun.valtag(msg, "{刷卡时间}"));
            }
            catch { dt = DateTime.MinValue; }
            if (dt < DateTime.Today.AddYears(-5) || dt > DateTime.Today.AddYears(5))
            {
                ps["时间"] = ps["刷卡时间"] = null;
            }
            bool success = this.query.ExecuteNonQuery("采集门禁数据", ps, ps, ps);

            if (!success || dt < DateTime.Today.AddYears(-5) || dt > DateTime.Today.AddYears(5))
            {
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
            this.AddRecord(info);
            DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);

            this.RaiseRecord(arg);
            return(msg);
        }
Пример #15
0
        /// <summary>
        /// 巡检响应,连续失败5分钟(tsbusy)则认为停机,间隔tsbusy巡检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdGather_ResponseHandle(object sender, ResponseEventArgs e)
        {
            if (null == sender || null == e)
            {
                return;
            }
            CmdProtocol cmdP = sender as CmdProtocol;

            if (null == cmdP || 0 == this.stateChecking)
            {
                return;
            }
            if (!e.Success)
            {
                //连续长时间失败时,增大巡检周期
                if (cmdP.TimeSendInv < this.tsbusy && DateTime.Now - cmdP.ResponseDatetime > this.tsbusy)
                {
                    if (cmdP.TimeSendInv < this.tsbusy)
                    {
                        cmdP.TimeSendInv = this.tsbusy.Add(this.tsinv);
                    }
                }
                return;
            }
            string msg = cmdP.ResponseFormat;

            if (string.IsNullOrEmpty(msg) || "true" != basefun.valtag(msg, "Success"))
            {
                return;
            }
            //离线后恢复在线时,恢复巡检周期
            if (cmdP.TimeSendInv > this.tsbusy)
            {
                cmdP.TimeSendInv = this.tsinv;
            }
            this.validateSate(msg);

            //2分钟内记录数大于20条,则设备处于繁忙状态,空闲时可采集
            string sumstr = basefun.valtag(msg, "{刷卡记录数}");
            string dtstr  = basefun.valtag(msg, "{刷卡时间}");

            if (string.IsNullOrEmpty(sumstr) || string.IsNullOrEmpty(dtstr))
            {
                return;
            }
            int      sum    = Convert.ToInt32(sumstr);
            DateTime dtcard = DateTime.MinValue;

            try { dtcard = Convert.ToDateTime(dtstr); }
            catch { }
            if ("考勤机" == this.devtype && this.posRecord > sum)
            {
                this.posRecord = 0;
                NameObjectList posps = new NameObjectList();
                posps["控制器"] = this.devid;
                this.query.ExecuteNonQuery("重置记录位置", posps, posps, posps);
            }
            //判定是否繁忙状态
            if (sum < 1 || (sum > 60 && DateTime.Now - dtcard < this.tsbusy))
            {
                return;
            }

            //有新记录且不繁忙时可采集新记录50条
            string[] cols = { "{卡号}", "状态编号", "读卡器", "{刷卡时间}" };
            string   tag  = "@设备地址=" + Convert.ToString(this.station);
            int      st   = this.stateChecking;

            this.reChecking(0);
            bool isreset = false;

            if (sum > 10 && DateTime.Now - dtcard > (this.tsbusy + this.tsbusy + this.tsbusy))
            {
                isreset = sum <= 50;
                sum     = sum > 50 ? 51 : sum + 1;
            }
            else
            {
                isreset = sum <= 10;
                sum     = sum > 10 ? 11 : sum + 1;
            }
            TimeSpan tswait = this.waitTime;

            for (int i = 0; i < sum; i++)
            {
                if ("考勤机" == this.devtype)
                {
                    tag = basefun.setvaltag(tag, "{记录索引}", Convert.ToString(this.posRecord));
                }
                cmdP.setCommand("门禁", "读取记录", tag);
                msg = getResponse(this.commimgr, e.Target, cmdP, tswait);
                if ("true" != basefun.valtag(msg, "Success"))
                {
                    break;
                }
                string cardnum = basefun.valtag(msg, "{卡号}");
                if (string.IsNullOrEmpty(cardnum) || "16777215" == cardnum || "0" == cardnum)
                {
                    this.posRecord++;
                    continue;
                }
                string info = "";
                for (int c = 0; c < cols.Length; c++)
                {
                    info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
                }
                info = basefun.setvaltag(info, "状态", this.getStateDoorCard(basefun.valtag(info, "状态编号")));
                NameObjectList ps = ParamManager.createParam(info);
                ps["控制器"] = this.devid;
                ps["时间"]  = ps["刷卡时间"];
                bool success = this.query.ExecuteNonQuery("采集门禁数据", ps, ps, ps);
                if (!success)
                {
                    ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
                }
                DateTime dt = Convert.ToDateTime(ps["刷卡时间"]);
                this.AddRecord(info);
                DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);
                this.RaiseRecord(arg);
                if (!success)
                {
                    break;
                }
                this.posRecord++;
                //最后一条提取记录
                if (i == sum - 1 && isreset && this.posRecord > 10000)
                {
                    cmdP.setCommand("门禁", "清空记录", tag);
                    cmdP.ResetState();
                    this.commimgr.SendCommand(this.target, cmdP);
                    if (cmdP.EventWh.WaitOne(this.waitTime, false))
                    {
                        string suc = basefun.valtag(cmdP.ResponseFormat, "Success");
                        if ("true" == suc)
                        {
                            this.query.ExecuteNonQuery("重置记录位置", ps, ps, ps);
                        }
                    }
                }
            }
            this.reChecking(st);
        }
Пример #16
0
        /// <summary>
        /// 巡检响应,连续失败5分钟(tsbusy)则认为停机,间隔tsbusy巡检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdGather_ResponseHandle(object sender, ResponseEventArgs e)
        {
            if (null == sender || null == e)
            {
                return;
            }
            CmdProtocol cmdP = sender as CmdProtocol;

            if (null == cmdP || 0 == this.stateChecking)
            {
                return;
            }
            if (!e.Success)
            {
                //连续长时间失败时,增大巡检周期
                if (cmdP.TimeSendInv < this.tsbusy && DateTime.Now - cmdP.ResponseDatetime > this.tsbusy)
                {
                    if (cmdP.TimeSendInv < this.tsbusy)
                    {
                        cmdP.TimeSendInv = this.tsbusy.Add(this.tsinv);
                    }
                }
                return;
            }
            string msg = cmdP.ResponseFormat;

            if ("true" != basefun.valtag(msg, "Success"))
            {
                return;
            }
            //离线后恢复在线时,恢复巡检周期
            if (cmdP.TimeSendInv > this.tsbusy)
            {
                cmdP.TimeSendInv = this.tsinv;
            }
            //工作中或没有新记录,则
            string valwk = basefun.valtag(msg, "工作中");
            int    sum   = Convert.ToInt32(basefun.valtag(msg, "{采集标志}"));

            if ("1" == valwk || sum < 1)
            {
                this.dtwork = DateTime.Now;
            }
            this.validateSate(msg);
            //有新记录且不繁忙时可采集新记录50条
            if (sum < 1 || DateTime.Now - this.dtwork < tsbusy)
            {
                return;
            }
            string[] cols = { "{卡号}", "{卡类}", "{消费时间}", "{消费金额}", "{卡余额}", "{累计补助金额}", "{消费机号}", "{操作员号}" };
            string   tag  = "@设备地址=" + Convert.ToString(this.station);

            this.reChecking(0);
            TimeSpan tswait = this.waitTime;

            for (int i = 0; i < 51; i++)
            {
                if (i < 1)
                {
                    cmdP.setCommand("消费", "取当前消费记录", tag);
                }
                else if (i < 2)
                {
                    cmdP.setCommand("消费", "取下一条消费记录", tag);
                }
                msg = getResponse(this.commimgr, this.target, cmdP, tswait);
                if ("true" != basefun.valtag(msg, "Success"))
                {
                    break;
                }
                string cardnum = basefun.valtag(msg, "{卡号}");
                if (string.IsNullOrEmpty(cardnum) || "16777215" == cardnum || "0" == cardnum)
                {
                    continue;
                }
                string info = "";
                for (int c = 0; c < cols.Length; c++)
                {
                    info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
                }
                NameObjectList ps = ParamManager.createParam(info);
                ps["消费机"] = this.devid;
                bool success = this.query.ExecuteNonQuery("采集数据", ps, ps, ps);
                if (!success)
                {
                    ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
                }
                DateTime dt = Convert.ToDateTime(ps["消费时间"]);
                this.AddRecord(info);
                DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);
                this.RaiseRecord(arg);
                if (!success)
                {
                    break;
                }
            }
            this.reChecking(1);
        }
Пример #17
0
        /// <summary>
        /// 直接采集一条数据,并自动存入数据库,返回获取数据的记录
        /// 没有记录时恢复巡检
        /// </summary>
        /// <param name="isfirst">是否首次提取,首次会补充采集当前记录以防漏采</param>
        public override string GatherData(bool isfirst)
        {
            if (null == this.commimgr || null == this.target || string.IsNullOrEmpty(this.devid) || this.station < 1)
            {
                return("");
            }
            CmdProtocol cmdP = this.cmdGather;

            if (null == cmdP)
            {
                cmdP = new CmdProtocol(false);
            }
            string[] cols = { "{卡号}", "{卡类}", "{消费时间}", "{消费金额}", "{卡余额}", "{累计补助金额}", "{消费机号}", "{操作员号}" };
            string   tag  = "@设备地址=" + Convert.ToString(this.station);

            this.reChecking(0);
            if (recordpos.ContainsKey(this.devid))
            {
                tag = basefun.setvaltag(tag, "{记录值指针}", Convert.ToString(recordpos[this.devid]));
                cmdP.setCommand("消费", "取指定记录", tag);
            }
            else
            {
                if (isfirst)
                {
                    this.getSubsidy();
                    cmdP.setCommand("消费", "取当前消费记录", tag);
                }
                else
                {
                    cmdP.setCommand("消费", "取下一条消费记录", tag);
                }
            }
            string msg     = getResponse(this.commimgr, this.target, cmdP, this.waitTime);
            string cardnum = basefun.valtag(msg, "{卡号}");
            string suc     = basefun.valtag(msg, "Success");

            if ("true" != suc || string.IsNullOrEmpty(cardnum) || "16777215" == cardnum || "0" == cardnum)
            {
                this.reChecking(1);
                if ("false" == suc || "16777215" == cardnum || "0" == cardnum)
                {
                    recordpos.Remove(this.devid);
                }
                return(msg);
            }
            string info = "";

            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
            }
            NameObjectList ps = ParamManager.createParam(info);

            ps["消费机"] = this.devid;
            bool success = this.query.ExecuteNonQuery("采集数据", ps, ps, ps);

            if (!success)
            {
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
            if (recordpos.ContainsKey(this.devid))
            {
                recordpos[this.devid]++;
                ps.Clear();
                ps["消费机"]  = this.devid;
                ps["记录位置"] = recordpos[this.devid];
                query.ExecuteNonQuery("记录指针", ps, ps, ps);
            }
            DateTime dt = DateTime.MinValue;

            try
            {
                dt = Convert.ToDateTime(ps["消费时间"]);
            }
            catch { }
            this.AddRecord(info);
            DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);

            this.RaiseRecord(arg);
            return(msg);
        }
Пример #18
0
        /// <summary>
        /// 写入记录
        /// </summary>
        /// <param name="msg">tag格式信息:{卡号}、状态编号,读卡器号,{刷卡时间}</param>
        private string writeRecord(string msg)
        {
            string cardnum = basefun.valtag(msg, "{卡号}");
            string suc     = basefun.valtag(msg, "Success");

            if ("true" != suc || "16777215" == cardnum || "0" == cardnum || string.IsNullOrEmpty(cardnum))
            {
                return(msg);
            }
            NameObjectList ps = new NameObjectList();

            ps["卡号"] = cardnum;
            DataTable tabuser = null;

            try
            {
                Monitor.Enter(objquery);
                tabuser = this.qyemp.getTable("卡用户信息", ps);
            }
            finally
            {
                Monitor.PulseAll(objquery);
                Monitor.Exit(objquery);
            }
            if (null != tabuser && tabuser.Rows.Count > 0)
            {
                DataRow  dr     = tabuser.Rows[0];
                string[] strcol = { "部门ID", "部门代码", "部门", "姓名", "用户编号", "照片", "卡号", "卡号NUM", "卡片序列号", "卡片SN" };
                foreach (string c in strcol)
                {
                    msg = basefun.setvaltag(msg, "{" + c + "}", Convert.ToString(dr[c]));
                }
                if (DBNull.Value != dr["进入时间"])
                {
                    msg = basefun.setvaltag(msg, "{进入时间}", Convert.ToDateTime(dr["进入时间"]).ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }
            string info = "";

            string[] cols = { "{卡号}", "{卡号NUM}", "{卡片序列号}", "{卡片SN}", "{部门}", "{部门ID}", "{部门代码}", "{姓名}", "{照片}", "{用户编号}", "状态编号", "读卡器", "{刷卡时间}", "{进入时间}" };
            for (int c = 0; c < cols.Length; c++)
            {
                info = basefun.setvaltag(info, cols[c], basefun.valtag(msg, cols[c]));
            }
            string idx    = basefun.valtag(info, "读卡器");
            string chname = channels[0];

            if ("进出口" == devtype || "出入口" == devtype)
            {
                chname = "2" == idx || "3" == idx ? channels[1] : chname;
            }
            info = basefun.setvaltag(info, "通道", chname);
            info = basefun.setvaltag(info, "区域", region);
            info = basefun.setvaltag(info, "区域ID", regionID);
            info = basefun.setvaltag(info, "控制器类型", devtype);
            string strstate = this.getStateDoorCard(cardnum, basefun.valtag(info, "状态编号"), idx);

            msg       = basefun.setvaltag(msg, "状态", strstate);
            info      = basefun.setvaltag(info, "状态", strstate);
            ps        = ParamManager.createParam(info);
            ps["控制器"] = this.devid;
            ps["时间"]  = ps["刷卡时间"] = DateTime.MinValue;
            DateTime dt = DateTime.MinValue;

            try
            {
                string dtstr = basefun.valtag(msg, "{刷卡时间}");
                if (!string.IsNullOrEmpty(dtstr) && "0001-1-1 0:00:00" != dtstr)
                {
                    ps["时间"] = ps["刷卡时间"] = dt = Convert.ToDateTime(dtstr);
                }
            }
            catch { dt = DateTime.MinValue; }
            if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
            {
                if (null == dtobj)
                {
                    return(msg);
                }
                try { dt = Convert.ToDateTime(dtobj); }
                catch { return(msg); }
                if (dt < DateTime.Today.AddYears(-3) || dt > DateTime.Today.AddYears(3))
                {
                    return(msg);
                }
                ps["时间"] = ps["刷卡时间"] = dtobj;
            }
            else
            {
                dtobj = ps["时间"];
            }
            bool success = true;

            try
            {
                Monitor.Enter(objquery);
                success = qydata.ExecuteNonQuery("采集道闸数据", ps, ps, ps);
            }
            finally
            {
                Monitor.PulseAll(objquery);
                Monitor.Exit(objquery);
            }
            if (!success)
            {
                NameValueCollection attr = new NameValueCollection();
                attr["功能"] = "采集道闸数据保存失败";
                ServiceTool.LogMessage(info, null, EventLogEntryType.Warning);
            }
            this.AddRecord(info);
            DvRecordEventArgs arg = new DvRecordEventArgs(this.devid, this.station, dt, info);

            this.RaiseRecord(arg);
            return(msg);
        }