readme
This commit is contained in:
@@ -11,14 +11,7 @@ namespace BITKit;
|
||||
/// </summary>
|
||||
public partial class BITAppForGodot : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖服务集合
|
||||
/// </summary>
|
||||
public static ServiceCollection ServiceCollection { get; private set; } = new();
|
||||
/// <summary>
|
||||
/// 依赖服务提供接口
|
||||
/// </summary>
|
||||
public static ServiceProvider ServiceProvider { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 在构造函数中注册Logger
|
||||
/// </summary>
|
||||
@@ -34,16 +27,15 @@ public partial class BITAppForGodot : Node
|
||||
BIT4Log.Log<BITAppForGodot>("已创建BITApp");
|
||||
}
|
||||
|
||||
public override async void _Ready()
|
||||
public override void _Ready()
|
||||
{
|
||||
BIT4Log.Log<BITAppForGodot>("正在创建BITWebApp");
|
||||
|
||||
//添加测试用HttpClient
|
||||
ServiceCollection.AddSingleton<HttpClient>();
|
||||
BITApp.ServiceCollection.AddSingleton<HttpClient>();
|
||||
|
||||
//构造依赖服务提供接口
|
||||
ServiceProvider = ServiceCollection.BuildServiceProvider();
|
||||
|
||||
BITApp.BuildService();
|
||||
}
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
|
@@ -15,7 +15,7 @@ public class IntervalTimer
|
||||
/// <summary>
|
||||
/// 在构造函数中声明间隔时间
|
||||
/// </summary>
|
||||
/// <param name="interval"></param>
|
||||
/// <param name="interval">间隔(秒)</param>
|
||||
public IntervalTimer(ulong interval)
|
||||
{
|
||||
this.interval = interval;
|
||||
|
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Packages.Core.LazyLoad;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
@@ -20,7 +21,7 @@ public partial class SCADAService : Node
|
||||
/// </summary>
|
||||
public SCADAService()
|
||||
{
|
||||
BITAppForGodot.ServiceCollection.AddSingleton(this);
|
||||
BITApp.ServiceCollection.AddSingleton(this);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取json的Url
|
||||
@@ -39,7 +40,7 @@ public partial class SCADAService : Node
|
||||
/// <summary>
|
||||
/// 最大并行请求数量
|
||||
/// </summary>
|
||||
private readonly LimitTimes requestRate =new (1);
|
||||
private readonly LimitTimes limitConcurrent =new (1);
|
||||
/// <summary>
|
||||
/// 请求数据的间隔
|
||||
/// </summary>
|
||||
@@ -51,7 +52,7 @@ public partial class SCADAService : Node
|
||||
/// <summary>
|
||||
/// http客户端
|
||||
/// </summary>
|
||||
private System.Net.Http.HttpClient httpClient;
|
||||
private readonly ServiceLoader<System.Net.Http.HttpClient> httpClient=new();
|
||||
/// <summary>
|
||||
/// 获取Entity并加载依赖
|
||||
/// </summary>
|
||||
@@ -62,7 +63,6 @@ public partial class SCADAService : Node
|
||||
await UniTask.Yield();
|
||||
LoadAllEntities();
|
||||
BIT4Log.Log<SCADAService>($"已加载{_entities.Count}个设备");
|
||||
httpClient = BITAppForGodot.ServiceProvider.GetService<System.Net.Http.HttpClient>();
|
||||
}
|
||||
/// <summary>
|
||||
/// 内部方法,从EntityService加载所有Entity
|
||||
@@ -83,14 +83,14 @@ public partial class SCADAService : Node
|
||||
/// <param name="delta"></param>
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
//依赖加载
|
||||
httpClient ??= BITAppForGodot.ServiceProvider.GetService<System.Net.Http.HttpClient>();
|
||||
//请求间隔控制+请求并发控制+检查依赖是否为Null
|
||||
if (_intervalTimer.Allow && requestRate && httpClient is not null)
|
||||
{
|
||||
//发送请求
|
||||
Request();
|
||||
}
|
||||
//等待依赖加载
|
||||
//请求间隔控制+请求并发控制
|
||||
if (_intervalTimer.Allow is false || httpClient.IsLoaded is false) return;
|
||||
if (!limitConcurrent.AllowOnly) return;
|
||||
//提交并发
|
||||
limitConcurrent.CanUpdate();
|
||||
//发送请求
|
||||
Request();
|
||||
}
|
||||
/// <summary>
|
||||
/// 从http请求json
|
||||
@@ -98,7 +98,7 @@ public partial class SCADAService : Node
|
||||
private async void Request()
|
||||
{
|
||||
//获取json
|
||||
var json =await httpClient.GetStringAsync(url, _cancellationToken);
|
||||
var json =await httpClient.Value.GetStringAsync(url, _cancellationToken);
|
||||
try
|
||||
{
|
||||
//取消执行,如果已取消令牌
|
||||
@@ -107,11 +107,11 @@ public partial class SCADAService : Node
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
//返回并发数量
|
||||
requestRate.Release();
|
||||
limitConcurrent.Release();
|
||||
return;
|
||||
}
|
||||
//返回并发数量
|
||||
requestRate.Release();
|
||||
limitConcurrent.Release();
|
||||
//处理json
|
||||
ProcessJson(json);
|
||||
}
|
||||
|
Reference in New Issue
Block a user