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.
76 lines
2.6 KiB
C#
76 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using CncSimulator.Config;
|
|
using CncSimulator.Device;
|
|
using CncSimulator.Generator;
|
|
|
|
namespace CncSimulator.Device
|
|
{
|
|
/// <summary>单台设备的状态机与仿真逻辑</summary>
|
|
public class DeviceSimulator
|
|
{
|
|
public DeviceState State { get; private set; }
|
|
private readonly Random _rnd;
|
|
private readonly List<string> _programs = new List<string> { "O0001", "O0002", "1566.NC", "PART-A", "TEST-03" };
|
|
private int _currentScenarioIndex = 0;
|
|
private int _tickCounter = 0;
|
|
|
|
public DeviceSimulator(DeviceConfig cfg, int dataChangeInterval)
|
|
{
|
|
State = new DeviceState
|
|
{
|
|
DeviceCode = cfg.DeviceCode,
|
|
Desc = cfg.Desc,
|
|
ProgramName = cfg.InitialProgram ?? "O0001",
|
|
PartCount = cfg.InitialPartCount,
|
|
DataChangeInterval = dataChangeInterval
|
|
};
|
|
// 使用不同种子避免同步
|
|
_rnd = new Random(cfg.DeviceCode.GetHashCode());
|
|
// 初始化一个随机偏移,确保场景起始不一致
|
|
_currentScenarioIndex = _rnd.Next(0, _programs.Count);
|
|
}
|
|
|
|
public void Tick()
|
|
{
|
|
if (!State.IsOnline) return;
|
|
// 简易定时器:每次 Tick 增加一个单位时间,依据当前场景更新数据
|
|
_tickCounter++;
|
|
// 每次数据变化的间隔由 DataChangeInterval 决定
|
|
if (_tickCounter < State.DataChangeInterval) return;
|
|
_tickCounter = 0;
|
|
|
|
// 根据当前场景执行更新逻辑
|
|
switch (State.CurrentScenario)
|
|
{
|
|
case "machining":
|
|
State.PartCount += 1;
|
|
State.RunStatus = 3;
|
|
State.OperateMode = 1;
|
|
State.MachiningStatus = "G01";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void SetScenario(string scenarioName, int duration)
|
|
{
|
|
State.CurrentScenario = scenarioName;
|
|
State.ScenarioDuration = duration;
|
|
State.ScenarioTick = 0;
|
|
}
|
|
|
|
// 简化的场景更新接口,供 ScenarioPlayer 调用
|
|
public void ApplyScenarioUpdate(string scenarioName, int duration, string programOverride = null)
|
|
{
|
|
SetScenario(scenarioName, duration);
|
|
if (!string.IsNullOrWhiteSpace(programOverride))
|
|
{
|
|
State.ProgramName = programOverride;
|
|
}
|
|
}
|
|
}
|
|
}
|