ScanServer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapDal;
  5. using DllEapEntity;
  6. using RabbitMQ.Client;
  7. using RabbitMQ.Client.Events;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Text;
  12. using System.Linq;
  13. using System.IO;
  14. using System.IO.Compression;
  15. using System.Runtime.Serialization.Formatters.Binary;
  16. using Microsoft.Extensions.Logging;
  17. namespace DllScanForFiles
  18. {
  19. public class ScanServer : IScanServer
  20. {
  21. private List<ScanCore> CurrHsms = new List<ScanCore>();
  22. public List<ConstItem> CurrItemDt { set; get; } = null;//固定项表
  23. private string CurrQueueName = "";//队列名称
  24. private List<MacOrder> CurrMacOrder = null;
  25. private string CurrLogFile = "";
  26. private List<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
  27. private IDatabase CurrDb = null;
  28. private int IsStart = -1;//启动状态
  29. private ILogger loger = null;
  30. public long CurrMaxBlock = 0;
  31. public string CurrShareFileDir = "";
  32. private string CurrProgramDir = "";
  33. public ScanServer(ILogger<ScanServer> loger)
  34. {
  35. this.loger = loger;
  36. CurrMaxBlock = long.Parse(AppConfigurtaionServices.Configuration["MaxBlock"]);
  37. CurrShareFileDir = AppConfigurtaionServices.Configuration["ShareFileDir"];
  38. CurrProgramDir = AppConfigurtaionServices.Configuration["ProgramDir"];
  39. }
  40. /// <summary>
  41. /// 记录错误信息
  42. /// </summary>
  43. /// <param name="str"></param>
  44. public void SetText(string str)
  45. {
  46. int id = 1;
  47. if (CurrInfo.Count > 0)
  48. {
  49. id = CurrInfo.Max(t => t.ID);
  50. id++;
  51. }
  52. ServerInfo entity = new ServerInfo();
  53. entity.ID = id;
  54. entity.Info = str;
  55. CurrInfo.Add(entity);
  56. }
  57. ////记录程序内部错误日志
  58. //private void WriteErrorInfoLog(string errorinfo)
  59. //{
  60. // try
  61. // {
  62. // if (CurrLogFile != "")
  63. // {
  64. // WriteLog.WriteLogFor(CurrLogFile, errorinfo);
  65. // }
  66. // }
  67. // catch (Exception ex)
  68. // {
  69. // SetText(ex.Message.ToString());
  70. // }
  71. //}
  72. //private string CreateLogFile(ref string errorinfo)
  73. //{
  74. // try
  75. // {
  76. // string sFilePath = System.IO.Directory.GetCurrentDirectory() + "\\log" + DateTime.Now.ToString("yyyyMMdd");
  77. // string sFileName = "MainFram" + DateTime.Now.ToString("HHmmss") + ".log";
  78. // sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
  79. // if (!Directory.Exists(sFilePath))//验证路径是否存在
  80. // {
  81. // Directory.CreateDirectory(sFilePath);
  82. // //不存在则创建
  83. // }
  84. // return sFileName;
  85. // }
  86. // catch (Exception ex)
  87. // {
  88. // errorinfo = ex.Message.ToString();
  89. // return "";
  90. // }
  91. //}
  92. //读取固定标准项数据
  93. private List<ConstItem> GetItem(ref string errorinfo)
  94. {
  95. try
  96. {
  97. string condition = $" and {EntityAttribute.GetPropertyCondition<ConstItem>(nameof(ConstItem.PreID))}=5";
  98. List<ConstItem> tempds = CurrDb.FindListForCondition<ConstItem>(condition, ref errorinfo).ToList();
  99. if (tempds == null)
  100. return null;
  101. if (tempds.Count <= 0)
  102. {
  103. errorinfo = "没有读取到代码对应表数据!";
  104. return null;
  105. }
  106. return tempds;
  107. }
  108. catch (Exception ex)
  109. {
  110. errorinfo = ex.Message.ToString();
  111. return null;
  112. }
  113. }
  114. /// <summary>
  115. /// 总启动
  116. /// </summary>
  117. public int Start(ref string errorinfo)
  118. {
  119. try
  120. {
  121. IsStart = -1;
  122. //创建数据库访问对象
  123. if (CurrDb == null)
  124. CurrDb = DbFactory.Base("eap");
  125. //CurrLogFile = CreateLogFile(ref errorinfo);
  126. //if (errorinfo != "")
  127. //{
  128. // return -1;
  129. //}
  130. CurrItemDt = GetItem(ref errorinfo);
  131. if (errorinfo != "")
  132. {
  133. return -1;
  134. }
  135. if (CurrMacOrder != null)
  136. {
  137. foreach (var item in CurrMacOrder)
  138. {
  139. StopSingleHsms(item);
  140. }
  141. CurrMacOrder.Clear();
  142. }
  143. string condition = $" and b.IsConn>0";
  144. CurrMacOrder = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  145. if (CurrMacOrder == null)
  146. {
  147. SetText("启动发生错误:" + errorinfo);
  148. return -1;
  149. }
  150. foreach (var item in CurrMacOrder)
  151. {
  152. //if (item.MacFCode != "DISW0001")
  153. // continue;
  154. StartSingleHsms(item);
  155. }
  156. IsStart = 1;
  157. //CurrInfo.Add("启动成功。");
  158. SetText("启动成功。");
  159. //int ttresult = ReadToFile("", ref errorinfo);
  160. //if (ttresult < 0)
  161. //{
  162. // CurrHsms.Clear();
  163. // CurrMacOrder.Clear();
  164. // return -1;
  165. //}
  166. return 1;
  167. }
  168. catch (Exception ex)
  169. {
  170. SetText(ex.Message.ToString());
  171. return -1;
  172. }
  173. }
  174. private int StopSingleHsms(MacOrder row)
  175. {
  176. try
  177. {
  178. row.IsStart = 0;
  179. string errorinfo = "";
  180. //先在链表里找,是否已经创建该实例
  181. List<ScanCore> temp = CurrHsms.Where(a => a.CurrMac.ID == row.ID).ToList();
  182. if (temp.Count > 0)
  183. {
  184. int result = temp[0].ShutDown(ref errorinfo);
  185. if (result <= 0)
  186. {
  187. row.Remark = "停止失败:" + errorinfo;
  188. return -1;
  189. }
  190. CurrHsms.Remove(temp[0]);
  191. }
  192. row.Remark = "停止";
  193. return 1;
  194. }
  195. catch (Exception ex)
  196. {
  197. row.Remark = "启动失败:" + ex.Message.ToString();
  198. return -1;
  199. }
  200. }
  201. private ScanCore CreateHsms(MacOrder row, ref string errorinfo)
  202. {
  203. try
  204. {
  205. //重新读取机台指令信息
  206. string condition = $" and {EntityAttribute.GetPropertyCondition<MacOrder>(nameof(MacOrder.ID))}={row.ID}";
  207. List<MacOrder> tempmacs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  208. if (tempmacs.Count <= 0)
  209. {
  210. errorinfo = $"未找到机台【{row.MacFCode}】的指令版次,可能被删除。";
  211. return null;
  212. }
  213. int issel = row.IsSelected;
  214. EntityHelper.CopyPropertyValue<MacOrder>(tempmacs[0], row);
  215. row.IsSelected = issel;
  216. ScanCore hsms = new ScanCore(row,loger);
  217. row.IsStart = 1;
  218. return hsms;
  219. }
  220. catch (Exception ex)
  221. {
  222. errorinfo = ex.Message.ToString();
  223. return null;
  224. }
  225. }
  226. private void GetErrorInfo(int id, string errorinfo)
  227. {
  228. lock (CurrMacOrder)
  229. {
  230. try
  231. {
  232. //锁定当前数据集合,防止被同时修改,引起混乱
  233. List<MacOrder> rows = CurrMacOrder.Where(t => t.ID == id).ToList();//.Tables[0].Select("id=" + id.ToString());
  234. if (rows.Count <= 0)
  235. return;
  236. rows[0].Remark = errorinfo;
  237. }
  238. catch (Exception ex)
  239. {
  240. //SetText("\r\n");
  241. //SetText(id.ToString() + "异常错误:" + ex.Message.ToString()+errorinfo);
  242. StringBuilder sqlstr = new StringBuilder(100);
  243. sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  244. sqlstr.AppendFormat("修改行出错,机台ID【{0}】\r\n", id);
  245. sqlstr.AppendFormat("修改信息:{0}\r\n", errorinfo);
  246. sqlstr.AppendFormat("错误信息:{0}\r\n", ex.Message.ToString());
  247. this.loger.LogError(sqlstr.ToString());
  248. }
  249. }
  250. }
  251. //启动指定行
  252. private int StartSingleHsms(MacOrder row)
  253. {
  254. try
  255. {
  256. string errorinfo = "";
  257. //先在链表里找,是否已经创建该实例
  258. List<ScanCore> temp = CurrHsms.Where(a => a.CurrMac.ID == row.ID).ToList();
  259. if (temp.Count > 0)
  260. {
  261. int tempresult = StopSingleHsms(row);
  262. if (tempresult <= 0)
  263. return -1;
  264. }
  265. ScanCore hsms = CreateHsms(row, ref errorinfo);// new Hsms03(row);
  266. if (hsms == null)
  267. {
  268. row.Remark = errorinfo;
  269. return -1;
  270. }
  271. hsms.CurrItemDt = CurrItemDt;
  272. //添加事件,如错误事件、上报数据事件
  273. hsms.eventSetErrorText += GetErrorInfo;
  274. //hsms.eventReportData += GetReportData;
  275. int result = hsms.Start(ref errorinfo);
  276. if (result <= 0)
  277. {
  278. row.Remark = "启动失败:" + errorinfo;
  279. return -1;
  280. }
  281. row.Remark = "正常";
  282. CurrHsms.Add(hsms);
  283. return 1;
  284. }
  285. catch (Exception ex)
  286. {
  287. row.Remark = "启动失败:" + ex.Message.ToString();
  288. return -1;
  289. }
  290. }
  291. /// <summary>
  292. /// 启动所有行
  293. /// </summary>
  294. public void StartAll()
  295. {
  296. try
  297. {
  298. //if(CurrMacOrder!=null)
  299. //{
  300. // foreach(var item in CurrMacOrder)
  301. // {
  302. // StopSingleHsms(item);
  303. // }
  304. //}
  305. //CurrMacOrder.Clear();
  306. //string condition = $" and b.IsConn>0";
  307. //string errorinfo = "";
  308. //CurrMacOrder = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  309. //if (CurrMacOrder == null)
  310. //{
  311. // SetText("启动发生错误:"+errorinfo);
  312. //}
  313. //foreach(var item in CurrMacOrder)
  314. //{
  315. // StartSingleHsms(item);
  316. //}
  317. }
  318. catch (Exception ex)
  319. {
  320. SetText(ex.Message.ToString());
  321. }
  322. }
  323. public List<MacOrder> GetMacOrder()
  324. {
  325. return CurrMacOrder;
  326. }
  327. public int GetStatus(ref string errorinfo)
  328. {
  329. if (IsStart <= 0 && CurrInfo.Count > 0)
  330. {
  331. errorinfo = CurrInfo.Last().Info;
  332. }
  333. return IsStart;
  334. }
  335. /// <summary>
  336. /// 停止通讯服务
  337. /// </summary>
  338. /// <param name="errorinfo"></param>
  339. /// <returns></returns>
  340. public int Stop(ref string errorinfo)
  341. {
  342. try
  343. {
  344. IsStart = -1;
  345. //创建数据库访问对象
  346. foreach (var item in CurrHsms)
  347. {
  348. string temperrorinfo = "";
  349. item.ShutDown(ref temperrorinfo);
  350. //CurrInfo.Add(temperrorinfo);
  351. SetText(temperrorinfo);
  352. }
  353. //CurrQueueChannel.Close();
  354. return 1;
  355. }
  356. catch (Exception ex)
  357. {
  358. SetText(ex.Message.ToString());
  359. return -1;
  360. }
  361. }
  362. public List<ServerInfo> GetInfo()
  363. {
  364. return CurrInfo;
  365. }
  366. public List<MacOrder> GetMacOrder(int preid)
  367. {
  368. List<MacOrder> templist = CurrMacOrder.Where(t => t.PreID == preid).ToList();
  369. return templist;
  370. }
  371. public int Start(string idstr, ref string errorinfo)
  372. {
  373. try
  374. {
  375. if (IsStart <= 0)
  376. {
  377. errorinfo = "服务已停止,请先开启服务。";
  378. return -1;
  379. }
  380. if (idstr.Substring(0, 1) == ",")
  381. idstr = idstr.Substring(1, idstr.Length - 1);
  382. if (idstr.Substring(idstr.Length - 2, 1) == ",")
  383. idstr = idstr.Substring(0, idstr.Length - 1);
  384. string condition = $" and a.id in({idstr})";
  385. List<MacOrder> templist = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  386. if (templist == null)
  387. return -1;
  388. List<MacOrder> ttlist = null;
  389. int result = 0;
  390. foreach (var item in templist)
  391. {
  392. ttlist = CurrMacOrder.Where(t => t.ID == item.ID).ToList();
  393. if (ttlist.Count <= 0)
  394. {
  395. CurrMacOrder.Add(item);
  396. result = StartSingleHsms(item);
  397. }
  398. else
  399. {
  400. result = StartSingleHsms(ttlist[0]);
  401. }
  402. }
  403. return 1;
  404. }
  405. catch (Exception ex)
  406. {
  407. errorinfo = ex.Message.ToString();
  408. return -1;
  409. }
  410. }
  411. public int Stop(string idstr, ref string errorinfo)
  412. {
  413. try
  414. {
  415. if (IsStart <= 0)
  416. {
  417. errorinfo = "服务已停止,请先开启服务。";
  418. return -1;
  419. }
  420. if (idstr.Substring(0, 1) == ",")
  421. idstr = idstr.Substring(1, idstr.Length - 1);
  422. if (idstr.Substring(idstr.Length - 2, 1) == ",")
  423. idstr = idstr.Substring(0, idstr.Length - 1);
  424. string condition = $" and a.id in({idstr})";
  425. List<MacOrder> templist = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  426. if (templist == null)
  427. return -1;
  428. List<MacOrder> ttlist = null;
  429. int result = 0;
  430. foreach (var item in templist)
  431. {
  432. ttlist = CurrMacOrder.Where(t => t.ID == item.ID).ToList();
  433. if (ttlist.Count <= 0)
  434. continue;
  435. result = StopSingleHsms(ttlist[0]);
  436. }
  437. return 1;
  438. }
  439. catch (Exception ex)
  440. {
  441. errorinfo = ex.Message.ToString();
  442. return -1;
  443. }
  444. }
  445. public int StopCheckThread(ref string errorinfo)
  446. {
  447. try
  448. {
  449. if (CurrHsms == null)
  450. return 1;
  451. foreach (var item in CurrHsms)
  452. {
  453. errorinfo = "";
  454. item.StopCheckThread(ref errorinfo);
  455. if (!string.IsNullOrEmpty(errorinfo))
  456. SetText(errorinfo);
  457. }
  458. return 1;
  459. }
  460. catch (Exception ex)
  461. {
  462. errorinfo = ex.Message.ToString();
  463. SetText(errorinfo);
  464. return -1;
  465. }
  466. }
  467. private int JudgeIsLost(DataSet ds, ref string errorinfo)
  468. {
  469. try
  470. {
  471. string tablename = "";
  472. int fnum = 0;
  473. StringBuilder sqlstr = new StringBuilder(100);
  474. IDatabase db = DbFactory.Base("eap");
  475. foreach (DataRow temprow in ds.Tables["&order&"].Rows)
  476. {
  477. tablename = temprow["tablename"].ToString();
  478. fnum = int.Parse(temprow["fcount"].ToString());
  479. sqlstr = new StringBuilder(100);
  480. sqlstr.AppendFormat("select min(a.rectime) from (select t.* from {0} t order by t.rectime desc limit 100) a", tablename);
  481. object maxobj = db.FindObject(sqlstr.ToString());
  482. if (string.IsNullOrEmpty(maxobj.ToString()))
  483. maxobj = DateTime.Now;
  484. sqlstr = new StringBuilder(100);
  485. sqlstr.AppendFormat("select max(a.rectime) from ( select t.* from {0} t order by t.rectime limit 100) a", tablename);
  486. object minobj = db.FindObject(sqlstr.ToString());
  487. if (string.IsNullOrEmpty(minobj.ToString()))
  488. minobj = DateTime.Now;
  489. TimeSpan lev = (DateTime)maxobj - (DateTime)minobj;
  490. if (lev.Days > fnum)
  491. {
  492. return -1;
  493. }
  494. }
  495. return 1;
  496. }
  497. catch (Exception ex)
  498. {
  499. errorinfo = ex.Message.ToString();
  500. return -1;
  501. }
  502. }
  503. private int ReadToFile(string FileFullName, ref string errorinfo)
  504. {
  505. try
  506. {
  507. FileStream fs = new FileStream("Microsoft.AspNetCore.HostFilteringe.dll", FileMode.Open, FileAccess.Read);
  508. int FileLength = (int)fs.Length;
  509. byte[] data = new byte[FileLength];
  510. fs.Read(data, 0, FileLength);
  511. fs.Close();
  512. MemoryStream input = new MemoryStream();
  513. input.Write(data, 0, data.Length);
  514. input.Position = 0;
  515. GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
  516. MemoryStream output = new MemoryStream();
  517. byte[] buff = new byte[4096];
  518. int read = -1;
  519. read = gzip.Read(buff, 0, buff.Length);
  520. while (read > 0)
  521. {
  522. output.Write(buff, 0, read);
  523. read = gzip.Read(buff, 0, buff.Length);
  524. }
  525. gzip.Close();
  526. byte[] rebytes = output.ToArray();
  527. output.Close();
  528. input.Close();
  529. string first = "jil;kkasd;mvL93l/dksoiem-2093lkciejd[j;lko214j";
  530. string end = "003209u50--235902939kdls;kcm;id";
  531. byte[] firsts = System.Text.Encoding.Default.GetBytes(first);
  532. byte[] ends = System.Text.Encoding.Default.GetBytes(end);
  533. byte[] tempbytes = new byte[rebytes.Length - firsts.Length - ends.Length];
  534. Array.Copy(rebytes, firsts.Length + 18, tempbytes, 0, rebytes.Length - firsts.Length - ends.Length - 18);
  535. Array.Copy(rebytes, 0, tempbytes, rebytes.Length - firsts.Length - ends.Length - 18, 18);
  536. MemoryStream ms = new MemoryStream(tempbytes);
  537. BinaryFormatter bf = new BinaryFormatter();
  538. object obj = bf.Deserialize(ms);
  539. DataSet ds = (DataSet)obj;
  540. int result = JudgeIsLost(ds, ref errorinfo);
  541. return result;
  542. }
  543. catch (Exception ex)
  544. {
  545. errorinfo = ex.Message.ToString();
  546. return -1;
  547. }
  548. }
  549. List<ServerInfo> IScanServer.GetInfo()
  550. {
  551. throw new NotImplementedException();
  552. }
  553. }
  554. }