iFactory.Godot/Mods/工业数据采集与分析应用分享/Scripts/IDIS_THService.cs

108 lines
2.8 KiB
C#
Raw Normal View History

using Godot;
using System;
2023-07-12 12:11:10 +08:00
using System.Globalization;
using System.Text;
using System.Timers;
using BITFactory;
2023-07-12 12:11:10 +08:00
using BITKit;
using Cysharp.Threading.Tasks;
using Timer = System.Timers.Timer;
namespace BITFactory;
public partial class IDIS_THService : Node
{
[ExportCategory("Services")]
[Export] private IDIS_Service service;
[Export] private 湿Reader thReader;
[ExportCategory("UI 绑定")]
[Export] private Button submitButton;
2023-07-12 12:11:10 +08:00
[Export] private CheckButton autoUpdateButton;
[Export] private LineEdit handleEdit;
[Export] private LineEdit temperatureEdit;
[Export] private LineEdit humidityEdit;
[Export] private RichTextLabel hintsLabel;
2023-07-12 12:11:10 +08:00
[Export] private RichTextLabel autoUpdateLabel;
private Timer autoUpdateTimer;
public override void _Ready()
{
submitButton.Pressed += Submit;
2023-07-12 12:11:10 +08:00
autoUpdateButton.Toggled += toggle =>
{
submitButton.Disabled = toggle;
handleEdit.Editable = !toggle;
temperatureEdit.Editable = !toggle;
humidityEdit.Editable = !toggle;
autoUpdateTimer.Enabled = toggle;
};
autoUpdateTimer = new Timer();
autoUpdateTimer.AutoReset = true;
autoUpdateTimer.Interval = 1000;
autoUpdateTimer.Elapsed += AutoUpdate;
}
2023-07-12 12:11:10 +08:00
private async void AutoUpdate(object sender, ElapsedEventArgs e)
{
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
UpdateByReader();
}
private void Submit()
{
switch (handleEdit.Text, temperatureEdit.Text, humidityEdit.Text)
{
case ("", _, _):
hintsLabel.Text = "请输入标识码";
return;
case (_, "", _):
hintsLabel.Text = "请输入温度";
return;
case (_, _, ""):
hintsLabel.Text = "请输入湿度";
return;
}
try
{
if (service.Update(handleEdit.Text, "温度", humidityEdit.Text) is false)
{
hintsLabel.Text = "温度更新失败: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
return;
}
if (service.Update(handleEdit.Text, "湿度", temperatureEdit.Text) is false)
{
hintsLabel.Text = "湿度更新失败: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
return;
}
hintsLabel.Text = "更新成功: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
catch (Exception e)
{
hintsLabel.Text = e.Message;
throw;
}
}
private void UpdateByReader()
{
2023-07-12 12:11:10 +08:00
if (string.IsNullOrEmpty(handleEdit.Text))
{
autoUpdateLabel.SetTextAsync("空的标识码");
return;
}
if (service.Update(handleEdit.Text, "温度", thReader.temperature.ToString(CultureInfo.InvariantCulture)) is false)
{
autoUpdateLabel.SetTextAsync("温度更新失败,未知异常");
return;
}
if (service.Update(handleEdit.Text, "湿度", thReader.humidity.ToString(CultureInfo.InvariantCulture)) is false)
{
autoUpdateLabel.SetTextAsync("湿度更新失败,未知异常");
return;
}
autoUpdateLabel.SetTextAsync($"温湿度已自动更新:{DateTime.Now}");
}
}