Before update to NET 7

This commit is contained in:
CortexCore
2023-07-12 12:11:10 +08:00
parent ca824c3b32
commit 4af7cec47b
15 changed files with 480 additions and 214 deletions

View File

@@ -11,11 +11,9 @@ public partial class IDIS_RegisterService : Node
[ExportCategory("Services")]
[Export] private IDIS_Service service;
[Export] private IDIS_TemplateService templateService;
[ExportCategory("TemplateList")]
[Export] private ItemList templateList;
[ExportCategory("Register Data")]
[ExportCategory("UI 绑定")]
[Export] private NodeBuilder templateIndexBuilder;
[Export] private LineEdit handleEdit;
[Export] private Button generateHandleButton;
[Export] private Control registerContainer;
@@ -34,8 +32,6 @@ public partial class IDIS_RegisterService : Node
public override void _Ready()
{
templateList.ItemClicked += OnItemClicked;
registerButton.Pressed += Register;
generateHandleButton.Pressed += ()=>handleEdit.Text = IDIS_Service.GenerateHandle();
@@ -51,21 +47,20 @@ public partial class IDIS_RegisterService : Node
private void Rebuild()
{
if (Engine.IsEditorHint()) return;
templateList.Clear();
templateIndexBuilder.Clear();
foreach (var x in templateService.templates)
{
templateList.AddItem(x.TemplateName);
var container = templateIndexBuilder.Build<UXContainer>();
container.button.Text = x.TemplateName;
container.button.Pressed += () =>
{
OnItemClicked(x);
};
}
}
private void OnItemClicked(long index, Vector2 atPosition, long mouseButtonIndex)
private void OnItemClicked(IDIS_Template template)
{
MathNode.ClearChild(registerContainer);
MathNode.ClearChild(referenceContainer);
var template = templateService.templates[(int)index];
var grid = new GridContainer();
grid.Columns = 2;
grid.AddThemeConstantOverride("h_separation", 64);

View File

@@ -179,7 +179,7 @@ public class IDIS_DBContext:DbContext
Handle = handle
};
Values.Add(value);
SaveChanges();
SaveChangesAsync();
return true;
}
public void Register(string handle,string format, string value,string category)
@@ -197,7 +197,7 @@ public class IDIS_DBContext:DbContext
Category = category,
};
Datas.Add(data);
SaveChanges();
SaveChangesAsync();
}
public void RegisterReference(string handle, string refenceHandle)
@@ -207,19 +207,17 @@ public class IDIS_DBContext:DbContext
Handle = handle,
RelatedHandle = refenceHandle,
});
SaveChanges();
SaveChangesAsync();
}
public bool Update(string handle, string format, string value)
{
var result = Datas.FirstOrDefault(x => x.Handle == handle && x.Format == format);
if (result is not null)
{
result.UpdateTime=DateTime.Now;
result.Value = value;
SaveChanges();
}
return result is not null;
if (result is null) return false;
result.UpdateTime=DateTime.Now;
result.Value = value;
SaveChangesAsync();
return true;
}
}
// ReSharper disable once IdentifierTypo

View File

@@ -1,6 +1,12 @@
using Godot;
using System;
using System.Globalization;
using System.Text;
using System.Timers;
using BITFactory;
using BITKit;
using Cysharp.Threading.Tasks;
using Timer = System.Timers.Timer;
namespace BITFactory;
public partial class IDIS_THService : Node
@@ -12,14 +18,38 @@ public partial class IDIS_THService : Node
[ExportCategory("UI 绑定")]
[Export] private Button submitButton;
[Export] private CheckButton autoUpdateButton;
[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()
{
submitButton.Pressed += Submit;
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;
}
private async void AutoUpdate(object sender, ElapsedEventArgs e)
{
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
UpdateByReader();
}
private void Submit()
{
switch (handleEdit.Text, temperatureEdit.Text, humidityEdit.Text)
@@ -57,6 +87,21 @@ public partial class IDIS_THService : Node
private void UpdateByReader()
{
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}");
}
}

