NumMachine.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. using System.Collections;
  7. using VzClientSDKDemo;
  8. using System.Runtime.InteropServices;
  9. using System.IO;
  10. using System.Net;
  11. using parkMonitor.entity;
  12. using System.Diagnostics;
  13. using System.Threading.Tasks;
  14. using System.Configuration;
  15. namespace NumMachine
  16. {
  17. /// <summary>
  18. /// 号牌机通信类
  19. /// </summary>
  20. public partial class NumMachineLinker : INumMachineLinker
  21. {
  22. /// <summary>
  23. /// 刷新时间间隔与个数
  24. /// </summary>
  25. public const int REFRESHINGTIME = 500, FILTERINGNUMBER = 8;
  26. ///<summary>通过设备句柄访问pic;链接时add,系统关闭时remove</summary>
  27. private static Dictionary<int, IntPtr> devPicMap = new Dictionary<int, IntPtr>();
  28. ///<summary>通过名字获取pic对象;创建pic时add,系统关闭remove</summary>
  29. private static Dictionary<string, IntPtr> namePicMap = new Dictionary<string, IntPtr>();
  30. ///<summary>通过ip获取设备id;产生ip时创建</summary>
  31. private static Dictionary<string, int> ipIdMap = new Dictionary<string, int>();
  32. ///<summary>通过ip获取设备句柄;产生句柄时创建</summary>
  33. private static Dictionary<string, int> ipHandleMap = new Dictionary<string, int>();
  34. /// <summary>句柄到号牌回调映射</summary>
  35. private static Dictionary<int, VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK> handleCallbackMap = new Dictionary<int, VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK>();
  36. ///<summary>号牌队列</summary>
  37. private static Queue<NumberMachineNode> LicBuffer = new Queue<NumberMachineNode>();
  38. ///<summary>计数Map</summary>
  39. private static Dictionary<string, Dictionary<NumberMachineNode, int>> filterMap = new Dictionary<string, Dictionary<NumberMachineNode, int>>();
  40. /// <summary>号牌机编号与激活计数</summary>
  41. private Dictionary<int, int> idCountMap = new Dictionary<int, int>();
  42. /// <summary>筛选计数</summary>
  43. private static int filterCount = 0;
  44. private static double filterRatio = 0.7;
  45. /// <summary>系统关闭</summary>
  46. private static bool isClosing = false;
  47. /// <summary>开启拍照的设备</summary>
  48. private static int snapshotDevHandle = -1;
  49. /// <summary>允许无号牌时拍照</summary>
  50. private static bool enableEmptySnapshot = true;
  51. private static NumberMachineMessage nmMsg = new NumberMachineMessage();
  52. private static VzClientSDK.VZLPRC_FIND_DEVICE_CALLBACK_EX find_DeviceCB = null;
  53. private const int MSG_PLATE_INFO = 0x901;
  54. private const int MSG_DEVICE_INFO = 0x902;
  55. /// <summary>
  56. /// 用于消息传递机制
  57. /// </summary>
  58. //public static IntPtr hwndMain;
  59. public static IntPtr flowLayoutPanel1;
  60. public static ToolStripStatusLabel timeLabel;
  61. /// <summary>
  62. /// 号牌机类构造函数
  63. /// </summary>
  64. public NumMachineLinker(IntPtr flpHandle)
  65. {
  66. //Control.CheckForIllegalCrossThreadCalls = false;
  67. //Thread thread = new Thread(new ThreadStart(DateTimeInfo));
  68. //thread.IsBackground = true;
  69. //thread.Start(); //显示当前时间
  70. flowLayoutPanel1 = flpHandle;
  71. //timeLabel = new ToolStripStatusLabel();
  72. try
  73. {
  74. VzClientSDK.VzLPRClient_Setup();
  75. filterRatio = Double.Parse(ConfigurationManager.AppSettings.Get("filterRatio"));
  76. }
  77. catch (Exception e) { Console.WriteLine(e.Message + ",号牌机配置文件异常"); }
  78. //m_sAppPath = System.IO.Directory.GetCurrentDirectory();
  79. }
  80. /// <summary>
  81. /// 定义时钟委托
  82. /// </summary>
  83. public delegate void SetDateTime();
  84. /// <summary>
  85. /// 实时采集
  86. /// </summary>
  87. public delegate void Begin_Read();
  88. private void FormSiemens_Load(object sender, EventArgs e)
  89. {
  90. //Thread thread = new Thread(new ThreadStart(DateTimeInfo));
  91. //thread.IsBackground = true;
  92. //thread.Start(); //显示当前时间
  93. //VzClientSDK.VzLPRClient_Setup();
  94. //hwndMain = this.Handle;
  95. //m_sAppPath = System.IO.Directory.GetCurrentDirectory();
  96. //this.ShowInTaskbar = false;
  97. //this.Opacity = 0;
  98. }
  99. private void FormSiemens_FormClosing(object sender, FormClosingEventArgs e)
  100. {
  101. Stop();
  102. }
  103. private void DateTimeInfo()
  104. {
  105. try
  106. {
  107. while (!isClosing)
  108. {
  109. SetDateTime setDate = new SetDateTime(
  110. delegate
  111. {
  112. timeLabel.Text = DateTime.Now.ToString();
  113. });
  114. setDate();
  115. Thread.Sleep(1000);
  116. }
  117. // ReSharper disable once FunctionNeverReturns
  118. }
  119. catch (Exception e) { Debug.Print(e.StackTrace); }
  120. }
  121. //******************************************************************************************************************
  122. private void UpdateStatus(NumberMachineNode nmn)
  123. {
  124. //提示次数计数
  125. int count = 0;
  126. while (!isClosing)
  127. {
  128. if (GetStatus(nmn.ip) == 1)
  129. {
  130. nmn.status = EnumNumberMachineStatus.Normal;
  131. count = 0;
  132. }
  133. else
  134. {
  135. count++;
  136. //号牌机断线写日志
  137. nmn.status = EnumNumberMachineStatus.Offline;
  138. if (count == 1)
  139. {
  140. Console.WriteLine("正在与ip为 " + nmn.ip + " 的号牌机 进行连接。");
  141. }
  142. else if (count == 30)
  143. {
  144. nmMsg.data.Remove(nmn);
  145. }
  146. Thread.Sleep(10000);
  147. }
  148. }
  149. }
  150. private void UpdateIpId()
  151. {
  152. }
  153. private void Run()
  154. {
  155. try
  156. {
  157. while (!isClosing)
  158. {
  159. Thread.Sleep(REFRESHINGTIME);
  160. //定时更新号牌
  161. lock (ipHandleMap)
  162. {
  163. Dictionary<string, int>.Enumerator HandleEnumer = ipHandleMap.GetEnumerator();
  164. while (HandleEnumer.MoveNext())
  165. {
  166. ActivateSnap(HandleEnumer.Current.Value);
  167. }
  168. }
  169. ////读取设备ip与id映射关系
  170. //lock (ipHandleMap)
  171. //{
  172. // Dictionary<string, int>.Enumerator ipEnumer = ipHandleMap.GetEnumerator();
  173. // while (ipEnumer.MoveNext())
  174. // {
  175. // //映射关系不存在则读取配置文件
  176. // lock (ipIdMap)
  177. // {
  178. // if (ipEnumer.Current.Key != null && !ipIdMap.ContainsKey(ipEnumer.Current.Key))
  179. // {
  180. // try
  181. // {
  182. // int id = Int32.Parse(ConfigurationManager.AppSettings.Get(ipEnumer.Current.Key));
  183. // ipIdMap.Add(ipEnumer.Current.Key, id);
  184. // idCountMap.Add(id, FILTERINGNUMBER);
  185. // }
  186. // catch (Exception) { Console.WriteLine("读取号牌机编号映射失败,配置文件填写有误"); }
  187. // }
  188. // }
  189. // }
  190. //}
  191. lock (LicBuffer)
  192. {
  193. //删除已停好车的号牌
  194. for (int i = 0; i < LicBuffer.Count; i++)
  195. {
  196. NumberMachineNode n = (NumberMachineNode)LicBuffer.Dequeue().Clone();
  197. if (n.ip != "")
  198. {
  199. LicBuffer.Enqueue((NumberMachineNode)n.Clone());
  200. }
  201. }
  202. }
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. Debug.WriteLine(ex.ToString());
  208. }
  209. }
  210. ///<summary>寻找设备回调函数</summary>
  211. private void FIND_DEVICE_CALLBACK_EX(string pStrDevName, string pStrIPAddr, ushort usPort1, ushort usPort2, uint SL, uint SH, string netmask, string gateway, IntPtr pUserData)
  212. {
  213. string pStrDev = pStrIPAddr.ToString() + ":" + usPort1.ToString();
  214. string serialNO = SL.ToString() + ":" + SH.ToString() + ":" + netmask + ":" + gateway;
  215. VzClientSDK.VZ_LPR_DEVICE_INFO device_info = new VzClientSDK.VZ_LPR_DEVICE_INFO();
  216. device_info.device_ip = pStrDev;
  217. device_info.serial_no = serialNO;
  218. DeviceLink(pStrDev, serialNO);
  219. }
  220. ///<summary>与设备连接,启动更新设备状态线程,输出视频</summary>
  221. private void DeviceLink(string pStrDev, string serialNO)
  222. {
  223. IPEndPoint ipe;
  224. GetIpEndPoint(pStrDev, out ipe);
  225. string ip = ipe.Address.ToString();
  226. if (ipHandleMap.ContainsKey(ip))
  227. {
  228. //MessageBox.Show("设备已分配句柄");
  229. return;
  230. }
  231. int handle = 0;
  232. //找到设备,加入list
  233. NumberMachineNode node = new NumberMachineNode(ip, 0, "", "", 1);
  234. nmMsg.data.Add(node);
  235. handle = VzClientSDK.VzLPRClient_Open(ip, (ushort)80, "admin", "admin");
  236. Task.Factory.StartNew(() =>
  237. {
  238. UpdateStatus(node);
  239. });
  240. if (handle == 0)
  241. {
  242. return;
  243. }
  244. VzClientSDK.VzLPRClient_SetVideoEncodeType(handle, 0);
  245. //将句柄加入
  246. lock (ipHandleMap)
  247. {
  248. ipHandleMap.Add(ip, handle);
  249. }
  250. //MessageBox.Show("摄像头打开成功");
  251. lock (handleCallbackMap)
  252. {
  253. handleCallbackMap.Add(handle, null);
  254. }
  255. //读到设备则添加id映射并初始化号牌计数器
  256. lock (ipIdMap)
  257. {
  258. if (!ipIdMap.ContainsKey(ip))
  259. {
  260. try
  261. {
  262. int id = Int32.Parse(ConfigurationManager.AppSettings.Get(ip));
  263. ipIdMap.Add(ip, id);
  264. idCountMap.Add(id, FILTERINGNUMBER);
  265. }
  266. catch (Exception) { Console.WriteLine("读取号牌机编号映射失败,配置文件填写有误"); }
  267. }
  268. }
  269. //链接句柄到新PictureBox
  270. VideoOutput(handle);
  271. }
  272. ///<summary>视频输出</summary>
  273. private void VideoOutput(int handle)
  274. {
  275. try
  276. {
  277. lock (devPicMap)
  278. {
  279. lock (handleCallbackMap)
  280. {
  281. //存在设备则复位并在原pic上输出
  282. if (devPicMap.ContainsKey(handle))
  283. {
  284. VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, null, IntPtr.Zero, 0);
  285. IntPtr pic;
  286. if (devPicMap.TryGetValue(handle, out pic) && handleCallbackMap.ContainsKey(handle))
  287. {
  288. int playHandle = VzClientSDK.VzLPRClient_StartRealPlay(handle, pic);
  289. // 设置车牌识别结果回调
  290. handleCallbackMap[handle] = new VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK(OnPlateResult);
  291. VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, handleCallbackMap[handle], IntPtr.Zero, 1);
  292. //m_PlateResultCB = new VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK(OnPlateResult);
  293. //VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, m_PlateResultCB, IntPtr.Zero, 1);
  294. //VzClientSDK.VzLPRClient_StopRealPlay(handle);
  295. }
  296. }
  297. else//否则找一个空位加pic
  298. {
  299. for (int i = 0; i < 20; i++)
  300. {
  301. string str = "PictureBox" + Convert.ToString(i);
  302. //该名称对应控件不存在,则创建并链接pic
  303. lock (namePicMap)
  304. {
  305. if (!namePicMap.ContainsKey(str) && handleCallbackMap.ContainsKey(handle))
  306. {
  307. IntPtr pic;
  308. if (CreatePic(i,handle, out pic))
  309. {
  310. devPicMap.Add(handle, pic);
  311. //ipIdMap.Add(Get_IP(handle), i);
  312. VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, null, IntPtr.Zero, 0);
  313. //Console.WriteLine(handle+","+pic.ToString());
  314. int playHandle = VzClientSDK.VzLPRClient_StartRealPlay(handle, pic);
  315. // 设置车牌识别结果回调
  316. handleCallbackMap[handle] = new VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK(OnPlateResult);
  317. VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, handleCallbackMap[handle], IntPtr.Zero, 1);
  318. //m_PlateResultCB = new VzClientSDK.VZLPRC_PLATE_INFO_CALLBACK(OnPlateResult);
  319. //VzClientSDK.VzLPRClient_SetPlateInfoCallBack(handle, m_PlateResultCB, IntPtr.Zero, 1);
  320. //VzClientSDK.VzLPRClient_StopRealPlay(handle);
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. catch (Exception e)
  331. {
  332. Debug.WriteLine(e.Message + ",jumped out");
  333. }
  334. }
  335. ///<summary>号牌信息回调</summary>
  336. private int OnPlateResult(int handle, IntPtr pUserData,
  337. IntPtr pResult, uint uNumPlates,
  338. VzClientSDK.VZ_LPRC_RESULT_TYPE eResultType,
  339. IntPtr pImgFull,
  340. IntPtr pImgPlateClip)
  341. {
  342. if (eResultType != VzClientSDK.VZ_LPRC_RESULT_TYPE.VZ_LPRC_RESULT_REALTIME)
  343. {
  344. VzClientSDK.TH_PlateResult result = (VzClientSDK.TH_PlateResult)Marshal.PtrToStructure(pResult, typeof(VzClientSDK.TH_PlateResult));
  345. string strLicense = (new string(result.license)).Split('\0')[0];
  346. VzClientSDK.VZ_LPR_MSG_PLATE_INFO plateInfo = new VzClientSDK.VZ_LPR_MSG_PLATE_INFO();
  347. plateInfo.plate = strLicense;
  348. SetDetail(plateInfo, Get_IP(handle));
  349. //根据setMessage中通过id信息找到的handle保存图片
  350. if (handle == snapshotDevHandle)
  351. {
  352. if (enableEmptySnapshot || !strLicense.Contains("_无_"))
  353. {
  354. string strFilePath = ConfigurationManager.AppSettings["LogPath"] + DateTime.Now.ToString("yyyyMMdd") + "\\";
  355. if (!Directory.Exists(strFilePath))
  356. {
  357. Directory.CreateDirectory(strFilePath);
  358. }
  359. string ip = Get_IP(handle);
  360. string path = strFilePath + ip + "-" + DateTime.Now.ToString("hh_mm_ss") + ".jpg";
  361. int temp = VzClientSDK.VzLPRClient_ImageSaveToJpeg(pImgFull, path, 50);
  362. if (temp != -1)
  363. {
  364. Console.WriteLine("号牌机" + ip + "已拍照,图片保存于 " + strFilePath);
  365. }
  366. else
  367. {
  368. Console.WriteLine("图片保存失败");
  369. }
  370. }
  371. snapshotDevHandle = -1;
  372. }
  373. }
  374. return 0;
  375. }
  376. ///<summary>记录车牌,时间,状态信息;刷新本地entity,并且入buffer</summary>
  377. private void SetDetail(VzClientSDK.VZ_LPR_MSG_PLATE_INFO plateInformation, string strIP)
  378. {
  379. int stat = GetStatus(strIP);
  380. bool found = false;
  381. int id = 0;
  382. //检查设备是否存在
  383. foreach (NumberMachineNode nmn in nmMsg.data)
  384. {
  385. //相同设备
  386. if (nmn.ip == strIP)
  387. {
  388. found = true;
  389. //号牌不为空
  390. if (!(plateInformation.plate.Contains("_无_")))
  391. {
  392. nmn.SetLic(strIP, nmn.id, plateInformation.plate, DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"), GetStatus(strIP));
  393. FilterLic(strIP, (NumberMachineNode)nmn.Clone());
  394. }
  395. //号牌为空
  396. else
  397. {
  398. nmn.SetLic(strIP, nmn.id, "", "", GetStatus(strIP));
  399. FilterLic(strIP, null);
  400. }
  401. }
  402. }
  403. //新设备
  404. if (!found)
  405. {
  406. lock (ipIdMap)
  407. {
  408. if (!(plateInformation.plate.Contains("_无_")) && ipIdMap.TryGetValue(strIP, out id))
  409. {
  410. NumberMachineNode nmn = new NumberMachineNode(strIP, id, plateInformation.plate, DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"), GetStatus(strIP));
  411. NumberMachineNode nmnc = (NumberMachineNode)nmn.Clone();
  412. nmMsg.data.Add(nmnc);
  413. FilterLic(strIP, nmnc);
  414. }
  415. else
  416. {
  417. nmMsg.data.Add(new NumberMachineNode(strIP, id, "", "", GetStatus(strIP)));
  418. FilterLic(strIP, null);
  419. }
  420. }
  421. }
  422. }
  423. ///<summary>筛选号牌</summary>
  424. private void FilterLic(string ip, NumberMachineNode nmn)
  425. {
  426. int id = 0, activeCount = 0;
  427. if (ipIdMap.TryGetValue(ip, out id) && idCountMap.TryGetValue(id, out activeCount) && activeCount < FILTERINGNUMBER)
  428. {
  429. Dictionary<NumberMachineNode, int> filter;
  430. //该filter不存在则创建
  431. if (!filterMap.ContainsKey(ip))
  432. {
  433. filter = new Dictionary<NumberMachineNode, int>();
  434. filterMap.Add(ip, filter);
  435. }
  436. else if (!filterMap.TryGetValue(ip, out filter))//计数器异常
  437. {
  438. return;
  439. }
  440. else if (activeCount == 0) //刚接到拍摄命令
  441. {
  442. filter.Clear();
  443. }
  444. //激活次数统计
  445. idCountMap[id] = activeCount + 1;
  446. //filter计数
  447. int count = 0;
  448. if (nmn == null)
  449. {
  450. if (activeCount >= FILTERINGNUMBER - 1)
  451. {
  452. //UILogServer.ins.error("本轮未扫描到号牌,请重新点击按钮停车");
  453. //string context = System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ":" + "号牌未扫描到,请重发指令";
  454. //string sql = "insert into messagequeue(userID,context,messageType) values (1,'" + context + "',1)";
  455. //List<string> strs = new List<string>();
  456. //strs.Add(sql);
  457. //DBOperation.InsertMessageQueue(EntityForCore.remoteBQ, strs);
  458. }
  459. return;
  460. }
  461. else if (filter.ContainsKey(nmn) && filter.TryGetValue(nmn, out count))//存在则数量+1
  462. {
  463. filter[nmn] = count + 1;
  464. }
  465. else//不存在则计数1
  466. {
  467. filter.Add(nmn, 1);
  468. }
  469. //计算总数
  470. filterCount = 0;
  471. Dictionary<NumberMachineNode, int>.Enumerator countEnumer = filter.GetEnumerator();
  472. while (countEnumer.MoveNext())
  473. {
  474. filterCount += countEnumer.Current.Value;
  475. }
  476. //达到计数限制,计算总数是否达标,达标则入队
  477. if (filterCount >= FILTERINGNUMBER)
  478. {
  479. lock (LicBuffer)
  480. {
  481. Dictionary<NumberMachineNode, int>.Enumerator enumer = filter.GetEnumerator();
  482. int maxCount = 0;
  483. NumberMachineNode node = null;
  484. while (enumer.MoveNext())
  485. {
  486. ////遍历,计数达标且队列中无此号牌
  487. //if (enumer.Current.Value >= (int)(filterCount * filterRatio) && enumer.Current.Key != null && !LicBuffer.Contains(enumer.Current.Key))
  488. //找到最大计数及相应号牌信息
  489. if (enumer.Current.Value >= maxCount && enumer.Current.Key != null && !LicBuffer.Contains(enumer.Current.Key))
  490. {
  491. //输出节点为空或与该号牌不同
  492. if (nmMsg.aNode == null || nmMsg.aNode.LicenseNum == null || nmMsg.aNode.LicenseNum != enumer.Current.Key.LicenseNum)
  493. {
  494. maxCount = enumer.Current.Value;
  495. node = (NumberMachineNode)enumer.Current.Key.Clone();
  496. if (node != null && ipIdMap.TryGetValue(node.ip, out node.id))
  497. {
  498. LicBuffer.Enqueue((NumberMachineNode)node.Clone());
  499. if (node.id == 1)
  500. {
  501. Task.Factory.StartNew(() =>
  502. {
  503. if (Monitor.Monitor.allInOneMachine != null)
  504. {
  505. Monitor.Monitor.allInOneMachine.Play(1, BroadcastModule.BroadcastBoard.PlayMode.temporary, node.LicenseNum, 6);
  506. Monitor.Monitor.allInOneMachine.Play(1, BroadcastModule.BroadcastBoard.PlayMode.audio, node.LicenseNum + ",入场");
  507. Thread.Sleep(5000);
  508. Monitor.Monitor.allInOneMachine.Play(1, BroadcastModule.BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  509. }
  510. });
  511. }
  512. }
  513. }
  514. }
  515. }
  516. }
  517. filterCount = 0;
  518. filter.Clear();
  519. }
  520. }
  521. }
  522. ///<summary>停止播放与设备句柄关联的视频</summary>
  523. private void StopPlay(int handleInput)
  524. {
  525. if (handleInput != 0)
  526. {
  527. int ret = VzClientSDK.VzLPRClient_StopRealPlay(handleInput);
  528. }
  529. }
  530. ///<summary>强制获取号牌</summary>
  531. private void ActivateSnap(int handle)
  532. {
  533. if (handle > 0)
  534. {
  535. VzClientSDK.VzLPRClient_ForceTrigger(handle);
  536. }
  537. }
  538. ///<summary>ip+port字符串转ipe</summary>
  539. private void GetIpEndPoint(string ipp, out IPEndPoint ipe)
  540. {
  541. IPAddress myIP = IPAddress.Parse(ipp.Remove(ipp.LastIndexOf(':')) + "");
  542. string myPort = ipp.Substring(ipp.IndexOf(':') + 1);
  543. ipe = new IPEndPoint(myIP, int.Parse(myPort));
  544. }
  545. ///<summary>创建新pic并记录在picNameMap</summary>
  546. private bool CreatePic(int index,int handle, out IntPtr pic)
  547. {
  548. pic = IntPtr.Zero;
  549. PictureBox pb = new PictureBox
  550. {
  551. Width = 179,
  552. Height = 127,
  553. //Bounds = new System.Drawing.Rectangle(0, 0, 230, 180),
  554. Name = "PictureBox" + Convert.ToString(index)
  555. };
  556. lock (namePicMap)
  557. {
  558. if (!namePicMap.ContainsKey(pb.Name))
  559. {
  560. PictureBox temp = pb;
  561. IntPtr picHandle = IntPtr.Zero;
  562. try
  563. {
  564. Control flp = Control.FromHandle(flowLayoutPanel1);
  565. //创建tableLayoutPanel显示号牌机信息
  566. string ip = Get_IP(handle);
  567. int id = 0;
  568. ipIdMap.TryGetValue(ip, out id);
  569. TableLayoutPanel tlp = new TableLayoutPanel()
  570. {
  571. Name = "tlp" + Convert.ToString(id),
  572. ColumnCount = 1,
  573. RowCount = 2,
  574. Size = new Size(185, 168),
  575. TabIndex = 3,
  576. };
  577. Label lb_numMachine = new Label()
  578. {
  579. Text = "编号" + id + "\nIP:" + ip,
  580. TextAlign = ContentAlignment.MiddleCenter,
  581. Size = new Size(179,27)
  582. };
  583. tlp.Controls.Add(temp, 0, 1);
  584. tlp.Controls.Add(lb_numMachine, 0, 0);
  585. tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 20.00f));
  586. tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 80.00f));
  587. //Control.FromHandle(flowLayoutPanel1).Controls.Add(temp);
  588. if (flp.InvokeRequired)
  589. {
  590. flp.Invoke(new Action(() => {
  591. tlp.Width = (int)(flp.Width * 0.3);
  592. tlp.Height = (int)(flp.Height * 0.49);
  593. temp.Width = tlp.Width - 5;
  594. temp.Height = (int)(tlp.Height * 0.79);
  595. lb_numMachine.Width = tlp.Width - 5;
  596. lb_numMachine.Height = (int)(tlp.Height * 0.19);
  597. flp.Controls.Add(tlp);
  598. picHandle = temp.Handle;
  599. }));
  600. }
  601. else
  602. {
  603. flp.Controls.Add(pb);
  604. picHandle = temp.Handle;
  605. }
  606. }
  607. catch (Exception e) { Console.WriteLine(e.Message); }
  608. namePicMap.Add(pb.Name, picHandle);
  609. pic = picHandle;
  610. //Console.WriteLine(picHandle.ToString());
  611. return true;
  612. }
  613. else
  614. return false;
  615. }
  616. }
  617. ///<summary>更新设备状态</summary>
  618. private int GetStatus(string devIP)
  619. {
  620. int myHandle;
  621. byte stat = 0;
  622. if (ipHandleMap.TryGetValue(devIP, out myHandle))
  623. {
  624. VzClientSDK.VzLPRClient_IsConnected(myHandle, ref stat);
  625. return stat;
  626. }
  627. return -1;
  628. }
  629. private string Get_IP(int lprHandle)
  630. {
  631. byte[] strDecIP = new byte[32];
  632. int max_count = 32;
  633. int ret = VzClientSDK.VzLPRClient_GetDeviceIP(lprHandle, ref strDecIP[0], max_count);
  634. string strIP = System.Text.Encoding.Default.GetString(strDecIP);
  635. strIP = strIP.TrimEnd('\0');
  636. return strIP;
  637. }
  638. private bool NodeValidation(NumberMachineNode node)
  639. {
  640. return (node != null && node.ip != null && node.ip != "" && node.ip != "used" && node.LicenseNum != null && node.LicenseNum != "") ? true : false;
  641. }
  642. /// <summary>
  643. /// 系统启动
  644. /// </summary>
  645. public void Start()
  646. {
  647. Task.Factory.StartNew(() =>
  648. {
  649. isClosing = false;
  650. try
  651. {
  652. VzClientSDK.VZLPRClient_StopFindDevice();
  653. find_DeviceCB = new VzClientSDK.VZLPRC_FIND_DEVICE_CALLBACK_EX(FIND_DEVICE_CALLBACK_EX);
  654. int ret = VzClientSDK.VZLPRClient_StartFindDeviceEx(find_DeviceCB, IntPtr.Zero);
  655. }
  656. catch (Exception ex)
  657. {
  658. Debug.WriteLine(ex.ToString());
  659. }
  660. });
  661. Task.Factory.StartNew(() =>
  662. {
  663. Run();
  664. });
  665. //Task imgTest = Task.Factory.StartNew(() =>
  666. //{
  667. // while (!isClosing)
  668. // {
  669. // Command cmd = new Command
  670. // {
  671. // id = 1
  672. // };
  673. // SetMessage(cmd);
  674. // Thread.Sleep(30000);
  675. // Command cmd2 = new Command
  676. // {
  677. // id = 2
  678. // };
  679. // SetMessage(cmd2);
  680. // Thread.Sleep(30000);
  681. // }
  682. //});
  683. }
  684. /// <summary>
  685. /// 系统关闭
  686. /// </summary>
  687. public void Stop()
  688. {
  689. VzClientSDK.VZLPRClient_StopFindDevice();
  690. lock (devPicMap)
  691. {
  692. Dictionary<int, IntPtr>.Enumerator enumer = devPicMap.GetEnumerator();
  693. while (enumer.MoveNext())
  694. {
  695. if (enumer.Current.Key != 0)
  696. {
  697. StopPlay(enumer.Current.Key);
  698. VzClientSDK.VzLPRClient_Close(enumer.Current.Key);
  699. //flowLayoutPanel1.Controls.Remove(enumer.Current.Value);
  700. }
  701. }
  702. isClosing = true;
  703. devPicMap.Clear();
  704. }
  705. lock (namePicMap) { namePicMap.Clear(); }
  706. lock (ipIdMap) { ipIdMap.Clear(); }
  707. lock (ipHandleMap) { ipHandleMap.Clear(); }
  708. //this.Close();
  709. }
  710. /// <summary>
  711. /// 监控线程获取号牌机信息,核心线程获取号牌信息
  712. /// </summary>
  713. public AbstractMessage GetMessage()
  714. {
  715. lock (LicBuffer)
  716. {
  717. //准备输出的数据中存在非法Node,且LicBuffer可出队产生一个合法Node,则替换该非法Node
  718. if (!NodeValidation(nmMsg.aNode))
  719. {
  720. for (int i = 0; i < LicBuffer.Count; i++)
  721. {
  722. NumberMachineNode n = LicBuffer.Dequeue();
  723. if (NodeValidation(n))
  724. {
  725. if (nmMsg.aNode != null && nmMsg.aNode.ip != null)
  726. {
  727. LicBuffer.Enqueue((NumberMachineNode)nmMsg.aNode.Clone());
  728. }
  729. nmMsg.aNode = n;
  730. break;
  731. }
  732. else
  733. {
  734. LicBuffer.Enqueue(n);
  735. }
  736. }
  737. //遍历licBuffer后仍不合法,则丢入队列,将node置为null
  738. if (!NodeValidation(nmMsg.aNode))
  739. {
  740. if (nmMsg.aNode != null && nmMsg.aNode.ip != null)
  741. {
  742. LicBuffer.Enqueue((NumberMachineNode)nmMsg.aNode.Clone());
  743. }
  744. nmMsg.aNode = null;
  745. }
  746. }
  747. }
  748. return nmMsg;
  749. }
  750. /// <summary>
  751. /// 一次停车流程完成时调用该方法,发送已完成车辆号牌信息
  752. /// </summary>
  753. /// <param name="message">已完成车辆的号牌相关信息存于message的aNode中,用于标记需清空的号牌</param>
  754. public void SetMessage(AbstractMessage message)
  755. {
  756. if (message.GetType().Equals(typeof(NumberMachineMessage)))
  757. {
  758. NumberMachineNode n = ((NumberMachineMessage)message).aNode;
  759. lock (LicBuffer)
  760. {
  761. //输入号牌格式无误
  762. if (n != null && n.ip != null && n.ip == "")
  763. {
  764. //与类成员变量中aNode号牌相同,将其ip复位表示已使用,重新入队等待清除
  765. if (nmMsg != null && nmMsg.aNode != null && nmMsg.aNode.LicenseNum == n.LicenseNum)
  766. {
  767. nmMsg.aNode.ip = "";
  768. LicBuffer.Enqueue((NumberMachineNode)nmMsg.aNode.Clone());
  769. nmMsg.aNode = null;
  770. }
  771. //搜索号牌队列,将相应号牌置空,准备清除
  772. else
  773. {
  774. for (int i = 0; i < LicBuffer.Count; i++)
  775. {
  776. NumberMachineNode temp = LicBuffer.Dequeue();
  777. //已匹配上,ip置空
  778. if (temp.LicenseNum == n.LicenseNum)
  779. {
  780. temp.ip = "";
  781. LicBuffer.Enqueue(temp);
  782. break;
  783. }
  784. LicBuffer.Enqueue(temp);
  785. }
  786. }
  787. }
  788. }
  789. }
  790. if (message.GetType().Equals(typeof(Command)))
  791. {
  792. Command cmd = (Command)message;
  793. if (cmd != null && cmd.id != 0)
  794. {
  795. //拍照功能根据ip找到handle,改变snapshotDevHandle的值在回调函数中截图
  796. lock (ipIdMap)
  797. {
  798. Dictionary<string, int>.Enumerator enumerator = ipIdMap.GetEnumerator();
  799. while (enumerator.MoveNext())
  800. {
  801. // if (enumerator.Current.Value == cmd.id && ipHandleMap.TryGetValue(enumerator.Current.Key, out int handle))
  802. int handle = 0;
  803. if (enumerator.Current.Value == cmd.id && ipHandleMap.TryGetValue(enumerator.Current.Key, out handle))
  804. {
  805. snapshotDevHandle = handle;
  806. break;
  807. }
  808. }
  809. }
  810. }
  811. else
  812. {
  813. Console.WriteLine("参数错误,图片未保存");
  814. }
  815. }
  816. }
  817. public string GetLicensePlate(int id)
  818. {
  819. string license = "";
  820. //将相应计数器归零,启动号牌记录
  821. lock (idCountMap)
  822. {
  823. if (idCountMap.ContainsKey(id))
  824. {
  825. idCountMap[id] = 0;
  826. }
  827. }
  828. Task.Factory.StartNew(() =>
  829. {
  830. NumberMachineNode node = null;
  831. int timeLimit = 8;
  832. while (node == null && timeLimit-- > 0)
  833. {
  834. lock (LicBuffer)
  835. {
  836. for (int i = 0; i < LicBuffer.Count; i++)
  837. {
  838. node = LicBuffer.Dequeue();
  839. if (node != null)
  840. {
  841. if (node.id == id)
  842. {
  843. license = node.LicenseNum;
  844. break;
  845. }
  846. else
  847. {
  848. LicBuffer.Enqueue(node);
  849. }
  850. }
  851. }
  852. }
  853. Thread.Sleep(1000);
  854. if (timeLimit == 0) { Console.WriteLine("本次未找到号牌"); }
  855. }
  856. }).Wait();
  857. return license;
  858. }
  859. public Image GetImage(int id)
  860. {
  861. return null;
  862. }
  863. }
  864. }