153 lines
6.1 KiB
C#
153 lines
6.1 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Threading.Tasks;
|
||
using Protocol376Simulator.CommandProcessor;
|
||
using Protocol376Simulator.Factory;
|
||
using Serilog;
|
||
|
||
namespace Protocol376Simulator
|
||
{
|
||
/// <summary>
|
||
/// 程序入口类
|
||
/// </summary>
|
||
class Program
|
||
{
|
||
private static string _serverAddress = "127.0.0.1";
|
||
private static int _serverPort = 10502; // 默认端口
|
||
private static readonly CommandHandler _commandHandler = new CommandHandler();
|
||
|
||
/// <summary>
|
||
/// 程序入口点
|
||
/// </summary>
|
||
static async Task Main(string[] args)
|
||
{
|
||
// 配置Serilog
|
||
ConfigureLogger();
|
||
|
||
Log.Information("376.1协议集中器模拟器启动");
|
||
Log.Information("--------------------------------------");
|
||
|
||
// 设置默认服务器配置
|
||
SimulatorFactory.SetServerConfig(_serverAddress, _serverPort);
|
||
Log.Information("服务器地址: {ServerAddress}:{ServerPort}", _serverAddress, _serverPort);
|
||
Log.Information("--------------------------------------");
|
||
Log.Information("输入help查看所有可用命令");
|
||
Log.Information("--------------------------------------");
|
||
|
||
// 处理命令行参数,支持自动启动
|
||
if (args.Length > 0)
|
||
{
|
||
await ProcessCommandLineArgs(args);
|
||
}
|
||
|
||
// 主循环
|
||
bool running = true;
|
||
while (running)
|
||
{
|
||
Console.Write("\n> ");
|
||
string input = Console.ReadLine();
|
||
|
||
if (string.IsNullOrWhiteSpace(input))
|
||
continue;
|
||
|
||
if (input.ToLower() == "exit" || input.ToLower() == "quit" || input == "0")
|
||
{
|
||
running = false;
|
||
continue;
|
||
}
|
||
|
||
// 处理命令
|
||
await _commandHandler.ProcessCommand(input.Split(' '));
|
||
}
|
||
|
||
// 清空所有模拟器
|
||
await SimulatorFactory.ClearAllSimulatorsAsync();
|
||
|
||
Log.Information("程序已退出");
|
||
Log.CloseAndFlush();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 配置日志记录器
|
||
/// </summary>
|
||
private static void ConfigureLogger()
|
||
{
|
||
// 确保日志目录存在
|
||
Directory.CreateDirectory("Logs");
|
||
|
||
// 配置Serilog
|
||
Log.Logger = new LoggerConfiguration()
|
||
.MinimumLevel.Information()
|
||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||
.WriteTo.File("Logs/Protocol376-.log",
|
||
rollingInterval: RollingInterval.Day,
|
||
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||
.CreateLogger();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理命令行参数
|
||
/// </summary>
|
||
private static async Task ProcessCommandLineArgs(string[] args)
|
||
{
|
||
try
|
||
{
|
||
// 示例: auto-login 10 表示自动创建10个集中器并登录
|
||
if (args[0].ToLower() == "auto-login" && args.Length > 1 && int.TryParse(args[1], out int count))
|
||
{
|
||
Log.Information("自动模式: 创建并登录 {Count} 个集中器", count);
|
||
|
||
// 使用工厂创建集中器
|
||
var addresses = SimulatorFactory.BatchCreateConcentrators(count);
|
||
|
||
// 批量连接并登录
|
||
foreach (var address in addresses)
|
||
{
|
||
var simulator = SimulatorFactory.GetSimulator(address);
|
||
if (simulator != null)
|
||
{
|
||
// 启动并自动登录,启用自动心跳
|
||
await simulator.StartAsync(true, true);
|
||
}
|
||
}
|
||
|
||
Log.Information("自动登录完成,集中器将在收到登录确认后每4分钟发送一次心跳");
|
||
}
|
||
// 示例: auto-server 127.0.0.1 8888 表示设置服务器地址和端口
|
||
else if (args[0].ToLower() == "auto-server" && args.Length > 2)
|
||
{
|
||
_serverAddress = args[1];
|
||
if (int.TryParse(args[2], out int port))
|
||
{
|
||
_serverPort = port;
|
||
}
|
||
SimulatorFactory.SetServerConfig(_serverAddress, _serverPort);
|
||
Log.Information("服务器设置已更新: {ServerAddress}:{ServerPort}", _serverAddress, _serverPort);
|
||
}
|
||
// 示例: auto-create 312003001 1 1 表示创建地址为312003001的集中器,启用自动登录和自动心跳
|
||
else if (args[0].ToLower() == "auto-create" && args.Length > 1)
|
||
{
|
||
string address = args[1];
|
||
bool autoLogin = args.Length > 2 && args[2] == "1";
|
||
bool autoHeartbeat = args.Length > 3 && args[3] == "1";
|
||
|
||
Log.Information("自动创建集中器: 地址={Address}, 自动登录={AutoLogin}, 自动心跳={AutoHeartbeat}",
|
||
address, autoLogin, autoHeartbeat);
|
||
|
||
var simulator = SimulatorFactory.CreateConcentrator(address);
|
||
await simulator.StartAsync(autoLogin, autoHeartbeat);
|
||
}
|
||
// 将命令行参数作为命令处理
|
||
else
|
||
{
|
||
await _commandHandler.ProcessCommand(args);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error(ex, "处理命令行参数时发生错误: {ErrorMessage}", ex.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|