提交更新

This commit is contained in:
CortexCore
2023-07-12 15:27:27 +08:00
parent 4af7cec47b
commit 498b0617f8
13 changed files with 294 additions and 154 deletions

View File

@@ -1,8 +1,10 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using BITKit;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
namespace BITFactory;
@@ -16,7 +18,7 @@ public partial class IDIS_RegisterService : Node
[Export] private NodeBuilder templateIndexBuilder;
[Export] private LineEdit handleEdit;
[Export] private Button generateHandleButton;
[Export] private Control registerContainer;
[Export] private NodeBuilder templateFormatBuilder;
[Export] private Button registerButton;
[Export] private ProgressBar registerProgress;
@@ -27,7 +29,7 @@ public partial class IDIS_RegisterService : Node
[ExportCategory("Hints")]
[Export] private Label hints;
private readonly Dictionary<int,IDIS_Data> _currentValues = new();
private readonly List<(string format,string value,string category)> _currentValues = new();
private readonly List<string> _currentReferences = new();
public override void _Ready()
@@ -48,51 +50,40 @@ public partial class IDIS_RegisterService : Node
{
if (Engine.IsEditorHint()) return;
templateIndexBuilder.Clear();
templateFormatBuilder.Clear();
foreach (var x in templateService.templates)
{
var container = templateIndexBuilder.Build<UXContainer>();
container.button.Text = x.TemplateName;
container.button.Pressed += () =>
{
OnItemClicked(x);
Entry(x);
};
}
}
private void OnItemClicked(IDIS_Template template)
private void Entry(IDIS_Template template)
{
var grid = new GridContainer();
grid.Columns = 2;
grid.AddThemeConstantOverride("h_separation", 64);
registerContainer.AddChild(grid);
_currentValues.Clear();
var _dirIndex = 0;
templateFormatBuilder.Clear();
var currentIndex = 0;
foreach (var x in template.Formats)
{
var label = new Label();
var lineEdit = new LineEdit();
var myIndex = currentIndex++;
var myIndex = _dirIndex++;
_currentValues.Add((x.format, string.Empty, x.category));
var formatContainer = templateFormatBuilder.Build<UXContainer>();
label.Text = x.format;
formatContainer.labels[0].Text = x.format;
formatContainer.lineEdits[0].PlaceholderText = x.hint;
lineEdit.PlaceholderText = x.hint;
lineEdit.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
_currentValues.Add(myIndex, new ()
formatContainer.lineEdits[0].TextChanged += s =>
{
Format = x.format,
Value = x.hint,
Category = x.category
});
lineEdit.TextChanged += (s) => { _currentValues[myIndex].Value = s; };
grid.AddChild(label);
grid.AddChild(lineEdit);
var value = _currentValues[myIndex];
value.value = s;
_currentValues[myIndex] = value;
};
}
handleEdit.Editable = true;
@@ -117,6 +108,13 @@ public partial class IDIS_RegisterService : Node
}
private void Register()
{
if (_currentValues.Any(x =>
string.IsNullOrEmpty(x.format) || string.IsNullOrEmpty(x.value) || string.IsNullOrEmpty(x.category)))
{
hints.SetTextAsync("请填写完整的数据格式");
return;
}
var handle = string.IsNullOrEmpty(handleEdit.Text) ? IDIS_Service.GenerateHandle() : handleEdit.Text;
var dataJson = JsonHelper.Get(_currentValues);
var referenceJson = JsonHelper.Get(_currentReferences);
@@ -131,7 +129,7 @@ public partial class IDIS_RegisterService : Node
service.Register(handle);
foreach (var x in _currentValues)
{
service.Register(handle, x.Value.Format, x.Value.Value,x.Value.Category);
service.Register(handle, x.format, x.value,x.category);
}
foreach (var x in _currentReferences)
{

View File

@@ -130,6 +130,7 @@ public class IDIS_DBContext:DbContext
//var sql = SQLiteContextHelper.GetConnectionString("IDIS");
SQLiteContextHelper.GetConnectionSqlAndPath("IDIS",out var sql,out var path);
optionsBuilder.UseSqlite(sql);
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll);
BIT4Log.Log<IDIS_DBContext>($"已创建标识数据库:{path}");
}
@@ -141,8 +142,9 @@ public class IDIS_DBContext:DbContext
/// <returns>是否查询到了内容</returns>
public bool Query(string key, out IDIS_Query[] queries)
{
ChangeTracker.DetectChanges();
ChangeTracker.Clear();
queries = Values
.ToList()
.Where(x => x.Handle.Contains(key))
.Select(x => new IDIS_Query()
{
@@ -233,6 +235,13 @@ public partial class IDIS_Service:Node
BIT4Log.Log<IDIS_Service>("已创建标识数据库");
UniTask.Run(()=>Context.Database.EnsureCreatedAsync());
}
public override void _ExitTree()
{
Context.Dispose();
Context = null;
}
public bool Query(string word,out IDIS_Query[] queries) => Context.Query(word, out queries);
public bool Register(string handle) => Context.Register(handle);
public void Register(string handle, string format, string value,string category) => Context.Register(handle, format, value,category);

View File

@@ -6,11 +6,22 @@ using System.Timers;
using BITFactory;
using BITKit;
using Cysharp.Threading.Tasks;
using SQLitePCL;
using Timer = System.Timers.Timer;
// ReSharper disable All
namespace BITFactory;
public partial class IDIS_THService : Node
{
public enum UpdateMode
{
None,
Update,
Insert,
}
[Export] private UpdateMode currentUpdateMode = UpdateMode.Insert;
[ExportCategory("Services")]
[Export] private IDIS_Service service;
@@ -19,12 +30,13 @@ public partial class IDIS_THService : Node
[ExportCategory("UI 绑定")]
[Export] private Button submitButton;
[Export] private CheckButton autoUpdateButton;
[Export] private OptionButton updateModeButton;
[Export] private LineEdit handleEdit;
[Export] private LineEdit temperatureEdit;
[Export] private LineEdit humidityEdit;
[Export] private RichTextLabel hintsLabel;
[Export] private RichTextLabel autoUpdateLabel;
private Timer autoUpdateTimer;
public override void _Ready()
{
@@ -38,6 +50,15 @@ public partial class IDIS_THService : Node
autoUpdateTimer.Enabled = toggle;
};
updateModeButton.GetPopup().Clear();
updateModeButton.GetPopup().AddItem("不更新");
updateModeButton.GetPopup().AddItem("更新记录");
updateModeButton.GetPopup().AddItem("添加记录");
updateModeButton.GetPopup().IndexPressed += index =>
{
currentUpdateMode = (UpdateMode)index;
};
autoUpdateTimer = new Timer();
autoUpdateTimer.AutoReset = true;
autoUpdateTimer.Interval = 1000;
@@ -47,7 +68,7 @@ public partial class IDIS_THService : Node
private async void AutoUpdate(object sender, ElapsedEventArgs e)
{
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
UpdateByReader();
UpdateTH(handleEdit.Text,thReader.temperature.ToString(),thReader.humidity.ToString());
}
private void Submit()
@@ -64,43 +85,38 @@ public partial class IDIS_THService : Node
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;
}
UpdateTH(handleEdit.Text,temperatureEdit.Text,humidityEdit.Text);
}
private void UpdateByReader()
private void UpdateTH(string handle,string temperature,string humidity)
{
if (string.IsNullOrEmpty(handleEdit.Text))
if (string.IsNullOrEmpty(handle))
{
autoUpdateLabel.SetTextAsync("空的标识码");
return;
}
if (service.Update(handleEdit.Text, "温度", thReader.temperature.ToString(CultureInfo.InvariantCulture)) is false)
switch (currentUpdateMode)
{
autoUpdateLabel.SetTextAsync("温度更新失败,未知异常");
return;
}
if (service.Update(handleEdit.Text, "湿度", thReader.humidity.ToString(CultureInfo.InvariantCulture)) is false)
{
autoUpdateLabel.SetTextAsync("湿度更新失败,未知异常");
return;
case UpdateMode.Insert:
service.Register(handle ,"温度", temperature, "环境");
service.Register(handle, "湿度", humidity, "环境");
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;
}
autoUpdateLabel.SetTextAsync($"温湿度已自动更新:{DateTime.Now}");
}

View File

@@ -27,9 +27,9 @@ public partial class IDIS_TemplateService : Node
[Export] private Button createButton;
[Export] private Button newFormatButton;
[Export] private NodeBuilder templateIndexBuilder;
[Export] private NodeBuilder templateFormatBuilder;
[Export] private LineEdit templateNameEdit;
[Export] private LineEdit templateDescriptionEdit;
[Export] private Control container;
[Export] private Label templateCreateTimeLabel;
[Export] private Label templateUpdateTimeLabel;
[ExportCategory("Template")]
@@ -56,9 +56,6 @@ public partial class IDIS_TemplateService : Node
templateNameEdit.TextChanged += OnTemplateNameChanged;
templateDescriptionEdit.TextChanged += OnTemplateDescriptionChanged;
MathNode.ClearChild(container);
EnsureConfigure();
}
@@ -108,9 +105,8 @@ public partial class IDIS_TemplateService : Node
private void EnsureConfigure()
{
MathNode.ClearChild(container);
templateIndexBuilder.Clear();
templateFormatBuilder.Clear();
foreach (var x in templates)
{
@@ -133,7 +129,7 @@ public partial class IDIS_TemplateService : Node
for (var i = 0; i < _selectedTemplate.Formats.Count; i++)
{
var x = _selectedTemplate.Formats[i];
var _container = templateContainer.Instantiate<UXContainer>();
var _container = templateFormatBuilder.Build<UXContainer>();
_container.lineEdits[0].Text = x.format;
_container.lineEdits[1].Text = x.hint;
@@ -165,8 +161,6 @@ public partial class IDIS_TemplateService : Node
_selectedTemplate.Formats.RemoveAt(index);
EnsureConfigure();
};
container.AddChild(_container);
}
}
}

