KateDoor.cs 7.5 KB

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