You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using CncSimulator.Core;
|
|
using CncSimulator.Config;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CncSimulator
|
|
{
|
|
/// <summary>
|
|
/// CNC模拟采集服务主入口。
|
|
/// 读取配置→启动引擎→等待退出。
|
|
/// </summary>
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// 初始化log4net
|
|
log4net.Config.XmlConfigurator.Configure();
|
|
|
|
Console.WriteLine("CNC 模拟采集服务 v1.0");
|
|
Console.WriteLine("================================================");
|
|
|
|
// 读取配置
|
|
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "simulator.json");
|
|
if (!File.Exists(configPath))
|
|
{
|
|
// 尝试从项目根目录读取(开发模式)
|
|
configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "simulator.json");
|
|
}
|
|
|
|
if (!File.Exists(configPath))
|
|
{
|
|
Console.WriteLine("错误: 找不到配置文件 simulator.json");
|
|
Console.WriteLine("按任意键退出...");
|
|
Console.ReadKey();
|
|
return;
|
|
}
|
|
|
|
string json = File.ReadAllText(configPath);
|
|
var config = JsonConvert.DeserializeObject<SimulatorConfig>(json);
|
|
|
|
// 显示配置
|
|
Console.WriteLine("加载配置: simulator.json");
|
|
foreach (var addr in config.Addresses)
|
|
{
|
|
Console.WriteLine($" - {addr.Name} (:{addr.Port}) {addr.Devices.Count}台设备");
|
|
}
|
|
|
|
// 创建引擎
|
|
var engine = new SimulatorEngine();
|
|
engine.LoadConfig(config);
|
|
|
|
// 从数据库读取采集地址和机床
|
|
Console.WriteLine("\n连接数据库...");
|
|
if (engine.LoadFromDatabase(out string dbError))
|
|
{
|
|
Console.WriteLine($" [✓] 数据库连接成功,读取到 {engine.DbAddresses.Count} 个采集地址");
|
|
foreach (var a in engine.DbAddresses)
|
|
{
|
|
Console.WriteLine($" - {a.Name} ({a.Machines.Count}台机床)");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($" [✗] 数据库连接失败: {dbError}");
|
|
Console.WriteLine(" 将以空配置启动,可在管理界面手动配置");
|
|
}
|
|
|
|
// 只启动管理页面(不自动启动模拟端口)
|
|
Console.WriteLine("\n启动管理页面...");
|
|
engine.StartGateway();
|
|
|
|
Console.WriteLine("\n请在浏览器中打开管理页面配置并启动模拟。");
|
|
Console.WriteLine("按任意键退出...");
|
|
Console.ReadKey();
|
|
|
|
engine.StopAll();
|
|
Console.WriteLine("已退出。");
|
|
}
|
|
}
|
|
}
|