99 lines
4.0 KiB
C#
99 lines
4.0 KiB
C#
using Coravel;
|
|
using Coravel.Scheduling.Schedule;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VideoAnalysisCore.Job;
|
|
using Microsoft.Extensions.Hosting;
|
|
using VideoAnalysisCore.Common;
|
|
using System.Diagnostics;
|
|
using FreeRedis;
|
|
|
|
namespace VideoAnalysisCore.Common.Expand
|
|
{
|
|
public static class ServiceSystemExpand
|
|
{
|
|
/// <summary>
|
|
/// 系统服务
|
|
/// </summary>
|
|
/// <param name="app1"></param>
|
|
/// <param name="openBrowser">打开浏览器?</param>
|
|
public static void UseServiceSystem(this IHost app1,Action? action=null,bool openBrowser =true)
|
|
{
|
|
var app = app1.Services;
|
|
// 注册启动后的回调
|
|
app.GetRequiredService<IHostApplicationLifetime>()
|
|
.ApplicationStarted.Register(() =>
|
|
{
|
|
// 立即上线一次,防止等待 Job 调度周期的空白
|
|
var redis = AppCommon.Services.GetService<RedisClient>();
|
|
// redis?.SAdd(RedisExpandKey.OnlineDevices, AppCommon.Config.ID); // 移除
|
|
redis?.Set(RedisExpandKey.DeviceHeartbeat(AppCommon.Config.ID.ToString()), DateTime.Now.ToString(), 60);
|
|
|
|
var server = AppCommon.Services.GetRequiredService<IServer>();
|
|
var addressFeature = server.Features.Get<IServerAddressesFeature>();
|
|
Console.WriteLine("===========================================");
|
|
Console.WriteLine($"Kestrel 监听地址: ");
|
|
foreach (var address in addressFeature.Addresses)
|
|
{
|
|
Console.WriteLine($"{address}");
|
|
}
|
|
Console.WriteLine("===========================================");
|
|
// 将 "*" 替换为 "localhost" 或 "127.0.0.1",因为 Uri 类不直接支持带有通配符的 Host
|
|
var normalizedAddress = addressFeature.Addresses.First()
|
|
.Replace("*", "127.0.0.1")
|
|
.Replace("+", "127.0.0.1");
|
|
var uri = new Uri(normalizedAddress);
|
|
int port = uri.Port; // 这里的 port 就是你要的数字 (int)
|
|
if (openBrowser && OperatingSystem.IsWindows())
|
|
OpenBrowser($"http://localhost:{uri.Port}/ui/index.html");
|
|
if(action != null)
|
|
action();
|
|
});
|
|
|
|
// 注册程序停止时的回调
|
|
app.GetRequiredService<IHostApplicationLifetime>()
|
|
.ApplicationStopping.Register(() =>
|
|
{
|
|
// 注销设备下线
|
|
var redis = AppCommon.Services.GetService<RedisClient>();
|
|
// redis?.SRem(RedisExpandKey.OnlineDevices, AppCommon.Config.ID); // 移除
|
|
redis?.Del(RedisExpandKey.DeviceHeartbeat(AppCommon.Config.ID.ToString())); // 立即删除心跳Key
|
|
Console.WriteLine($"设备 {AppCommon.Config.ID} 已下线");
|
|
});
|
|
}
|
|
|
|
// 跨平台打开浏览器的方法
|
|
static void OpenBrowser(string url)
|
|
{
|
|
try
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
|
|
}
|
|
else if (OperatingSystem.IsLinux())
|
|
{
|
|
Process.Start("xdg-open", url);
|
|
}
|
|
else if (OperatingSystem.IsMacOS())
|
|
{
|
|
Process.Start("open", url);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"无法自动打开浏览器: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|