From 4df6d9f2d67e4018b76282d09b71fa78ebd0b893 Mon Sep 17 00:00:00 2001 From: DucDangAnh Date: Mon, 22 Jun 2020 13:58:13 +0700 Subject: [PATCH] C3DeviceController - Remove C3DeviceEvent, C3RTLogEventHandler. Add Event C3DeviceEvent. Add Method GetLogToReceiveNewCard --- AIParkingApplication/C3DeviceController.cs | 42 ++++++++++------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/AIParkingApplication/C3DeviceController.cs b/AIParkingApplication/C3DeviceController.cs index b251667..10d72df 100644 --- a/AIParkingApplication/C3DeviceController.cs +++ b/AIParkingApplication/C3DeviceController.cs @@ -1,11 +1,12 @@ using System; using System.Runtime.InteropServices; using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace AIParkingApplication { - public delegate void C3EventHandler(ActionResult actionResult); - public delegate void C3RTLogEventHandler(ActionResult actionResult, int doorId, string cardNumber); + public delegate void C3DeviceEvent(int doorId, string cardNumber); public class C3DeviceController { @@ -14,17 +15,10 @@ namespace AIParkingApplication private IntPtr oneTimeConnectedParamHandler; private string connectionString; private bool isReconnected; - public event C3EventHandler OnConnected; - public event C3EventHandler OnConnectFailed; - public event C3EventHandler OnReconnect; - public event C3EventHandler OnDoorOpenned; - public event C3EventHandler OnDoorOpenFailed; - public event C3EventHandler OnPingFailed; - public event C3EventHandler OnDisconnected; - public event C3RTLogEventHandler OnGetRTLog; - public event C3EventHandler OnGetRTLogFailed; private int reconnectCounter; + public event C3DeviceEvent OnNewCardReceived; + public C3DeviceController(string ipAddress, int port = C3Constant.DEFAULT_C3_PORT) { IPAddress = ipAddress; @@ -67,7 +61,6 @@ namespace AIParkingApplication HasError = false, Message = string.Format(C3Constant.CONNECTED_SUCCESSFULLY, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnConnected?.Invoke(result); return result; } else @@ -77,7 +70,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.FAILED_TO_CONNECT, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress, PullLastError()) }; - OnConnectFailed?.Invoke(result); return result; } } @@ -88,8 +80,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.FAILED_TO_PING, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnPingFailed?.Invoke(result); - OnConnectFailed?.Invoke(result); return result; } } @@ -106,7 +96,6 @@ namespace AIParkingApplication HasError = false, Message = string.Format(C3Constant.OPENNED_DOOR_SUCCESSFULLY, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnDoorOpenned?.Invoke(result); return result; } else @@ -116,7 +105,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.FAILED_TO_OPEN_DOOR, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnDoorOpenFailed?.Invoke(result); return result; } } @@ -127,11 +115,23 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.NOT_CONNECTTED, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnDoorOpenFailed?.Invoke(result); return result; } } + public async Task GetLogToReceiveNewCard() + { + while (true) + { + await Task.Delay(1000); + ActionResult getRTLogResult = GetRTLog(out int doorId, out string cardNumber); + if (doorId != C3Constant.DEFAULT_DOOR_ID && cardNumber != C3Constant.DEFAULT_CARD_NUMBER) + { + OnNewCardReceived?.Invoke(doorId, cardNumber); + } + } + } + public ActionResult GetRTLog(out int doorId, out string cardNumber) { doorId = C3Constant.DEFAULT_DOOR_ID; @@ -145,7 +145,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.FAILED_TO_PING, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnPingFailed?.Invoke(result); return result; } @@ -167,7 +166,6 @@ namespace AIParkingApplication { HasError = false }; - OnGetRTLog?.Invoke(result, doorId, cardNumber); return result; } else @@ -182,7 +180,6 @@ namespace AIParkingApplication reconnectCounter = 0; reconnectResult.Message = string.Format(C3Constant.RECONNECTED_SUCCESSFULLY, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress); } - OnReconnect?.Invoke(reconnectResult); return reconnectResult; } @@ -191,7 +188,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.FAILED_TO_READ_LOG, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnGetRTLogFailed?.Invoke(result); return result; } } @@ -204,7 +200,6 @@ namespace AIParkingApplication HasError = true, Message = string.Format(C3Constant.WRONG_FORMAT_DATA, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress, ex.Message) }; - OnGetRTLogFailed?.Invoke(result); return result; } } @@ -218,7 +213,6 @@ namespace AIParkingApplication HasError = false, Message = string.Format(C3Constant.DISCONNECTED_SUCCESSFULLY, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), IPAddress) }; - OnDisconnected?.Invoke(result); return result; } }