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.
66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
using CncSimulator.Config;
|
|
using CncSimulator.Core;
|
|
using CncSimulator.Device;
|
|
|
|
namespace CncSimulator.Core
|
|
{
|
|
/// <summary>引擎:管理多个 SimulatorServer 实例</summary>
|
|
public class SimulatorEngine
|
|
{
|
|
private readonly List<SimulatorServer> _servers = new List<SimulatorServer>();
|
|
|
|
public void LoadConfig(string jsonPath)
|
|
{
|
|
var json = File.ReadAllText(jsonPath);
|
|
var cfg = JsonConvert.DeserializeObject<SimulatorConfig>(json);
|
|
LoadConfig(cfg);
|
|
}
|
|
|
|
public void LoadConfig(SimulatorConfig cfg)
|
|
{
|
|
_servers.Clear();
|
|
foreach (var addr in cfg.Addresses)
|
|
{
|
|
var devices = new List<DeviceSimulator>();
|
|
foreach (var d in addr.Devices)
|
|
{
|
|
devices.Add(new DeviceSimulator(d, addr.DataChangeInterval));
|
|
}
|
|
var server = new SimulatorServer(addr, devices);
|
|
_servers.Add(server);
|
|
}
|
|
}
|
|
|
|
public void StartAll()
|
|
{
|
|
foreach (var s in _servers) s.Start();
|
|
}
|
|
|
|
public void StopAll()
|
|
{
|
|
foreach (var s in _servers) s.Stop();
|
|
}
|
|
|
|
public object GetStatus()
|
|
{
|
|
return _servers.Select(s => new
|
|
{
|
|
address = s.Address.Name,
|
|
port = s.Address.Port,
|
|
running = s.IsRunning,
|
|
totalDevices = s.TotalDeviceCount,
|
|
onlineDevices = s.OnlineDeviceCount
|
|
}).ToList();
|
|
}
|
|
|
|
public SimulatorServer FindByPort(int port)
|
|
{
|
|
return _servers.FirstOrDefault(s => s.Address.Port == port);
|
|
}
|
|
}
|
|
}
|