温湿度传感器更新

现在可以手动更新温湿度了
This commit is contained in:
CortexCore
2023-07-10 00:00:20 +08:00
parent 10c90cee9a
commit 6301e2d3ad
19 changed files with 754 additions and 132 deletions

View File

@@ -29,7 +29,7 @@ public partial class IDIS_RegisterService : Node
[ExportCategory("Hints")]
[Export] private Label hints;
private readonly Dictionary<int, KeyValuePair<string, string>> _currentValues = new();
private readonly Dictionary<int,IDIS_Data> _currentValues = new();
private readonly List<string> _currentReferences = new();
public override void _Ready()
@@ -79,7 +79,7 @@ public partial class IDIS_RegisterService : Node
{
var label = new Label();
var lineEdit = new LineEdit();
var myIndex = _dirIndex++;
label.Text = x.format;
@@ -87,9 +87,14 @@ public partial class IDIS_RegisterService : Node
lineEdit.PlaceholderText = x.hint;
lineEdit.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
_currentValues.Add(myIndex, new KeyValuePair<string, string>(x.format, x.hint));
_currentValues.Add(myIndex, new ()
{
Format = x.format,
Value = x.hint,
Category = x.category
});
lineEdit.TextChanged += (s) => { _currentValues[myIndex] = new KeyValuePair<string, string>(x.format, s); };
lineEdit.TextChanged += (s) => { _currentValues[myIndex].Value = s; };
grid.AddChild(label);
grid.AddChild(lineEdit);
@@ -131,7 +136,7 @@ public partial class IDIS_RegisterService : Node
service.Register(handle);
foreach (var x in _currentValues)
{
service.Register(handle, x.Value.Key, x.Value.Value);
service.Register(handle, x.Value.Format, x.Value.Value,x.Value.Category);
}
foreach (var x in _currentReferences)
{

View File

@@ -1,5 +1,6 @@
using Godot;
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using BITKit;
@@ -14,10 +15,25 @@ public partial class IDIS_SearchService : Node
[Export] private LineEdit searchEdit;
[Export] private Control searchCandidateContainer;
[Export] private StringResource searchButtonVariation;
[Export] private Control searchEditPadding;
[ExportCategory("Query 绑定")]
[Export] private Label handleLabel;
[Export] private Label nameLabel;
[Export] private Label createTimeLabel;
[Export] private Label updateTimeLabel;
[Export] private Control valueContainer;
[Export] private Control referenceContainer;
[ExportCategory("Template")]
[Export] private PackedScene valueTemplate;
[Export] private PackedScene referenceTemplate;
[Export] private PackedScene categoryTemplate;
public override void _Ready()
{
MathNode.RemoveAllChild(searchCandidateContainer);
MathNode.RemoveAllChild(valueContainer);
searchEdit.TextChanged += Search;
//searchEdit.FocusExited += Clear;
@@ -25,7 +41,7 @@ public partial class IDIS_SearchService : Node
private void Search(string word)
{
MathNode.RemoveAllChild(searchCandidateContainer);
if (service.TrySearch(word, out var queries) is false) return;
if (service.Query(word, out IDIS_Query[] queries) is false) return;
if(queries.Length is 1 && queries.First().Handle == word)return;
foreach (var query in queries)
{
@@ -45,6 +61,7 @@ public partial class IDIS_SearchService : Node
{
searchEdit.Text = query.Handle;
Search(query.Handle);
QueryIDIS(query);
}
}
}
@@ -53,6 +70,51 @@ public partial class IDIS_SearchService : Node
await Task.Delay(100);
MathNode.RemoveAllChild(searchCandidateContainer);
}
private void QueryIDIS(IDIS_Query query)
{
searchEditPadding.Hide();
handleLabel.Text = query.Handle;
createTimeLabel.Text = query.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
createTimeLabel.Text = query.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss");
MathNode.RemoveAllChild(valueContainer);
MathNode.RemoveAllChild(referenceContainer);
foreach (var categoryGroup in query.Datas.GroupBy(x=>x.Category))
{
var categoryContainer = categoryTemplate.Instantiate<UXContainer>();
categoryContainer.Text = categoryGroup.First().Category;
foreach (var x in categoryGroup)
{
var container = valueTemplate.Instantiate<UXContainer>();
container.labels[0].Text = x.Format;
container.labels[1].Text = x.Value;
container.labels[2].Text = x.UpdateTime.ToString(CultureInfo.InvariantCulture);
container.labels[3].Text = x.CreateTime.ToString(CultureInfo.InvariantCulture);
categoryContainer.contextContainer.AddChild(container);
}
valueContainer.AddChild(categoryContainer);
}
foreach (var x in query.References)
{
var container = referenceTemplate.Instantiate<UXContainer>();
container.Text = x.RelatedHandle;
container.button.Pressed += () =>
{
service.Query(x.RelatedHandle,out IDIS_Query _query);
QueryIDIS(_query);
};
referenceContainer.AddChild(container);
}
}
}

View File

@@ -3,11 +3,14 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Mime;
using System.Reflection.Metadata;
using BITKit;
using Cysharp.Threading.Tasks;
using Godot;
using Microsoft.EntityFrameworkCore;
using RosSharp.RosBridgeClient.MessageTypes.Sensor;
using Constant = BITKit.Constant;
using String = RosSharp.RosBridgeClient.MessageTypes.Std.String;
namespace BITFactory;
// ReSharper disable once IdentifierTypo
@@ -17,11 +20,11 @@ public abstract class IDIS_Base
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateDate { get; set; }=DateTime.Now;
public DateTime CreateTime { get; set; }=DateTime.Now;
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdateDate { get; set; }=DateTime.Now;
public DateTime UpdateTime { get; set; }=DateTime.Now;
}
// ReSharper disable once IdentifierTypo
/// <summary>
@@ -76,6 +79,10 @@ public class IDIS_Data:IDIS_Base
/// 值
/// </summary>
public string Value { get; set; }
/// <summary>
/// 类型,用于解析时对数据条目进行分类
/// </summary>
public string Category { get; set; }
}
/// <summary>
/// 标识的引用或关联
@@ -140,10 +147,10 @@ public class IDIS_DBContext:DbContext
.Select(x => new IDIS_Query()
{
Handle = x.Handle,
CreateDate = x.CreateDate,
UpdateDate = x.UpdateDate,
Datas = Datas.Where(data => data.Handle == key).ToArray(),
References = References.Where(reference => reference.Handle == key).ToArray()
CreateTime = x.CreateTime,
UpdateTime = x.UpdateTime,
Datas = Datas.Where(data => data.Handle == x.Handle).ToArray(),
References = References.Where(reference => reference.Handle == x.Handle).ToArray()
}).ToArray();
return queries.Any();
}
@@ -158,15 +165,6 @@ public class IDIS_DBContext:DbContext
{
Query(key, out IDIS_Query[] queries);
query = queries.FirstOrDefault();
var = Datas
//查询相关的标识
.Where(x => x.Handle == "MyHandle")
.OrderBy(x=>x.CreateDate)
//从标识中查找"MyFormat"
.First(x => x.Format == "Temperature").Value;
return queries.Any();
}
public bool Register(string handle)
@@ -184,7 +182,7 @@ public class IDIS_DBContext:DbContext
SaveChanges();
return true;
}
public void Register(string handle,string format, string value)
public void Register(string handle,string format, string value,string category)
{
var handleExists = Values.Any(x => x.Handle == handle);
if (!handleExists)
@@ -195,7 +193,8 @@ public class IDIS_DBContext:DbContext
{
Handle = handle,
Format = format,
Value = value
Value = value,
Category = category,
};
Datas.Add(data);
SaveChanges();
@@ -210,6 +209,27 @@ public class IDIS_DBContext:DbContext
});
SaveChanges();
}
public void Update(string handle, string format, string value)
{
// var datas =Datas.ToList();
// var result = datas.Where(x => x.Handle == handle && x.Format == format).ToArray();
// foreach (var x in result)
// {
// Datas.Remove(x);
// }
// foreach (var element in result)
// {
// element.Value = value;
// element.UpdateTime=DateTime.Now;
// Datas.Add(element);
// }
// SaveChanges();
var result = Datas.Single(x => x.Handle == handle && x.Format == format);
result.UpdateTime=DateTime.Now;
result.Value = value;
SaveChanges();
}
}
// ReSharper disable once IdentifierTypo
/// <summary>
@@ -224,9 +244,11 @@ public partial class IDIS_Service:Node
BIT4Log.Log<IDIS_Service>("已创建标识数据库");
UniTask.Run(()=>Context.Database.EnsureCreatedAsync());
}
public bool TrySearch(string word,out IDIS_Query[] queries) => Context.Query(word, out queries);
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) => Context.Register(handle, format, value);
public void Register(string handle, string format, string value,string category) => Context.Register(handle, format, value,category);
public void RegisterReference(string handle,string refenceHandle) => Context.RegisterReference(handle,refenceHandle);
public static string GenerateHandle() => $"88.123.99/{Mathf.Abs(Guid.NewGuid().GetHashCode())}";
public bool Query(string key, out IDIS_Query query) => Context.Query(key, out query);
public void Update(string handle, string format, string value) => Context.Update(handle, format, value);
}

