| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- #pragma warning disable CS0436 // 类型与导入类型冲突
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- using CardApi;
- using log4net;
- namespace CardApi
- {
- public class KateDoor : CardApi
- {
- public static readonly ILog log = LogManager.GetLogger("KateDoor");
- [DllImport("MRFCOM.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
- public static extern int GetDLLVersion([MarshalAs(UnmanagedType.LPStr)] StringBuilder szVersion);
- [DllImport("MRFCOM.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
- public static extern int Buzzer(byte bt);
- [DllImport("MRFCOM.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
- public static extern int YGetGuestCardinfo(IntPtr dlscoid, byte[] carddata, byte[] lockinfo);
- [DllImport("MRFCOM.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
- public static extern int YGetcoid(byte[] coid);
- [DllImport("katedoor.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
- public static extern int writedoorcard(string dbPath, byte lockFlag, int buildingNo, string lockId, string etime, byte[] cardHex);
- public override string version()
- {
- StringBuilder s = new StringBuilder();
- int code = GetDLLVersion(s);
- if (code != 0)
- {
- throw new Exception("获取失败");
- }
- return s.ToString();
- }
- public override void beep(byte b)
- {
- int code = Buzzer(20);
- if (code != 0)
- {
- throw new Exception("读卡器未连接");
- }
- }
- public override object info()
- {
- return new
- {
- version = version()
- };
- }
- public override object readCard(IMySettings settings, Dictionary<string, string> data)
- {
- if (settings == null || settings.Flag == null)
- {
- throw new Exception("卡标识错误");
- }
- int flag = int.Parse(settings.Flag);
- Buzzer(20);
- byte[] bcarddata = new byte[128];
- byte[] blockinfo = new byte[128];
- int code = YGetGuestCardinfo(new IntPtr(flag), bcarddata, blockinfo);
- Buzzer(20);
- string carddata = btoa2string(bcarddata);
- string lockinfo = btoa2string(blockinfo);
- if (code != 6)
- {
- return new { code, message = getErrMsg(code), carddata };
- }
- string cardNo = lockinfo.Substring(34, 10);
- string lockNo = lockinfo.Substring(0, 6);
- string issueTime = $"{lockinfo.Substring(8, 4)}-{lockinfo.Substring(12, 2)}-{lockinfo.Substring(14, 2)} {lockinfo.Substring(16, 2)}:{lockinfo.Substring(18, 2)}";
- string expireTime = $"{lockinfo.Substring(20, 4)}-{lockinfo.Substring(24, 2)}-{lockinfo.Substring(26, 2)} {lockinfo.Substring(28, 2)}:{lockinfo.Substring(30, 2)}";
- string lockFlag = lockinfo.Substring(32, 1);
- string uid = lockinfo.Substring(44, 12);
- return new
- {
- code,
- carddata,
- cardNo,
- lockNo,
- issueTime,
- expireTime,
- lockFlag,
- uid,
- };
- }
- public override void writeCard(IMySettings settings, Dictionary<string, string> data)
- {
- if (data == null)
- {
- throw new Exception("缺少参数");
- }
- if (settings == null || settings.Flag == null)
- {
- throw new Exception("卡标识错误");
- }
- if (string.IsNullOrWhiteSpace(settings.DBPath))
- {
- throw new Exception("数据库配置错误");
- }
- string room = data["room"];
- if (string.IsNullOrWhiteSpace(room))
- {
- throw new Exception("缺少参数room");
- }
- string date = data["date"];
- if (string.IsNullOrWhiteSpace(date))
- {
- throw new Exception("缺少参数date");
- }
- string buildingNo = room.Split(',', ',')[0];
- string roomNo = room.Split(',', ',')[1];
- DateTime expire = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", null);
- log.Info($"write card: {buildingNo} {roomNo} {expire}");
- int code = Buzzer(20);
- byte[] bcarddata = new byte[128];
- code = writedoorcard(settings.DBPath, 1, int.Parse(buildingNo), roomNo, expire.ToString("yyMMddHHmm"), bcarddata);
- log.Info($"write card return code: {code}");
- log.Info($"write card return msg: {getErrMsg(code)}");
- log.Info($"write card return data: {btoa2string(bcarddata)}");
- if (code != 0)
- {
- throw new Exception(getErrMsg(code));
- }
- }
- public override string readFlag()
- {
- byte[] data = new byte[128];
- int code = YGetcoid(data);
- if (code != 0)
- {
- throw new Exception(getErrMsg(code));
- }
- Buzzer(20);
- string s = btoa2string(data);
- int flag = (int)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber);
- return flag.ToString();
- }
- private string btoa2string(byte[] btoa)
- {
- for (int i = 0; i < btoa.Length; i++)
- {
- if (btoa[i] == 0)
- {
- return Encoding.Default.GetString(btoa, 0, i);
- }
- }
- return "";
- }
- public string getErrMsg(int code)
- {
- switch (code)
- {
- case 0:
- return "成功"; ;
- case -1:
- return "读卡器未连接";
- case -2:
- return "没有读到有效卡片";
- case -3:
- return "此卡非本酒店卡";
- case -4:
- return "空白卡或已注销";
- case 100:
- return "数据库文件不存在,请重新选择现场运行的门锁软件数据库";
- case 101:
- return "楼栋号输入有错误";
- case 103:
- return "到期时间格式有错误,必须传送";
- case 107:
- return "门锁软件中的注册码过期";
- case 109:
- return "门锁软件没有注册或者注册码错误";
- case 110:
- return "选择的数据库文件错误";
- case 111:
- return "输入的楼栋号或者门牌号不存在";
- case 131:
- return "读卡失败或者发卡器连接失败";
- case 132:
- return "卡片没有在门锁软件里进行认证过";
- case 133:
- return "卡片数据错误,有可能是复制的卡,或者卡片数据已经篡改导致";
- case 90:
- return "写卡失败或者注销卡片失败,有可能发卡器旁边有干扰";
- default:
- return "未知错误";
- }
- }
- }
- }
|