add-添加朗读监听业务

dev
liuwf 1 month ago
parent a285df3194
commit ae5f646864

@ -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();
}
/// <summary>
/// CreateHostBuilder
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<ReadBusiness>();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<ConsoleApp.Startup>();
});
}
}

@ -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);
}
}
});
}
}
}

@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlnMesnac.Business\SlnMesnac.Business.csproj" />
<ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" />
<ProjectReference Include="..\SlnMesnac.Config\SlnMesnac.Config.csproj" />
<ProjectReference Include="..\SlnMesnac.Extensions\SlnMesnac.Extensions.csproj" />
<ProjectReference Include="..\SlnMesnac.Generate\SlnMesnac.Generate.csproj" />
<ProjectReference Include="..\SlnMesnac.Ioc\SlnMesnac.Ioc.csproj" />
<ProjectReference Include="..\SlnMesnac.Model\SlnMesnac.Model.csproj" />
<ProjectReference Include="..\SlnMesnac.Redis\SlnMesnac.Redis.csproj" />
<ProjectReference Include="..\SlnMesnac.Repository\SlnMesnac.Repository.csproj" />
<ProjectReference Include="..\SlnMesnac.Serilog\SlnMesnac.Serilog.csproj" />
</ItemGroup>
</Project>

@ -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
* CLR4.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
{
/// <summary>
///
/// </summary>
public class Startup
{
/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
///
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
[Obsolete]
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//注册AppConfig
services.AddSingleton<AppConfig>(provider =>
{
var configuration = provider.GetService<IConfiguration>();
return configuration.GetSection("AppConfig").Get<AppConfig>();
});
//注册ORM
services.AddSqlSugarSetup();
//// 注册 ElectricBusiness
//services.AddScoped<ElectricBusiness>();
}
/// <summary>
/// AutoFac自动注入
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
DependencyConfigurator.Configure(builder);
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//启用Serilog中间件
app.UseSerilogExtensions();
}
}
}

@ -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"
}
}

@ -11,6 +11,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" /> <PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" /> <PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="System.Speech" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" /> <PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup> </ItemGroup>

@ -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<SpeechStr> lazy = new Lazy<SpeechStr>(() => 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();
}
}
}

@ -0,0 +1,69 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Text;
namespace SlnMesnac.Model.domain
{
/// <summary>
/// 待朗读信息信息
/// </summary>
[SugarTable("mes_product_read_info"), TenantAttribute("mes")]
public class MesProductReadInfo
{
/// <summary>
/// 主键ID
/// </summary>
[SugarColumn(ColumnName = "read_id", IsPrimaryKey = true, IsIdentity = true)]
public long ReadId { get; set; }
/// <summary>
/// 待朗读信息
/// </summary>
[SugarColumn(ColumnName = "read_info")]
public string ReadInfo { get; set; }
/// <summary>
/// 可用标志0-不可用,1-可朗读
/// </summary>
[SugarColumn(ColumnName = "use_flag")]
public int? UseFlag { get; set; }
/// <summary>
/// 朗读总次数
/// </summary>
[SugarColumn(ColumnName = "read_total")]
public int ReadTotal { get; set; }
/// <summary>
/// 已朗读次数
/// </summary>
[SugarColumn(ColumnName = "read_count")]
public int? ReadCount { get; set; }
/// <summary>
/// 朗读状态:0-未开始1-朗读中;2-朗读结束
/// </summary>
[SugarColumn(ColumnName = "read_status")]
public int? ReadStatus { get; set; }
/// <summary>
/// 创建时间
/// </summary>
[SugarColumn(ColumnName = "creat_time")]
public DateTime? CreatTime { get; set; } // datetime 对应 DateTime?
/// <summary>
/// 更新时间
/// </summary>
[SugarColumn(ColumnName = "update_time")]
public DateTime? UpdateTime { get; set; } // datetime 对应 DateTime?
/// <summary>
/// 朗读结束时间
/// </summary>
[SugarColumn(ColumnName = "end_time")]
public DateTime? EndTime { get; set; } // datetime 对应 DateTime
}
}

@ -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<MesProductReadInfo>
{
}
}

@ -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
* CLR4.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<MesProductReadInfo>, IMesProductReadInfoService
{
private IServiceProvider _serviceProvider;
public MesProductReadInfoServiceImpl(Repository<MesProductReadInfo> rep, IServiceProvider serviceProvider) : base(rep)
{
_serviceProvider = serviceProvider;
}
}
}

@ -33,6 +33,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Extensions", "Sln
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{DD53E273-786B-4947-8CF0-B7C472E10B8D}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{19FF8FA9-3FC6-4755-9468-4B66D9D6A7EC} = {30102E9A-38CE-4BD6-AA98-98498DE35329}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D414D640-C2DE-44E2-930E-DD095881CDC1} SolutionGuid = {D414D640-C2DE-44E2-930E-DD095881CDC1}
EndGlobalSection EndGlobalSection

Loading…
Cancel
Save