From ae5f6468646732fffd9c395cfc9d1a36299aa448 Mon Sep 17 00:00:00 2001 From: liuwf Date: Wed, 9 Oct 2024 14:10:44 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0=E6=9C=97=E8=AF=BB?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=B8=9A=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ReadService/Program.cs | 38 ++++++ ReadService/ReadBusiness.cs | 83 +++++++++++++ ReadService/ReadService.csproj | 39 +++++++ ReadService/Startup.cs | 109 ++++++++++++++++++ ReadService/appsettings.json | 77 +++++++++++++ SlnMesnac.Common/SlnMesnac.Common.csproj | 1 + SlnMesnac.Common/SpeechStr.cs | 65 +++++++++++ SlnMesnac.Model/domain/MesProductReadInfo.cs | 69 +++++++++++ .../service/IMesProductReadInfoService.cs | 13 +++ .../Impl/MesProductReadInfoServiceImpl.cs | 42 +++++++ SlnMesnac.sln | 11 ++ 11 files changed, 547 insertions(+) create mode 100644 ReadService/Program.cs create mode 100644 ReadService/ReadBusiness.cs create mode 100644 ReadService/ReadService.csproj create mode 100644 ReadService/Startup.cs create mode 100644 ReadService/appsettings.json create mode 100644 SlnMesnac.Common/SpeechStr.cs create mode 100644 SlnMesnac.Model/domain/MesProductReadInfo.cs create mode 100644 SlnMesnac.Repository/service/IMesProductReadInfoService.cs create mode 100644 SlnMesnac.Repository/service/Impl/MesProductReadInfoServiceImpl.cs diff --git a/ReadService/Program.cs b/ReadService/Program.cs new file mode 100644 index 0000000..343061c --- /dev/null +++ b/ReadService/Program.cs @@ -0,0 +1,38 @@ +using Autofac.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Serilog; +namespace ReadService +{ + public class Program + { + + static void Main(string[] args) + { + var host = CreateHostBuilder(args).Build(); + host.Run(); + + //ReadBusiness.ListenRead(); + } + + + /// + /// CreateHostBuilder + /// + /// + /// + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseSerilog() + .UseServiceProviderFactory(new AutofacServiceProviderFactory()) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} \ No newline at end of file diff --git a/ReadService/ReadBusiness.cs b/ReadService/ReadBusiness.cs new file mode 100644 index 0000000..095cf8b --- /dev/null +++ b/ReadService/ReadBusiness.cs @@ -0,0 +1,83 @@ +using Microsoft.Extensions.Hosting; +using Serilog; +using SlnMesnac.Common; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ReadService +{ + public class ReadBusiness : IHostedService + { + + private IMesProductReadInfoService _mesProductReadInfoService; + + + public ReadBusiness(IMesProductReadInfoService mesProductReadInfoService) + { + _mesProductReadInfoService = mesProductReadInfoService; + } + + + public Task StartAsync(CancellationToken cancellationToken) + { + // 启动你的监听逻辑 + ListenRead(); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + // 对于清理工作,可以在这里实现 + return Task.CompletedTask; + } + + + + public void ListenRead() + { + SpeechStr.Instance.Speak("开始监听朗读业务"); + Console.WriteLine("开始监听朗读业务"); + Task.Run(() => + { + + while (true) + { + try + { + // Log.Information("读取业务"); + MesProductReadInfo? mesProductReadInfo = _mesProductReadInfoService.Query(x => x.UseFlag == 1 && x.ReadStatus != 2).OrderBy(x => x.ReadId).FirstOrDefault(); + if (mesProductReadInfo != null) + { + Log.Information($"总次数:{mesProductReadInfo.ReadTotal} 朗读第次数{mesProductReadInfo.ReadCount} 开始朗读:====>{mesProductReadInfo.ReadInfo}"); + SpeechStr.Instance.Speak(mesProductReadInfo.ReadInfo); + mesProductReadInfo.ReadCount++; + mesProductReadInfo.ReadStatus = 1; + mesProductReadInfo.UpdateTime = DateTime.Now; + if (mesProductReadInfo.ReadCount >= mesProductReadInfo.ReadTotal) + { + mesProductReadInfo.ReadStatus = 2; + mesProductReadInfo.EndTime = DateTime.Now; + } + _mesProductReadInfoService.Update(mesProductReadInfo); + Thread.Sleep(1500); + } + else + { + Thread.Sleep(1000 * 10); + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + } + }); + } + + } +} diff --git a/ReadService/ReadService.csproj b/ReadService/ReadService.csproj new file mode 100644 index 0000000..e702a31 --- /dev/null +++ b/ReadService/ReadService.csproj @@ -0,0 +1,39 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ReadService/Startup.cs b/ReadService/Startup.cs new file mode 100644 index 0000000..752a072 --- /dev/null +++ b/ReadService/Startup.cs @@ -0,0 +1,109 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SlnMesnac.Config; +using SlnMesnac.Serilog; +using System; +using Autofac; +using Microsoft.Extensions.Configuration; +using SlnMesnac.Ioc; +using SlnMesnac.Extensions; +using SlnMesnac.Generate; +using SlnMesnac.TouchSocket; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:ConsoleApp +* 唯一标识:04133b44-1390-4f6a-aaef-88f4127d88f0 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-23 18:06:27 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace ConsoleApp +{ + /// + /// + /// + public class Startup + { + /// + /// + /// + /// + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + /// + /// + /// + public IConfiguration Configuration { get; } + + /// + /// This method gets called by the runtime. Use this method to add services to the container. + /// + /// + [Obsolete] + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + + + //注册AppConfig + services.AddSingleton(provider => + { + var configuration = provider.GetService(); + return configuration.GetSection("AppConfig").Get(); + + }); + + //注册ORM + services.AddSqlSugarSetup(); + + //// 注册 ElectricBusiness + //services.AddScoped(); + + } + + /// + /// AutoFac自动注入 + /// + /// + public void ConfigureContainer(ContainerBuilder builder) + { + DependencyConfigurator.Configure(builder); + } + + /// + /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + /// + /// + /// + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + + //启用Serilog中间件 + app.UseSerilogExtensions(); + + + + + } + } +} \ No newline at end of file diff --git a/ReadService/appsettings.json b/ReadService/appsettings.json new file mode 100644 index 0000000..16cfa9b --- /dev/null +++ b/ReadService/appsettings.json @@ -0,0 +1,77 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "AppConfig": { + "logPath": "E:\\c#\\京源环保\\程序设计\\生产控制\\SlnMesnac\\SlnMesnac.WPF\\bin\\Debug\\net6.0-windows", + "stationId": 11, + "SqlConfig": [ + { + "configId": "mes", + "dbType": 0, + // "connStr": "Data Source=175.27.215.92;Port=3306;Initial Catalog=hwjy-cloud;uid=root;pwd=haiwei@123;Charset=utf8mb4;SslMode=none" + "connStr": "Data Source=172.16.12.100;Port=3306;Initial Catalog=hwjy-cloud;uid=root;pwd=JyhbRk@123456;Charset=utf8mb4;SslMode=none" + } + //{ + // "configId": "local", + // "dbType": 2, + // "connStr": "DataSource=E:\\c#\\京源环保\\程序设计\\生产控制\\SlnMesnac\\SlnMesnac.WPF\\bin\\Debug\\net6.0-windows\\data\\data.db" + //} + //{ + // "configId": "mesTD", + // "dbType": 17, + // "connStr": "Host=175.27.215.92;Port=6030;Username=root;Password=taosdata;Database=db_hwmes" + //}, + + //{ + // "configId": "scada", + // "dbType": 0, + // "connStr": "Data Source=175.27.215.92;Port=3306;Initial Catalog=jy-scada;uid=root;pwd=haiwei@123;Charset=utf8mb4;SslMode=none" + //} + ], + "PlcConfig": [ + { + "configId": 1, + "plcType": "SiemensPlc", + "plcIp": "192.168.2.220", + // "plcIp": "127.0.0.1", + "plcPort": 102, + "plcKey": "plc", + "isFlage": true + }, + { + "configId": 2, //MES信号处理 + "plcType": "SiemensPlc", + "plcIp": "127.0.0.1", + "plcPort": 103, + "plcKey": "mesSingal", + "isFlage": false + } + ], + "RfidConfig": [ + { + // 小包码垛位RFID + "configId": 1, + "equipIp": "192.168.2.27", + "equipPort": 20108, + "equipKey": "secondFloorPallet", + "isFlage": true + }, + { + // 小包出口位RFID + "configId": 2, + "equipIp": "192.168.2.28", + "equipPort": 20108, + "equipKey": "secondFloorOut", + "isFlage": true + } + ] + //"redisConfig": "175.27.215.92:6379,password=redis@2023" + + } +} diff --git a/SlnMesnac.Common/SlnMesnac.Common.csproj b/SlnMesnac.Common/SlnMesnac.Common.csproj index 1b54830..cabb13c 100644 --- a/SlnMesnac.Common/SlnMesnac.Common.csproj +++ b/SlnMesnac.Common/SlnMesnac.Common.csproj @@ -11,6 +11,7 @@ + diff --git a/SlnMesnac.Common/SpeechStr.cs b/SlnMesnac.Common/SpeechStr.cs new file mode 100644 index 0000000..419ced2 --- /dev/null +++ b/SlnMesnac.Common/SpeechStr.cs @@ -0,0 +1,65 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Speech.Synthesis; +using System.Text; +using System.Threading.Tasks; + + +namespace SlnMesnac.Common +{ + public class SpeechStr + { + private static readonly Lazy lazy = new Lazy(() => new SpeechStr()); + private readonly SpeechSynthesizer synthesizer; + + + public static SpeechStr Instance + { + get + { + return lazy.Value; + } + } + + + public SpeechStr() + { + synthesizer = new SpeechSynthesizer(); + // 可选:配置语音合成器 + synthesizer.Rate = 1; // 设置语速 + synthesizer.Volume = 100; // 设置音量 + } + + // 异步播放文本的方法 + public async Task SpeakAsync(string text) + { + if (string.IsNullOrEmpty(text)) + { + throw new ArgumentException("Text cannot be null or empty.", nameof(text)); + } + + await Task.Run(() => synthesizer.SpeakAsync(text)); + } + + // 同步播放文本的方法 + public void Speak(string text) + { + if (string.IsNullOrEmpty(text)) + { + throw new ArgumentException("Text cannot be null or empty.", nameof(text)); + } + + synthesizer.Speak(text); + } + + public void StopSpeaking() + { + synthesizer.SpeakAsyncCancelAll(); + } + } + + + +} diff --git a/SlnMesnac.Model/domain/MesProductReadInfo.cs b/SlnMesnac.Model/domain/MesProductReadInfo.cs new file mode 100644 index 0000000..1f83c24 --- /dev/null +++ b/SlnMesnac.Model/domain/MesProductReadInfo.cs @@ -0,0 +1,69 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Security.Principal; +using System.Text; + +namespace SlnMesnac.Model.domain +{ + /// + /// 待朗读信息信息 + /// + [SugarTable("mes_product_read_info"), TenantAttribute("mes")] + public class MesProductReadInfo + { + /// + /// 主键ID + /// + [SugarColumn(ColumnName = "read_id", IsPrimaryKey = true, IsIdentity = true)] + public long ReadId { get; set; } + + /// + /// 待朗读信息 + /// + [SugarColumn(ColumnName = "read_info")] + public string ReadInfo { get; set; } + + /// + /// 可用标志:0-不可用,1-可朗读 + /// + [SugarColumn(ColumnName = "use_flag")] + public int? UseFlag { get; set; } + + /// + /// 朗读总次数 + /// + [SugarColumn(ColumnName = "read_total")] + public int ReadTotal { get; set; } + + /// + /// 已朗读次数 + /// + [SugarColumn(ColumnName = "read_count")] + public int? ReadCount { get; set; } + + /// + /// 朗读状态:0-未开始;1-朗读中;2-朗读结束 + /// + [SugarColumn(ColumnName = "read_status")] + public int? ReadStatus { get; set; } + + /// + /// 创建时间 + /// + [SugarColumn(ColumnName = "creat_time")] + public DateTime? CreatTime { get; set; } // datetime 对应 DateTime? + + /// + /// 更新时间 + /// + [SugarColumn(ColumnName = "update_time")] + public DateTime? UpdateTime { get; set; } // datetime 对应 DateTime? + + /// + /// 朗读结束时间 + /// + [SugarColumn(ColumnName = "end_time")] + public DateTime? EndTime { get; set; } // datetime 对应 DateTime + } +} diff --git a/SlnMesnac.Repository/service/IMesProductReadInfoService.cs b/SlnMesnac.Repository/service/IMesProductReadInfoService.cs new file mode 100644 index 0000000..89232f2 --- /dev/null +++ b/SlnMesnac.Repository/service/IMesProductReadInfoService.cs @@ -0,0 +1,13 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; +using System.Text; + +namespace SlnMesnac.Repository.service +{ + public interface IMesProductReadInfoService : IBaseService + { + + } +} diff --git a/SlnMesnac.Repository/service/Impl/MesProductReadInfoServiceImpl.cs b/SlnMesnac.Repository/service/Impl/MesProductReadInfoServiceImpl.cs new file mode 100644 index 0000000..e94133f --- /dev/null +++ b/SlnMesnac.Repository/service/Impl/MesProductReadInfoServiceImpl.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.DependencyInjection; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Repository.service.Impl +* 唯一标识:052967bb-62b3-4a80-b2b9-135493fa7a73 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-10 16:46:27 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Repository.service.Impl +{ + public class MesProductReadInfoServiceImpl : BaseServiceImpl, IMesProductReadInfoService + { + private IServiceProvider _serviceProvider; + public MesProductReadInfoServiceImpl(Repository rep, IServiceProvider serviceProvider) : base(rep) + { + _serviceProvider = serviceProvider; + } + + } +} diff --git a/SlnMesnac.sln b/SlnMesnac.sln index 6fe744e..0f719af 100644 --- a/SlnMesnac.sln +++ b/SlnMesnac.sln @@ -33,6 +33,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Extensions", "Sln EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Other", "Other", "{30102E9A-38CE-4BD6-AA98-98498DE35329}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadService", "ReadService\ReadService.csproj", "{19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -99,10 +103,17 @@ Global {DD53E273-786B-4947-8CF0-B7C472E10B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU {DD53E273-786B-4947-8CF0-B7C472E10B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU {DD53E273-786B-4947-8CF0-B7C472E10B8D}.Release|Any CPU.Build.0 = Release|Any CPU + {19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC} = {30102E9A-38CE-4BD6-AA98-98498DE35329} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D414D640-C2DE-44E2-930E-DD095881CDC1} EndGlobalSection