From 4c53caf7faafb58bc658907e80fb5633c71be266 Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Thu, 22 Oct 2026 11:27:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=95=99=E6=9D=90=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=EF=BC=9A=E9=A1=B9=E7=9B=AE=201=20=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=202=E3=80=81=E9=A1=B9=E7=9B=AE=204=20=E4=BB=BB=E5=8A=A1=201=20?= =?UTF-8?q?=E5=92=8C=203=E3=80=81=E9=A1=B9=E7=9B=AE=205=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 界面取自配套参考工程,用设计器引擎按书上表格改控件名(label1 → lbl_port 等), 去掉 BOM、换行统一为 LF;代码按书整理,修正 boolIsOpen 等印刷错误 - 测试:所有示例都能编译(10 个),以及计算器 / 多态、红外对射、报警灯、火焰的行为测试 Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +- docs/设计方案.md | 9 +- samples/p1-t2-面向对象特性/American.cs | 16 ++ samples/p1-t2-面向对象特性/Chinese.cs | 16 ++ samples/p1-t2-面向对象特性/Form1.Designer.cs | 162 +++++++++++++++++++ samples/p1-t2-面向对象特性/Form1.cs | 73 +++++++++ samples/p1-t2-面向对象特性/People.cs | 17 ++ samples/p1-t2-面向对象特性/Program.cs | 22 +++ samples/p1-t2-面向对象特性/meta.json | 4 + samples/p4-t1-红外对射/Form1.Designer.cs | 153 ++++++++++++++++++ samples/p4-t1-红外对射/Form1.cs | 61 +++++++ samples/p4-t1-红外对射/Program.cs | 22 +++ samples/p4-t1-红外对射/meta.json | 4 + samples/p4-t3-报警灯/Form1.Designer.cs | 138 ++++++++++++++++ samples/p4-t3-报警灯/Form1.cs | 73 +++++++++ samples/p4-t3-报警灯/Program.cs | 22 +++ samples/p4-t3-报警灯/meta.json | 4 + samples/p5-t1-火焰/Form1.Designer.cs | 153 ++++++++++++++++++ samples/p5-t1-火焰/Form1.cs | 67 ++++++++ samples/p5-t1-火焰/Program.cs | 22 +++ samples/p5-t1-火焰/meta.json | 4 + server/test/runner.test.ts | 62 ++++++- 22 files changed, 1101 insertions(+), 7 deletions(-) create mode 100644 samples/p1-t2-面向对象特性/American.cs create mode 100644 samples/p1-t2-面向对象特性/Chinese.cs create mode 100644 samples/p1-t2-面向对象特性/Form1.Designer.cs create mode 100644 samples/p1-t2-面向对象特性/Form1.cs create mode 100644 samples/p1-t2-面向对象特性/People.cs create mode 100644 samples/p1-t2-面向对象特性/Program.cs create mode 100644 samples/p1-t2-面向对象特性/meta.json create mode 100644 samples/p4-t1-红外对射/Form1.Designer.cs create mode 100644 samples/p4-t1-红外对射/Form1.cs create mode 100644 samples/p4-t1-红外对射/Program.cs create mode 100644 samples/p4-t1-红外对射/meta.json create mode 100644 samples/p4-t3-报警灯/Form1.Designer.cs create mode 100644 samples/p4-t3-报警灯/Form1.cs create mode 100644 samples/p4-t3-报警灯/Program.cs create mode 100644 samples/p4-t3-报警灯/meta.json create mode 100644 samples/p5-t1-火焰/Form1.Designer.cs create mode 100644 samples/p5-t1-火焰/Form1.cs create mode 100644 samples/p5-t1-火焰/Program.cs create mode 100644 samples/p5-t1-火焰/meta.json diff --git a/README.md b/README.md index 1d5b151..3b67236 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ bun test/load.ts 100 # 压力测试:同时运行 100 个学生程序 - **虚拟实训台**:按教材实训台布局图排列(红外对射、警示灯、LED 屏、照明灯、人体红外、温湿度、光照、烟雾、火焰、ADAM 模块)。每个浏览器会话一台,状态由服务器统一保存:传感器读数由网页上的滑块和开关设定,经服务器推给学生程序;灯、继电器、LED 屏由学生程序控制。 - 新控件:CheckBox、RadioButton(事件顺序与 WinForms 一致),教材控件库的 Lamp、Alarm。 -- 示例工程(按书上代码整理):项目 1 任务 1、项目 2 任务 1–3、项目 3 任务 1–2。网址带 `?sample=示例编号` 可直接打开指定示例。 +- 示例工程(按书上代码整理):项目 1 任务 1–2、项目 2 任务 1–3、项目 3 任务 1–2、项目 4 任务 1 和 3、项目 5 任务 1,共 10 个。网址带 `?sample=示例编号` 可直接打开指定示例。 - 代码编辑器(Monaco):C# 高亮、编译错误波浪线、点错误跳到对应行、F5 运行 / Shift+F5 停止、草稿自动保存在浏览器本地。 - 窗体设计器(仿 VS,界面中文):工具箱、拖放与改大小、属性窗口、事件窗口、双击生成事件处理方法、撤销 / 重做;读写 VS 格式的 Designer.cs。 - 多窗体:新建、删除窗体;运行时多个窗口可切换前后。 @@ -69,4 +69,4 @@ bun test/load.ts 100 # 压力测试:同时运行 100 个学生程序 尚未完成: - 学生代码目前直接在本机进程里运行,**还没有沙箱**,只能在本机开发时使用;部署前要改为在 Docker 容器中运行。 -- 导入教材参考工程、账号和班级、自动评测。 +- 教材项目 4–8 的其余任务(需要摄像头、线程 API、ListBox、数据库、TrackBar、PictureBox、图表)、账号和班级、自动评测。 diff --git a/docs/设计方案.md b/docs/设计方案.md index f15cc30..d6fc82d 100644 --- a/docs/设计方案.md +++ b/docs/设计方案.md @@ -200,11 +200,11 @@ IOT/ | 项目 | 教材内容 | 平台需要支持 | 状态 | | --- | --- | --- | --- | -| 1 C# 入门基础 | 登录与新增用户(两个窗体)、计算器、继承与多态演示 | 多窗体、Label、TextBox(PasswordChar)、Button、ComboBox、MessageBox、控制台输出 | 任务 1 已完成,任务 2 示例待整理 | +| 1 C# 入门基础 | 登录与新增用户(两个窗体)、计算器、继承与多态演示 | 多窗体、Label、TextBox(PasswordChar)、Button、ComboBox、MessageBox、控制台输出 | 已完成 | | 2 环境监测 | LED 屏推送文字;4017 采集温湿度和光照;采集后推送 LED | LED 控件、ledHelper、adam4017Helper、GroupBox、虚拟 COM 口 | 已完成 | | 3 智慧路灯 | ZigBee 开关路灯;人体红外每 5 秒检测并联动路灯 | Lamp 控件、zigBeeHelper、adam4150Helper、CheckBox、RadioButton、Timer | 已完成 | -| 4 入侵监测 | 红外对射检测;网络摄像机画面与云台;报警灯 | PictureBox、摄像头仿真、Alarm 控件、图片保存 | | -| 5 灾情监测 | 火焰(Timer)、烟雾(Thread)、灾情记录与推送 | Thread(含 Suspend / Resume / Abort)、跨线程改界面、ListBox、FormClosing | | +| 4 入侵监测 | 红外对射检测;网络摄像机画面与云台;报警灯 | PictureBox、摄像头仿真、Alarm 控件、图片保存 | 任务 1、3 已完成;任务 2 需摄像头 | +| 5 灾情监测 | 火焰(Timer)、烟雾(Thread)、灾情记录与推送 | Thread(含 Suspend / Resume / Abort)、跨线程改界面、ListBox、FormClosing | 任务 1 已完成;任务 2 需线程 API,任务 3 需 ListBox | | 6 数据存储 | 建库建表、采集入库与增删改、按时间段查询 | SQL 窗口(代替 SSMS)、SqlClient 仿冒(SQLite)、DataGridView、DateTimePicker | | | 7 情景控制 | 智慧教室、寝室、会议室三种情景联动 | TrackBar、资源图片、PictureBox 当按钮、mp3 播放、虚拟时钟 | | | 8 管理平台 | 主界面、校园环境(自动控制、7 天温度图表)、校园安防(抓拍入库、历史图片) | Panel Dock、无边框窗体、透明背景、Chart、ShowDialog、图片存数据库 | | @@ -227,7 +227,7 @@ IOT/ ```mermaid flowchart TB P0["阶段 0 · 技术验证(已完成 2026-10-22)
教材项目 2 任务 1 原样运行;实测编译、延迟、内存"] - P1["阶段 1 · MVP(进行中,约 3 个月)
虚拟实训台 ✔、项目 1–3 示例 ✔、代码编辑器 ✔、窗体设计器 ✔、多窗体 ✔
导入教材参考工程"] + P1["阶段 1 · MVP(进行中,约 3 个月)
虚拟实训台 ✔、代码编辑器 ✔、窗体设计器 ✔、多窗体 ✔
教材示例:项目 1–3 全部 ✔,项目 4–5 中平台已支持的 3 个任务 ✔"] P2["阶段 2 · 教学闭环(约 2 个月)
班级码登录、提交作业、自动评测、老师看板、成绩导出;一个班试用"] P3["阶段 3 · 扩展(下学期)
教材项目 4–8:摄像头、线程、SQLite 模拟数据库、图表、虚拟时钟"] P4["阶段 4 · 进阶(之后)
校园街景场景、故障注入题库、导出 VS 工程"] @@ -258,6 +258,7 @@ flowchart TB - 2026-10-22:窗体设计器:工具箱拖放或双击添加控件,选中、拖动、改大小、拖进拖出分组框,Delete 删除、方向键微调;属性窗口和事件窗口;双击控件或事件生成事件处理方法并跳到代码;Timer 等组件显示在组件栏。Designer.cs 在浏览器端直接解析和生成:未改动的语句保留原文,教材 25 个参考窗体和全部示例读入再写出逐字节不变;布局语句(SuspendLayout / ResumeLayout / PerformLayout)的增删规则与 VS 一致。 - 2026-10-22:设计器撤销 / 重做(撤销"生成事件处理方法"时连同插入的方法一起撤掉,不覆盖学生已写的代码);工具箱、属性、事件窗口全部显示中文,英文名放在悬停提示里;支持中文的控件名和类名。 - 2026-10-22:多窗体:新建窗体(与 VS 新建窗体相同的两个文件,沿用工程命名空间)、删除窗体(启动窗体不能删);标签页按窗体分组;运行时点哪个窗口哪个到最前。新增示例:项目 1 任务 1 登录与增加用户(两个窗体的 Designer.cs 由设计器引擎生成)。 +- 2026-10-22:整理教材示例:项目 1 任务 2(计算器、多态)、项目 4 任务 1(红外对射)、项目 4 任务 3(报警灯)、项目 5 任务 1(火焰)。界面取自配套参考工程,用设计器引擎按书上的控件名改名。至此 10 个示例,全部有"能编译"检查和端到端行为测试。项目 4–8 其余任务需要的能力(摄像头、线程 API、ListBox、数据库、TrackBar、PictureBox、图表)放到第 3 阶段。 ## 技术风险与待确认事项 diff --git a/samples/p1-t2-面向对象特性/American.cs b/samples/p1-t2-面向对象特性/American.cs new file mode 100644 index 0000000..066955b --- /dev/null +++ b/samples/p1-t2-面向对象特性/American.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace C_基础 +{ + class American : People//继承People类 + { + public override void Speak(System.Windows.Forms.TextBox Txtbox, string name)//重写父类中的虚方法 + { + base.Speak(Txtbox, name+"说的是英语!\r\n"); + } + } +} diff --git a/samples/p1-t2-面向对象特性/Chinese.cs b/samples/p1-t2-面向对象特性/Chinese.cs new file mode 100644 index 0000000..71c0ead --- /dev/null +++ b/samples/p1-t2-面向对象特性/Chinese.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace C_基础 +{ + class Chinese:People//继承People类 + { + public override void Speak(System.Windows.Forms.TextBox Txtbox, string name)//重写父类中的虚方法 + { + base.Speak(Txtbox, name+"说的是汉语!\r\n"); + } + } +} diff --git a/samples/p1-t2-面向对象特性/Form1.Designer.cs b/samples/p1-t2-面向对象特性/Form1.Designer.cs new file mode 100644 index 0000000..64815c4 --- /dev/null +++ b/samples/p1-t2-面向对象特性/Form1.Designer.cs @@ -0,0 +1,162 @@ +namespace C_基础 +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.btnCalculate = new System.Windows.Forms.Button(); + this.txtResult = new System.Windows.Forms.TextBox(); + this.cmboxType = new System.Windows.Forms.ComboBox(); + this.txtNum2 = new System.Windows.Forms.TextBox(); + this.txtNum1 = new System.Windows.Forms.TextBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.txtDispaly = new System.Windows.Forms.TextBox(); + this.btnDisplay = new System.Windows.Forms.Button(); + this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); + this.SuspendLayout(); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.btnCalculate); + this.groupBox1.Controls.Add(this.txtResult); + this.groupBox1.Controls.Add(this.cmboxType); + this.groupBox1.Controls.Add(this.txtNum2); + this.groupBox1.Controls.Add(this.txtNum1); + this.groupBox1.Location = new System.Drawing.Point(21, 12); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(516, 80); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "简易计算器"; + // + // btnCalculate + // + this.btnCalculate.Location = new System.Drawing.Point(325, 31); + this.btnCalculate.Name = "btnCalculate"; + this.btnCalculate.Size = new System.Drawing.Size(49, 23); + this.btnCalculate.TabIndex = 5; + this.btnCalculate.Text = "="; + this.btnCalculate.UseVisualStyleBackColor = true; + this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); + // + // txtResult + // + this.txtResult.Location = new System.Drawing.Point(390, 31); + this.txtResult.Name = "txtResult"; + this.txtResult.ReadOnly = true; + this.txtResult.Size = new System.Drawing.Size(100, 21); + this.txtResult.TabIndex = 4; + // + // cmboxType + // + this.cmboxType.FormattingEnabled = true; + this.cmboxType.Items.AddRange(new object[] { + "+", + "-", + "*", + "/"}); + this.cmboxType.Location = new System.Drawing.Point(112, 31); + this.cmboxType.Name = "cmboxType"; + this.cmboxType.Size = new System.Drawing.Size(64, 20); + this.cmboxType.TabIndex = 2; + // + // txtNum2 + // + this.txtNum2.Location = new System.Drawing.Point(195, 31); + this.txtNum2.Name = "txtNum2"; + this.txtNum2.Size = new System.Drawing.Size(100, 21); + this.txtNum2.TabIndex = 1; + // + // txtNum1 + // + this.txtNum1.Location = new System.Drawing.Point(6, 31); + this.txtNum1.Name = "txtNum1"; + this.txtNum1.Size = new System.Drawing.Size(100, 21); + this.txtNum1.TabIndex = 0; + // + // groupBox2 + // + this.groupBox2.Controls.Add(this.txtDispaly); + this.groupBox2.Controls.Add(this.btnDisplay); + this.groupBox2.Location = new System.Drawing.Point(21, 98); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(516, 116); + this.groupBox2.TabIndex = 1; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "类的多态性"; + // + // txtDispaly + // + this.txtDispaly.Location = new System.Drawing.Point(46, 20); + this.txtDispaly.Multiline = true; + this.txtDispaly.Name = "txtDispaly"; + this.txtDispaly.Size = new System.Drawing.Size(249, 82); + this.txtDispaly.TabIndex = 1; + // + // btnDisplay + // + this.btnDisplay.Location = new System.Drawing.Point(344, 45); + this.btnDisplay.Name = "btnDisplay"; + this.btnDisplay.Size = new System.Drawing.Size(75, 23); + this.btnDisplay.TabIndex = 0; + this.btnDisplay.Text = "显示"; + this.btnDisplay.UseVisualStyleBackColor = true; + this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(549, 226); + this.Controls.Add(this.groupBox2); + this.Controls.Add(this.groupBox1); + this.Name = "Form1"; + this.Text = "C#入门基础"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.TextBox txtResult; + private System.Windows.Forms.ComboBox cmboxType; + private System.Windows.Forms.TextBox txtNum2; + private System.Windows.Forms.TextBox txtNum1; + private System.Windows.Forms.Button btnCalculate; + private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.TextBox txtDispaly; + private System.Windows.Forms.Button btnDisplay; + } +} + diff --git a/samples/p1-t2-面向对象特性/Form1.cs b/samples/p1-t2-面向对象特性/Form1.cs new file mode 100644 index 0000000..c1233e8 --- /dev/null +++ b/samples/p1-t2-面向对象特性/Form1.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace C_基础 +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void btnCalculate_Click(object sender, EventArgs e) + { + if(txtNum1.Text.Trim()!=""&&txtNum2.Text.Trim()!="") + { + float a, b; + a = float.Parse(txtNum1.Text);//字符串转化为float + b = float.Parse(txtNum2.Text);//字符串转化为float + float res = (float)Result(a, b, cmboxType.Text);//两数进行装箱,调用计算函数,返回值进行拆箱 + txtResult.Text = res.ToString();//将返回值显示到界面上 + } + } + public Object Result(Object a,Object b,String type)//计算结果,使用拆箱装箱方式 + { + float result=0; + float a1,b1; + a1=(float)a;//拆箱 + b1=(float)b;//拆箱 + switch(type) + { + case "+": result = a1 + b1; break;//计算两个数的和 + case "-": result = a1 - b1; break;//计算两个数的差 + case "*": result = a1 * b1; break;//计算两个数的积 + case "/": result = a1 / b1; break;//计算两个数的商 + default: result=0;break;//默认值 + } + return (Object)result;//装箱 + } + + private void btnDisplay_Click(object sender, EventArgs e) + { + People pleTemp;//定义People临时存储变量 + Dictionary dicName = new Dictionary();//定义Dictionary字典类型,用于存储姓名与序号 + dicName.Add(1, "张三");//向字典中添加数据 + dicName.Add(2, "李四"); + dicName.Add(3, "Mary"); + dicName.Add(4, "Jimmy"); + List peoplelist = new List();//定义People的List泛型数据 + for (int i = 0; i < 2; i++)//添加两个中国人 + { + pleTemp = new Chinese();//实例化中国人 + peoplelist.Add(pleTemp); + } + for (int i = 0; i < 2; i++)//添加两个美国人 + { + pleTemp = new American();//实例化美国人 + peoplelist.Add(pleTemp); + } + for (int i = 0; i < peoplelist.Count; i++)//遍历List + { + peoplelist[i].Speak(this.txtDispaly, dicName[i + 1]);//执行派生类重写的方法 + } + } + } +} diff --git a/samples/p1-t2-面向对象特性/People.cs b/samples/p1-t2-面向对象特性/People.cs new file mode 100644 index 0000000..af4c6ed --- /dev/null +++ b/samples/p1-t2-面向对象特性/People.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace C_基础 +{ + class People//定义一个父类People + { + public virtual void Speak(TextBox Txtbox,string name)//定义一个虚方法,用来表示人类的说话语言 + { + Txtbox.Text += name;//向控件中输出名字 + } + } +} diff --git a/samples/p1-t2-面向对象特性/Program.cs b/samples/p1-t2-面向对象特性/Program.cs new file mode 100644 index 0000000..350f2cb --- /dev/null +++ b/samples/p1-t2-面向对象特性/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace C_基础 +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/samples/p1-t2-面向对象特性/meta.json b/samples/p1-t2-面向对象特性/meta.json new file mode 100644 index 0000000..446a8e6 --- /dev/null +++ b/samples/p1-t2-面向对象特性/meta.json @@ -0,0 +1,4 @@ +{ + "title": "项目1 任务2 C#面向对象特性(计算器、多态)", + "source": "教材书 p13–16,界面取自配套参考工程,控件名和窗体标题按书修改(BtnCalculate → btnCalculate)" +} diff --git a/samples/p4-t1-红外对射/Form1.Designer.cs b/samples/p4-t1-红外对射/Form1.Designer.cs new file mode 100644 index 0000000..e328a02 --- /dev/null +++ b/samples/p4-t1-红外对射/Form1.Designer.cs @@ -0,0 +1,153 @@ +namespace 红外对射 +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.lbl_port = new System.Windows.Forms.Label(); + this.com_port = new System.Windows.Forms.ComboBox(); + this.btn_open = new System.Windows.Forms.Button(); + this.timer1 = new System.Windows.Forms.Timer(this.components); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.txt_even = new System.Windows.Forms.TextBox(); + this.txt_time = new System.Windows.Forms.TextBox(); + this.lbl_even = new System.Windows.Forms.Label(); + this.lbl_time = new System.Windows.Forms.Label(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // lbl_port + // + this.lbl_port.AutoSize = true; + this.lbl_port.Location = new System.Drawing.Point(40, 25); + this.lbl_port.Name = "lbl_port"; + this.lbl_port.Size = new System.Drawing.Size(53, 12); + this.lbl_port.TabIndex = 1; + this.lbl_port.Text = "环境串口"; + // + // com_port + // + this.com_port.FormattingEnabled = true; + this.com_port.Location = new System.Drawing.Point(99, 22); + this.com_port.Name = "com_port"; + this.com_port.Size = new System.Drawing.Size(121, 20); + this.com_port.TabIndex = 2; + // + // btn_open + // + this.btn_open.Location = new System.Drawing.Point(236, 20); + this.btn_open.Name = "btn_open"; + this.btn_open.Size = new System.Drawing.Size(75, 23); + this.btn_open.TabIndex = 3; + this.btn_open.Text = "开启"; + this.btn_open.UseVisualStyleBackColor = true; + this.btn_open.Click += new System.EventHandler(this.btn_open_Click); + // + // timer1 + // + this.timer1.Interval = 2000; + this.timer1.Tick += new System.EventHandler(this.timer1_Tick); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.txt_even); + this.groupBox1.Controls.Add(this.txt_time); + this.groupBox1.Controls.Add(this.lbl_even); + this.groupBox1.Controls.Add(this.lbl_time); + this.groupBox1.Location = new System.Drawing.Point(20, 57); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(362, 151); + this.groupBox1.TabIndex = 5; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "数据显示"; + // + // txt_even + // + this.txt_even.Location = new System.Drawing.Point(100, 70); + this.txt_even.Name = "txt_even"; + this.txt_even.Size = new System.Drawing.Size(191, 21); + this.txt_even.TabIndex = 3; + // + // txt_time + // + this.txt_time.Location = new System.Drawing.Point(100, 34); + this.txt_time.Name = "txt_time"; + this.txt_time.Size = new System.Drawing.Size(191, 21); + this.txt_time.TabIndex = 2; + // + // lbl_even + // + this.lbl_even.AutoSize = true; + this.lbl_even.Location = new System.Drawing.Point(34, 73); + this.lbl_even.Name = "lbl_even"; + this.lbl_even.Size = new System.Drawing.Size(29, 12); + this.lbl_even.TabIndex = 1; + this.lbl_even.Text = "入侵"; + // + // lbl_time + // + this.lbl_time.AutoSize = true; + this.lbl_time.Location = new System.Drawing.Point(32, 37); + this.lbl_time.Name = "lbl_time"; + this.lbl_time.Size = new System.Drawing.Size(29, 12); + this.lbl_time.TabIndex = 0; + this.lbl_time.Text = "时间"; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(405, 220); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.btn_open); + this.Controls.Add(this.com_port); + this.Controls.Add(this.lbl_port); + this.Name = "Form1"; + this.Text = "红外对射"; + this.Load += new System.EventHandler(this.Form1_Load); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label lbl_port; + private System.Windows.Forms.ComboBox com_port; + private System.Windows.Forms.Button btn_open; + private System.Windows.Forms.Timer timer1; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.TextBox txt_even; + private System.Windows.Forms.TextBox txt_time; + private System.Windows.Forms.Label lbl_even; + private System.Windows.Forms.Label lbl_time; + } +} + diff --git a/samples/p4-t1-红外对射/Form1.cs b/samples/p4-t1-红外对射/Form1.cs new file mode 100644 index 0000000..10dd3d1 --- /dev/null +++ b/samples/p4-t1-红外对射/Form1.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.IO.Ports; +using DeviceCtrlLibrary; + +namespace 红外对射 +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + bool IsOpen = false; + + private void Form1_Load(object sender, EventArgs e) + { + com_port.Items.Clear(); + foreach (string item in SerialPort.GetPortNames()) + { + com_port.Items.Add(item); + } + } + + private void btn_open_Click(object sender, EventArgs e) + { + IsOpen = !IsOpen; + if (IsOpen) + { + //打开数字量采集器所在的串口,开始定时采集 + DeviceCtrlHelper.adam4150Helper.Open(com_port.Text); + timer1.Start(); + btn_open.Text = "关闭"; + } + else + { + DeviceCtrlHelper.adam4150Helper.Close(); + timer1.Stop(); + btn_open.Text = "开启"; + } + } + + private void timer1_Tick(object sender, EventArgs e) + { + txt_time.Text = DateTime.Now.ToString(); + //获取红外对射入侵传感器的数据:true 表示有入侵 + if (DeviceCtrlHelper.adam4150Helper.SetData().infraredValue) + txt_even.Text = "发现入侵"; + else + txt_even.Text = "正常"; + } + } +} diff --git a/samples/p4-t1-红外对射/Program.cs b/samples/p4-t1-红外对射/Program.cs new file mode 100644 index 0000000..51bbb00 --- /dev/null +++ b/samples/p4-t1-红外对射/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace 红外对射 +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/samples/p4-t1-红外对射/meta.json b/samples/p4-t1-红外对射/meta.json new file mode 100644 index 0000000..9730ce9 --- /dev/null +++ b/samples/p4-t1-红外对射/meta.json @@ -0,0 +1,4 @@ +{ + "title": "项目4 任务1 采集红外对射入侵传感器数据", + "source": "教材书 p73–76,界面取自配套参考工程,控件名按书上表格修改;修正书上 boolIsOpen 少空格" +} diff --git a/samples/p4-t3-报警灯/Form1.Designer.cs b/samples/p4-t3-报警灯/Form1.Designer.cs new file mode 100644 index 0000000..3378301 --- /dev/null +++ b/samples/p4-t3-报警灯/Form1.Designer.cs @@ -0,0 +1,138 @@ +namespace 报警灯 +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.alarm1 = new WinFormsDeviceControlLibrary.Alarm(); + this.btn_alarmOpen = new System.Windows.Forms.Button(); + this.btn_alarmClose = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.btn_open = new System.Windows.Forms.Button(); + this.com_port = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // alarm1 + // + this.alarm1.BackColor = System.Drawing.Color.White; + this.alarm1.Location = new System.Drawing.Point(12, 12); + this.alarm1.Name = "alarm1"; + this.alarm1.Size = new System.Drawing.Size(150, 150); + this.alarm1.TabIndex = 0; + // + // btn_alarmOpen + // + this.btn_alarmOpen.Location = new System.Drawing.Point(12, 192); + this.btn_alarmOpen.Name = "btn_alarmOpen"; + this.btn_alarmOpen.Size = new System.Drawing.Size(35, 23); + this.btn_alarmOpen.TabIndex = 1; + this.btn_alarmOpen.Text = "开"; + this.btn_alarmOpen.UseVisualStyleBackColor = true; + this.btn_alarmOpen.Click += new System.EventHandler(this.btn_alarmOpen_Click); + // + // btn_alarmClose + // + this.btn_alarmClose.Location = new System.Drawing.Point(127, 192); + this.btn_alarmClose.Name = "btn_alarmClose"; + this.btn_alarmClose.Size = new System.Drawing.Size(35, 23); + this.btn_alarmClose.TabIndex = 2; + this.btn_alarmClose.Text = "关"; + this.btn_alarmClose.UseVisualStyleBackColor = true; + this.btn_alarmClose.Click += new System.EventHandler(this.btn_alarmClose_Click); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.btn_open); + this.groupBox1.Controls.Add(this.com_port); + this.groupBox1.Controls.Add(this.label1); + this.groupBox1.Location = new System.Drawing.Point(195, 58); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(218, 114); + this.groupBox1.TabIndex = 3; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "配置"; + // + // btn_open + // + this.btn_open.Location = new System.Drawing.Point(113, 71); + this.btn_open.Name = "btn_open"; + this.btn_open.Size = new System.Drawing.Size(75, 23); + this.btn_open.TabIndex = 2; + this.btn_open.Text = "开启"; + this.btn_open.UseVisualStyleBackColor = true; + this.btn_open.Click += new System.EventHandler(this.btn_open_Click); + // + // com_port + // + this.com_port.FormattingEnabled = true; + this.com_port.Location = new System.Drawing.Point(67, 33); + this.com_port.Name = "com_port"; + this.com_port.Size = new System.Drawing.Size(121, 20); + this.com_port.TabIndex = 1; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(20, 36); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(41, 12); + this.label1.TabIndex = 0; + this.label1.Text = "报警灯"; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(425, 262); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.btn_alarmClose); + this.Controls.Add(this.btn_alarmOpen); + this.Controls.Add(this.alarm1); + this.Name = "Form1"; + this.Text = "报警灯"; + this.Load += new System.EventHandler(this.Form1_Load); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private WinFormsDeviceControlLibrary.Alarm alarm1; + private System.Windows.Forms.Button btn_alarmOpen; + private System.Windows.Forms.Button btn_alarmClose; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button btn_open; + private System.Windows.Forms.ComboBox com_port; + private System.Windows.Forms.Label label1; + } +} + diff --git a/samples/p4-t3-报警灯/Form1.cs b/samples/p4-t3-报警灯/Form1.cs new file mode 100644 index 0000000..c303a32 --- /dev/null +++ b/samples/p4-t3-报警灯/Form1.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.IO.Ports; +using DeviceCtrlLibrary; + +namespace 报警灯 +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + bool IsOpen = false; + + private void Form1_Load(object sender, EventArgs e) + { + com_port.Items.Clear(); + foreach (string item in SerialPort.GetPortNames()) + { + com_port.Items.Add(item); + } + } + + private void AlarmOnOff(string Device, bool IsOpen) + { + //利用switch语句打开或关闭继电器模块 + switch (Device) + { + case "Alarm": + DeviceCtrlHelper.adam4150Helper.OnOff(0, IsOpen);//报警灯接入DO0 + alarm1.OnOff(IsOpen);//更改界面报警灯状态 + break; + case "LED1": DeviceCtrlHelper.adam4150Helper.OnOff(1, IsOpen); break;//开启或关闭继电器1模块 + case "LED2": DeviceCtrlHelper.adam4150Helper.OnOff(2, IsOpen); break;//开启或关闭继电器2模块 + default: MessageBox.Show("没有合适的接口程序!", "错误提示!"); break;//若为其他模块,则提示错误 + } + } + + private void btn_open_Click(object sender, EventArgs e) + { + IsOpen = !IsOpen; + if (IsOpen) + { + DeviceCtrlHelper.adam4150Helper.Open(com_port.Text); + btn_open.Text = "关闭"; + } + else + { + DeviceCtrlHelper.adam4150Helper.Close(); + btn_open.Text = "开启"; + } + } + + private void btn_alarmOpen_Click(object sender, EventArgs e) + { + AlarmOnOff("Alarm", true); + } + + private void btn_alarmClose_Click(object sender, EventArgs e) + { + AlarmOnOff("Alarm", false); + } + } +} diff --git a/samples/p4-t3-报警灯/Program.cs b/samples/p4-t3-报警灯/Program.cs new file mode 100644 index 0000000..d2280d7 --- /dev/null +++ b/samples/p4-t3-报警灯/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace 报警灯 +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/samples/p4-t3-报警灯/meta.json b/samples/p4-t3-报警灯/meta.json new file mode 100644 index 0000000..fbfe88d --- /dev/null +++ b/samples/p4-t3-报警灯/meta.json @@ -0,0 +1,4 @@ +{ + "title": "项目4 任务3 控制报警灯", + "source": "教材书 p84–87,界面取自配套参考工程;修正书上 boolIsOpen 少空格" +} diff --git a/samples/p5-t1-火焰/Form1.Designer.cs b/samples/p5-t1-火焰/Form1.Designer.cs new file mode 100644 index 0000000..4f778eb --- /dev/null +++ b/samples/p5-t1-火焰/Form1.Designer.cs @@ -0,0 +1,153 @@ +namespace 火焰 +{ + partial class Form1 + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.lbl_port = new System.Windows.Forms.Label(); + this.com_port = new System.Windows.Forms.ComboBox(); + this.btn_open = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.txt_even = new System.Windows.Forms.TextBox(); + this.lbl_even = new System.Windows.Forms.Label(); + this.txt_time = new System.Windows.Forms.TextBox(); + this.lbl_time = new System.Windows.Forms.Label(); + this.timer1 = new System.Windows.Forms.Timer(this.components); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // lbl_port + // + this.lbl_port.AutoSize = true; + this.lbl_port.Location = new System.Drawing.Point(53, 21); + this.lbl_port.Name = "lbl_port"; + this.lbl_port.Size = new System.Drawing.Size(29, 12); + this.lbl_port.TabIndex = 0; + this.lbl_port.Text = "串口"; + // + // com_port + // + this.com_port.FormattingEnabled = true; + this.com_port.Location = new System.Drawing.Point(100, 18); + this.com_port.Name = "com_port"; + this.com_port.Size = new System.Drawing.Size(121, 20); + this.com_port.TabIndex = 1; + // + // btn_open + // + this.btn_open.Location = new System.Drawing.Point(227, 16); + this.btn_open.Name = "btn_open"; + this.btn_open.Size = new System.Drawing.Size(75, 23); + this.btn_open.TabIndex = 2; + this.btn_open.Text = "开启"; + this.btn_open.UseVisualStyleBackColor = true; + this.btn_open.Click += new System.EventHandler(this.btn_open_Click); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.txt_even); + this.groupBox1.Controls.Add(this.lbl_even); + this.groupBox1.Controls.Add(this.txt_time); + this.groupBox1.Controls.Add(this.lbl_time); + this.groupBox1.Location = new System.Drawing.Point(30, 63); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(272, 110); + this.groupBox1.TabIndex = 3; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "数据显示"; + // + // txt_even + // + this.txt_even.Location = new System.Drawing.Point(70, 70); + this.txt_even.Name = "txt_even"; + this.txt_even.Size = new System.Drawing.Size(174, 21); + this.txt_even.TabIndex = 3; + // + // lbl_even + // + this.lbl_even.AutoSize = true; + this.lbl_even.Location = new System.Drawing.Point(23, 73); + this.lbl_even.Name = "lbl_even"; + this.lbl_even.Size = new System.Drawing.Size(29, 12); + this.lbl_even.TabIndex = 2; + this.lbl_even.Text = "事件"; + // + // txt_time + // + this.txt_time.Location = new System.Drawing.Point(70, 34); + this.txt_time.Name = "txt_time"; + this.txt_time.Size = new System.Drawing.Size(174, 21); + this.txt_time.TabIndex = 1; + // + // lbl_time + // + this.lbl_time.AutoSize = true; + this.lbl_time.Location = new System.Drawing.Point(23, 37); + this.lbl_time.Name = "lbl_time"; + this.lbl_time.Size = new System.Drawing.Size(29, 12); + this.lbl_time.TabIndex = 0; + this.lbl_time.Text = "时间"; + // + // timer1 + // + this.timer1.Interval = 2000; + this.timer1.Tick += new System.EventHandler(this.timer1_Tick); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(327, 193); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.btn_open); + this.Controls.Add(this.com_port); + this.Controls.Add(this.lbl_port); + this.Name = "Form1"; + this.Text = "火焰"; + this.Load += new System.EventHandler(this.Form1_Load); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label lbl_port; + private System.Windows.Forms.ComboBox com_port; + private System.Windows.Forms.Button btn_open; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.TextBox txt_even; + private System.Windows.Forms.Label lbl_even; + private System.Windows.Forms.TextBox txt_time; + private System.Windows.Forms.Label lbl_time; + private System.Windows.Forms.Timer timer1; + } +} + diff --git a/samples/p5-t1-火焰/Form1.cs b/samples/p5-t1-火焰/Form1.cs new file mode 100644 index 0000000..cab5874 --- /dev/null +++ b/samples/p5-t1-火焰/Form1.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.IO.Ports; +using DeviceCtrlLibrary; + +namespace 火焰 +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + bool IsOpen = false; + + private void Form1_Load(object sender, EventArgs e) + { + //用 for 循环把所有可用的串口号加入列表 + for (int i = 0; i < SerialPort.GetPortNames().Length; i++) + { + com_port.Items.Add(SerialPort.GetPortNames()[i].ToString()); + } + } + + private void btn_open_Click(object sender, EventArgs e) + { + IsOpen = !IsOpen; + if (IsOpen) + { + //打开数字量采集器所在的串口,开始定时采集 + DeviceCtrlHelper.adam4150Helper.Open(com_port.Text); + timer1.Start(); + btn_open.Text = "关闭"; + } + else + { + DeviceCtrlHelper.adam4150Helper.Close(); + timer1.Stop(); + btn_open.Text = "开启"; + } + } + + private String GetFireInfo() + { + //判断火焰传感器返回bool值是否为true,若为true,则返回火情信息 + if (DeviceCtrlHelper.adam4150Helper.SetData().fireValue) + { + return "发现火情!"; + } + return "正常!";//无火情返回正常信息 + } + + private void timer1_Tick(object sender, EventArgs e) + { + txt_time.Text = DateTime.Now.ToString(); + txt_even.Text = GetFireInfo(); + } + } +} diff --git a/samples/p5-t1-火焰/Program.cs b/samples/p5-t1-火焰/Program.cs new file mode 100644 index 0000000..0926518 --- /dev/null +++ b/samples/p5-t1-火焰/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace 火焰 +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/samples/p5-t1-火焰/meta.json b/samples/p5-t1-火焰/meta.json new file mode 100644 index 0000000..7778918 --- /dev/null +++ b/samples/p5-t1-火焰/meta.json @@ -0,0 +1,4 @@ +{ + "title": "项目5 任务1 采集火焰传感器数据", + "source": "教材书 p91–94,界面取自配套参考工程,控件名按书上表格修改" +} diff --git a/server/test/runner.test.ts b/server/test/runner.test.ts index fdb307d..c4765fb 100644 --- a/server/test/runner.test.ts +++ b/server/test/runner.test.ts @@ -1,6 +1,6 @@ // 端到端测试:直接启动 C# 运行器,模拟浏览器操作,检查教材示例工程的行为。 // 运行前先编译运行时:bun run build:runtime -import { afterAll, afterEach, expect, test } from 'bun:test' +import { afterAll, afterEach, describe, expect, test } from 'bun:test' import { cp, rm } from 'node:fs/promises' import { resolve } from 'node:path' import type { ControlNode, RunnerInput, RunnerMessage } from '@iot/shared' @@ -264,3 +264,63 @@ test('项目1 任务1:多窗体——打开增加用户窗体、添加用户 expect(second.form.id).not.toBe(addUser.id) expect(runner.messages.some((m) => m.type === 'exception')).toBe(false) }, 30000) + +describe('所有示例都能编译通过', async () => { + const ids = (await Array.fromAsync(new Bun.Glob('*/meta.json').scan({ cwd: resolve(ROOT, 'samples') }))).map((p) => p.split('/')[0]).sort() + for (const id of ids) { + test(id, async () => { + const result = await compileService.compile(await prepareSample(id)) + expect(result.diagnostics.filter((d) => d.severity === 'error')).toEqual([]) + }, 30000) + } +}) + +test('项目1 任务2:计算器(装箱拆箱)和多态', async () => { + const { runner, id } = await startSample('p1-t2-面向对象特性') + const textOf = (name: string) => + runner.next('patch', (m) => m.changes.some((c) => c.id === id(name) && c.prop === 'Text')).then( + (m) => m.changes.findLast((c) => c.id === id(name) && c.prop === 'Text')!.value, + ) + runner.send({ type: 'action', id: id('txtNum1'), action: 'text', value: '7' }) + runner.send({ type: 'action', id: id('txtNum2'), action: 'text', value: '2' }) + runner.send({ type: 'action', id: id('cmboxType'), action: 'select', index: 3 }) + const result = textOf('txtResult') + runner.send({ type: 'action', id: id('btnCalculate'), action: 'click' }) + expect(await result).toBe('3.5') + + const display = textOf('txtDispaly') + runner.send({ type: 'action', id: id('btnDisplay'), action: 'click' }) + expect(await display).toBe('张三说的是汉语!\r\n李四说的是汉语!\r\nMary说的是英语!\r\nJimmy说的是英语!\r\n') +}, 30000) + +/** 选 COM3、点"开启",等定时器读到实训台上的传感器 */ +async function openAdam4150(runner: Runner, id: (name: string) => string) { + runner.send({ type: 'action', id: id('com_port'), action: 'select', index: 2 }) + runner.send({ type: 'action', id: id('btn_open'), action: 'click' }) +} + +test('项目4 任务1:红外对射被遮挡时显示"发现入侵"', async () => { + const { runner, id } = await startSample('p4-t1-红外对射') + await openAdam4150(runner, id) + const even = (value: string) => runner.next('patch', (m) => m.changes.some((c) => c.id === id('txt_even') && c.value === value), 5000) + await even('正常') + runner.send({ type: 'board', analog: ANALOG_DEFAULT, digital: [false, false, false, true] }) + await even('发现入侵') +}, 30000) + +test('项目4 任务3:开报警灯——实训台 DO0 动作,窗体上的报警灯亮', async () => { + const { runner, id } = await startSample('p4-t3-报警灯') + await openAdam4150(runner, id) + const relay = runner.next('device', (m) => m.device === 'adam4150') + const lamp = runner.next('patch', (m) => m.changes.some((c) => c.id === id('alarm1') && c.prop === 'IsOn' && c.value === true)) + runner.send({ type: 'action', id: id('btn_alarmOpen'), action: 'click' }) + expect((await relay).data).toEqual([0, 1]) + await lamp +}, 30000) + +test('项目5 任务1:有火焰时显示"发现火情!"', async () => { + const { runner, id } = await startSample('p5-t1-火焰') + await openAdam4150(runner, id) + runner.send({ type: 'board', analog: ANALOG_DEFAULT, digital: [false, true, false, false] }) + await runner.next('patch', (m) => m.changes.some((c) => c.id === id('txt_even') && c.value === '发现火情!'), 5000) +}, 30000)