From 18f73cd51a33113158e8d97a657c0e82d5fb285b Mon Sep 17 00:00:00 2001 From: SoulStar Date: Tue, 28 Oct 2025 18:48:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20-=20=E5=8E=BB=E6=8E=89DI=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=9A=E5=8A=A1=E7=B1=BB=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=96=84PLC=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sln.Iot.Business/RFID01Business.cs | 82 ++++++++++++ Sln.Iot.Business/RFID02Business.cs | 49 +++++++ Sln.Iot.Business/Sln.Iot.Business.csproj | 7 + Sln.Iot.CFX/CFXHelper.cs | 6 +- Sln.Iot.Config/AppConfig.cs | 9 +- Sln.Iot.Config/AppConfigSetting.cs | 33 +++++ Sln.Iot.Config/Sln.Iot.Config.csproj | 4 +- Sln.Iot.Config/SqlConfig.cs | 23 ++-- Sln.Iot.Model/Sln.Iot.Model.csproj | 7 +- Sln.Iot.PLC/PLCConnect.cs | 124 ++++++++++++------ Sln.Iot.Repository/Sln.Iot.Repository.csproj | 1 + Sln.Iot.Repository/SqliteHelper.cs | 2 - Sln.Iot.Repository/dao/TrayRfidBinding.cs | 5 +- .../service/TrayBindingService.cs | 54 +++++++- Sln.Iot.Serilog/SerilogExtensions.cs | 6 +- Sln.Iot.Serilog/SerilogHelper.cs | 10 ++ Sln.Iot.Test/UnitTest1.cs | 1 + Sln.Iot/Program.cs | 63 ++------- Sln.Iot/Sln.Iot.csproj | 3 +- Sln.Iot/appsettings.json | 2 +- 20 files changed, 368 insertions(+), 123 deletions(-) create mode 100644 Sln.Iot.Business/RFID01Business.cs create mode 100644 Sln.Iot.Business/RFID02Business.cs create mode 100644 Sln.Iot.Config/AppConfigSetting.cs diff --git a/Sln.Iot.Business/RFID01Business.cs b/Sln.Iot.Business/RFID01Business.cs new file mode 100644 index 0000000..83aebec --- /dev/null +++ b/Sln.Iot.Business/RFID01Business.cs @@ -0,0 +1,82 @@ +using HslCommunication; +using Sln.Iot.PLC; +using Sln.Iot.Repository; +using Sln.Iot.Repository.dao; +using Sln.Iot.Repository.service; +using Sln.Iot.Serilog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sln.Iot.Business +{ + public class RFID01Business + { + private Timer _timer; + + private readonly PLCConnect _plc; + + private readonly SerilogHelper _log; + + public RFID01Business() + { + _timer = new Timer(TimerCallback, null, 0, 2000); + } + + /// + /// RFID01上料提升机数据处理流程业务刷新 + /// + /// + public void TimerCallback(object? state) + { + // 定时任务逻辑 + //读取信号 + OperateResult signalRes = _plc.ReadInt16(_plc.DeltaInstance3, "D500"); + //成功验证 + if (signalRes.IsSuccess) + { + short signalValue = signalRes.Content; + //如果有读取信号 + if (signalValue == 1001) + { + //读取托盘码和产品码 + OperateResult trayBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D5000", 20); + OperateResult pordBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D8000", 120); + //成功验证 + if (trayBytesResult.IsSuccess && pordBytesResult.IsSuccess) + { + //转换托盘吗 + string traycode = Encoding.ASCII.GetString(trayBytesResult.Content); + + byte[] prodBytes = trayBytesResult.Content; + string[] prodcode = new string[6]; + //分割转换产品码 + for (int i = 0; i < 5; i++) + { + prodcode[i] = Encoding.ASCII.GetString(prodBytes[(i * 20)..(i * 20 + 20)]); + } + //sql更新 + bool res = TrayBindingService.Instance.TrayBindingRefresh(traycode, prodcode); + if (!res) + { + _log.Error("上料提升机数据处理流程异常"); + } + //写入完成信号 + res = _plc.PlcWrite(_plc.DeltaInstance3, "D500", 1002, DataTypeEnum.UInt16).IsSuccess; + if (!res) + { + _log.Error("上料提升机写入完成信号异常"); + } + } + //流程完成 + } + } + else + { + _log.Error("上料提升机读取信号异常"); + } + } + } +} diff --git a/Sln.Iot.Business/RFID02Business.cs b/Sln.Iot.Business/RFID02Business.cs new file mode 100644 index 0000000..20f5c84 --- /dev/null +++ b/Sln.Iot.Business/RFID02Business.cs @@ -0,0 +1,49 @@ +using HslCommunication; +using Sln.Iot.PLC; +using Sln.Iot.Serilog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sln.Iot.Business +{ + public class RFID02Business + { + private Timer _timer; + + private readonly PLCConnect _plc; + + private readonly SerilogHelper _log; + + public RFID02Business() + { + _timer = new Timer(TimerCallback, null, 0, 5000); + } + + /// + /// RFID02上料提升机数据处理流程业务刷新 + /// + /// + public void TimerCallback(object? state) + { + // 定时任务逻辑 + OperateResult signalRes = _plc.ReadInt16(_plc.DeltaInstance1, "D500"); + if (signalRes.IsSuccess) + { + short signalValue = signalRes.Content; + if(signalValue == 1001) + { + OperateResult trayBytesResult = _plc.ReadBytes(_plc.DeltaInstance1, "D5000", 10); + OperateResult pordBytesResult = _plc.ReadBytes(_plc.DeltaInstance1, "D8000", 60); + if(trayBytesResult.IsSuccess && pordBytesResult.IsSuccess) + { + string traycode = "abcdefg"; + string prodcode = "1234567890"; + } + } + } + } + } +} diff --git a/Sln.Iot.Business/Sln.Iot.Business.csproj b/Sln.Iot.Business/Sln.Iot.Business.csproj index 26c138d..0ba6126 100644 --- a/Sln.Iot.Business/Sln.Iot.Business.csproj +++ b/Sln.Iot.Business/Sln.Iot.Business.csproj @@ -12,9 +12,16 @@ + + + + ..\DLL\HslCommunication.dll + + + diff --git a/Sln.Iot.CFX/CFXHelper.cs b/Sln.Iot.CFX/CFXHelper.cs index 842ade8..32e3b62 100644 --- a/Sln.Iot.CFX/CFXHelper.cs +++ b/Sln.Iot.CFX/CFXHelper.cs @@ -68,6 +68,11 @@ public class CFXHelper } } + /// + /// 请求接收处理 + /// + /// + /// private CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request) { CFXMessage response = null; @@ -102,7 +107,6 @@ public class CFXHelper /// public void PublishEvent(CFXEnvelope env) { - try { if (_endpoint == null) diff --git a/Sln.Iot.Config/AppConfig.cs b/Sln.Iot.Config/AppConfig.cs index 2530742..2514e82 100644 --- a/Sln.Iot.Config/AppConfig.cs +++ b/Sln.Iot.Config/AppConfig.cs @@ -22,8 +22,7 @@ *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> - -using System.Collections.Generic; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; namespace Sln.Iot.Config @@ -35,11 +34,7 @@ namespace Sln.Iot.Config /// public string logPath { get; set; } - /// - /// Sql连接配置 - /// - public List sqlConfig { get; set; } - + public List PLCConfig { get; set; } public AppConfig Value => this; } diff --git a/Sln.Iot.Config/AppConfigSetting.cs b/Sln.Iot.Config/AppConfigSetting.cs new file mode 100644 index 0000000..5956321 --- /dev/null +++ b/Sln.Iot.Config/AppConfigSetting.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Configuration; + + +namespace Sln.Iot.Config +{ + public class AppConfigSetting + { + private static AppConfig? _config; + + /// + /// 加载配置文件并解析为 AppConfig 对象。 + /// + public static AppConfig Load() + { + if (_config != null) + return _config; // 已经加载过就直接返回(模拟单例) + + var configBuilder = new ConfigurationBuilder() + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + + IConfiguration configuration = configBuilder.Build(); + _config = configuration.GetSection("AppConfig").Get(); + + if (_config == null) + { + throw new InvalidOperationException("无法从 appsettings.json 加载 AppConfig 配置。"); + } + + return _config; + } + } +} diff --git a/Sln.Iot.Config/Sln.Iot.Config.csproj b/Sln.Iot.Config/Sln.Iot.Config.csproj index 2a5999f..82fdeb8 100644 --- a/Sln.Iot.Config/Sln.Iot.Config.csproj +++ b/Sln.Iot.Config/Sln.Iot.Config.csproj @@ -7,7 +7,9 @@ - + + + diff --git a/Sln.Iot.Config/SqlConfig.cs b/Sln.Iot.Config/SqlConfig.cs index 33ba963..1541b46 100644 --- a/Sln.Iot.Config/SqlConfig.cs +++ b/Sln.Iot.Config/SqlConfig.cs @@ -25,26 +25,21 @@ namespace Sln.Iot.Config { - public class SqlConfig + public class PLCConfig { /// - /// Sql 配置ID,实体通过该ID关联数据源 + /// PLCIP地址 /// - public string configId { get; set; } - + public string IPAddress { get; set; } + /// - /// 数据库类型,MySql-0;SqlServer-1;Sqlite-2;Oracle-3 + /// PLC端口号 /// - public int dbType { get; set; } - + public string Port { get; set; } + /// - /// 是否启用:true-是;false-否 + /// 站号 /// - public bool isFlag{get;set;} - - /// - /// 连接字符串 - /// - public string connStr { get; set; } + public int StationNo { get; set; } } } \ No newline at end of file diff --git a/Sln.Iot.Model/Sln.Iot.Model.csproj b/Sln.Iot.Model/Sln.Iot.Model.csproj index 72a2ff0..e7b0c62 100644 --- a/Sln.Iot.Model/Sln.Iot.Model.csproj +++ b/Sln.Iot.Model/Sln.Iot.Model.csproj @@ -6,13 +6,18 @@ enable + + + + + + - diff --git a/Sln.Iot.PLC/PLCConnect.cs b/Sln.Iot.PLC/PLCConnect.cs index 1c89343..25f2829 100644 --- a/Sln.Iot.PLC/PLCConnect.cs +++ b/Sln.Iot.PLC/PLCConnect.cs @@ -1,26 +1,64 @@ using HslCommunication; using HslCommunication.Profinet.Delta; -using Microsoft.Extensions.DependencyInjection; -using Serilog; using Sln.Iot.Serilog; +using System; +using System.Drawing; namespace Sln.Iot.PLC { public class PLCConnect { - private SerilogHelper? LogHelper; + private static readonly Lazy lazy = new Lazy(() => new PLCConnect()); - public static DeltaSerialOverTcp DeltaInstance1; + public static PLCConnect Instance + { + get + { + return lazy.Value; + } + } + + private readonly SerilogHelper _log = SerilogHelper.Instance; + + /// + /// PLC1 192.168.1.21 + /// + public DeltaSerialOverTcp DeltaInstance1; + + /// + /// PLC2 192.168.1.22 + /// + public DeltaSerialOverTcp DeltaInstance2; + + /// + /// PLC3 192.168.1.23 + /// + public DeltaSerialOverTcp DeltaInstance3; + + /// + /// PLC4 192.168.1.24 + /// + public DeltaSerialOverTcp DeltaInstance4; /// /// 锁对象1 /// private static readonly object locker1 = new object(); - public PLCConnect(SerilogHelper serilogHelper) + public async void InitConnect() { - LogHelper = serilogHelper; - DeltaInstance1 = CreateDeltaConnect("127.0.0.1", 502, 1, serilogHelper); + List> tasks = new List> + { + CreateDeltaConnect("192.168.1.21", 502, 1), + CreateDeltaConnect("192.168.1.22", 502, 1), + CreateDeltaConnect("192.168.1.23", 502, 1), + CreateDeltaConnect("192.168.1.24", 502, 1) + }; + + DeltaInstance1 = tasks[0].GetAwaiter().GetResult(); + DeltaInstance2 = tasks[1].GetAwaiter().GetResult(); + DeltaInstance3 = tasks[2].GetAwaiter().GetResult(); + DeltaInstance4 = tasks[3].GetAwaiter().GetResult(); } /// @@ -29,31 +67,43 @@ namespace Sln.Iot.PLC /// /// /// - /// /// - public static DeltaSerialOverTcp CreateDeltaConnect(string ip, int port, byte stationNo, SerilogHelper log) + public async Task CreateDeltaConnect(string ip, int port, byte stationNo) { DeltaSerialOverTcp plc = new DeltaSerialOverTcp(ip, port, stationNo); try { - var res = plc.ConnectServer(); + OperateResult res = new OperateResult(); + res.IsSuccess = true; + int count = 0; + do + { + if (res.IsSuccess == false) + { + count++; + _log.Error($"PLC[{ip}]连接失败,正在进行第[{count}]次重连..."); + } + res = await plc.ConnectServerAsync(); + } + while (!res.IsSuccess); + } catch (Exception ex) { - log.Error($"连接PLC[{ip}]失败", ex); + _log.Error($"连接PLC[{ip}]失败", ex); } return plc; } /// - /// PLC2写入数据 + /// PLC写入数据 /// /// 地址 /// 值 /// 数据类型 /// - public static OperateResult PlcWrite(string address, object value, DataTypeEnum type) + public OperateResult PlcWrite(DeltaSerialOverTcp connect, string address, object value, DataTypeEnum type) { var result = new OperateResult() { IsSuccess = false }; @@ -62,37 +112,37 @@ namespace Sln.Iot.PLC switch (type) { case DataTypeEnum.Bool: - result = DeltaInstance1.Write(address, Convert.ToBoolean(value)); + result = connect.Write(address, Convert.ToBoolean(value)); break; case DataTypeEnum.Byte: - result = DeltaInstance1.Write(address, Convert.ToByte(value)); + result = connect.Write(address, Convert.ToByte(value)); break; case DataTypeEnum.Int16: - result = DeltaInstance1.Write(address, Convert.ToInt16(value)); + result = connect.Write(address, Convert.ToInt16(value)); break; case DataTypeEnum.UInt16: - result = DeltaInstance1.Write(address, Convert.ToUInt16(value)); + result = connect.Write(address, Convert.ToUInt16(value)); break; case DataTypeEnum.Int32: - result = DeltaInstance1.Write(address, Convert.ToInt32(value)); + result = connect.Write(address, Convert.ToInt32(value)); break; case DataTypeEnum.UInt32: - result = DeltaInstance1.Write(address, Convert.ToUInt32(value)); + result = connect.Write(address, Convert.ToUInt32(value)); break; case DataTypeEnum.Int64: - result = DeltaInstance1.Write(address, Convert.ToInt64(value)); + result = connect.Write(address, Convert.ToInt64(value)); break; case DataTypeEnum.UInt64: - result = DeltaInstance1.Write(address, Convert.ToUInt64(value)); + result = connect.Write(address, Convert.ToUInt64(value)); break; case DataTypeEnum.Float: - result = DeltaInstance1.Write(address, Convert.ToSingle(value)); + result = connect.Write(address, Convert.ToSingle(value)); break; case DataTypeEnum.Double: - result = DeltaInstance1.Write(address, Convert.ToDouble(value)); + result = connect.Write(address, Convert.ToDouble(value)); break; case DataTypeEnum.String: - result = DeltaInstance1.Write(address, Convert.ToString(value)); + result = connect.Write(address, Convert.ToString(value)); break; default: throw new ArgumentException($"Unsupported data type: {type}"); @@ -107,11 +157,11 @@ namespace Sln.Iot.PLC /// /// /// - public static OperateResult PlcWriteBytes(string address, byte[] data) + public OperateResult PlcWriteBytes(DeltaSerialOverTcp connect, string address, byte[] data) { lock (locker1) { - var res = DeltaInstance1.Write(address, data); + var res = connect.Write(address, data); return res; } } @@ -120,11 +170,11 @@ namespace Sln.Iot.PLC /// 读取int32 /// /// - public OperateResult ReadInt32(string address) + public OperateResult ReadInt32(DeltaSerialOverTcp connect, string address) { lock (locker1) { - var res = DeltaInstance1.ReadInt32(address); + var res = connect.ReadInt32(address); return res; } } @@ -133,11 +183,11 @@ namespace Sln.Iot.PLC /// 读取int16 /// /// - public OperateResult ReadInt16(string address) + public OperateResult ReadInt16(DeltaSerialOverTcp connect, string address) { lock (locker1) { - var res = DeltaInstance1.ReadInt16(address); + var res = connect.ReadInt16(address); return res; } } @@ -147,11 +197,11 @@ namespace Sln.Iot.PLC /// /// /// - public OperateResult ReadFloat(string address) + public OperateResult ReadFloat(DeltaSerialOverTcp connect, string address) { lock (locker1) { - var res = DeltaInstance1.ReadFloat(address); + var res = connect.ReadFloat(address); return res; } } @@ -161,11 +211,11 @@ namespace Sln.Iot.PLC /// /// /// - public OperateResult ReadBool(string address) + public OperateResult ReadBool(DeltaSerialOverTcp connect, string address) { lock (locker1) { - var res = DeltaInstance1.ReadBool(address); + var res = connect.ReadBool(address); return res; } } @@ -176,15 +226,15 @@ namespace Sln.Iot.PLC /// /// /// - public static OperateResult ReadByte2(string address, ushort length) + public OperateResult ReadBytes(DeltaSerialOverTcp connect, string address, ushort length) { lock (locker1) { - var res = DeltaInstance1.Read(address, length); + var res = connect.Read(address, length); return res; } } - + } } diff --git a/Sln.Iot.Repository/Sln.Iot.Repository.csproj b/Sln.Iot.Repository/Sln.Iot.Repository.csproj index f09ebd2..2857a41 100644 --- a/Sln.Iot.Repository/Sln.Iot.Repository.csproj +++ b/Sln.Iot.Repository/Sln.Iot.Repository.csproj @@ -12,6 +12,7 @@ + diff --git a/Sln.Iot.Repository/SqliteHelper.cs b/Sln.Iot.Repository/SqliteHelper.cs index 7df7334..807eb16 100644 --- a/Sln.Iot.Repository/SqliteHelper.cs +++ b/Sln.Iot.Repository/SqliteHelper.cs @@ -11,8 +11,6 @@ namespace Sln.Iot.Repository { /// /// SQLite同步方法帮助类 - /// 作者:追逐时光者 - /// 创建时间:2023年11月30日 /// /// public class SQLiteHelper where T : new() diff --git a/Sln.Iot.Repository/dao/TrayRfidBinding.cs b/Sln.Iot.Repository/dao/TrayRfidBinding.cs index 99c2592..230a174 100644 --- a/Sln.Iot.Repository/dao/TrayRfidBinding.cs +++ b/Sln.Iot.Repository/dao/TrayRfidBinding.cs @@ -8,10 +8,13 @@ using System.Threading.Tasks; namespace Sln.Iot.Repository.dao { /// - /// 数据中转表 + /// 托盘绑定实体类 /// public class TrayRfidBinding { + /// + /// 主键 + /// [PrimaryKey] public string GUID { get; set; } diff --git a/Sln.Iot.Repository/service/TrayBindingService.cs b/Sln.Iot.Repository/service/TrayBindingService.cs index 30692c7..e0b5e28 100644 --- a/Sln.Iot.Repository/service/TrayBindingService.cs +++ b/Sln.Iot.Repository/service/TrayBindingService.cs @@ -1,4 +1,7 @@ -using System; +using Sln.Iot.Repository.dao; +using Sln.Iot.Serilog; +using SQLitePCL; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,8 +9,57 @@ using System.Threading.Tasks; namespace Sln.Iot.Repository.service { + /// + /// 托盘绑定服务类 + /// public class TrayBindingService { + private static readonly Lazy lazy = new Lazy(() => new TrayBindingService()); + public static TrayBindingService Instance + { + get + { + return lazy.Value; + } + } + + private SQLiteHelper _helper = SQLiteHelper.Instance; + + private SerilogHelper _log = SerilogHelper.Instance; + + /// + /// 托盘二维码重新绑定 + /// + /// + /// + /// + public bool TrayBindingRefresh(string trayCode, string[] prodCode) + { + try + { + //先删除原来的 + _helper.DeleteRange(trayCode); + //再绑定(插入)新的 + var entities = new List(); + foreach (string code in prodCode) + { + TrayRfidBinding entity = new TrayRfidBinding() + { + GUID = Guid.NewGuid().ToString("N"), + TrayCode = trayCode, + ProductionCode = code + }; + entities.Add(entity); + } + _helper.InsertRange(entities); + return true; + } + catch(Exception ex) + { + _log.Error("托盘二维码重新绑定错误", ex); + return false; + } + } } } diff --git a/Sln.Iot.Serilog/SerilogExtensions.cs b/Sln.Iot.Serilog/SerilogExtensions.cs index 8dc9f59..6733231 100644 --- a/Sln.Iot.Serilog/SerilogExtensions.cs +++ b/Sln.Iot.Serilog/SerilogExtensions.cs @@ -33,13 +33,11 @@ namespace Sln.Iot.Serilog { public static class SerilogExtensions { - public static void UseSerilogExtensions(this IServiceProvider service) + public static void UseSerilogExtensions() { //启用Serilog中间件 - - #region 通过配置文件读取日志存放位置 - var appConfig = service.GetService(); + var appConfig = AppConfigSetting.Load(); var logPath = Path.Combine(appConfig.logPath, "Logs"); #endregion diff --git a/Sln.Iot.Serilog/SerilogHelper.cs b/Sln.Iot.Serilog/SerilogHelper.cs index bb38ae6..b34d91c 100644 --- a/Sln.Iot.Serilog/SerilogHelper.cs +++ b/Sln.Iot.Serilog/SerilogHelper.cs @@ -30,6 +30,16 @@ namespace Sln.Iot.Serilog { public class SerilogHelper { + private static readonly Lazy lazy = new Lazy(() => new SerilogHelper()); + + public static SerilogHelper Instance + { + get + { + return lazy.Value; + } + } + private readonly ILogger? Info_logger = Log.ForContext("Module", "Info"); private readonly ILogger? Iot_logger = Log.ForContext("Module", "Iot"); private readonly ILogger? Error_logger = Log.ForContext("Module", "Error"); diff --git a/Sln.Iot.Test/UnitTest1.cs b/Sln.Iot.Test/UnitTest1.cs index 723ac92..bd1bad3 100644 --- a/Sln.Iot.Test/UnitTest1.cs +++ b/Sln.Iot.Test/UnitTest1.cs @@ -49,6 +49,7 @@ namespace Sln.Iot.Test }, }; + var ress = SQLiteHelper.Instance.InsertRange(testDatas); var res = SQLiteHelper.Instance.DeleteRange("Tray001"); Assert.Equal(5, res.Count); diff --git a/Sln.Iot/Program.cs b/Sln.Iot/Program.cs index a69744b..f3219d0 100644 --- a/Sln.Iot/Program.cs +++ b/Sln.Iot/Program.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Sln.Iot.Config; +using Sln.Iot.PLC; using Sln.Iot.Repository; using Sln.Iot.Serilog; using TouchSocket.Sockets; @@ -9,66 +10,24 @@ using TouchSocket.Sockets; namespace Sln.Iot { /// - /// + /// 程序主入口 /// - internal class Program + public class Program { - public static IServiceProvider? ServiceProvider = null; - static async Task Main(string[] args) { - var services = new ServiceCollection(); - ConfigureServices(services); - ServiceProvider = services.BuildServiceProvider(); - ServiceProvider.UseSerilogExtensions(); + //Serilog中间件 + SerilogExtensions.UseSerilogExtensions(); + //日志实例 + var log = SerilogHelper.Instance; + //配置文件加载 + var appConfig = AppConfigSetting.Load(); + + PLCConnect.Instance.InitConnect(); - var appConfig = ServiceProvider.GetService(); - var log = ServiceProvider.GetService(); log.Info($"系统启动成功,日志存放位置:{appConfig.logPath}"); await Task.Delay(-1); } - - private static void ConfigureServices(IServiceCollection services) - { - services.AddSingleton(provider => - { - var configurationBuilder = new ConfigurationBuilder() - .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); - IConfiguration configuration = configurationBuilder.Build(); - var ap = configuration.GetSection("AppConfig").Get(); - return ap; - }); - - //services.AddSingleton(provider => - //{ - // TcpClient tcpClient = new TcpClient(); - // tcpClient.Setup(new TouchSocketConfig() - // .SetRemoteIPHost("127.0.0.1:6000") - // .ConfigureContainer(a => - // { - // a.AddConsoleLogger(); - // })); - // tcpClient.Connect(); - // return tcpClient; - //}); - - Assembly[] assemblies = - { - Assembly.LoadFrom("Sln.Iot.Repository.dll"), - Assembly.LoadFrom("Sln.Iot.Socket.dll"), - Assembly.LoadFrom("Sln.Iot.Common.dll"), - Assembly.LoadFrom("Sln.Iot.Business.dll"), - }; - - services.Scan(scan => scan.FromAssemblies(assemblies) - .AddClasses() - .AsImplementedInterfaces() - .AsSelf() - .WithSingletonLifetime()); - - services.AddSingleton(typeof(SerilogHelper)); - } } } \ No newline at end of file diff --git a/Sln.Iot/Sln.Iot.csproj b/Sln.Iot/Sln.Iot.csproj index ad97bf8..a6b2f86 100644 --- a/Sln.Iot/Sln.Iot.csproj +++ b/Sln.Iot/Sln.Iot.csproj @@ -12,13 +12,14 @@ + - + diff --git a/Sln.Iot/appsettings.json b/Sln.Iot/appsettings.json index 880627a..d080ff9 100644 --- a/Sln.Iot/appsettings.json +++ b/Sln.Iot/appsettings.json @@ -1,6 +1,6 @@ { "AppConfig": { - "logPath": "\\\\Mac\\Home\\Public\\WorkSpace\\Mesnac\\项目资料\\IOT物联网数据采集\\日志信息", + "logPath": "D:\\log", "listernPort": 6000, "virtualFlag": true, "virtualValue": 9999999,