Learn.VideoAnalysis/VideoAnalysisCore/Common/Expand/AppConfigExpand.cs

107 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Coravel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using VideoAnalysisCore.Common;
using VideoAnalysisCore.Job;
namespace VideoAnalysisCore.Common.Expand
{
public static class AppConfigExpand
{
public static ushort GenerateIdFromIpPower()
{
var mac = NetworkInterface.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up && n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(n => n.GetPhysicalAddress())
.FirstOrDefault(addr => addr.GetAddressBytes().Length > 0);
if (mac == null) return 0;
byte[] bytes = mac.GetAddressBytes();
// 取 MAC 地址的最后两个字节生成 ushort
return (ushort)((bytes[^2] << 8) | bytes[^1]);
}
public static ushort GetPersistentDeviceId()
{
string path = "device_id.config";
if (File.Exists(path))
{
return ushort.Parse(File.ReadAllText(path));
}
// 首次启动,生成 ID (比如用你之前的 IP 幂运算法)
ushort newId = GenerateIdFromIpPower();
File.WriteAllText(path, newId.ToString());
return newId;
}
public static void AddAppConfig(this IHostApplicationBuilder hb, string[] args)
{
Console.WriteLine($"{DateTime.Now}=>初始化 AppConfig");
hb.Configuration.GetSection("AppConfig").Bind(AppCommon.Config);
AppCommon.Config.ID = GetPersistentDeviceId();
var argList = args.ToList();
var eArgs = Environment.GetEnvironmentVariable("va_args");
if (!string.IsNullOrEmpty(eArgs))
argList.AddRange(eArgs.Split(","));
Console.WriteLine("===========================================");
Console.WriteLine($"设备ID{AppCommon.Config.ID}");
Console.WriteLine($"当前环境: {hb.Environment.EnvironmentName}");
Console.WriteLine("启动参数如下:");
Console.WriteLine(string.Join(',', args));
Console.WriteLine("===========================================");
if (args.Contains("IS_Server"))
AppCommon.Config.TaskSetting.IS_Server = true;
}
/// <summary>
/// 交互式环境选择函数
/// </summary>
public static void SelectEnvironment(ref string[] args)
{
// 如果没有控制台(如在某些后台服务或重定向中),则跳过交互
if (Console.IsInputRedirected) return;
Console.Title = "ASP.NET Core 环境选择器";
Console.WriteLine("==================================================");
Console.WriteLine(" 请选择程序运行环境 (直接回车默认使用开发环境)");
Console.WriteLine("==================================================");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" [1] 生产环境 (Production)");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" [2] 测试环境 (Staging)");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" [3] 开发环境 (Development)");
Console.ResetColor();
Console.WriteLine("==================================================");
Console.Write(" 请输入编号 [1/2/3] (默认 3): ");
// 获取输入
string? input = Console.ReadLine()?.Trim();
// 映射逻辑
string selectedEnv = input switch
{
"1" => Environments.Production,
"2" => Environments.Staging,
"3" => Environments.Development,
_ => Environments.Development // 输入错误或直接回车,默认走开发环境
};
// 核心:在 Build 之前覆盖环境名
args = args.Concat( new[] { "--environment", selectedEnv }).ToArray();
// 成功反馈
Console.Clear();
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($" 已确认切换至: {selectedEnv} 模式 ");
Console.ResetColor();
Console.WriteLine();
}
}
}