View File

@@ -1,12 +1,10 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using BITKit;
using BITKit.IO;
using RosSharp.RosBridgeClient.MessageTypes.Moveit;
using Godot;
namespace BITFactory;
[Serializable]
@@ -24,11 +22,11 @@ public partial class IDIS_TemplateService : Node
private static string assetPath => PathHelper.GetPath("标识注册模板.zip");
private const string templateName = $"{nameof(IDIS_Template)}.json";
public readonly List<IDIS_Template> templates=new();
private readonly Queue<IDIS_Template> selectedQueue = new();
[ExportCategory("Quick Start")]
[Export] private Button createButton;
[Export] private Button newFormatButton;
[Export] private ItemList itemList;
[Export] private NodeBuilder templateIndexBuilder;
[Export] private LineEdit templateNameEdit;
[Export] private LineEdit templateDescriptionEdit;
[Export] private Control container;
@@ -39,13 +37,13 @@ public partial class IDIS_TemplateService : Node
private bool isDirty;
private IDIS_Template _selectedTemplate=>_selectedIndex==-1?null:templates[_selectedIndex];
private int _selectedIndex=-1;
private IDIS_Template _selectedTemplate;
public override void _Ready()
{
if (File.Exists(assetPath) is false)
{
BIT4Log.Log<IDIS_TemplateService>($"未找到配置文件:{assetPath}");
Save();
}
var temp = BITAssets.Read<List<IDIS_Template>>(assetPath, templateName);
@@ -55,9 +53,6 @@ public partial class IDIS_TemplateService : Node
createButton.Pressed += CreateTemplate;
newFormatButton.Pressed += CreateFormat;
itemList.ItemSelected += OnItemSelected;
itemList.ItemClicked += OnItemClicked;
templateNameEdit.TextChanged += OnTemplateNameChanged;
templateDescriptionEdit.TextChanged += OnTemplateDescriptionChanged;
@@ -78,11 +73,7 @@ public partial class IDIS_TemplateService : Node
public override void _ExitTree()
{
BITAssets.Build(assetPath,new IAsset[]
{
new BITAsset(templateName,JsonHelper.Get(templates))
});
BIT4Log.Log<IDIS_TemplateService>($"已创建配置文件:{assetPath}");
Save();
}
private void CreateTemplate()
@@ -93,6 +84,15 @@ public partial class IDIS_TemplateService : Node
});
EnsureConfigure();
}
private void Save()
{
BITAssets.Build(assetPath,new IAsset[]
{
new BITAsset(templateName,JsonHelper.Get(templates))
});
BIT4Log.Log<IDIS_TemplateService>($"已创建配置文件:{assetPath}");
}
private void OnTemplateNameChanged(string text)
{
if (_selectedTemplate is null) return;
@@ -105,70 +105,68 @@ public partial class IDIS_TemplateService : Node
_selectedTemplate.TemplateDescription = text;
_selectedTemplate.UpdateTime= DateTime.Now;
}
private void EnsureConfigure()
{
itemList.Clear();
MathNode.ClearChild(container);
templateIndexBuilder.Clear();
foreach (var x in templates)
{
itemList.AddItem(x.TemplateName);
var _container = templateIndexBuilder.Build<UXContainer>();
_container.button.Text = x.TemplateName;
_container.button.Pressed += () =>
{
_selectedTemplate = x;
EnsureConfigure();
};
}
if (_selectedTemplate is null) return;
templateNameEdit.Text=_selectedTemplate.TemplateName;
templateDescriptionEdit.Text=_selectedTemplate.TemplateDescription;
templateCreateTimeLabel.Text = _selectedTemplate.CreateTime.ToString(CultureInfo.InvariantCulture);
templateUpdateTimeLabel.Text = _selectedTemplate.UpdateTime.ToString(CultureInfo.InvariantCulture);
for (var i = 0; i < _selectedTemplate.Formats.Count; i++)
templateNameEdit.Text = _selectedTemplate.TemplateName;
templateDescriptionEdit.Text = _selectedTemplate.TemplateDescription;
templateCreateTimeLabel.Text = _selectedTemplate.CreateTime.ToString(CultureInfo.InvariantCulture);
templateUpdateTimeLabel.Text = _selectedTemplate.UpdateTime.ToString(CultureInfo.InvariantCulture);
for (var i = 0; i < _selectedTemplate.Formats.Count; i++)
{
var x = _selectedTemplate.Formats[i];
var _container = templateContainer.Instantiate<UXContainer>();
_container.lineEdits[0].Text = x.format;
_container.lineEdits[1].Text = x.hint;
_container.lineEdits[2].Text = x.category;
var index = i;
_container.lineEdits[0].TextChanged += (s) =>
{
var x = _selectedTemplate.Formats[i];
var _container = templateContainer.Instantiate<UXContainer>();
_container.lineEdits[0].Text = x.format;
_container.lineEdits[1].Text = x.hint;
_container.lineEdits[2].Text = x.category;
var index = i;
_container.lineEdits[0].TextChanged += (s) =>
{
var current = _selectedTemplate.Formats[index];
current.format = s;
_selectedTemplate.Formats[index] = current;
_selectedTemplate.UpdateTime= DateTime.Now;
};
_container.lineEdits[1].TextChanged += s =>
{
var current = _selectedTemplate.Formats[index];
current.hint = s;
_selectedTemplate.Formats[index] = current;
};
_container.lineEdits[2].TextChanged += s =>
{
var current = _selectedTemplate.Formats[index];
current.category = s;
_selectedTemplate.Formats[index] = current;
};
_container.button.Pressed += () =>
{
_selectedTemplate.Formats.RemoveAt(index);
EnsureConfigure();
};
container.AddChild(_container);
}
}
private void OnItemSelected(long id)
{
}
private void OnItemClicked(long index, Vector2 atPosition, long mouseButtonIndex)
{
// var currentItem = _templates[(int)index];
// templateNameEdit.Text = currentItem.TemplateName;
_selectedIndex = (int)index;
EnsureConfigure();
var current = _selectedTemplate.Formats[index];
current.format = s;
_selectedTemplate.Formats[index] = current;
_selectedTemplate.UpdateTime = DateTime.Now;
};
_container.lineEdits[1].TextChanged += s =>
{
var current = _selectedTemplate.Formats[index];
current.hint = s;
_selectedTemplate.Formats[index] = current;
};
_container.lineEdits[2].TextChanged += s =>
{
var current = _selectedTemplate.Formats[index];
current.category = s;
_selectedTemplate.Formats[index] = current;
};
_container.button.Pressed += () =>
{
_selectedTemplate.Formats.RemoveAt(index);
EnsureConfigure();
};
container.AddChild(_container);
}
}
}

