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

124 lines
3.1 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;
2023-07-12 15:27:27 +08:00
using SQLitePCL;
2023-07-12 12:11:10 +08:00
using Timer = System.Timers.Timer;
2023-07-12 15:27:27 +08:00
// ReSharper disable All
namespace BITFactory;
public partial class IDIS_THService : Node
{
2023-07-12 15:27:27 +08:00
public enum UpdateMode
{
None,
Update,
Insert,
}
[Export] private UpdateMode currentUpdateMode = UpdateMode.Insert;
[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;
2023-07-12 15:27:27 +08:00
[Export] private OptionButton updateModeButton;
[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;
2023-07-12 15:27:27 +08:00
2023-07-12 12:11:10 +08:00
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;
};
2023-07-12 15:27:27 +08:00
updateModeButton.GetPopup().Clear();
updateModeButton.GetPopup().AddItem("不更新");
updateModeButton.GetPopup().AddItem("更新记录");
updateModeButton.GetPopup().AddItem("添加记录");
updateModeButton.GetPopup().IndexPressed += index =>
{
currentUpdateMode = (UpdateMode)index;
};
2023-07-12 12:11:10 +08:00
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);
2023-07-12 15:27:27 +08:00
UpdateTH(handleEdit.Text,thReader.temperature.ToString(),thReader.humidity.ToString());
2023-07-12 12:11:10 +08:00
}
private void Submit()
{
switch (handleEdit.Text, temperatureEdit.Text, humidityEdit.Text)
{
case ("", _, _):
hintsLabel.Text = "请输入标识码";
return;
case (_, "", _):
hintsLabel.Text = "请输入温度";
return;
case (_, _, ""):
hintsLabel.Text = "请输入湿度";
return;
}
2023-07-12 15:27:27 +08:00
UpdateTH(handleEdit.Text,temperatureEdit.Text,humidityEdit.Text);
}
2023-07-12 15:27:27 +08:00
private void UpdateTH(string handle,string temperature,string humidity)
{
2023-07-12 15:27:27 +08:00
if (string.IsNullOrEmpty(handle))
2023-07-12 12:11:10 +08:00
{
autoUpdateLabel.SetTextAsync("空的标识码");
return;
}
2023-07-12 15:27:27 +08:00
switch (currentUpdateMode)
2023-07-12 12:11:10 +08:00
{
2023-07-12 15:27:27 +08:00
case UpdateMode.Insert:
2023-07-17 04:10:14 +08:00
service.Register(handle,"温度" ,"float", temperature, "环境");
service.Register(handle, "湿度","float", humidity, "环境");
2023-07-12 15:27:27 +08:00
break;
case UpdateMode.Update:
if (service.Update(handle, "温度",temperature) is false)
{
autoUpdateLabel.SetTextAsync("温度更新失败,未知异常");
return;
}
if (service.Update(handle, "湿度",humidity) is false)
{
autoUpdateLabel.SetTextAsync("湿度更新失败,未知异常");
return;
}
break;
default:
break;
2023-07-12 12:11:10 +08:00
}
autoUpdateLabel.SetTextAsync($"温湿度已自动更新:{DateTime.Now}");
}
}