View File

@@ -82,6 +82,7 @@ public partial class 温湿度Reader : Node
public override void _ExitTree()
{
_CancellationTokenSource.Cancel();
timer.Stop();
}
/// <summary>
@@ -107,14 +108,16 @@ public partial class 温湿度Reader : Node
/// <param name="e"></param>
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
_CancellationTokenSource.Cancel();
await UniTask.SwitchToTaskPool();
try
{
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
hintsLabel.SetTextAsync( "正在读取温湿度数据..."+DateTime.Now);
var vs = _modbus.ReadInputRegisters(1, 0, 2);
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
hintsLabel.SetTextAsync( "已采集到数据,正在解析..."+DateTime.Now);
if (vs is not { Length: 2 })
@@ -131,17 +134,19 @@ public partial class 温湿度Reader : Node
temperatureContaier.label.SetTextAsync(temperature.ToString(CultureInfo.InvariantCulture)); ;
humidityContainer.label.SetTextAsync(humidity.ToString(CultureInfo.InvariantCulture));
timer.Start();
}
catch (OperationCanceledException)
{
hintsLabel.SetTextAsync("连接超时:"+DateTime.Now);
BIT4Log.Log<湿Reader>($"Modbus读取任务已取消");
}
catch (Exception ex)
{
hintsLabel.SetTextAsync(ex.Message);
//GD.Print("ex:" + ex);
timer.Start();
}
timer.Start();
}
}