View File

@@ -1,5 +1,7 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using BITKit;
namespace BITFactory;
@@ -7,18 +9,26 @@ public partial class IDIS_UpdateService : Node
{
[ExportCategory("Service")]
[Export] private IDIS_Service service;
[Export] private IDIS_TemplateService templateService;
[ExportCategory("UI 绑定")]
[Export] private NodeBuilder indexBuilder;
[Export] private NodeBuilder templateBuilder;
[Export] private LineEdit handleEdit;
[Export] private Button submitButton;
[Export] private RichTextLabel hintsLabel;
private ButtonGroup _buttonGroup;
private readonly Dictionary<string,LineEdit> dataEdits = new();
public override void _Ready()
{
_buttonGroup = new ButtonGroup();
submitButton.Pressed += Submit;
submitButton.Disabled = true;
Refresh();
}
@@ -38,11 +48,36 @@ public partial class IDIS_UpdateService : Node
private void Entry(IDIS_Template template)
{
templateBuilder.Clear();
dataEdits.Clear();
foreach (var x in template.Formats)
{
var container = templateBuilder.Build<UXContainer>();
container.Text = x.format;
container.lineEdit.PlaceholderText = x.hint;
dataEdits.TryAdd(x.format, container.lineEdit);
}
submitButton.Disabled = false;
}
private void Submit()
{
if (string.IsNullOrEmpty(handleEdit.Text))
{
hintsLabel.Text="请填写标识码";
return;
}
var handle = handleEdit.Text;
var values = new Dictionary<string, string>(
dataEdits
.Where(x=>string.IsNullOrEmpty(x.Value.Text) is false)
.Select(x => new KeyValuePair<string, string>(x.Key, x.Value.Text))
);
foreach (var x in values)
{
service.Update(handle, x.Key, x.Value);
}
hintsLabel.Text=$"更新成功,已更新{values.Count}个值"+DateTime.Now;
}
}

