private void InputDetected(UInt32 deviceID, IntPtr log) { if (log != IntPtr.Zero) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(log, typeof(BS2Event)); Console.WriteLine("Notification : " + Util.GetLogMsg(eventLog)); } }
private void RealtimeLogReceived(UInt32 deviceID, IntPtr log) { if (log != IntPtr.Zero) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(log, typeof(BS2Event)); Console.WriteLine(Util.GetLogMsg(eventLog)); } }
private void RealtimeLogReceivedEx(UInt32 deviceID, IntPtr log, UInt32 temperature) { if (log != IntPtr.Zero) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(log, typeof(BS2Event)); float temper = (float)temperature / 100.0F; Console.WriteLine("{0} temperature[{1}]", Util.GetLogMsg(eventLog), temper); } }
void getLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { const UInt32 defaultLogPageSize = 1024; Type structureType = typeof(BS2Event); int structSize = Marshal.SizeOf(structureType); bool getAllLog = false; UInt32 lastEventId = 0; UInt32 amount; IntPtr outEventLogObjs = IntPtr.Zero; UInt32 outNumEventLogs = 0; cbOnLogReceived = new API.OnLogReceived(NormalLogReceived); Console.WriteLine("What is the ID of the last log which you have? [0: None]"); Console.Write(">>>> "); lastEventId = Util.GetInput((UInt32)0); Console.WriteLine("How many logs do you want to get? [0: All]"); Console.Write(">>>> "); amount = Util.GetInput((UInt32)0); if (amount == 0) { getAllLog = true; amount = defaultLogPageSize; } do { outEventLogObjs = IntPtr.Zero; BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, lastEventId, amount, out outEventLogObjs, out outNumEventLogs); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); break; } if (outNumEventLogs > 0) { IntPtr curEventLogObjs = outEventLogObjs; for (UInt32 idx = 0; idx < outNumEventLogs; idx++) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, structureType); Console.WriteLine(Util.GetLogMsg(eventLog)); curEventLogObjs += structSize; lastEventId = eventLog.id; } API.BS2_ReleaseObject(outEventLogObjs); } if (outNumEventLogs < defaultLogPageSize) { break; } }while (getAllLog); }
private static string GetGeneralMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] image[{4}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, Convert.ToBoolean(eventLog.image & (byte)BS2EventImageBitPos.BS2_IMAGEFIELD_POS_IMAGE))); }
private static string GetGeneralMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] image[{4}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, eventLog.image)); }
private static string GetZoneIdMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); BS2EventDetail eventDetail = ConvertTo <BS2EventDetail>(eventLog.userID); return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] zoneID[{4}] image[{5}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, eventDetail.zoneID, eventLog.image)); }
private static string GetIOInfoMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); BS2EventDetail eventDetail = ConvertTo <BS2EventDetail>(eventLog.userID); return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] device[{4, 10}] port[{5}] value[{6}] image[{7}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, eventDetail.ioDeviceID, eventDetail.port, eventDetail.value, Convert.ToBoolean(eventLog.image & (byte)BS2EventImageBitPos.BS2_IMAGEFIELD_POS_IMAGE))); }
private static string GetUserIdMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); string userID = System.Text.Encoding.ASCII.GetString(eventLog.userID).TrimEnd('\0'); if (userID.Length == 0) { userID = "unknown"; } return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] userID[{4}] image[{5}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, userID, eventLog.image)); }
private static string GetUserIdMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); string userID = System.Text.Encoding.ASCII.GetString(eventLog.userID).TrimEnd('\0'); if (userID.Length == 0) { userID = "unknown"; } return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] userID[{4}] image[{5}] where[{6}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, userID, Convert.ToBoolean(eventLog.image & (byte)BS2EventImageBitPos.BS2_IMAGEFIELD_POS_IMAGE), Convert.ToBoolean(eventLog.param) ? "Device" : "Server")); }
public List <BS2Event> ReadLog(ref UInt32 deviceID, uint start, uint size) { _log.InfoFormat("开始采集设备记录,起始ID {0},最多采集数量 {1}", start, size); List <BS2Event> events = new List <BS2Event>(); Type structureType = typeof(BS2Event); int structSize = Marshal.SizeOf(structureType); IntPtr uid = IntPtr.Zero; IntPtr outEventLogObjs = IntPtr.Zero; UInt32 outNumEventLogs = 0; BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, start, size, out outEventLogObjs, out outNumEventLogs); //API.BS2_GetFilteredLogSinceEventId(sdkContext, deviceID,null,BS2EventCodeEnum.VERIFY_DURESS,start,0,0,) if (result != BS2ErrorCode.BS_SDK_SUCCESS) { _log.ErrorFormat("采集设备 {0} 记录失败:{1}", parseIdToIp(deviceID), result); } else if (outNumEventLogs > 0) { IntPtr curEventLogObjs = outEventLogObjs; for (int idx = 0; idx < outNumEventLogs; idx++) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, structureType); Console.WriteLine(Util.GetLogMsg(eventLog)); curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize); events.Add(eventLog); } API.BS2_ReleaseObject(outEventLogObjs); _log.InfoFormat("采集设备 {0} 记录数量:{1}", parseIdToIp(deviceID), outNumEventLogs); } else { _log.InfoFormat("采集设备 {0} 记录为0", parseIdToIp(deviceID)); } return(events); }
private static string GetUserIdAndTnaKeyMsg(BS2Event eventLog) { DateTime eventTime = ConvertFromUnixTimestamp(eventLog.dateTime); string userID = System.Text.Encoding.ASCII.GetString(eventLog.userID).TrimEnd('\0'); if (userID.Length == 0) { userID = "unknown"; } string subMsg = ""; if ((BS2EventCodeEnum)eventLog.code != BS2EventCodeEnum.VERIFY_FAIL_CARD) { BS2TNAKeyEnum tnaKeyEnum = (BS2TNAKeyEnum)eventLog.param; if (tnaKeyEnum != BS2TNAKeyEnum.UNSPECIFIED) { subMsg = String.Format("userID[{0}] T&A[{1}]", userID, tnaKeyEnum.ToString()); } else { subMsg = String.Format("userID[{0}]", userID); } } else { subMsg = String.Format("cardID[{0}]", userID); } return(String.Format("Log => device[{0, 10}] : timestamp[{1}] event id[{2, 10}] event code[{3}] {4} image[{5}]", eventLog.deviceID, eventTime.ToString("yyyy-MM-dd HH:mm:ss"), eventLog.id, (BS2EventCodeEnum)eventLog.code, subMsg, Convert.ToBoolean(eventLog.image & (byte)BS2EventImageBitPos.BS2_IMAGEFIELD_POS_IMAGE))); }
void getFilteredLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { Type structureType = typeof(BS2Event); int structSize = Marshal.SizeOf(structureType); IntPtr uid = IntPtr.Zero; UInt16 eventCode = 0; UInt32 start = 0; UInt32 end = 0; byte tnaKey = 0; IntPtr outEventLogObjs = IntPtr.Zero; UInt32 outNumEventLogs = 0; Console.WriteLine("Which event do you want to get? [0: All]"); Console.Write(">>>> "); eventCode = Util.GetInput((UInt16)BS2EventCodeEnum.ALL); Console.WriteLine("When do you want to get the log from? [yyyy-MM-dd HH:mm:ss]"); Console.Write(">>>> "); if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out start)) { return; } Console.WriteLine("When do you want to get the log to? [yyyy-MM-dd HH:mm:ss]"); Console.Write(">>>> "); if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out end)) { return; } Console.WriteLine("Which tnaKey do you want to get? [0: All(default), 1-16]"); Console.Write(">>>> "); tnaKey = Util.GetInput(0); if (tnaKey > BS2Environment.BS2_MAX_TNA_KEY) { Console.WriteLine("Invalid tnaKey : {0}", tnaKey); return; } Console.WriteLine("Which user do you want to the log for? [userID]"); Console.Write(">>>> "); string userIDStr = Console.ReadLine(); if (userIDStr.Length > 0) { byte[] uidArray = Encoding.ASCII.GetBytes(userIDStr); byte[] outUidArray = new byte[BS2Environment.BS2_USER_ID_SIZE]; uid = Marshal.AllocHGlobal(BS2Environment.BS2_USER_ID_SIZE); for (int idx = 0; idx < BS2Environment.BS2_USER_ID_SIZE; idx++) { outUidArray[idx] = 0; } Array.Copy(uidArray, outUidArray, uidArray.Length); Marshal.Copy(outUidArray, 0, uid, BS2Environment.BS2_USER_ID_SIZE); } BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetFilteredLog(sdkContext, deviceID, uid, eventCode, start, end, tnaKey, out outEventLogObjs, out outNumEventLogs); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } else if (outNumEventLogs > 0) { IntPtr curEventLogObjs = outEventLogObjs; for (int idx = 0; idx < outNumEventLogs; idx++) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, structureType); Console.WriteLine(Util.GetLogMsg(eventLog)); curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize); } API.BS2_ReleaseObject(outEventLogObjs); } else { Console.WriteLine("There are no matching logs."); } if (uid != IntPtr.Zero) { Marshal.FreeHGlobal(uid); } }
void syncLocalAndDeviceLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { UInt32 lastEventId = 0; string dbPath = "Data Source=log.db"; SQLiteConnection connection = new SQLiteConnection(dbPath); connection.Open(); SQLiteCommand cmd = new SQLiteCommand("CREATE TABLE IF NOT EXISTS log (id int PRIMARY KEY, dateTime int, deviceID int, code int, msg text)", connection); cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT * FROM log ORDER BY id DESC LIMIT 1"; SQLiteDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { lastEventId = Convert.ToUInt32(rdr["id"]); } rdr.Close(); Console.WriteLine("lastEventId[{0}]", lastEventId); const UInt32 logPageSize = 1024; int structSize = Marshal.SizeOf(typeof(BS2Event)); bool runnging = false; IntPtr outEventLogObjs = IntPtr.Zero; UInt32 outNumEventLogs = 0; cmd.CommandText = "INSERT INTO log (Id, dateTime, deviceID, code, msg) VALUES (@IdParam, @dateTimeParam, @deviceIDParam, @codeParam, @msgParam)"; do { Console.WriteLine("Get logs from device[{0}] lastEventId[{1}].", deviceID, lastEventId); BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, lastEventId, logPageSize, out outEventLogObjs, out outNumEventLogs); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); break; } if (outNumEventLogs > 0) { IntPtr curEventLogObjs = outEventLogObjs; SQLiteTransaction transaction = connection.BeginTransaction(); for (int idx = 0; idx < outNumEventLogs; idx++) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, typeof(BS2Event)); Console.WriteLine(">>> Insert log[{0}] into database.", eventLog.id); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@IdParam", eventLog.id); cmd.Parameters.AddWithValue("@dateTimeParam", eventLog.dateTime); cmd.Parameters.AddWithValue("@deviceIDParam", eventLog.deviceID); cmd.Parameters.AddWithValue("@codeParam", eventLog.code); cmd.Parameters.AddWithValue("@msgParam", Util.GetLogMsg(eventLog)); cmd.ExecuteNonQuery(); curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize); lastEventId = eventLog.id; } transaction.Commit(); API.BS2_ReleaseObject(outEventLogObjs); } if (outNumEventLogs < logPageSize) { runnging = false; } }while (runnging); connection.Close(); }
void getImageLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { BS2SimpleDeviceInfo deviceInfo; int structSize = Marshal.SizeOf(typeof(BS2Event)); UInt16 imageLogEventCode = (UInt16)BS2EventCodeEnum.DEVICE_TCP_CONNECTED; BS2EventConfig eventConfig = Util.AllocateStructure <BS2EventConfig>(); eventConfig.numImageEventFilter = 1; eventConfig.imageEventFilter[0].mainEventCode = (byte)(imageLogEventCode >> 8); eventConfig.imageEventFilter[0].scheduleID = (UInt32)BS2ScheduleIDEnum.ALWAYS; Console.WriteLine("Trying to get the device[{0}] information.", deviceID); BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Can't get device information(errorCode : {0}).", result); return; } Console.WriteLine("Trying to activate image log."); result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } Console.WriteLine("Trying to clear log for quick test."); result = (BS2ErrorCode)API.BS2_ClearLog(sdkContext, deviceID); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } Console.WriteLine("Trying to disconnect device[{0}] for quick test.", deviceID); result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } Thread.Sleep(500); //waiting for socket close Console.WriteLine("Trying to connect device[{0}].", deviceID); result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(), deviceInfo.port, out deviceID); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } IntPtr outEventLogObjs = IntPtr.Zero; UInt32 outNumEventLogs = 0; result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, 0, 1024, out outEventLogObjs, out outNumEventLogs); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } if (outNumEventLogs > 0) { IntPtr curEventLogObjs = outEventLogObjs; for (int idx = 0; idx < outNumEventLogs; idx++) { BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, typeof(BS2Event)); if (Convert.ToBoolean(eventLog.image)) { Console.WriteLine("Trying to get image log[{0}].", eventLog.id); IntPtr imageObj = IntPtr.Zero; UInt32 imageSize = 0; result = (BS2ErrorCode)API.BS2_GetImageLog(sdkContext, deviceID, eventLog.id, out imageObj, out imageSize); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } else { int written = 0; FileStream file = new FileStream(String.Format("{0}.jpg", eventLog.id), FileMode.Create, FileAccess.Write); Console.WriteLine("Trying to save image log[{0}].", eventLog.id); WriteFile(file.Handle, imageObj, (int)imageSize, out written, IntPtr.Zero); file.Close(); if (written != imageSize) { Console.WriteLine("Got error({0}).", result); } else { Console.WriteLine("Successfully saved the image log[{0}].", eventLog.id); Process.Start(file.Name); } } break; } curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize); } API.BS2_ReleaseObject(outEventLogObjs); } eventConfig.numImageEventFilter = 0; Console.WriteLine("Trying to deactivate image log."); result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } }
public static string GetLogMsg(BS2Event eventLog) { #if false return("eventlog : "); #else switch (((BS2EventCodeEnum)eventLog.code & BS2EventCodeEnum.MASK)) { case BS2EventCodeEnum.DOOR_LOCKED: case BS2EventCodeEnum.DOOR_UNLOCKED: case BS2EventCodeEnum.DOOR_CLOSED: case BS2EventCodeEnum.DOOR_OPENED: case BS2EventCodeEnum.DOOR_FORCED_OPEN: case BS2EventCodeEnum.DOOR_FORCED_OPEN_ALARM: case BS2EventCodeEnum.DOOR_FORCED_OPEN_ALARM_CLEAR: case BS2EventCodeEnum.DOOR_HELD_OPEN: case BS2EventCodeEnum.DOOR_HELD_OPEN_ALARM: case BS2EventCodeEnum.DOOR_HELD_OPEN_ALARM_CLEAR: case BS2EventCodeEnum.DOOR_APB_ALARM: case BS2EventCodeEnum.DOOR_APB_ALARM_CLEAR: return(GetDoorIdMsg(eventLog)); case BS2EventCodeEnum.ZONE_APB_ALARM: case BS2EventCodeEnum.ZONE_APB_ALARM_CLEAR: case BS2EventCodeEnum.ZONE_TIMED_APB_ALARM: case BS2EventCodeEnum.ZONE_TIMED_APB_ALARM_CLEAR: case BS2EventCodeEnum.ZONE_FIRE_ALARM: case BS2EventCodeEnum.ZONE_FIRE_ALARM_CLEAR: case BS2EventCodeEnum.ZONE_SCHEDULED_LOCK_VIOLATION: case BS2EventCodeEnum.ZONE_SCHEDULED_LOCK_START: case BS2EventCodeEnum.ZONE_SCHEDULED_LOCK_END: case BS2EventCodeEnum.ZONE_SCHEDULED_UNLOCK_START: case BS2EventCodeEnum.ZONE_SCHEDULED_UNLOCK_END: case BS2EventCodeEnum.ZONE_SCHEDULED_LOCK_ALARM: case BS2EventCodeEnum.ZONE_SCHEDULED_LOCK_ALARM_CLEAR: return(GetZoneIdMsg(eventLog)); case BS2EventCodeEnum.SUPERVISED_INPUT_OPEN: case BS2EventCodeEnum.SUPERVISED_INPUT_SHORT: case BS2EventCodeEnum.DEVICE_INPUT_DETECTED: return(GetIOInfoMsg(eventLog)); case BS2EventCodeEnum.USER_ENROLL_SUCCESS: case BS2EventCodeEnum.USER_ENROLL_FAIL: case BS2EventCodeEnum.USER_UPDATE_SUCCESS: case BS2EventCodeEnum.USER_UPDATE_FAIL: case BS2EventCodeEnum.USER_DELETE_SUCCESS: case BS2EventCodeEnum.USER_DELETE_FAIL: return(GetUserIdMsg(eventLog)); case BS2EventCodeEnum.VERIFY_SUCCESS: case BS2EventCodeEnum.VERIFY_FAIL: case BS2EventCodeEnum.VERIFY_DURESS: case BS2EventCodeEnum.IDENTIFY_SUCCESS: case BS2EventCodeEnum.IDENTIFY_FAIL: case BS2EventCodeEnum.IDENTIFY_DURESS: return(GetUserIdAndTnaKeyMsg(eventLog)); default: return(GetGeneralMsg(eventLog)); } #endif }