129 lines
4.2 KiB
C#
129 lines
4.2 KiB
C#
using NAudio.Wave;
|
||
using System.Collections.Concurrent;
|
||
using System.Runtime.InteropServices;
|
||
using System.Diagnostics;
|
||
using System.Threading;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using ShengShengBuXi.ConsoleApp.Models;
|
||
using ShengShengBuXi.ConsoleApp.Services;
|
||
|
||
namespace ShengShengBuXi.ConsoleApp;
|
||
|
||
public class Program
|
||
{
|
||
|
||
public static async Task Main(string[] args)
|
||
{
|
||
Console.WriteLine("生生不息电话亭启动中...");
|
||
|
||
try
|
||
{
|
||
// 加载应用程序设置
|
||
var appSettings = AppSettings.LoadFromFile("appsettings.json");
|
||
|
||
// 配置依赖注入
|
||
var services = ConfigureServices(appSettings);
|
||
|
||
// 获取服务
|
||
var phoneBoothService = services.GetRequiredService<IPhoneBoothService>();
|
||
var signalRService = services.GetRequiredService<ISignalRService>();
|
||
|
||
// 初始化电话亭服务
|
||
await phoneBoothService.InitializeAsync();
|
||
|
||
// 如果配置为自动连接,则连接到SignalR服务器
|
||
if (appSettings.AutoConnectToServer)
|
||
{
|
||
Console.WriteLine($"正在连接到服务器: {appSettings.SignalRHubUrl}");
|
||
int retryCount = 0;
|
||
bool connected = false;
|
||
while (retryCount < 10)
|
||
{
|
||
try
|
||
{
|
||
await signalRService.StartConnectionAsync(appSettings.SignalRHubUrl);
|
||
connected = true;
|
||
break;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
retryCount++;
|
||
Console.WriteLine($"连接失败 (第{retryCount}次): {ex.Message}");
|
||
if (retryCount >= 10)
|
||
{
|
||
if (!appSettings.AllowOfflineStart)
|
||
{
|
||
Console.WriteLine("达到最大重试次数,程序将退出");
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("达到最大重试次数,但配置允许离线启动,将继续运行");
|
||
break;
|
||
}
|
||
}
|
||
await Task.Delay(1000);
|
||
}
|
||
}
|
||
|
||
if (!connected && !appSettings.AllowOfflineStart)
|
||
{
|
||
Console.WriteLine("无法连接到服务器且不允许离线启动,程序将退出");
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 启动电话亭服务
|
||
await phoneBoothService.StartAsync();
|
||
|
||
// 注册Ctrl+C处理
|
||
var cts = new CancellationTokenSource();
|
||
Console.CancelKeyPress += (s, e) =>
|
||
{
|
||
e.Cancel = true;
|
||
cts.Cancel();
|
||
};
|
||
|
||
// 等待程序退出信号
|
||
try
|
||
{
|
||
await Task.Delay(-1, cts.Token);
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
// 正常退出
|
||
}
|
||
|
||
// 停止服务
|
||
await phoneBoothService.StopAsync();
|
||
await signalRService.StopConnectionAsync();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"程序启动失败: {ex.Message}");
|
||
Console.WriteLine("按任意键退出...");
|
||
Console.ReadKey();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 配置依赖注入服务
|
||
/// </summary>
|
||
private static ServiceProvider ConfigureServices(AppSettings appSettings)
|
||
{
|
||
var services = new ServiceCollection();
|
||
|
||
// 注册配置
|
||
services.AddSingleton(appSettings);
|
||
|
||
// 注册服务
|
||
services.AddSingleton<IAudioFileService, AudioFileService>();
|
||
services.AddSingleton<ISignalRService>(provider =>
|
||
new SignalRService(appSettings.ConfigBackupPath));
|
||
services.AddSingleton<IPhoneBoothService, PhoneBoothService>();
|
||
|
||
return services.BuildServiceProvider();
|
||
}
|
||
|
||
}
|