change-朗读语音小程序单体修改为Redis订阅

dev
liuwf 1 month ago
parent b5d0b35fa9
commit 8f9d1c235f

@ -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<MessageClient> _logger;
private readonly AppConfig _appConfig;
public MessageClient(RedisHandler redisHandler, ILogger<MessageClient> logger, IConfiguration configuration)
{
_redisHandler = redisHandler;
_logger = logger;
_appConfig = configuration.GetSection("AppConfig").Get<AppConfig>();
}
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}");
}
}
}

@ -28,7 +28,7 @@ namespace ReadService
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) .UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
services.AddHostedService<ReadBusiness>(); //services.AddHostedService<ReadBusiness>();
}) })
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {

@ -11,6 +11,8 @@ using SlnMesnac.Ioc;
using SlnMesnac.Extensions; using SlnMesnac.Extensions;
using SlnMesnac.Generate; using SlnMesnac.Generate;
using SlnMesnac.TouchSocket; using SlnMesnac.TouchSocket;
using ReadService;
using SlnMesnac.Redis;
#region << 版 本 注 释 >> #region << 版 本 注 释 >>
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
@ -64,6 +66,8 @@ namespace ConsoleApp
{ {
services.AddControllers(); services.AddControllers();
services.AddSingleton<RedisHandler>();
services.AddSingleton<MessageClient>();
//注册AppConfig //注册AppConfig
services.AddSingleton<AppConfig>(provider => services.AddSingleton<AppConfig>(provider =>
@ -95,9 +99,9 @@ namespace ConsoleApp
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="env"></param> /// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MessageClient messageClient)
{ {
messageClient.StartListening();
//启用Serilog中间件 //启用Serilog中间件
app.UseSerilogExtensions(); app.UseSerilogExtensions();

@ -70,8 +70,7 @@
"equipKey": "secondFloorOut", "equipKey": "secondFloorOut",
"isFlage": true "isFlage": true
} }
] ],
//"redisConfig": "175.27.215.92:6379,password=redis@2023" "redisConfig": "172.16.12.100:6379,password=JyhbRk@2024"
} }
} }

@ -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<MessageService> _logger;
private readonly AppConfig _appConfig;
private IMesProductReadInfoService _mesProductReadInfoService;
public MessageService(IMesProductReadInfoService mesProductReadInfoService,RedisHandler redisHandler, ILogger<MessageService> logger, IConfiguration configuration)
{
_mesProductReadInfoService = mesProductReadInfoService;
_redisHandler = redisHandler;
_logger = logger;
_appConfig = configuration.GetSection("AppConfig").Get<AppConfig>();
}
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, "消息服务发生错误");
}
}
}
}
}

@ -9,6 +9,7 @@
<ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" /> <ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" />
<ProjectReference Include="..\SlnMesnac.Model\SlnMesnac.Model.csproj" /> <ProjectReference Include="..\SlnMesnac.Model\SlnMesnac.Model.csproj" />
<ProjectReference Include="..\SlnMesnac.Plc\SlnMesnac.Plc.csproj" /> <ProjectReference Include="..\SlnMesnac.Plc\SlnMesnac.Plc.csproj" />
<ProjectReference Include="..\SlnMesnac.Redis\SlnMesnac.Redis.csproj" />
<ProjectReference Include="..\SlnMesnac.Repository\SlnMesnac.Repository.csproj" /> <ProjectReference Include="..\SlnMesnac.Repository\SlnMesnac.Repository.csproj" />
<ProjectReference Include="..\SlnMesnac.Rfid\SlnMesnac.Rfid.csproj" /> <ProjectReference Include="..\SlnMesnac.Rfid\SlnMesnac.Rfid.csproj" />
</ItemGroup> </ItemGroup>

@ -11,6 +11,8 @@ using SlnMesnac.Ioc;
using SlnMesnac.Extensions; using SlnMesnac.Extensions;
using SlnMesnac.Generate; using SlnMesnac.Generate;
using SlnMesnac.TouchSocket; using SlnMesnac.TouchSocket;
using SlnMesnac.Business;
using SlnMesnac.Redis;
namespace SlnMesnac.WPF namespace SlnMesnac.WPF
{ {
@ -27,6 +29,8 @@ namespace SlnMesnac.WPF
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddSingleton<RedisHandler>();
services.AddHostedService<MessageService>();
services.AddControllers(); services.AddControllers();

@ -70,8 +70,8 @@
"equipKey": "secondFloorOut", "equipKey": "secondFloorOut",
"isFlage": true "isFlage": true
} }
] ],
//"redisConfig": "175.27.215.92:6379,password=redis@2023" "redisConfig": "172.16.12.100:6379,password=JyhbRk@2024"
} }
} }

Loading…
Cancel
Save