diff --git a/.idea/.idea.SlnMesnac/.idea/.name b/.idea/.idea.SlnMesnac/.idea/.name new file mode 100644 index 0000000..bdf328a --- /dev/null +++ b/.idea/.idea.SlnMesnac/.idea/.name @@ -0,0 +1 @@ +SlnMesnac \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/ApiResult.cs b/SlnMesnac.TouchSocket/ApiResult.cs new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/SlnMesnac.TouchSocket/ApiResult.cs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/ApiServer.cs b/SlnMesnac.TouchSocket/ApiServer.cs new file mode 100644 index 0000000..6d0ab5b --- /dev/null +++ b/SlnMesnac.TouchSocket/ApiServer.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service; +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; + + public Action? ScanLogSocketAction; + public Action? ScanStatusSocketAction; + + + public ApiServer(IScanLogService scanService, IBaseCodeService baseCodeService) + { + _scanService = scanService; + _baseCodeService = baseCodeService; + } + + /// + /// 扫描的状态回传 + /// + /// + /// + /// + [EnableCors("cors")] + [WebApi(HttpMethodType.POST)] + public Back ScanStatus(ScanStatusSocket scanStatus) + { + + 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); + + + + return new Back(); + } + + + /// + /// 入库开始 + /// + /// + /// + /// + [EnableCors("cors")] + [WebApi(HttpMethodType.POST)] + public Back ScanBack(ScanLogSocket scanStatus) + { + ScanLogSocketAction?.Invoke(scanStatus); + _scanService.Insert(new ScanLogModel() + { + CreateTime = scanStatus.CreateTime, + DataType = scanStatus.data_type, + Code = scanStatus.code, + Rfid = scanStatus.rfid, + Ocr = scanStatus.ocr, + Url = scanStatus.url + + }); + return new Back(); + } + } +} diff --git a/SlnMesnac.TouchSocket/TouchSocketSetup.cs b/SlnMesnac.TouchSocket/TouchSocketSetup.cs index de34f28..b969241 100644 --- a/SlnMesnac.TouchSocket/TouchSocketSetup.cs +++ b/SlnMesnac.TouchSocket/TouchSocketSetup.cs @@ -37,8 +37,8 @@ namespace SlnMesnac.TouchSocket public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app) { - var _server = app.ApplicationServices.GetService(); - _server.Init(20108); + var _server = app.ApplicationServices.GetService(); + _server.Init(); return app; } diff --git a/SlnMesnac.TouchSocket/WebApiServerApp.cs b/SlnMesnac.TouchSocket/WebApiServerApp.cs new file mode 100644 index 0000000..6c35703 --- /dev/null +++ b/SlnMesnac.TouchSocket/WebApiServerApp.cs @@ -0,0 +1,79 @@ +using Microsoft.AspNetCore.Hosting.Server; + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +using TouchSocket.Core; + +using TouchSocket.Http; +using TouchSocket.Rpc; +using TouchSocket.Sockets; +using TouchSocket.WebApi.Swagger; + +namespace SlnMesnac.TouchSocket +{ + public class WebApiServerApp + { + private ApiServer apiServer; + + public WebApiServerApp(ApiServer apiServer) + { + this.apiServer = apiServer; + } + + public void Init() + { + try + { + var service = new HttpService(); + service.Setup(new TouchSocketConfig() + .SetListenIPHosts(7789) + .ConfigureContainer(a => + { + a.AddRpcStore(store => + { + + store.RegisterServer(apiServer);//注册服务 + }); + a.AddCors(corsOption => + { + corsOption.Add("cors", corsBuilder => + { + corsBuilder.AllowAnyMethod() + .AllowAnyOrigin(); + }); + }); + a.AddLogger(logger => + { + logger.AddConsoleLogger(); + logger.AddFileLogger(); + }); + }) + .ConfigurePlugins(a => + { + a.UseCheckClear(); + a.UseWebApi() + .ConfigureConverter(converter => + { + converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None }); + + }); + a.UseSwagger();//使用Swagger页面 + //.UseLaunchBrowser(); + a.UseDefaultHttpServicePlugin(); + })); + service.Start(); + Console.WriteLine("以下连接用于测试webApi"); + Console.WriteLine($"使用:http://127.0.0.1:7789/swagger/index.html"); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + //Console.ReadLine(); + } + } + +} diff --git a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs index 566e0fd..70edd2f 100644 --- a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs @@ -75,7 +75,9 @@ namespace SlnMesnac.WPF.ViewModel public MainWindowViewModel() { - _logger = App.ServiceProvider.GetService>(); + var sp = App.ServiceProvider; + + _logger = App.ServiceProvider.GetService>(); ControlOnClickCommand = new RelayCommand(obj => ControlOnClick(obj)); FormControlCommand = new RelayCommand(x => FormControl(x));