View File

@@ -1,6 +1,8 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Mime;
using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;
@@ -13,7 +15,7 @@ namespace BITFactory;
public partial class 湿Reader : Node
{
[ExportCategory("参数")]
[Export(PropertyHint.Range,"100,1000")] private int interval=500;
[Export(PropertyHint.Range,"100,2000")] private int interval=500;
[ExportCategory("网络设置")]
[Export] private string ip="192.168.3.7";
@@ -33,6 +35,7 @@ public partial class 温湿度Reader : Node
private ModbusMaster _modbus;
private System.Timers.Timer timer;
private CancellationTokenSource _CancellationTokenSource;
public override void _Ready()
{
_CancellationTokenSource = new CancellationTokenSource();
@@ -67,11 +70,13 @@ public partial class 温湿度Reader : Node
}
}
}
UpdatePortAndIP();
timer = new Timer();
timer.Interval = interval;
timer.Elapsed += OnTimerElapsed;
timer.AutoReset = true;
timer.AutoReset = false;
timer.Start();
}
@@ -79,29 +84,42 @@ public partial class 温湿度Reader : Node
{
timer.Stop();
}
/// <summary>
/// 内部方法,用于更新IP和端口
/// </summary>
private void UpdatePortAndIP()
{
_modbus.Dispose();
_modbus = ModbusMaster.TCP(ip, port);
_modbus?.Dispose();
try
{
_modbus = ModbusMaster.TCP(ip, port);
}
catch (Exception e)
{
BIT4Log.Log<湿Reader>(e.Message);
}
}
/// <summary>
/// 内部方法,定时器回调用于读取Modbus
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
SetHints( "正在获取温湿度数据..."+DateTime.Now);
_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 })
{
SetHints(hintsLabel.Text = $"获取温湿度数据失败:数据长度为:{vs.Length}"+DateTime.Now);
hintsLabel.SetTextAsync(hintsLabel.Text = $"获取温湿度数据失败:数据长度为:{vs.Length}"+DateTime.Now);
return;
}
@@ -109,25 +127,21 @@ public partial class 温湿度Reader : Node
temperature = vs[0] / 10.0;
humidity = vs[1] / 10.0;
SetHints("已获取到温湿度数据:"+DateTime.Now);
hintsLabel.SetTextAsync("已获取到温湿度数据:"+DateTime.Now);
temperatureContaier.Text = temperature.ToString(CultureInfo.InvariantCulture);
humidityContainer.Text = humidity.ToString(CultureInfo.InvariantCulture);
temperatureContaier.label.SetTextAsync(temperature.ToString(CultureInfo.InvariantCulture)); ;
humidityContainer.label.SetTextAsync(humidity.ToString(CultureInfo.InvariantCulture));
}
catch (OperationCanceledException)
{
//SetHints("连接超时:"+DateTime.Now);
hintsLabel.SetTextAsync("连接超时:"+DateTime.Now);
}
catch (Exception ex)
{
SetHints(ex.Message);
GD.Print("ex:" + ex);
hintsLabel.SetTextAsync(ex.Message);
//GD.Print("ex:" + ex);
}
timer.Start();
}
private async void SetHints(string hints)
{
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
hintsLabel.Text = hints;
}
}