#pragma warning disable CS0436 // 类型与导入类型冲突 using log4net; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace CardApi { public class ProUsb : CardApi { public static readonly ILog log = LogManager.GetLogger("ProUsb"); [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int GetDLLVersion([MarshalAs(UnmanagedType.LPStr)] StringBuilder szVersion); [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int initializeUSB(byte flag); [DllImport("proRFL.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int CloseUSB(byte flag); [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int Buzzer(byte flagUsb, byte bt); [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int ReadCard(byte flagUsb, byte[] carddata); [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int GetGuestLockNoByCardDataStr(int coId, byte[] carddata, byte[] lockNo); [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int GetGuestETimeByCardDataStr(int coId, byte[] carddata, byte[] eTime); [DllImport("proRFL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 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); public override void beep(byte b) { throw new NotImplementedException(); } public override object info() { return new { version = version() }; } public override object readCard(IMySettings settings, Dictionary data) { byte[] bcarddata = new byte[128]; initializeUSB(1); ReadCard(1, bcarddata); CloseUSB(1); return btoa2string(bcarddata); } public override string version() { StringBuilder s = new StringBuilder(); int code = GetDLLVersion(s); if (code != 0) { throw new Exception("获取失败"); } return s.ToString(); } public override void writeCard(IMySettings settings, Dictionary data) { if (data == null) { throw new Exception("缺少参数"); } if (settings == null || settings.Flag == null) { 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"); } DateTime expire = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", null); initializeUSB(1); byte[] bcarddata = new byte[128]; int code = GuestCard(1, int.Parse(settings.Flag), 0, 0, 0, 1, DateTime.Now.ToString("yyMMddHHmm"), expire.ToString("yyMMddHHmm"), room, bcarddata); Buzzer(1, 20); CloseUSB(1); 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() { initializeUSB(1); byte[] bcarddata = new byte[128]; ReadCard(1, bcarddata); Buzzer(1, 20); CloseUSB(1); string cardData = btoa2string(bcarddata); string s = cardData.Substring(10, 4); int i = (int)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber) % 16384; s = cardData.Substring(8, 2); i = ((int)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber) * 65536) + i; return i.ToString(); } public static 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 "未知错误"; } } } }