View File

@@ -17,7 +17,7 @@ public class IDIS_Template
public string IconPath="Register";
public DateTime CreateTime=DateTime.Now;
public DateTime UpdateTime=DateTime.Now;
public List<(string format,string hint)> Formats=new();
public List<(string format,string hint,string category)> Formats=new();
}
public partial class IDIS_TemplateService : Node
{
@@ -71,7 +71,7 @@ public partial class IDIS_TemplateService : Node
{
if (_selectedTemplate is null) return;
_selectedTemplate.Formats ??= new();
_selectedTemplate.Formats.Add(("新的数据格式","格式类型"));
_selectedTemplate.Formats.Add(("新的数据格式","格式类型","数据类型"));
_selectedTemplate.UpdateTime= DateTime.Now;
EnsureConfigure();
}
@@ -128,6 +128,7 @@ public partial class IDIS_TemplateService : Node
_container.lineEdits[0].Text = x.format;
_container.lineEdits[1].Text = x.hint;
_container.lineEdits[2].Text = x.category;
var index = i;
@@ -144,6 +145,12 @@ public partial class IDIS_TemplateService : Node
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);

View File

@@ -0,0 +1,35 @@
using Godot;
using System;
using BITFactory;
public partial class IDIS_UpdateService : Node
{
[ExportCategory("Services")]
[Export] private IDIS_Service service;
[ExportCategory("UI 绑定")]
[Export] private Button submitButton;
[Export] private LineEdit handleEdit;
[Export] private LineEdit temperatureEdit;
[Export] private LineEdit humidityEdit;
[Export] private Label hintsLabel;
public override void _Ready()
{
submitButton.Pressed += Submit;
}
private void Submit()
{
try
{
service.Update(handleEdit.Text, "温度", humidityEdit.Text);
service.Update(handleEdit.Text, "湿度", temperatureEdit.Text);
hintsLabel.Text = "更新成功: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
catch (Exception e)
{
hintsLabel.Text = e.Message;
throw;
}
}
}

View File

@@ -0,0 +1,71 @@
using Godot;
using System;
using System.Threading;
using System.Timers;
using Cysharp.Threading.Tasks;
using SharpModbus;
using Timer = System.Timers.Timer;
namespace BITFactory;
public partial class 湿Reader : Node
{
[ExportCategory("参数")]
[Export(PropertyHint.Range,"100,1000")] private int interval=500;
[ExportCategory("网络设置")]
[Export] private string ip="192.168.3.7";
[Export] private int port=502;
[ExportCategory("运行时")]
[Export]public double humidity = 50.0;
[Export]public double temperature = 26.0;
[ExportCategory("UI 绑定")]
[Export] private Label temperatureLabel;
[Export] private Label humidityLabel;
private ModbusMaster _modbus;
private System.Timers.Timer timer;
private CancellationTokenSource _CancellationTokenSource;
public override void _Ready()
{
_CancellationTokenSource = new CancellationTokenSource();
_modbus = ModbusMaster.TCP(ip, port);
timer = new Timer();
timer.Interval = interval;
timer.Elapsed += OnTimerElapsed;
timer.AutoReset = true;
timer.Start();
}
public override void _ExitTree()
{
timer.Stop();
}
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
_CancellationTokenSource.Cancel();
await UniTask.SwitchToTaskPool();
try
{
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
var vs = _modbus.ReadInputRegisters(1, 0, 2);
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
if (vs is not { Length: 2 }) return;
temperature = vs[0] / 10.0;
humidity = vs[1] / 10.0;
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
GD.Print("ex:" + ex);
}
}
}