添加了教育平台
This commit is contained in:
@@ -22,6 +22,10 @@ public partial class RotationComponent : EntityComponent
|
||||
/// 默认角度的缓存
|
||||
/// </summary>
|
||||
public Vector3 OriginalEuler { get; private set; }
|
||||
/// <summary>
|
||||
/// 可读可写的当前角度
|
||||
/// </summary>
|
||||
public float CurrentAngle;
|
||||
public override void _Ready()
|
||||
{
|
||||
//保存默认角度
|
||||
|
@@ -14,7 +14,7 @@ namespace BITKit;
|
||||
/// <summary>
|
||||
/// 单例SCADA Service,从http接口获取json后解析为指定数据
|
||||
/// </summary>
|
||||
public partial class SCADAService : Node
|
||||
public partial class SCADAService : Node,IProvider<string>,IActivable
|
||||
{
|
||||
public const string _CurrentAngle="CurrentAngle";
|
||||
public const string _CurrentRotation="CurrentRotation";
|
||||
@@ -35,31 +35,20 @@ public partial class SCADAService : Node
|
||||
[Export]
|
||||
private bool fixedEntities;
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
[Export]
|
||||
public bool Enabled { get; set; } = true;
|
||||
/// <summary>
|
||||
/// 已加载的Entity
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, IEntity> _entities = new();
|
||||
/// <summary>
|
||||
/// 最大并行请求数量
|
||||
/// </summary>
|
||||
private readonly LimitTimes limitConcurrent =new (1);
|
||||
/// <summary>
|
||||
/// 请求数据的间隔
|
||||
/// </summary>
|
||||
private readonly IntervalTimer _intervalTimer = new(1);
|
||||
/// <summary>
|
||||
/// 取消令牌,用于取消Http Get
|
||||
/// </summary>
|
||||
private CancellationToken _cancellationToken;
|
||||
/// <summary>
|
||||
/// http客户端
|
||||
/// </summary>
|
||||
private readonly ServiceLoader<System.Net.Http.HttpClient> httpClient=new();
|
||||
|
||||
/// <summary>
|
||||
/// 获取Entity并加载依赖
|
||||
/// </summary>
|
||||
public override async void _Ready()
|
||||
{
|
||||
_cancellationToken = new CancellationToken();
|
||||
if (!fixedEntities) return;
|
||||
await UniTask.Yield();
|
||||
LoadAllEntities();
|
||||
@@ -87,60 +76,20 @@ public partial class SCADAService : Node
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 物理帧用于控制并发和间隔的同时请求数据
|
||||
/// </summary>
|
||||
/// <param name="delta"></param>
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
//等待依赖加载
|
||||
//请求间隔控制+请求并发控制
|
||||
if (_intervalTimer.Allow is false || httpClient.IsLoaded is false) return;
|
||||
//如果url为空
|
||||
if (string.IsNullOrEmpty(_url)) return;
|
||||
if (!limitConcurrent.AllowOnly) return;
|
||||
//提交并发
|
||||
limitConcurrent.CanUpdate();
|
||||
//发送请求
|
||||
Request();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从http请求json
|
||||
/// </summary>
|
||||
private async void Request()
|
||||
{
|
||||
//获取json
|
||||
|
||||
try
|
||||
{
|
||||
var url = _url.Get();
|
||||
var json = await httpClient.Value.GetStringAsync(url, _cancellationToken);
|
||||
//取消执行,如果已取消令牌
|
||||
_cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
//处理json
|
||||
ProcessJson(json);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
BIT4Log.Warnning(_url.Get());
|
||||
//返回并发数量
|
||||
limitConcurrent.Release();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
//返回并发数量
|
||||
limitConcurrent.Release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解析json
|
||||
/// </summary>
|
||||
/// <param name="json">从SCADA获取的Json</param>
|
||||
private void ProcessJson(string json)
|
||||
public void Set(string json)
|
||||
{
|
||||
if(Enabled is false)return;
|
||||
//首先从result中获取数组
|
||||
var jArray = JsonConvert.DeserializeObject<JObject>(json)["result"]!.ToObject<JArray>();
|
||||
//然后遍历所有数组的内容
|
||||
@@ -151,7 +100,17 @@ public partial class SCADAService : Node
|
||||
//通过Id查找已加载的Entity
|
||||
if (!_entities.TryGetValue(id, out var entity)) continue;
|
||||
//加载数组中的"value"为json
|
||||
var _json = element["value"]!.ToObject<string>();
|
||||
var _key = element["value"]!.ToString();
|
||||
string _json;
|
||||
if (_key.Substring(0) is "\"")
|
||||
{
|
||||
_json = element["value"]!.ToObject<string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_json = element["value"].ToString();
|
||||
}
|
||||
|
||||
//获取被加载为string的json
|
||||
var obj = JsonConvert.DeserializeObject(_json);
|
||||
//反序列化string为原始json
|
||||
@@ -159,13 +118,11 @@ public partial class SCADAService : Node
|
||||
//提交json和entity
|
||||
ProcessEntity(value,entity);
|
||||
}
|
||||
//返回并发数量
|
||||
limitConcurrent.Release();
|
||||
}
|
||||
/// <summary>
|
||||
/// 提交jObject数据和Entity进行解析和处理
|
||||
/// </summary>
|
||||
/// <param name="jObject">json [result] [0] [value] 中的原始json</param>
|
||||
/// <param name="jObject">json [result] [index] [value] 中的原始json</param>
|
||||
/// <param name="entity">引用实体,如PLC-ZL</param>
|
||||
private static void ProcessEntity(JObject jObject, IEntity entity)
|
||||
{
|
||||
@@ -180,14 +137,31 @@ public partial class SCADAService : Node
|
||||
//加载rotation需要的path,如 var angle = value["J1"]
|
||||
var path = rotationComponent.Path;
|
||||
//加载以获取到的角度
|
||||
var currentAngle = jObject[path]!.ToObject<float>();
|
||||
var rawAngle = jObject[path]!.ToObject<float>();
|
||||
//补间角度
|
||||
var currentAngle = rotationComponent.CurrentAngle = Mathf.Lerp(rotationComponent.CurrentAngle, rawAngle,
|
||||
90 * BITAppForGodot.DeltaTime);
|
||||
//最终角度 = 当前角度*角度权重 + 角度偏移 + 原始角度
|
||||
var euler = currentAngle * rotationComponent.Weight + rotationComponent.Offset + rotationComponent.OriginalEuler;
|
||||
//为Node3D.Rotation提交最后的角度计算结果
|
||||
rotationComponent.RotationDegrees = euler;
|
||||
|
||||
euler.X = Mathf.DegToRad(euler.X);
|
||||
euler.Y = Mathf.DegToRad(euler.Y);
|
||||
euler.Z = Mathf.DegToRad(euler.Z);
|
||||
rotationComponent.Rotation = euler;
|
||||
//rotationComponent.RotationDegrees = euler;
|
||||
|
||||
|
||||
rotationComponent.SetMeta(_CurrentAngle,(int)currentAngle);
|
||||
rotationComponent.SetMeta(_CurrentAngle,(int)rawAngle);
|
||||
rotationComponent.SetMeta(_CurrentRotation,euler);
|
||||
}
|
||||
}
|
||||
|
||||
public string Get()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetActive(bool active) => Enabled = active;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user