//读取数据库命令线程 public void CheckDataBaseQueue(object state) { //add 3-17 粗心掉了一句 checkDataBaseQueueResetEvent.Reset(); //Reset()将事件状态设置为非终止状态,导致线程阻止。 while (IsServerOpen) { try { /*1.先读取tcommand表中所有deviceID的cmdName字段 * 2.“-1”表示无命令,其余值表示命令名称“name” * 3.根据name来读取对应ID的operation和data * 4.构造命令 * 5.把命令加入发送队列 * 6.如果是升级文件的路径,则需要读取文件内容并存入dataitem,设置标志位 * 7.如果是设置卡号,需要在dataitem设置大数组,加标志位并分多包发送。 * */ /* * ret[i, 0] = ds1.Tables[0].Rows[i]["deviceID"].ToString(); * ret[i, 1] = ds1.Tables[0].Rows[i]["cmdName"].ToString(); * ret[i, 2] = ds1.Tables[0].Rows[i]["operation"].ToString(); * ret[i, 3] = ds1.Tables[0].Rows[i]["data"].ToString(); */ string[,] cmdStrings = DbClass.readCmd(); if (cmdStrings != null)//先判定是否为空 { //add 3-14,强制处理一次数据,缩减时间,避免等待 byte[] Cmd = new byte[13]; Cmd[0] = 0xA5; Cmd[1] = 0xA5; Cmd[2] = 0x00; Cmd[3] = 0x00; Cmd[4] = 0x00; Cmd[5] = 0x00; Cmd[6] = 0x00; Cmd[7] = 0x00; Cmd[8] = 0x00; Cmd[9] = 0x00; Cmd[10 + 0] = 0xFF; Cmd[10 + 1] = 0x5A; Cmd[10 + 2] = 0x5A; for (int i = 0; i < cmdStrings.Length / 4; i++) { if (htClient.ContainsKey(cmdStrings[i, 0])) { DataItem dataItem = (DataItem)htClient[cmdStrings[i, 0]]; //有一些指令需要多包发送和读取 if (cmdStrings[i, 1] == "history") { //先清空表的记录,再采集新纪录 DbClass.deleteHistory(dataItem.strID); dataItem.tHistory.IsNeedHistory = true; //add 3-14,强制处理一次数据,缩减时间,避免等待 dataItem.recDataQueue.Enqueue(Cmd); //Enqueue 将对象添加到 Queue<T> 的结尾处 } else if (cmdStrings[i, 1] == "update") { using (FileStream fsSource = new FileStream(cmdStrings[i, 3], FileMode.Open, FileAccess.Read)) { // Read the source file into a byte array. for (int j = 0; j < updateDataLength; j++) //先用0xFF填充 { dataItem.updateData[j] = 0xFF; } int numBytesToRead = (int)fsSource.Length; if (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. fsSource.Read(dataItem.updateData, 0, numBytesToRead); } } //设置升级属性 dataItem.tUpdate.IsNeedUpdate = true; } else if (cmdStrings[i, 1] == "generalCardID") { if (cmdStrings[i, 2] == "write") { dataItem.tGeneralCardId.rw = 1; dataItem.generalCardID = UtilClass.hexStrToByte(cmdStrings[i, 3]); dataItem.tGeneralCardId.IsNeedSet = true; } else if (cmdStrings[i, 2] == "read") { dataItem.tGeneralCardId.rw = 0; dataItem.tGeneralCardId.IsNeedSet = true; } //add 3-14,强制处理一次数据,缩减时间,避免等待 dataItem.recDataQueue.Enqueue(Cmd); //Enqueue 将对象添加到 Queue<T> 的结尾处 } else if (cmdStrings[i, 1] == "fingerID") { if (cmdStrings[i, 2] == "write") { dataItem.tFingerId.rw = 1; dataItem.fingerID = UtilClass.hexStrToByte(cmdStrings[i, 3]); dataItem.tFingerId.IsNeedSet = true; } else if (cmdStrings[i, 2] == "read") { dataItem.tFingerId.rw = 0; dataItem.tFingerId.IsNeedSet = true; } } else//普通指令可以直接构造并发送 { byte[] cmd = CmdClass.makeCommand(cmdStrings[i, 1], cmdStrings[i, 2], cmdStrings[i, 3], dataItem.byteID); if (cmd != null) { dataItem.sendDataQueue.Enqueue(cmd); } } } }//end of for } } catch (Exception ex) { Console.WriteLine(ex); UtilClass.writeLog(ex.ToString()); } Thread.Sleep(checkDataBaseQueueTimeInterval); } checkDataBaseQueueResetEvent.Set(); }