191 lines
5.4 KiB
C#
191 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Protocol376Simulator.Models
|
|
{
|
|
/// <summary>
|
|
/// 测试场景,用于保存和加载测试配置
|
|
/// </summary>
|
|
public class TestScenario
|
|
{
|
|
/// <summary>
|
|
/// 场景名称
|
|
/// </summary>
|
|
public string Name { get; set; }
|
|
|
|
/// <summary>
|
|
/// 场景描述
|
|
/// </summary>
|
|
public string Description { get; set; }
|
|
|
|
/// <summary>
|
|
/// 服务器地址
|
|
/// </summary>
|
|
public string ServerAddress { get; set; }
|
|
|
|
/// <summary>
|
|
/// 服务器端口
|
|
/// </summary>
|
|
public int ServerPort { get; set; }
|
|
|
|
/// <summary>
|
|
/// 集中器配置列表
|
|
/// </summary>
|
|
public List<ConcentratorConfig> Concentrators { get; set; } = new List<ConcentratorConfig>();
|
|
|
|
/// <summary>
|
|
/// 测试步骤列表
|
|
/// </summary>
|
|
public List<TestStep> Steps { get; set; } = new List<TestStep>();
|
|
|
|
/// <summary>
|
|
/// 将测试场景保存到文件
|
|
/// </summary>
|
|
public static void SaveToFile(TestScenario scenario, string filePath)
|
|
{
|
|
string directory = Path.GetDirectoryName(filePath);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
var json = JsonSerializer.Serialize(scenario, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
});
|
|
|
|
File.WriteAllText(filePath, json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从文件加载测试场景
|
|
/// </summary>
|
|
public static TestScenario LoadFromFile(string filePath)
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
throw new FileNotFoundException($"测试场景文件不存在: {filePath}");
|
|
}
|
|
|
|
string json = File.ReadAllText(filePath);
|
|
return JsonSerializer.Deserialize<TestScenario>(json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有保存的测试场景文件
|
|
/// </summary>
|
|
public static List<string> GetAllScenarioFiles(string directory = "Scenarios")
|
|
{
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
return new List<string>();
|
|
}
|
|
|
|
var files = Directory.GetFiles(directory, "*.json");
|
|
return new List<string>(files);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 集中器配置
|
|
/// </summary>
|
|
public class ConcentratorConfig
|
|
{
|
|
/// <summary>
|
|
/// 集中器地址
|
|
/// </summary>
|
|
public string Address { get; set; }
|
|
|
|
/// <summary>
|
|
/// 是否自动登录
|
|
/// </summary>
|
|
public bool AutoLogin { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// 是否自动心跳
|
|
/// </summary>
|
|
public bool AutoHeartbeat { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// 是否自动响应
|
|
/// </summary>
|
|
public bool AutoResponse { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// 是否启用断线重连
|
|
/// </summary>
|
|
public bool AutoReconnect { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// 最大重连尝试次数
|
|
/// </summary>
|
|
public int MaxReconnectAttempts { get; set; } = 5;
|
|
|
|
/// <summary>
|
|
/// 重连延迟时间(秒)
|
|
/// </summary>
|
|
public int ReconnectDelaySeconds { get; set; } = 5;
|
|
|
|
/// <summary>
|
|
/// 初始表计数据
|
|
/// </summary>
|
|
public Dictionary<byte, string> InitialMeterData { get; set; } = new Dictionary<byte, string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试步骤
|
|
/// </summary>
|
|
public class TestStep
|
|
{
|
|
/// <summary>
|
|
/// 步骤类型枚举
|
|
/// </summary>
|
|
[JsonConverter(typeof(JsonStringEnumConverter))]
|
|
public enum StepType
|
|
{
|
|
Login,
|
|
Heartbeat,
|
|
ValveControl,
|
|
DataUpload,
|
|
ReadData,
|
|
SetParameter,
|
|
Wait,
|
|
SetMeterData
|
|
}
|
|
|
|
/// <summary>
|
|
/// 步骤类型
|
|
/// </summary>
|
|
public StepType Type { get; set; }
|
|
|
|
/// <summary>
|
|
/// 目标集中器地址,"all"表示所有集中器
|
|
/// </summary>
|
|
public string ConcentratorAddress { get; set; }
|
|
|
|
/// <summary>
|
|
/// 步骤描述
|
|
/// </summary>
|
|
public string Description { get; set; }
|
|
|
|
/// <summary>
|
|
/// 步骤参数,根据不同步骤类型使用不同的参数
|
|
/// </summary>
|
|
public Dictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();
|
|
|
|
/// <summary>
|
|
/// 执行本步骤前的延迟时间(毫秒)
|
|
/// </summary>
|
|
public int DelayBeforeStepMs { get; set; } = 0;
|
|
|
|
/// <summary>
|
|
/// 执行下一步骤前的延迟时间(毫秒)
|
|
/// </summary>
|
|
public int DelayAfterStepMs { get; set; } = 1000;
|
|
}
|
|
} |