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.Config/SqlConfig.cs b/SlnMesnac.Config/SqlConfig.cs index cc3de31..b1207d2 100644 --- a/SlnMesnac.Config/SqlConfig.cs +++ b/SlnMesnac.Config/SqlConfig.cs @@ -34,7 +34,7 @@ namespace SlnMesnac.Config public string configId { get; set; } /// - /// 数据库类型,MySql-0;SqlServer-1;Sqlite-2;Oracle-3 + /// 数据库类型,MySql-0;SqlServer-1;Sqlite-2;Oracle-3,4 /// public int dbType { get; set; } diff --git a/SlnMesnac.Model/domain/BaseCode.cs b/SlnMesnac.Model/domain/BaseCode.cs new file mode 100644 index 0000000..f7b6c64 --- /dev/null +++ b/SlnMesnac.Model/domain/BaseCode.cs @@ -0,0 +1,37 @@ +using System.Runtime.Serialization; +using SqlSugar; + +namespace SlnMesnac.Model.domain +{ + /// + /// 异常监控编码对应表 + /// + [SugarTable("BaseCode"), TenantAttribute("mes")] + [DataContract(Name = "BaseCode 基础表")] + public class BaseCode + { + /// + /// + /// + [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + /// + /// + /// + [SugarColumn(ColumnName = "code")] + public string Code { get; set; } + /// + /// + /// + [SugarColumn(ColumnName = "state")] + public string State { get; set; } + + + /// + /// 1 RFid 2 视觉 + /// + [SugarColumn(ColumnName = "category")] + public int Category { get; set; } + + } +} \ No newline at end of file diff --git a/SlnMesnac.Model/domain/Page.cs b/SlnMesnac.Model/domain/Page.cs new file mode 100644 index 0000000..deab148 --- /dev/null +++ b/SlnMesnac.Model/domain/Page.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace SlnMesnac.Model.domain +{ + public class Page + { + public int PageIndex { get; set; } + public int PageSize { get; set; } + public int TotalCount { get; set; } + public int TotalPage { get; set; } + public List Data { get; set; } + + } + + public class Page:Page + { + public List Data { get; set; } + + /// + /// 判断有没有下一页 + /// + public bool HasNext => TotalPage > PageIndex; + } +} \ No newline at end of file diff --git a/SlnMesnac.Model/domain/ScanLog.cs b/SlnMesnac.Model/domain/ScanLog.cs new file mode 100644 index 0000000..8d20e2f --- /dev/null +++ b/SlnMesnac.Model/domain/ScanLog.cs @@ -0,0 +1,29 @@ +using System; +using System.Runtime.Serialization; +using SqlSugar; + +namespace SlnMesnac.Model.domain +{ + [SugarTable("ScanLog"), Tenant("mes")] + [DataContract(Name = "ScanLog ɨ־")] + public class ScanLogModel + { + /// + /// + /// + [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + [SugarColumn(ColumnName = "create_time")] + public DateTime CreateTime { get; set; } + [SugarColumn(ColumnName = "data_type")] + public int DataType { get; set; } + [SugarColumn(ColumnName = "rfid")] + public string Rfid { get; set; } + [SugarColumn(ColumnName = "code")] + public string Code { get; set; } + [SugarColumn(ColumnName = "ocr")] + public string Ocr { get; set; } + [SugarColumn(ColumnName = "url")] + public string Url { get; set; } + } +} \ No newline at end of file diff --git a/SlnMesnac.Repository/SlnMesnac.Repository.csproj b/SlnMesnac.Repository/SlnMesnac.Repository.csproj index b733819..c432d79 100644 --- a/SlnMesnac.Repository/SlnMesnac.Repository.csproj +++ b/SlnMesnac.Repository/SlnMesnac.Repository.csproj @@ -6,6 +6,7 @@ + diff --git a/SlnMesnac.Repository/service/ScanLog/IScanLogService.cs b/SlnMesnac.Repository/service/ScanLog/IScanLogService.cs new file mode 100644 index 0000000..4a87885 --- /dev/null +++ b/SlnMesnac.Repository/service/ScanLog/IScanLogService.cs @@ -0,0 +1,13 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; + +namespace SlnMesnac.Repository.service.ScanLog +{ + public interface IScanLogService:IBaseService + { + void CreatTable(); + + + Page QueryPage(int pageIndex, int pageSize); + } +} \ No newline at end of file diff --git a/SlnMesnac.Repository/service/ScanLog/ScanLogServiceImpl.cs b/SlnMesnac.Repository/service/ScanLog/ScanLogServiceImpl.cs new file mode 100644 index 0000000..3b7b0c8 --- /dev/null +++ b/SlnMesnac.Repository/service/ScanLog/ScanLogServiceImpl.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; + +namespace SlnMesnac.Repository.service.ScanLog +{ + public class ScanLogServiceImpl:BaseServiceImpl,IScanLogService + { + public ScanLogServiceImpl(Repository rep) : base(rep) + { + } + + public void CreatTable() + { + _rep.Context.CodeFirst.InitTables(); + List ls = new List(); + for (int i = 0; i < 100; i++) + { + ls.Add(new ScanLogModel() + { + CreateTime = DateTime.Now, + DataType = 1, + Rfid = "12", + Code = "21", + Ocr = "2", + Url="badi.com" + }); + } + + this.Insert(ls); + } + + public Page QueryPage(int pageIndex, int pageSize) + { + int totalCount = 0; + var list= _rep.Context.Queryable().OrderByDescending(x => x.CreateTime) + .ToPageList(pageIndex, pageSize, ref totalCount); + Page page = new Page(); + page.Data=list; + page.PageIndex = pageIndex; + page.PageSize = pageSize; + page.TotalCount = totalCount; + page.TotalPage = (int)Math.Ceiling(totalCount / (double)pageSize); + return page; + + } + } +} \ No newline at end of file diff --git a/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs b/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs new file mode 100644 index 0000000..6dde356 --- /dev/null +++ b/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Caching.Memory; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; + +namespace SlnMesnac.Repository.service +{ + public class BaseCodeServiceImpl:BaseServiceImpl,IBaseCodeService + { + private readonly IMemoryCache _memoryCache; + + public BaseCodeServiceImpl(Repository rep, IMemoryCache memoryCache) : base(rep) + { + _memoryCache = memoryCache; + } + + public void CreateBaseCode() + { + _rep.Context.CodeFirst.InitTables(typeof(BaseCode)); + } + + + public List QuListCache() + { + string key = "QuListCache"; + if (_memoryCache.TryGetValue>(key, out var list)) + { + return list; + } + var entity = Query(); + _memoryCache.Set(key, entity, TimeSpan.FromMinutes(5)); + + return entity; + } + + /// + /// 根据Code去查询单个数据 + /// + /// + /// + public BaseCode QueryCode(string code) + { + return Query(x => x.Code == code).First(); + } + } +} \ No newline at end of file diff --git a/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs b/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs new file mode 100644 index 0000000..32ee284 --- /dev/null +++ b/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; + +namespace SlnMesnac.Repository.service +{ + public interface IBaseCodeService:IBaseService + { + void CreateBaseCode(); + List QuListCache(); + BaseCode QueryCode(string code); + } +} \ 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/Back.cs b/SlnMesnac.TouchSocket/Back.cs new file mode 100644 index 0000000..2e8df36 --- /dev/null +++ b/SlnMesnac.TouchSocket/Back.cs @@ -0,0 +1,13 @@ +using System; + +namespace SlnMesnac.TouchSocket +{ + public class Back + { + public int Code { get; set; } =200; + public string msg { get; set; } = "操作成功"; + public string timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); + } + + +} \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/ScanLogSocket.cs b/SlnMesnac.TouchSocket/ScanLogSocket.cs new file mode 100644 index 0000000..c0fa47d --- /dev/null +++ b/SlnMesnac.TouchSocket/ScanLogSocket.cs @@ -0,0 +1,30 @@ +using System; + +public class ScanLogSocket +{ + /// + /// + /// + + + 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; } +} \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/ScanStatusSocket.cs b/SlnMesnac.TouchSocket/ScanStatusSocket.cs new file mode 100644 index 0000000..4f49e1f --- /dev/null +++ b/SlnMesnac.TouchSocket/ScanStatusSocket.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; + +public class ScanStatusSocket +{ + public DateTime CreateTime + { + get + { + DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp); + return dateTimeOffset.UtcDateTime; + } + } + + public long timestamp { get; set; } + + public int data_type { get; set; } + + public int is_alarm { get; set; } + + public List device_status { get; set; } + + + public List status_txt { get; set; } + + +} \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj b/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj index 94f69ee..ae8a9c3 100644 --- a/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj +++ b/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj @@ -12,6 +12,7 @@ + diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index 70f8afc..7615732 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -1,10 +1,16 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Linq; using System.Text; +using Newtonsoft.Json.Linq; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service; +using SlnMesnac.Repository.service.ScanLog; using TouchSocket.Core; using TouchSocket.Sockets; + #region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2024 WenJY 保留所有权利。 @@ -29,23 +35,37 @@ using TouchSocket.Sockets; #endregion << 版 本 注 释 >> namespace SlnMesnac.TouchSocket { + + public class TcpServer { private ILogger _logger; private readonly TcpService _service; - /// - /// 接收客户端指令委托 - /// - public delegate void ReceivedClientBuffer(byte[] buffer); - public event ReceivedClientBuffer? ReceivedClientBufferEvent; - public delegate void RefreshClientInfo(TcpService tcpService); - public event RefreshClientInfo? RefreshClientInfoEvent; + private readonly IScanLogService _scanService; + + private readonly IBaseCodeService _baseCodeService; + // /// + // /// 接收客户端指令委托 + // /// + // public delegate void ReceivedClientBuffer(byte[] buffer); + // public event ReceivedClientBuffer? ReceivedClientBufferEvent; + // + // public delegate void RefreshClientInfo(TcpService tcpService); + // public event RefreshClientInfo? RefreshClientInfoEvent; - public TcpServer(ILogger logger,TcpService tcpService) + + + public Action? ScanLogSocketAction; + public Action? ScanStatusSocketAction; + + + public TcpServer(ILogger logger,TcpService tcpService, IScanLogService scan, IBaseCodeService baseCodeService) { _logger = logger; _service = tcpService; + _scanService = scan; + _baseCodeService = baseCodeService; } public void Init(int serverPort) @@ -53,28 +73,84 @@ namespace SlnMesnac.TouchSocket try { _service.Connecting = (client, e) => { - _logger.LogInformation($"客户端{client.IP}正在接入服务"); + _logger.LogInformation("客户端{ClientIp}正在接入服务", client.IP); return EasyTask.CompletedTask; }; _service.Connected = (client, e) => { - _logger.LogInformation($"客户端{client.IP}接入服务成功"); - RefreshClientInfoEvent?.Invoke(_service); + _logger.LogInformation("客户端{ClientIp}接入服务成功", client.IP); + // RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; _service.Disconnected = (client, e) => { - _logger.LogInformation($"客户端{client.IP}断开连接"); - RefreshClientInfoEvent?.Invoke(_service); + _logger.LogInformation("客户端{ClientIp}断开连接", client.IP); + // RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; _service.Received = (client, e) => { - //从客户端收到信息 - var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len - byte[] receivedBuffer = new byte[e.ByteBlock.Len]; - Array.Copy(e.ByteBlock.Buffer, 0, receivedBuffer, 0, e.ByteBlock.Len); - ReceivedClientBufferEvent?.Invoke(receivedBuffer); + try + { + var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len + _logger.LogInformation("收到数据:{Data}",mes); + // byte[] receivedBuffer = new byte[e.ByteBlock.Len]; + // Array.Copy(e.ByteBlock.Buffer, 0, receivedBuffer, 0, e.ByteBlock.Len); + // ReceivedClientBufferEvent?.Invoke(receivedBuffer); + + int dataType = 0; + var jObject = JObject.Parse(mes); + if (jObject.TryGetValue("data_type", out var value)) + { + dataType=value.Value(); + } + + if (dataType == 1) + { + + ScanLogSocket scanStatus= mes.FromJsonString(); + 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 + + }); + + + } + else + { + if (dataType == 2) + { + ScanStatusSocket scanStatus = mes.FromJsonString(); + ScanStatusSocketAction?.Invoke(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; + } + } + } + client.SendAsync(new Back().ToJsonString()); + } + catch (Exception exception) + { + _logger.LogError(exception,exception.Message); + } return EasyTask.CompletedTask; }; 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/Startup.cs b/SlnMesnac.WPF/Startup.cs index 502d594..f097aa8 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -26,7 +26,7 @@ namespace SlnMesnac.WPF public void ConfigureServices(IServiceCollection services) { services.AddControllers(); - + services.AddMemoryCache(); //注册AppConfig services.AddSingleton(provider => { diff --git a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs index 84f3401..1e5d543 100644 --- a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs @@ -78,7 +78,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)); diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index 93c9b42..ee2a6a3 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -12,13 +12,8 @@ "SqlConfig": [ { "configId": "mes", - "dbType": 1, - "connStr": "server=.;uid=sa;pwd=123456;database=JiangYinMENS" - }, - { - "configId": "mcs", - "dbType": 3, - "connStr": "Data Source=175.27.215.92/helowin;User ID=aucma_scada;Password=aucma" + "dbType": 4, + "connStr": "PORT=5433;DATABASE=daxing;HOST=127.0.0.1;PASSWORD=123456;USER ID=postgres" } ], "PlcConfig": [