ProUsb.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma warning disable CS0436 // 类型与导入类型冲突
  2. using log4net;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace CardApi
  8. {
  9. public class ProUsb : CardApi
  10. {
  11. public static readonly ILog log = LogManager.GetLogger("ProUsb");
  12. [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  13. public static extern int GetDLLVersion([MarshalAs(UnmanagedType.LPStr)] StringBuilder szVersion);
  14. [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  15. public static extern int initializeUSB(byte flag);
  16. [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  17. public static extern int CloseUSB(byte flag);
  18. [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  19. public static extern int Buzzer(byte flagUsb, byte bt);
  20. [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  21. public static extern int ReadCard(byte flagUsb, byte[] carddata);
  22. [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  23. public static extern int GetGuestLockNoByCardDataStr(int coId, byte[] carddata, byte[] lockNo);
  24. [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  25. public static extern int GetGuestETimeByCardDataStr(int coId, byte[] carddata, byte[] eTime);
  26. [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  27. public static extern int GuestCard(byte flagUsb, int coId, byte CardNo, byte dai, byte llock, byte pdoors, string BDate, string EDate, string RoomNo, byte[] CardHexStr);
  28. public override void beep(byte b)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. public override object info()
  33. {
  34. return new
  35. {
  36. version = version()
  37. };
  38. }
  39. public override object readCard(IMySettings settings, Dictionary<string, string> data)
  40. {
  41. byte[] bcarddata = new byte[128];
  42. initializeUSB(1);
  43. ReadCard(1, bcarddata);
  44. CloseUSB(1);
  45. return btoa2string(bcarddata);
  46. }
  47. public override string version()
  48. {
  49. StringBuilder s = new StringBuilder();
  50. int code = GetDLLVersion(s);
  51. if (code != 0)
  52. {
  53. throw new Exception("获取失败");
  54. }
  55. return s.ToString();
  56. }
  57. public override void writeCard(IMySettings settings, Dictionary<string, string> data)
  58. {
  59. if (data == null)
  60. {
  61. throw new Exception("缺少参数");
  62. }
  63. if (settings == null || settings.Flag == null)
  64. {
  65. throw new Exception("卡标识错误");
  66. }
  67. string room = data["room"];
  68. if (string.IsNullOrWhiteSpace(room))
  69. {
  70. throw new Exception("缺少参数room");
  71. }
  72. string date = data["date"];
  73. if (string.IsNullOrWhiteSpace(date))
  74. {
  75. throw new Exception("缺少参数date");
  76. }
  77. DateTime expire = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", null);
  78. initializeUSB(1);
  79. byte[] bcarddata = new byte[128];
  80. int code = GuestCard(1, int.Parse(settings.Flag), 0, 0, 0, 1, DateTime.Now.ToString("yyMMddHHmm"), expire.ToString("yyMMddHHmm"), room, bcarddata);
  81. Buzzer(1, 20);
  82. CloseUSB(1);
  83. log.Info($"write card return code: {code}");
  84. log.Info($"write card return msg: {getErrMsg(code)}");
  85. log.Info($"write card return data: {btoa2string(bcarddata)}");
  86. if (code != 0)
  87. {
  88. throw new Exception(getErrMsg(code));
  89. }
  90. }
  91. public override string readFlag()
  92. {
  93. initializeUSB(1);
  94. byte[] bcarddata = new byte[128];
  95. ReadCard(1, bcarddata);
  96. Buzzer(1, 20);
  97. CloseUSB(1);
  98. string cardData = btoa2string(bcarddata);
  99. string s = cardData.Substring(10, 4);
  100. int i = (int)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber) % 16384;
  101. s = cardData.Substring(8, 2);
  102. i = ((int)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber) * 65536) + i;
  103. return i.ToString();
  104. }
  105. public static string btoa2string(byte[] btoa)
  106. {
  107. for (int i = 0; i < btoa.Length; i++)
  108. {
  109. if (btoa[i] == 0)
  110. {
  111. return Encoding.Default.GetString(btoa, 0, i);
  112. }
  113. }
  114. return "";
  115. }
  116. public string getErrMsg(int code)
  117. {
  118. switch (code)
  119. {
  120. case 0:
  121. return "成功"; ;
  122. case -1:
  123. return "读卡器未连接";
  124. case -2:
  125. return "没有读到有效卡片";
  126. case -3:
  127. return "此卡非本酒店卡";
  128. case -4:
  129. return "空白卡或已注销";
  130. case 100:
  131. return "数据库文件不存在,请重新选择现场运行的门锁软件数据库";
  132. case 101:
  133. return "楼栋号输入有错误";
  134. case 103:
  135. return "到期时间格式有错误,必须传送";
  136. case 107:
  137. return "门锁软件中的注册码过期";
  138. case 109:
  139. return "门锁软件没有注册或者注册码错误";
  140. case 110:
  141. return "选择的数据库文件错误";
  142. case 111:
  143. return "输入的楼栋号或者门牌号不存在";
  144. case 131:
  145. return "读卡失败或者发卡器连接失败";
  146. case 132:
  147. return "卡片没有在门锁软件里进行认证过";
  148. case 133:
  149. return "卡片数据错误,有可能是复制的卡,或者卡片数据已经篡改导致";
  150. case 90:
  151. return "写卡失败或者注销卡片失败,有可能发卡器旁边有干扰";
  152. default:
  153. return "未知错误";
  154. }
  155. }
  156. }
  157. }