using System; using System.Collections.Generic; using System.Linq; using System.Text; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.Repository.service.LogImpl; using SlnMesnac.Repository.service.ScanLog; using TouchSocket.Core; using TouchSocket.Rpc; using TouchSocket.WebApi; namespace SlnMesnac.TouchSocket { public class ApiServer : RpcServer { private readonly IScanLogService _scanService; private readonly IBaseCodeService _baseCodeService; IBaseLogService _baseLogService; public Action? ScanLogSocketAction; public Action? ScanStatusSocketAction; public ApiServer(IScanLogService scanService, IBaseCodeService baseCodeService, IBaseLogService baseLogService) { _scanService = scanService; _baseCodeService = baseCodeService; _baseLogService = baseLogService; } /// /// 扫描的状态回传 /// /// /// /// [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public Back ScanStatus(ScanStatusSocket scanStatus) { BaseLog log = new BaseLog(); log.LogLevel = "Info"; log.ErrMsg = ""; log.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) { log.LogLevel = "Error"; log.ErrMsg=e.Message; } _baseLogService.Insert(log); return new Back(); } /// /// 入库开始 /// /// /// /// [EnableCors("cors")] [WebApi(HttpMethodType.POST)] public Back ScanBack(ScanLogSocket scanLogStatus) { BaseLog log = new BaseLog(); log.LogLevel = "Info"; log.ErrMsg = ""; log.Content = scanLogStatus.ToJsonString(); try { var model = new ScanLogModel() { CreateTime = scanLogStatus.CreateTime, DataType = scanLogStatus.data_type, Code = scanLogStatus.code, Rfid = scanLogStatus.rfid, Ocr = scanLogStatus.ocr, Url = scanLogStatus.url }; ScanLogSocketAction?.Invoke(model); _scanService.Insert(model); } catch (Exception e) { log.LogLevel = "Error"; log.ErrMsg = e.Message; } _baseLogService.Insert(log); return new Back(); } } }