using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.Logging; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.Repository.service.LogImpl; using SlnMesnac.Repository.service.ScanLog; using TouchSocket.Core; using TouchSocket.Http; using TouchSocket.Rpc; using TouchSocket.WebApi; namespace SlnMesnac.TouchSocket { public class ApiServer : RpcServer { private readonly IScanLogService _scanService; private readonly IBaseCodeService _baseCodeService; readonly IBaseLogService _baseLogService; public Action? ScanLogSocketAction; public Action? ScanStatusSocketAction; private ILogger _logger; public ApiServer(IScanLogService scanService, IBaseCodeService baseCodeService, IBaseLogService baseLogService, ILogger logger) { _scanService = scanService; _baseCodeService = baseCodeService; _baseLogService = baseLogService; _logger=logger; } [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public string Test(IWebApiCallContext callContext) { //http内容 var httpContext = callContext.HttpContext; //http请求 var request = httpContext.Request; var str = request.GetBody(); _logger.LogInformation("测试body{Data}", str); return str; } [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public Back Back(Hk hk) { Back ba=new Back(); _logger.LogInformation("=========================="); _logger.LogInformation("{Data}", hk.ToJsonString()); _logger.LogInformation("=========================="); if (hk.data_type == 1) { if(hk.rfid == "noread") { hk.rfid = hk.ocr; } else { string rfidStr = hk.rfid; if (!string.IsNullOrEmpty(hk.rfid)) { AnasysBuffer(ref rfidStr); hk.rfid = rfidStr; } } ScanLogSocket scan = new ScanLogSocket() { code = hk.code, data_type = hk.data_type, ocr = hk.ocr, rfid = hk.rfid, timestamp = hk.timestamp, url = hk.url }; ba= ScanBack(scan); } else { if (hk.data_type == 2) { ScanStatusSocket scan = new ScanStatusSocket() { data_type = hk.data_type, is_alarm = hk.is_alarm, device_status = hk.device_status, timestamp = hk.timestamp, }; ba = ScanStatus(scan); } } return ba; } /// /// 扫描的状态回传 /// /// /// [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public Back ScanStatus(ScanStatusSocket scanStatus) { _logger.LogInformation("扫描的状态接口"); _logger.LogInformation("{Data}",scanStatus.ToJsonString()); BaseLog log = new BaseLog { LogLevel = "Info", ErrMsg = "", Content = scanStatus.ToJsonString() }; try { if (scanStatus.is_alarm == 1 && scanStatus.device_status != null && scanStatus.device_status.Count > 0) { List ls = new List(); var baseCodes = _baseCodeService.QuListCache(); foreach (var baseCode in scanStatus.device_status) { var enBaseCode = baseCodes.FirstOrDefault(x => x.Code == baseCode); if (enBaseCode != null) { ls.Add(enBaseCode.State); } } scanStatus.status_txt = ls; } ScanStatusSocketAction?.Invoke(scanStatus); } catch (Exception e) { _logger.LogError(e,e.Message); log.LogLevel = "Error"; log.ErrMsg=e.Message; } _baseLogService.Insert(log); return new Back(); } /// /// 扫描数据回传接口 /// /// /// [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public Back ScanBack(ScanLogSocket scanLog) { _logger.LogInformation("扫描数据回传接口"); _logger.LogInformation("{Data}", scanLog.ToJsonString()); BaseLog log = new BaseLog(); log.LogLevel = "Info"; log.ErrMsg = ""; log.Content = scanLog.ToJsonString(); try { var model = new ScanLogModel() { CreateTime = scanLog.CreateTime, DataType = scanLog.data_type, Code = scanLog.code, Rfid = scanLog.rfid, Ocr = scanLog.ocr, Url = scanLog.url }; ScanLogSocketAction?.Invoke(model); _scanService.Insert(model); } catch (Exception e) { _logger.LogError(e, e.Message); log.LogLevel = "Error"; log.ErrMsg = e.Message; } _baseLogService.Insert(log); return new Back(); } private const string bufferStart = "0C1104"; private const string bufferBody = "EE5EFB65"; /// /// RFID信息解析 /// /// private void AnasysBuffer(ref string rfidStr) { if(rfidStr != "noread") { string bufferHex = rfidStr.Substring(bufferStart.Length, bufferBody.Length); long decimalValue = Convert.ToInt64(bufferHex, 16); rfidStr = decimalValue.ToString(); } } } } public class Hk { public DateTime CreateTime { get { DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp); return dateTimeOffset.UtcDateTime; } } public long timestamp { get; set; } public int data_type { get; set; } public string rfid { get; set; } public string code { get; set; } public string ocr { get; set; } public string url { get; set; } public int is_alarm { get; set; } public List device_status { get; set; } public List status_txt { get; set; } }