const int defaultTimeout = 3 * 1000; // 打印机抓取超时,5秒 // 获取不包含香港门店的打印机信息 private static List <PrinterInformation> GetAllPrinter() { List <PrinterInformation> list = new List <PrinterInformation>(); DataSet ds = SqlHelper.GetStorePrinter(); int rows = ds.Tables[0].Rows.Count; for (int i = 0; i < rows; i++) { PrinterInformation p = new PrinterInformation(); p.StoreNo = ds.Tables[0].Rows[i]["storeNo"].ToString(); p.StoreRegion = ds.Tables[0].Rows[i]["storeRegion"].ToString(); p.StoreType = ds.Tables[0].Rows[i]["storeType"].ToString(); p.PrinterType = ds.Tables[0].Rows[i]["printerType"].ToString(); p.TonerType = ds.Tables[0].Rows[i]["tonerType"].ToString(); p.IP = ds.Tables[0].Rows[i]["IP"].ToString(); p.Url = "http://" + ds.Tables[0].Rows[i]["IP"].ToString(); p.Html = ""; p.I = i; p.StoreCount = rows; list.Add(p); } return(list); }
private static void GetPrinterStatus(RequestState state, Pattern pat) { string printerStatus, tonerStatus, percent = string.Empty; int ps, pe, ts, te; string html = state.Printer.Html; PrinterInformation printer = state.Printer; try { printerStatus = html.Substring(html.IndexOf(pat.SearchString1)).Replace(" ", " "); tonerStatus = printerStatus.Substring(printerStatus.IndexOf(pat.SearchString2, pat.SearchStartIndex)); if (!String.IsNullOrEmpty(pat.SearchString3)) { int idx = tonerStatus.IndexOf(pat.SearchString3) == -1 ? tonerStatus.IndexOf(pat.SearchString3N) : tonerStatus.IndexOf(pat.SearchString3); percent = tonerStatus.Substring(idx); } ps = printerStatus.IndexOf(pat.Anchor1); pe = printerStatus.IndexOf(pat.Anchor2); printer.PrinterStatus = printerStatus.Substring(ps + pat.Anchor1.Length, pe - ps - pat.Anchor1.Length).Trim().Replace("<br>", ""); ts = tonerStatus.IndexOf(pat.Anchor3); te = tonerStatus.IndexOf(pat.Anchor4); printer.TonerStatus = tonerStatus.Substring(ts + pat.Anchor3.Length, te - ts - pat.Anchor3.Length).Trim().Replace(pat.ReplaceString, pat.ReplaceString + " "); if (!string.IsNullOrEmpty(pat.SearchString3) && !string.IsNullOrEmpty(pat.Anchor5) && !string.IsNullOrEmpty(pat.Anchor6)) { int idxs, idxe; idxs = percent.IndexOf(pat.Anchor5); idxe = percent.IndexOf(pat.Anchor6); percent = " " + percent.Substring(idxs + pat.Anchor5.Length, idxe - idxs - pat.Anchor5.Length).Trim(); printer.TonerStatus += percent; } printer.PrinterNetwork = "UP"; } catch (System.Exception ex) { printer.PrinterStatus = ex.Message.ToString(); printer.TonerStatus = ex.Message.ToString(); printer.PrinterNetwork = "DOWN"; } }
public static void PingPrinterAsyncBase(object state) { RequestState requestState = (RequestState)state; List <PrinterInformation> printerList = requestState.List; int count = printerList.Count; string[] completedFlag = new string[count]; List <PrinterInformation> list = new List <PrinterInformation>();; int storeCount = printerList.Count; for (int i = 0; i < printerList.Count; i++) { RequestState rstate = new RequestState(); PrinterInformation printer = new PrinterInformation(); rstate.Printer = printer; rstate.Printer.StoreNo = printerList[i].StoreNo; rstate.Printer.StoreRegion = printerList[i].StoreRegion; rstate.Printer.StoreType = printerList[i].StoreType; rstate.Printer.PrinterType = printerList[i].PrinterType; rstate.Printer.TonerType = printerList[i].TonerType; rstate.Printer.IP = printerList[i].IP; rstate.Printer.Url = printerList[i].Url; rstate.Printer.StoreCount = printerList[i].StoreCount; rstate.Printer.I = i; rstate.Printer.Total = printerList.Count; rstate.Printer.PrinterList = list; rstate.Printer.PingCompletedFlag = completedFlag; rstate.IsIndexQuery = false; try { if (new Ping().Send(rstate.Printer.IP).Status == IPStatus.Success) { BeginResponse(rstate); } else { rstate.Printer.PingCompletedFlag[rstate.Printer.I] = "True"; rstate.Printer.PrinterStatus = "打印机无法连接"; rstate.Printer.TonerStatus = "打印机无法连接"; rstate.Printer.PrinterNetwork = "DOWN"; rstate.Printer.Date = DateTime.Now.ToString(); rstate.Printer.PrinterList.Add(rstate.Printer); Console.WriteLine(rstate.Printer.I.ToString() + "/" + rstate.Printer.Total.ToString() + " " + rstate.Printer.StoreNo + ": 打印机无法连接"); } } catch (System.Exception ex) { rstate.Printer.PingCompletedFlag[rstate.Printer.I] = "True"; rstate.Printer.PrinterStatus = ex.Message; rstate.Printer.TonerStatus = ex.Message; rstate.Printer.PrinterNetwork = "DOWN"; rstate.Printer.Date = DateTime.Now.ToString(); rstate.Printer.PrinterList.Add(rstate.Printer); Console.WriteLine(rstate.Printer.I.ToString() + "/" + rstate.Printer.Total.ToString() + " " + rstate.Printer.StoreNo + ": " + ex.Message.ToString()); continue; } } while (true) { if (count == PingCompletedFlagCount(completedFlag)) { string[] clist = { "storeNo", "storeRegion", "storeType", "printerNetwork", "printerStatus", "tonerStatus", "printerType", "tonerType", "date" }; DataTable dt = new DataTable(); foreach (string colname in clist) { dt.Columns.Add(colname); } int rowsCount = list.Count; for (int i = 0; i < rowsCount; i++) { DataRow row = dt.NewRow(); row["storeNo"] = list[i].StoreNo; row["storeRegion"] = list[i].StoreRegion; row["storeType"] = list[i].StoreType; row["printerNetwork"] = list[i].PrinterNetwork; row["printerStatus"] = list[i].PrinterStatus; row["tonerStatus"] = list[i].TonerStatus; row["printerType"] = list[i].PrinterType; row["tonerType"] = list[i].TonerType; row["date"] = list[i].Date; dt.Rows.Add(row); } SqlHelper.CommonBulkInsert(dt, "PrinterInformation"); Console.WriteLine("写入数据库成功!"); requestState.manualEvent.Set(); break; } } }