diff --git a/ReadService/MessageClient.cs b/ReadService/MessageClient.cs new file mode 100644 index 0000000..036119d --- /dev/null +++ b/ReadService/MessageClient.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using SlnMesnac.Common; +using SlnMesnac.Config; +using SlnMesnac.Model.domain; +using SlnMesnac.Redis; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ReadService +{ + public class MessageClient + { + private readonly RedisHandler _redisHandler; + private readonly ILogger _logger; + private readonly AppConfig _appConfig; + + public MessageClient(RedisHandler redisHandler, ILogger logger, IConfiguration configuration) + { + _redisHandler = redisHandler; + _logger = logger; + _appConfig = configuration.GetSection("AppConfig").Get(); + } + + public void StartListening() + { + Console.WriteLine("Redis启动监听朗读声音服务~~~"); + _redisHandler.SubscribeToChannel("read_messages", OnMessageReceived); + } + + private async void OnMessageReceived(string channel, string message) + { + await SpeechStr.Instance.SpeakAsync(message); + Console.WriteLine($"客户端朗读消息{message}"); + } + + } +} diff --git a/ReadService/Program.cs b/ReadService/Program.cs index 343061c..4e83bb0 100644 --- a/ReadService/Program.cs +++ b/ReadService/Program.cs @@ -28,7 +28,7 @@ namespace ReadService .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureServices((hostContext, services) => { - services.AddHostedService(); + //services.AddHostedService(); }) .ConfigureWebHostDefaults(webBuilder => { diff --git a/ReadService/Startup.cs b/ReadService/Startup.cs index 752a072..f9639fe 100644 --- a/ReadService/Startup.cs +++ b/ReadService/Startup.cs @@ -11,6 +11,8 @@ using SlnMesnac.Ioc; using SlnMesnac.Extensions; using SlnMesnac.Generate; using SlnMesnac.TouchSocket; +using ReadService; +using SlnMesnac.Redis; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- @@ -64,6 +66,8 @@ namespace ConsoleApp { services.AddControllers(); + services.AddSingleton(); + services.AddSingleton(); //注册AppConfig services.AddSingleton(provider => @@ -95,9 +99,9 @@ namespace ConsoleApp /// /// /// - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MessageClient messageClient) { - + messageClient.StartListening(); //启用Serilog中间件 app.UseSerilogExtensions(); diff --git a/ReadService/appsettings.json b/ReadService/appsettings.json index 16cfa9b..e413b00 100644 --- a/ReadService/appsettings.json +++ b/ReadService/appsettings.json @@ -14,7 +14,7 @@ { "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=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" } //{ @@ -39,7 +39,7 @@ "configId": 1, "plcType": "SiemensPlc", "plcIp": "192.168.2.220", - // "plcIp": "127.0.0.1", + // "plcIp": "127.0.0.1", "plcPort": 102, "plcKey": "plc", "isFlage": true @@ -70,8 +70,7 @@ "equipKey": "secondFloorOut", "isFlage": true } - ] - //"redisConfig": "175.27.215.92:6379,password=redis@2023" - + ], + "redisConfig": "172.16.12.100:6379,password=JyhbRk@2024" } } diff --git a/SlnMesnac.Business/MessageService.cs b/SlnMesnac.Business/MessageService.cs new file mode 100644 index 0000000..78657eb --- /dev/null +++ b/SlnMesnac.Business/MessageService.cs @@ -0,0 +1,76 @@ +using Microsoft.Data.SqlClient; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using SlnMesnac.Config; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using System.Threading; +using SlnMesnac.Redis; +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service; +using System.Linq; +using Serilog; +using SlnMesnac.Common; +using ServiceStack.Messaging; + +namespace SlnMesnac.Business +{ + public class MessageService : BackgroundService + { + private readonly RedisHandler _redisHandler; + private readonly ILogger _logger; + private readonly AppConfig _appConfig; + private IMesProductReadInfoService _mesProductReadInfoService; + + public MessageService(IMesProductReadInfoService mesProductReadInfoService,RedisHandler redisHandler, ILogger logger, IConfiguration configuration) + { + _mesProductReadInfoService = mesProductReadInfoService; + _redisHandler = redisHandler; + _logger = logger; + _appConfig = configuration.GetSection("AppConfig").Get(); + + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + try + { + 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); + _redisHandler.PublishMessage("read_messages", 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); + await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); + } + else + { + await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); // 每10秒检查一次 + // Thread.Sleep(1000 * 10); + } + + } + catch (Exception ex) + { + _logger.LogError(ex, "消息服务发生错误"); + } + } + } + } +} diff --git a/SlnMesnac.Business/SlnMesnac.Business.csproj b/SlnMesnac.Business/SlnMesnac.Business.csproj index a5a954f..41252af 100644 --- a/SlnMesnac.Business/SlnMesnac.Business.csproj +++ b/SlnMesnac.Business/SlnMesnac.Business.csproj @@ -9,6 +9,7 @@ + diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index 0933c25..a592272 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -11,6 +11,8 @@ using SlnMesnac.Ioc; using SlnMesnac.Extensions; using SlnMesnac.Generate; using SlnMesnac.TouchSocket; +using SlnMesnac.Business; +using SlnMesnac.Redis; namespace SlnMesnac.WPF { @@ -27,6 +29,8 @@ namespace SlnMesnac.WPF public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(); + services.AddHostedService(); services.AddControllers(); diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index 6ab59f1..51816c0 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -14,7 +14,7 @@ { "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=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" }, { @@ -39,7 +39,7 @@ "configId": 1, "plcType": "SiemensPlc", "plcIp": "192.168.2.220", - // "plcIp": "127.0.0.1", + // "plcIp": "127.0.0.1", "plcPort": 102, "plcKey": "plc", "isFlage": true @@ -70,8 +70,8 @@ "equipKey": "secondFloorOut", "isFlage": true } - ] - //"redisConfig": "175.27.215.92:6379,password=redis@2023" - + ], + "redisConfig": "172.16.12.100:6379,password=JyhbRk@2024" + } }