MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using DeviceCenter.model;
  2. using DeviceCenter.utils;
  3. using DeviceCenter.views;
  4. using log4net;
  5. using Notifications.Wpf;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Timers;
  17. using System.Windows;
  18. using System.Windows.Interop;
  19. namespace DeviceCenter
  20. {
  21. /// <summary>
  22. /// MainWindow.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. public static readonly ILog log = LogManager.GetLogger("DeviceCenter");
  27. private NotificationManager notificationManager = new NotificationManager();
  28. private ObservableCollection<Device> devices = new ObservableCollection<Device>();
  29. private Config config = Config.getInstance();
  30. private CHCNetSDK.MSGCallBack alarmCallback = null;
  31. private static CarCamSDK.FGetImageCB2 carPlateCallback;
  32. private int m_lGetCardCfgHandle = -1;
  33. private CHCNetSDK.RemoteConfigCallback g_fGetGatewayCardCallback = null;
  34. private System.Windows.Forms.NotifyIcon notifyIcon = null;
  35. private System.Windows.Forms.ContextMenu contextMenuExit;
  36. private System.Windows.Forms.MenuItem menuItem1;
  37. private System.ComponentModel.IContainer components;
  38. public MainWindow()
  39. {
  40. InitializeComponent();
  41. notifyIcon = new System.Windows.Forms.NotifyIcon();
  42. notifyIcon.Click += new EventHandler(notifyIcon_Click);
  43. notifyIcon.Icon = new Icon("assets\\icon.ico");
  44. this.components = new System.ComponentModel.Container();
  45. this.contextMenuExit = new System.Windows.Forms.ContextMenu();
  46. this.menuItem1 = new System.Windows.Forms.MenuItem();
  47. // Initialize contextMenuExit
  48. this.contextMenuExit.MenuItems.AddRange(
  49. new System.Windows.Forms.MenuItem[] { this.menuItem1 });
  50. // Initialize menuItem1
  51. this.menuItem1.Index = 0;
  52. this.menuItem1.Text = "退出";
  53. this.menuItem1.Click += new EventHandler(this.menuItem1_Click);
  54. notifyIcon.ContextMenu = this.contextMenuExit;
  55. initSdk();
  56. lv_device.ItemsSource = devices;
  57. System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
  58. dispatcherTimer.Tick += GetAllCard;
  59. dispatcherTimer.Interval = new TimeSpan(0, 30, 0);
  60. dispatcherTimer.Start();
  61. System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
  62. timer.Tick += (s, e) =>
  63. {
  64. ((System.Windows.Threading.DispatcherTimer)s).Stop();
  65. GetAllCard(null, null);
  66. };
  67. timer.Interval = new TimeSpan(0, 0, 5);
  68. timer.Start();
  69. }
  70. private void Window_Loaded(object sender, RoutedEventArgs e)
  71. {
  72. notifyIcon.Visible = true;
  73. }
  74. private void notifyIcon_Click(object sender, EventArgs e)
  75. {
  76. Show();
  77. }
  78. private void menuItem1_Click(object Sender, EventArgs e)
  79. {
  80. ExitConfirm ex = new ExitConfirm();
  81. if (ex.ShowDialog() ?? false)
  82. {
  83. if (ex.password.Password == config.password)
  84. {
  85. notifyIcon.Dispose();
  86. Application.Current.Shutdown();
  87. }
  88. else
  89. {
  90. notificationManager.Show(new NotificationContent
  91. {
  92. Title = "Error",
  93. Message = "密码错误",
  94. Type = NotificationType.Error
  95. });
  96. }
  97. }
  98. }
  99. private async void initSdk()
  100. {
  101. await Task.Run(() =>
  102. {
  103. long ts = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
  104. carPlateCallback = new CarCamSDK.FGetImageCB2(onGetCarPlate);
  105. CarCamSDK.Net_RegImageRecv2(carPlateCallback);
  106. //UsbSwitch.usb_relay_init();
  107. alarmCallback = new CHCNetSDK.MSGCallBack(onAlarm);
  108. if (CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V50(0, alarmCallback, IntPtr.Zero))
  109. {
  110. log.Info("NET_DVR_SetDVRMessageCallBack_V50 Succeed");
  111. notificationManager.Show(new NotificationContent
  112. {
  113. Title = "Success",
  114. Message = "NET_DVR_SetDVRMessageCallBack_V50 Succeed",
  115. Type = NotificationType.Success
  116. });
  117. }
  118. else
  119. {
  120. notificationManager.Show(new NotificationContent
  121. {
  122. Title = "Error",
  123. Message = "NET_DVR_SetDVRMessageCallBack_V50 Fail",
  124. Type = NotificationType.Error
  125. });
  126. }
  127. notificationManager.Show(new NotificationContent
  128. {
  129. Title = "Info",
  130. Message = "init finish, toke " + (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds() - ts) + "ms",
  131. Type = NotificationType.Information
  132. });
  133. Application.Current.Dispatcher.Invoke((Action)(() =>
  134. {
  135. config.devices.ForEach(i =>
  136. {
  137. i.Init();
  138. devices.Add(i);
  139. });
  140. }));
  141. });
  142. }
  143. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  144. {
  145. e.Cancel = true;
  146. Hide();
  147. }
  148. private void btn_add_device_Click(object sender, RoutedEventArgs e)
  149. {
  150. AddDevice addDevice = new AddDevice();
  151. if (addDevice.ShowDialog() ?? false)
  152. {
  153. config.devices.Add(addDevice.device);
  154. config.save();
  155. devices.Add(addDevice.device);
  156. addDevice.device.Init();
  157. }
  158. }
  159. private void btn_del_device_Click(object sender, RoutedEventArgs e)
  160. {
  161. //NotificationUtil.show("连接设备失败", "请检查网络");
  162. if (lv_device.SelectedIndex == -1)
  163. {
  164. return;
  165. }
  166. Console.WriteLine(lv_device.SelectedItem);
  167. MessageBoxResult messageBoxResult = MessageBox.Show("确认删除?", "提示", MessageBoxButton.YesNo);
  168. if (messageBoxResult == MessageBoxResult.Yes)
  169. {
  170. int i = lv_device.SelectedIndex;
  171. if (devices[i].status != DeviceStatus.IDLE)
  172. {
  173. devices[i].dispose();
  174. }
  175. devices.RemoveAt(i);
  176. config.devices.RemoveAt(i);
  177. config.save();
  178. // lv_device.Items.Refresh();
  179. }
  180. }
  181. private void onAlarm(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
  182. {
  183. switch (lCommand)
  184. {
  185. case CHCNetSDK.COMM_ALARM_ACS:
  186. ProcessCommAlarmACS(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. private void ProcessCommAlarmACS(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
  193. {
  194. foreach (Device device in devices)
  195. {
  196. if (device.type == DeviceType.ACS)
  197. {
  198. AcsDevice acsDevice = (AcsDevice)device;
  199. if (acsDevice.userId == pAlarmer.lUserID)
  200. {
  201. acsDevice.onAlarm(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
  202. }
  203. }
  204. }
  205. }
  206. private int onGetCarPlate(int tHandle, uint uiImageId, ref CarCamSDK.T_ImageUserInfo2 tImageInfo, ref CarCamSDK.T_PicInfo tPicInfo)
  207. {
  208. foreach (Device device in devices)
  209. {
  210. if (device.type == DeviceType.ACS)
  211. {
  212. CarCamDevice carCamDevice = (CarCamDevice)device;
  213. if (tHandle == carCamDevice.userId)
  214. {
  215. return carCamDevice.onGetCarPlate(tHandle, uiImageId, ref tImageInfo, ref tPicInfo);
  216. }
  217. }
  218. }
  219. return 0;
  220. }
  221. private void GetAllCard(object source, EventArgs e)
  222. {
  223. int m_userId = -1;
  224. AcsDevice device = null;
  225. try
  226. {
  227. device = (AcsDevice)devices.First(i => i.type == DeviceType.ACS && i.status == DeviceStatus.CONNECTED);
  228. }
  229. catch { }
  230. if (device == null)
  231. {
  232. log.Info("no connected device");
  233. return;
  234. }
  235. m_userId = device.userId;
  236. if (-1 != m_lGetCardCfgHandle)
  237. {
  238. if (CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetCardCfgHandle))
  239. {
  240. m_lGetCardCfgHandle = -1;
  241. }
  242. }
  243. CHCNetSDK.NET_DVR_CARD_CFG_COND struCond = new CHCNetSDK.NET_DVR_CARD_CFG_COND();
  244. struCond.dwSize = (uint)Marshal.SizeOf(struCond);
  245. struCond.wLocalControllerID = 0;
  246. struCond.dwCardNum = 0xffffffff;
  247. struCond.byCheckCardNo = 1;
  248. int dwSize = Marshal.SizeOf(struCond);
  249. IntPtr ptrStruCond = Marshal.AllocHGlobal(dwSize);
  250. Marshal.StructureToPtr(struCond, ptrStruCond, false);
  251. g_fGetGatewayCardCallback = new CHCNetSDK.RemoteConfigCallback(ProcessGetGatewayCardCallback);
  252. m_lGetCardCfgHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(m_userId, CHCNetSDK.NET_DVR_GET_CARD_CFG_V50, ptrStruCond, dwSize, g_fGetGatewayCardCallback, new WindowInteropHelper(this).Handle);
  253. if (m_lGetCardCfgHandle == -1)
  254. {
  255. log.Info(string.Format("NET_DVR_GET_CARD_CFG_V50 FAIL, ERROR CODE {0}", CHCNetSDK.NET_DVR_GetLastError()));
  256. Marshal.FreeHGlobal(ptrStruCond);
  257. return;
  258. }
  259. else
  260. {
  261. log.Info("SUCC NET_DVR_GET_CARD_CFG_V50");
  262. }
  263. Marshal.FreeHGlobal(ptrStruCond);
  264. }
  265. private void ProcessGetGatewayCardCallback(uint dwType, IntPtr lpBuffer, uint dwBufLen, IntPtr pUserData)
  266. {
  267. if (pUserData == null)
  268. {
  269. return;
  270. }
  271. if (dwType == (uint)CHCNetSDK.NET_SDK_CALLBACK_TYPE.NET_SDK_CALLBACK_TYPE_DATA)
  272. {
  273. CHCNetSDK.NET_DVR_CARD_CFG_V50 struCardCfg = new CHCNetSDK.NET_DVR_CARD_CFG_V50();
  274. struCardCfg = (CHCNetSDK.NET_DVR_CARD_CFG_V50)Marshal.PtrToStructure(lpBuffer, typeof(CHCNetSDK.NET_DVR_CARD_CFG_V50));
  275. string strCardNo = System.Text.Encoding.UTF8.GetString(struCardCfg.byCardNo);
  276. IntPtr pCardInfo = Marshal.AllocHGlobal(Marshal.SizeOf(struCardCfg));
  277. Marshal.StructureToPtr(struCardCfg, pCardInfo, true);
  278. CHCNetSDK.PostMessage(pUserData, 1003, (Int64)pCardInfo, 0);
  279. string cardNo = Encoding.UTF8.GetString(struCardCfg.byCardNo);
  280. string isValid = 1 == struCardCfg.byCardValid ? "YES" : "NO";
  281. string cardPasswod = Encoding.UTF8.GetString(struCardCfg.byCardPassword);
  282. string cardType = (struCardCfg.byCardType == 0 || struCardCfg.byCardType > 7) ?
  283. AcsDemoPublic.strCardType[0] : AcsDemoPublic.strCardType[struCardCfg.byCardType];
  284. string leaderCard = 1 == struCardCfg.byLeaderCard ? "YES" : "NO";
  285. string name = Encoding.UTF8.GetString(struCardCfg.byName);
  286. log.Info(string.Format("cardNo:{0} name:{1}", cardNo, name));
  287. //var request = new RestRequest("staffInfo/saveStaffInfo", DataFormat.Json);
  288. //request.AddParameter("name", name);
  289. //request.AddParameter("cardNo", cardNo);
  290. //restClient.Post(request);
  291. }
  292. else if (dwType == (uint)CHCNetSDK.NET_SDK_CALLBACK_TYPE.NET_SDK_CALLBACK_TYPE_STATUS)
  293. {
  294. uint dwStatus = (uint)Marshal.ReadInt32(lpBuffer);
  295. if (dwStatus == (uint)CHCNetSDK.NET_SDK_CALLBACK_STATUS_NORMAL.NET_SDK_CALLBACK_STATUS_SUCCESS)
  296. {
  297. log.Info("NET_DVR_GET_CARD_CFG_V50 Get finish");
  298. CHCNetSDK.PostMessage(pUserData, 1002, 0, 0);
  299. }
  300. else if (dwStatus == (uint)CHCNetSDK.NET_SDK_CALLBACK_STATUS_NORMAL.NET_SDK_CALLBACK_STATUS_FAILED)
  301. {
  302. byte[] bRawData = new byte[40];//4字节状态 + 4字节错误码 + 32字节卡号
  303. Marshal.Copy(lpBuffer, bRawData, 0, 40);//将非托管内存指针数据复制到数组中
  304. byte[] errorb = new byte[4];//4字节错误码
  305. Array.Copy(bRawData, 4, errorb, 0, 4);
  306. int errorCode = BitConverter.ToInt32(errorb, 0);
  307. byte[] byCardNo = new byte[32];//32字节卡号
  308. Array.Copy(bRawData, 8, byCardNo, 0, 32);
  309. string strCardNo = Encoding.ASCII.GetString(byCardNo).TrimEnd('\0');
  310. log.Info(string.Format("NET_DVR_GET_CARD_CFG_V50 Get Failed,ErrorCode:{0},CardNo:{1}", errorCode, byCardNo));
  311. CHCNetSDK.PostMessage(pUserData, 1002, 0, 0);
  312. }
  313. }
  314. return;
  315. }
  316. }
  317. }