- 实训台状态由服务器统一保存:滑块和开关设定传感器读数,推给学生程序; 学生程序控制的灯、继电器、LED 屏回写到实训台 - 网页按教材实训台布局图绘制实训台,传感器卡片上直接调节环境 - 新控件:CheckBox、RadioButton(事件顺序与 WinForms 一致),Lamp、Alarm - 设备类库:串口未打开时控制无效,与实物一致 - 修正:列表属性序列化时数字被转成字符串 - 示例:项目 2 任务 2–3、项目 3 任务 1–2;网址参数 ?sample= 打开指定示例 - 测试:实训台单元测试,项目 2、3 的端到端测试 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.2 KiB
C#
58 lines
2.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Drawing;
|
||
using System.Globalization;
|
||
using System.Text.Json.Nodes;
|
||
using System.Windows.Forms;
|
||
|
||
namespace Sim.Host;
|
||
|
||
/// <summary>把控件属性值转换成发给浏览器的 JSON(格式见 web/src/winforms/format.ts)</summary>
|
||
public static class PropValue
|
||
{
|
||
public static JsonNode? From(object? value) => value switch
|
||
{
|
||
null => null,
|
||
string s => JsonValueOf(s),
|
||
bool b => JsonValueOf(b),
|
||
char c => JsonValueOf(c == '\0' ? "" : c.ToString()),
|
||
int or long or short or byte => JsonValueOf(Convert.ToInt64(value, CultureInfo.InvariantCulture)),
|
||
float or double or decimal => JsonValueOf(Convert.ToDouble(value, CultureInfo.InvariantCulture)),
|
||
Enum e => JsonValueOf(e.ToString()),
|
||
Point p => new JsonObject { ["x"] = p.X, ["y"] = p.Y },
|
||
Size s => new JsonObject { ["width"] = s.Width, ["height"] = s.Height },
|
||
Color c => ColorCss(c),
|
||
Font f => new JsonObject
|
||
{
|
||
["family"] = f.Name,
|
||
["size"] = f.SizeInPoints,
|
||
["bold"] = f.Bold,
|
||
["italic"] = f.Italic,
|
||
["underline"] = f.Underline,
|
||
["strikeout"] = f.Strikeout,
|
||
},
|
||
Padding p => new JsonObject { ["left"] = p.Left, ["top"] = p.Top, ["right"] = p.Right, ["bottom"] = p.Bottom },
|
||
IEnumerable list => ListOf(list),
|
||
_ => JsonValueOf(value.ToString() ?? ""),
|
||
};
|
||
|
||
/// <summary>未设置的颜色(Color.Empty)返回 null,表示沿用父控件的颜色</summary>
|
||
public static JsonNode? ColorCss(Color color)
|
||
{
|
||
if (color.IsEmpty) return null;
|
||
if (color.A == 0) return "transparent";
|
||
if (color.A < 255)
|
||
return $"rgba({color.R},{color.G},{color.B},{(color.A / 255.0).ToString("0.###", CultureInfo.InvariantCulture)})";
|
||
return $"#{color.R:x2}{color.G:x2}{color.B:x2}";
|
||
}
|
||
|
||
private static JsonArray ListOf(IEnumerable items)
|
||
{
|
||
var array = new JsonArray();
|
||
foreach (var item in items) array.Add(From(item));
|
||
return array;
|
||
}
|
||
|
||
private static JsonNode JsonValueOf<T>(T value) => JsonValue.Create(value)!;
|
||
}
|