TouchSocket 数据保护

master
nodyang@aliyun.com 4 months ago
parent 8c133187b6
commit 990ee083e6

@ -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<ScanLogSocket>? ScanLogSocketAction;
public Action<ScanStatusSocket>? ScanStatusSocketAction;
public ApiServer(IScanLogService scanService, IBaseCodeService baseCodeService)
{
_scanService = scanService;
_baseCodeService = baseCodeService;
}
/// <summary>
/// 扫描的状态回传
/// </summary>
/// <param name="messageHeader"></param>
/// <param name="containerRegisterQuery"></param>
/// <returns></returns>
[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<string> ls = new List<string>();
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();
}
/// <summary>
/// 入库开始
/// </summary>
/// <param name="messageHeader"></param>
/// <param name="containerRegisterQuery"></param>
/// <returns></returns>
[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();
}
}
}

@ -37,8 +37,8 @@ namespace SlnMesnac.TouchSocket
public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app)
{
var _server = app.ApplicationServices.GetService<TcpServer>();
_server.Init(20108);
var _server = app.ApplicationServices.GetService<WebApiServerApp>();
_server.Init();
return app;
}

@ -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>(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();
}
}
}

@ -75,6 +75,8 @@ namespace SlnMesnac.WPF.ViewModel
public MainWindowViewModel()
{
var sp = App.ServiceProvider;
_logger = App.ServiceProvider.GetService<ILogger<MainWindowViewModel>>();
ControlOnClickCommand = new RelayCommand<object>(obj => ControlOnClick(obj));

Loading…
Cancel
Save