NumMachine.cs 40 KB

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