NumMachine.cs 33 KB

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