From 58d5fe540a1d5507872b0d8fdc57f280f8d2afe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=BD=87=E9=98=B3?= Date: Thu, 6 Jun 2024 12:16:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=BA=E8=AF=81=E5=90=88?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/WebSocketClientWithHeartbeat.cs | 105 +++++++++++---- CPF_Cef/FrmMain.cs | 8 +- CPF_Cef/MainModel.cs | 124 ++---------------- CPF_Cef/Parame.cs | 6 +- 4 files changed, 95 insertions(+), 148 deletions(-) diff --git a/CPF_Cef/Common/WebSocketClientWithHeartbeat.cs b/CPF_Cef/Common/WebSocketClientWithHeartbeat.cs index e2b1f59..8392942 100644 --- a/CPF_Cef/Common/WebSocketClientWithHeartbeat.cs +++ b/CPF_Cef/Common/WebSocketClientWithHeartbeat.cs @@ -8,6 +8,10 @@ using System.Net.WebSockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Net.Sockets; +using System.Timers; +using CPF.Mac.Foundation; +using CPF.Windows; namespace AksWebBrowser.Common { @@ -16,6 +20,8 @@ namespace AksWebBrowser.Common private readonly string _serverUri; private readonly ClientWebSocket _webSocket = new ClientWebSocket(); public string funName = string.Empty; + //开启定时 + System.Timers.Timer timer = new System.Timers.Timer(30000); public WebSocketClientWithHeartbeat() { try @@ -53,9 +59,10 @@ namespace AksWebBrowser.Common string body = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count); if (!string.IsNullOrEmpty(body)) { - if (funName == "CF_StartLiveDetect") + JObject jo = (JObject)JsonConvert.DeserializeObject(body); + string functionName = jo["functionName"].ToString(); + if (functionName == "LiveDetectResult") { - JObject jo = (JObject)JsonConvert.DeserializeObject(body); string status = string.Empty; if (body.Contains("success")) { @@ -63,24 +70,58 @@ namespace AksWebBrowser.Common } if (string.IsNullOrEmpty(status)) { - string functionName = jo["functionName"].ToString(); string nEventId = jo["nEventId"].ToString(); - if (functionName == "LiveDetectResult" && nEventId == "100") + if (nEventId == "100") { - Parame.messageData = body; + string results = Parame.ImgBase64; + if (!string.IsNullOrEmpty(results)) + { + JObject jo1 = (JObject)JsonConvert.DeserializeObject(results); + string data = jo1["data"].ToString(); + jo1 = (JObject)JsonConvert.DeserializeObject(data); + string data2 = jo1["Data"].ToString(); + jo1 = (JObject)JsonConvert.DeserializeObject(data2); + string ImageBase64 = jo1["ImageBase64"].ToString(); + + var faceA = "/data/home/aks/aks/wwwroot/TmpFile/faceA.jpg"; + var faceB = "/data/home/aks/aks/wwwroot/TmpFile/faceB.jpg"; + //string faceA = Utils.Base64ByImagesPath(ImageBase64.Replace("data:image/jpg;base64,", ""), "faceA"); + //string faceB = Utils.Base64ByImagesPath(jo["ImgBase64"].ToString(), "faceB"); + var mode2 = new + { + function = "CF_FaceCompare", + filePathA = faceA, + filePathB = faceB + }; + //开始人脸比对 + Send(JsonConvert.SerializeObject(mode2), "CF_FaceCompare"); + } + else + { + results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败." + "\"}"; + Parame.socket.Send(results); + } + } } + } + else if (functionName == "CF_FaceCompare") + { + //停止活检 + Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); + //关闭摄像头 + Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); + string status = jo["success"].ToString(); + string results = string.Empty; + if (status == "0") + { + results = Parame.ImgBase64; + } else { - if (status != "0") - { - Parame.messageData = body; - } + results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败." + "\"}"; } - } - else - { - Parame.messageData = body; + Parame.socket.Send(results); } } } @@ -88,7 +129,6 @@ namespace AksWebBrowser.Common catch (Exception ex) { Log.Error("接收消息双目websockt" + ex.Message); - Parame.messageData = "400"; } } } @@ -121,7 +161,7 @@ namespace AksWebBrowser.Common /// /// /// - public async Task Send(string message, string _funName) + public async Task Send(string message, string _funName) { try { @@ -132,27 +172,36 @@ namespace AksWebBrowser.Common // 发送消息到服务器 var buffer = System.Text.Encoding.UTF8.GetBytes(message); await _webSocket.SendAsync(new ArraySegment(buffer), WebSocketMessageType.Text, true, CancellationToken.None); - int timeout = 0; - while (string.IsNullOrEmpty(Parame.messageData) && timeout < 30000) - { - timeout = timeout + 10; - Task.Delay(10).Wait(); - } - if (timeout >= 30000) - { - Parame.messageData = "500"; - } } - else + if (_funName== "CF_StartLiveDetect") { - Parame.messageData = "700"; + timer.Elapsed += OnTimedEvent; + timer.Enabled = true;//启动定时器 } } catch (Exception ex) { Log.Error("推送消息到双目websockt" + ex.Message); } - return Parame.messageData; + } + + + + //打开串口 + private void OnTimedEvent(Object source, ElapsedEventArgs e) + { + try + { + timer.Stop(); + //停止活检 + Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); + //关闭摄像头 + Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); + string results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "活体检测超时." + "\"}"; + Parame.socket.Send(results); + } + catch { } + finally { } } /// diff --git a/CPF_Cef/FrmMain.cs b/CPF_Cef/FrmMain.cs index 5322c6c..760153b 100644 --- a/CPF_Cef/FrmMain.cs +++ b/CPF_Cef/FrmMain.cs @@ -18,10 +18,10 @@ namespace AKS.EnterpriseLibrary.WebBrowser { public class FrmMain : Window { - public int w = 1080; - public int h = 1920; - //public int h = 1080; - //public int w = 1920; + //public int w = 1080; + //public int h = 1920; + public int h = 1080; + public int w = 1920; protected override void InitializeComponent() { diff --git a/CPF_Cef/MainModel.cs b/CPF_Cef/MainModel.cs index 4049030..db2e173 100644 --- a/CPF_Cef/MainModel.cs +++ b/CPF_Cef/MainModel.cs @@ -32,7 +32,6 @@ namespace AKS.EnterpriseLibrary.WebBrowser public class MainModel : CPF.CpfObject { private static List allSockets; - //初始化网络连接 public static void DeviceControllerInit() { @@ -182,7 +181,10 @@ namespace AKS.EnterpriseLibrary.WebBrowser } try { - socket.Send(result); + if (!string.IsNullOrEmpty(result)) + { + socket.Send(result); + } } catch { result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "hert" + "\",\"message\":\"seccse\",\"code\":\"400\",\"status\":true,\"data\":\"返回参数失败\"}"; } finally { } @@ -273,44 +275,10 @@ namespace AKS.EnterpriseLibrary.WebBrowser { Task.Run(async () => { - JObject jo = (JObject)JsonConvert.DeserializeObject(IdcardStr); - string data = jo["Data"].ToString(); - jo = (JObject)JsonConvert.DeserializeObject(data); - string ImageBase64 = jo["ImageBase64"].ToString(); - StartFace(ImageBase64); + string results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"seccse\",\"code\":\"200\",\"status\":true,\"data\":" + IdcardStr + "}"; + StartFace(results); }); - @event9.WaitOne(); - if (!string.IsNullOrEmpty(smresults)) - { - if (smresults == "200") - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"seccse\",\"code\":\"200\",\"status\":true,\"data\":" + IdcardStr + "}"; - } - else if (smresults == "500") - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证超时" + "\"}"; - } - else if (smresults == "600") - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一活体检测失败" + "\"}"; - } - else if (smresults == "700") - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "服务端连接异常" + "\"}"; - } - else if (smresults == "800") - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "身份证持有者非本人" + "\"}"; - } - else - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败。" + "\"}"; - } - } - else - { - result = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + param.code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败." + "\"}"; - } + result = ""; } else { @@ -1654,7 +1622,9 @@ namespace AKS.EnterpriseLibrary.WebBrowser { try { + Parame.ImgBase64 = ImageBase64; WebSocketClientWithHeartbeat webSocket = new WebSocketClientWithHeartbeat(); + Task.Delay(800).Wait(); var mode = new { function = "CF_OpenCamera", @@ -1664,84 +1634,10 @@ namespace AKS.EnterpriseLibrary.WebBrowser fw = 800, fh = 600 }; - Task.Delay(1000).Wait(); //打开摄像头 webSocket.Send(JsonConvert.SerializeObject(mode), "CF_OpenCamera"); //开始活检 - smresults = await webSocket.Send("{\"function\":\"CF_StartLiveDetect\"}", "CF_StartLiveDetect"); - if (smresults == "500" || smresults == "400")//500:超时,400:接收数据错误 - { - //关闭摄像头 - webSocket.Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); - webSocket.Close(); - } - else if (smresults == "700")//服务端未启动 - { webSocket.Close(); } - else - { - JObject jo = (JObject)JsonConvert.DeserializeObject(smresults); - string nEventId = jo["nEventId"].ToString(); - if (nEventId == "100") - { - //var faceA = "/data/home/aks/aks/wwwroot/TmpFile/faceA.jpg"; - //var faceB = "/data/home/aks/aks/wwwroot/TmpFile/faceB.jpg"; - string faceA = Utils.Base64ByImagesPath(ImageBase64.Replace("data:image/jpg;base64,", ""), "faceA"); - string faceB = Utils.Base64ByImagesPath(jo["ImgBase64"].ToString(), "faceB"); - var mode2 = new - { - function = "CF_FaceCompare", - filePathA = faceA, - filePathB = faceB - }; - //开始人脸比对 - smresults = await webSocket.Send(JsonConvert.SerializeObject(mode2), "CF_FaceCompare"); - Log.Info(smresults); - //停止活检 - webSocket.Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); - //关闭摄像头 - webSocket.Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); - if (smresults == "500" || smresults == "400")//500:超时,400:接收数据错误 - { - //关闭摄像头 - webSocket.Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); - webSocket.Close(); - } - else if (smresults == "700")//服务端未启动 - { webSocket.Close(); } - else - { - jo = (JObject)JsonConvert.DeserializeObject(smresults); - string success = jo["success"].ToString(); - if (success == "0") - { - string score = jo["score"].ToString(); - if (Convert.ToInt32(score) >= Parame.score) - { - smresults = "200";//比对成功 - } - else - { - smresults = "800";//非本人 - } - } - else - { - smresults = "400"; - } - } - } - else - { - //停止活检 - webSocket.Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); - //关闭摄像头 - webSocket.Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); - //活体检测失败 - smresults = "600"; - } - webSocket.Close(); - } - @event9.Set(); + webSocket.Send("{\"function\":\"CF_StartLiveDetect\"}", "CF_StartLiveDetect"); } catch (Exception ex) { diff --git a/CPF_Cef/Parame.cs b/CPF_Cef/Parame.cs index 9e36ddf..48763d5 100644 --- a/CPF_Cef/Parame.cs +++ b/CPF_Cef/Parame.cs @@ -30,7 +30,7 @@ namespace AksWebBrowser //高拍仪devIndex public static string gpydevIndex = "0"; //签字版、指纹、身份证 - public static string signUrl = "http://127.0.0.1:9399/device"; + public static string signUrl = "http://192.168.0.234:9399/device"; //请求超时 public static int timeout = 3000; //票据打印机 @@ -44,11 +44,13 @@ namespace AksWebBrowser //是否开启高拍仪 public static bool isGPY = false; //双目摄像头 - public static string smWebsocket = "ws://127.0.0.1:22226"; + public static string smWebsocket = "ws://192.168.0.234:22226"; //消息推送 public static string messageData { get; set; } //人脸比对阈值 public static int score = 65; + //人脸比对阈值 + public static string ImgBase64 = string.Empty; } public struct Func {