Files
IOT_SIM/samples/p7-t2-智慧寝室/Form1.cs
T
xuyueandClaude Opus 5.5 eedf1aa2fb 项目 7 情景控制:虚拟时钟、项目资源、Windows Media Player、TrackBar
- 虚拟时钟:实训台时钟面板可拨时间、1×/10×/60× 倍速、恢复真实时间;编译时把
  DateTime.Now / Today / UtcNow 改写成 SimClock;评分规则加 clock 步骤
- 项目资源(Properties.Resources):平台素材库(自绘开关图片、合成门铃声),"项目资源"对话框勾选,
  平台生成 Properties/Resources.Designer.cs;设计器图片框的"图片"属性可选资源;程序目录下的 Resources
  指向素材库;未添加资源时给中文提示
- Windows Media Player(AxWMPLib):设置 URL 自动播放、Ctlcontrols.play/stop,浏览器里播放声音,
  实训台加广播喇叭;编译时给窗体嵌空资源,OcxState 取到 null 不报错;图片和声音统一按内容缓存(blob)
- TrackBar:拖动、点轨道、方向键,先 ValueChanged 再 Scroll;设计器加"滑动条""媒体播放器"
- 评分规则加 value、media 检查;示例项目 7 任务 1–3(修正书上开关灯判断写反、漏填串口列表)
- 修正:Properties/ 下的资源类不当作窗体;带命名空间的 System.AppDomain.CurrentDomain.BaseDirectory 放行
- 测试:虚拟时钟、TrackBar、项目 7 示例评分、作业起点带资源类

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:11:05 +08:00

164 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;//引用系统IO端口库
using DeviceCtrlLibrary;//引用自定义设备控制库
using DeviceCtrlLibrary.Model;//引用自定义设备控制模型库
using System.Threading;//引用线程库
namespace 寝室情景模式
{
public partial class frm_control : Form
{
bool IsOpen = false;//定义布尔变量记录串口打开情况,默认为false
bool IsLampOopen = false;//定义布尔变量记录灯光打开情况,默认为false
bool IsBoardcastOpen = false;//定义布尔变量记录广播打开情况,默认为false
ADAM4017ResModel ResModel;//创建4017接收数据模型
DateTime now;
DateTime GetUp = DateTime.Today.AddHours(6.75);//6:45起床时间
DateTime ClassBegin = DateTime.Today.AddHours(7.5);//7:30上课开始时间
DateTime ClassEnd = DateTime.Today.AddHours(21.0);//21:00上课结束时间
DateTime Sleep = DateTime.Today.AddHours(22.0);//22:00睡觉时间
public frm_control()
{
InitializeComponent();
}
private void frm_control_Load(object sender, EventArgs e)
{
com_import.Items.Clear();
//将系统串口名依次加入下拉列表框中
foreach (string item in SerialPort.GetPortNames())
{
com_import.Items.Add(item);
}
}
private void btn_apply_Click(object sender, EventArgs e)
{
if (!IsOpen)
{
IsOpen = true;
btn_apply.Text = "停止情景模式";
DeviceCtrlHelper.adam4017Helper.Open(com_import.Text);
DeviceCtrlHelper.adam4150Helper.Open(com_import.Text);
timer.Start();
}
else
{
btn_apply.Text = "开启情景模式";
timer.Stop();
IsOpen = false;
DeviceCtrlHelper.adam4017Helper.Close();
DeviceCtrlHelper.adam4150Helper.Close();
}
}
private void CheckMode()//切换各个模式
{
if (now >= GetUp && now < ClassBegin)
{
lbl_mode.Text = "智慧寝室正处于起床模式";
if (IsLampOopen == false)
{
IsLampOopen = true;
DeviceCtrlHelper.adam4150Helper.OnOff(1, true);
lamp.OnOff(true);
}
if (IsBoardcastOpen == false)
{
IsBoardcastOpen = true;
Thread playthd = new Thread(PlayMp3);
playthd.Start();
}
trb_curtain.Value = 5;
}
else if (now >= ClassBegin && now < ClassEnd)
{
lbl_mode.Text = "智慧寝室正处于上课模式";
if (IsLampOopen == true)
{
IsLampOopen = false;
DeviceCtrlHelper.adam4150Helper.OnOff(1, false);
lamp.OnOff(false);
}
if (IsBoardcastOpen == true)
{
IsBoardcastOpen = false;
pic_boardcastStatus.Image = Properties.Resources.switch_off;
}
trb_curtain.Value = 0;
}
else if (now >= ClassEnd && now < Sleep)
{
lbl_mode.Text = "智慧寝室正处于回寝模式";
if (IsLampOopen == false)
{
IsLampOopen = true;
DeviceCtrlHelper.adam4150Helper.OnOff(1, true);
lamp.OnOff(true);
}
if (IsBoardcastOpen == false)
{
IsBoardcastOpen = true;
Thread playthd = new Thread(PlayMp3);
playthd.Start();
}
trb_curtain.Value = 10;
}
else
{
lbl_mode.Text = "智慧寝室正处于熄灯模式";
if (IsLampOopen == true)
{
IsLampOopen = false;
DeviceCtrlHelper.adam4150Helper.OnOff(1, false);
lamp.OnOff(false);
}
if (IsBoardcastOpen == true)
{
IsBoardcastOpen = false;
pic_boardcastStatus.Image = Properties.Resources.switch_off;
}
trb_curtain.Value = 10;
}
}
private void timer_Tick(object sender, EventArgs e)
{
now = DateTime.Now;
txt_time.Text = now.ToString("HH:mm:ss");//显示时间
ResModel = DeviceCtrlHelper.adam4017Helper.SetData();//采集数据
//显示采集到的数据
txt_light.Text = ResModel.lightValue;//ResModel.airPressureValue
txt_temperature.Text = ResModel.temperatureValue;//ResModel.airQualityValue
txt_humidity.Text = ResModel.humidityValue;//ResModel.CO2Value
CheckMode();
}
private void PlayMp3()//播放mp3音乐
{
pic_boardcastStatus.Image = Properties.Resources.switch_on;
axWindowsMediaPlayer1.URL = System.AppDomain.CurrentDomain.BaseDirectory + "//Resources//ring.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
Thread.Sleep(4000);
pic_boardcastStatus.Image = Properties.Resources.switch_off;
}
}
}