Auth.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using EntityDataSet;
  12. using IntelligentControlForsx.Service.FingerSvc;
  13. using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
  14. using IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc;
  15. using libzkfpcsharp;
  16. namespace Gyee_Control.View
  17. {
  18. public partial class Auth : Form
  19. {
  20. #region 指纹读取常用量
  21. bool bIsTimeToDie = false;
  22. IntPtr mDevHandle = IntPtr.Zero;
  23. IntPtr mDBHandle = IntPtr.Zero;
  24. IntPtr FormHandle = IntPtr.Zero;
  25. const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
  26. byte[] FPBuffer;//返回图像
  27. private byte[] fingerTemp = new byte[2048];//指纹模板
  28. int fingerTempLength = 2048;//实际返回指纹模板长度
  29. private int mfpWidth = 0;//图片宽度
  30. private int mfpHeight = 0;//图片高度
  31. #endregion
  32. IList<string> faultSendList = new List<string>();//发送失败的风机集合
  33. private UserSvc userSvc;
  34. private string describeString;
  35. private IList<WindturbineCmdInfo> cmdInfoList;
  36. private Confirm confirmForm;
  37. public Auth(Confirm confirm, IList<WindturbineCmdInfo> infoList, string describe)
  38. {
  39. InitializeComponent();
  40. confirmForm = confirm;
  41. describeString = describe;
  42. cmdInfoList = infoList;
  43. string windturbineName = String.Join(",", infoList.Select(s => s.WindturbineId).ToArray());
  44. string cmdString = "";
  45. switch (infoList[0].CmdType)
  46. {
  47. case CmdType.Start:
  48. cmdString = "启动操作";
  49. break;
  50. case CmdType.Stop:
  51. cmdString = "停止操作";
  52. break;
  53. case CmdType.Reset:
  54. cmdString = "复位操作";
  55. break;
  56. case CmdType.Maintain:
  57. cmdString = "维护操作";
  58. break;
  59. case CmdType.UnMaintain:
  60. cmdString = "取消维护操作";
  61. break;
  62. }
  63. this.lblWaringInfo.Text = cmdString;
  64. this.passed = false;
  65. userSvc = UserSvc.GetUserSvc();
  66. userSvc.InitUser();
  67. FingerInit();
  68. StartsScanning();
  69. }
  70. /// <summary>
  71. /// 初始化指纹设备
  72. /// </summary>
  73. private void FingerInit()
  74. {
  75. int ret = zkfperrdef.ZKFP_ERR_OK;
  76. if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
  77. {
  78. int nCount = zkfp2.GetDeviceCount();
  79. if (nCount <= 0)
  80. {
  81. zkfp2.Terminate();
  82. MessageBox.Show("找不到设备,请检查指纹刷是否连接");
  83. }
  84. }
  85. else
  86. {
  87. MessageBox.Show("指纹输入设备初始化失败,请检查指纹刷是否连接");
  88. }
  89. }
  90. /// <summary>
  91. /// 打开扫描
  92. /// </summary>
  93. private void StartsScanning()
  94. {
  95. Console.WriteLine("设备初始化");
  96. int ret = zkfp.ZKFP_ERR_OK;
  97. if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
  98. {
  99. MessageBox.Show("打开扫描失败");
  100. return;
  101. }
  102. if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
  103. {
  104. MessageBox.Show("打开扫描失败");
  105. zkfp2.CloseDevice(mDevHandle);
  106. mDevHandle = IntPtr.Zero;
  107. return;
  108. }
  109. byte[] paramValue = new byte[4];
  110. int size = 4;
  111. zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
  112. zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
  113. size = 4;
  114. zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
  115. zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
  116. FPBuffer = new byte[mfpWidth * mfpHeight];
  117. Thread captureThread = new Thread(new ThreadStart(DoCapture));
  118. captureThread.IsBackground = true;
  119. captureThread.Start();
  120. bIsTimeToDie = false;
  121. }
  122. /// <summary>
  123. /// 指纹扫描线程
  124. /// </summary>
  125. private void DoCapture()
  126. {
  127. while (!bIsTimeToDie)
  128. {
  129. int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength);
  130. if (ret == zkfp.ZKFP_ERR_OK)
  131. {
  132. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  133. }
  134. Thread.Sleep(200);
  135. }
  136. }
  137. protected override void DefWndProc(ref Message m)
  138. {
  139. switch (m.Msg)
  140. {
  141. case MESSAGE_CAPTURED_OK:
  142. {
  143. MemoryStream ms = new MemoryStream();
  144. BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
  145. BitmapFormat.WriteBitmap(FPBuffer, mfpWidth, mfpHeight);
  146. Bitmap bmp = new Bitmap(ms);
  147. //this.picFingerPhoto.Image = bmp;
  148. user user = UserSvc.GetUserSvc().LoginUserCheck(mDBHandle, fingerTemp);
  149. if (user == null)
  150. {
  151. lblInfo.Text = "验证失败,请重试!";
  152. }
  153. else
  154. {
  155. //发送控制命令
  156. try
  157. {
  158. ControlSvc controlSvc = ControlSvc.GetControlSvc();
  159. IList<string> faultSendList = new List<string>();
  160. bool isSend = controlSvc.SendCmdByWindturbineIdList(cmdInfoList.Select(s => s.WindturbineId).ToList(), "SBQ_FDC", cmdInfoList[0].CmdType, out faultSendList, describeString, user);
  161. if (isSend)
  162. {
  163. MessageBox.Show("命令发送成功");
  164. this.Close();
  165. this.passed = false;
  166. zkfp2.Terminate();
  167. zkfp2.CloseDevice(mDevHandle);
  168. zkfp2.CloseDevice(mDBHandle);
  169. }
  170. else
  171. {
  172. string windturbineIds = "";
  173. for (int i = 0; i < faultSendList.Count; i++)
  174. {
  175. windturbineIds = windturbineIds + faultSendList[i] + ",";
  176. }
  177. MessageBox.Show("命令发送失败的风机有:" + windturbineIds);
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. Console.WriteLine(ex.Message);
  183. }
  184. }
  185. }
  186. break;
  187. default:
  188. base.DefWndProc(ref m);
  189. break;
  190. }
  191. }
  192. private void lblClose_Click(object sender, EventArgs e)
  193. {
  194. this.passed = false;
  195. zkfp2.Terminate();
  196. zkfp2.CloseDevice(mDevHandle);
  197. zkfp2.CloseDevice(mDBHandle);
  198. this.Close();
  199. }
  200. private void Auth_Load(object sender, EventArgs e)
  201. {
  202. FormHandle = this.Handle;
  203. }
  204. }
  205. }