290 lines
9.3 KiB
C#
Raw Normal View History

2025-04-02 17:46:33 +08:00
using System;
using System.Collections.Generic;
2024-12-19 16:07:07 +08:00
using System.Threading.Tasks;
2025-04-07 10:56:40 +08:00
using Apache.IoTDB.DataStructure;
using Apache.IoTDB;
using Confluent.Kafka;
2025-04-02 17:46:33 +08:00
using JiShe.CollectBus.Ammeters;
2024-12-19 16:07:07 +08:00
using JiShe.CollectBus.FreeSql;
2025-04-02 17:46:33 +08:00
using JiShe.CollectBus.IoTDBProvider;
2025-03-14 14:28:04 +08:00
using JiShe.CollectBus.IotSystems.PrepayModel;
2024-12-19 16:07:07 +08:00
using Microsoft.AspNetCore.Authorization;
2025-04-03 16:46:26 +08:00
using Microsoft.AspNetCore.Mvc;
2025-04-07 10:56:40 +08:00
using Microsoft.Extensions.Options;
2025-04-07 16:44:25 +08:00
using JiShe.CollectBus.IoTDBProvider.Context;
2025-04-08 17:44:42 +08:00
using Microsoft.Extensions.Logging;
2025-04-11 11:56:23 +08:00
using JiShe.CollectBus.IotSystems.AFNEntity;
2025-04-11 17:12:29 +08:00
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.Enums;
using System.Diagnostics.Metrics;
using JiShe.CollectBus.Common.DeviceBalanceControl;
2025-04-15 15:49:22 +08:00
using JiShe.CollectBus.Kafka.Attributes;
using System.Text.Json;
using JiShe.CollectBus.Kafka;
2025-04-16 17:36:46 +08:00
using JiShe.CollectBus.Application.Contracts;
using JiShe.CollectBus.Common.Models;
using System.Diagnostics;
2024-12-19 16:07:07 +08:00
namespace JiShe.CollectBus.Samples;
2025-04-15 15:49:22 +08:00
public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaSubscribe
2024-12-19 16:07:07 +08:00
{
2025-04-08 17:44:42 +08:00
private readonly ILogger<SampleAppService> _logger;
2025-04-02 17:46:33 +08:00
private readonly IIoTDBProvider _iotDBProvider;
2025-04-07 16:44:25 +08:00
private readonly IoTDBRuntimeContext _dbContext;
2025-04-07 10:56:40 +08:00
private readonly IoTDBOptions _options;
2025-04-16 17:36:46 +08:00
private readonly IRedisDataCacheService _redisDataCacheService;
2025-04-02 17:46:33 +08:00
2025-04-07 16:44:25 +08:00
public SampleAppService(IIoTDBProvider iotDBProvider, IOptions<IoTDBOptions> options,
2025-04-16 17:36:46 +08:00
IoTDBRuntimeContext dbContext, ILogger<SampleAppService> logger, IRedisDataCacheService redisDataCacheService)
2025-04-02 17:46:33 +08:00
{
_iotDBProvider = iotDBProvider;
2025-04-07 10:56:40 +08:00
_options = options.Value;
2025-04-07 16:44:25 +08:00
_dbContext = dbContext;
2025-04-08 17:44:42 +08:00
_logger = logger;
2025-04-16 17:36:46 +08:00
_redisDataCacheService = redisDataCacheService;
2025-04-02 17:46:33 +08:00
}
2025-04-16 17:36:46 +08:00
/// <summary>
/// 测试 UseSessionPool
/// </summary>
/// <param name="timestamps"></param>
/// <returns></returns>
2025-04-03 16:46:26 +08:00
[HttpGet]
2025-04-08 17:44:42 +08:00
public async Task UseSessionPool(long timestamps)
2025-04-02 17:46:33 +08:00
{
2025-04-08 17:44:42 +08:00
string? messageHexString = null;
if (timestamps == 0)
{
timestamps = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_logger.LogError($"timestamps_{timestamps}");
}
else
{
messageHexString = messageHexString + timestamps;
}
2025-04-02 17:46:33 +08:00
ElectricityMeter meter = new ElectricityMeter()
{
2025-04-03 16:46:26 +08:00
SystemName = "energy",
2025-04-03 15:38:31 +08:00
DeviceId = "402440506",
2025-04-02 17:46:33 +08:00
DeviceType = "Ammeter",
Current = 10,
MeterModel = "DDZY-1980",
ProjectCode = "10059",
2025-04-07 16:44:25 +08:00
Voltage = 10,
2025-04-08 17:44:42 +08:00
IssuedMessageHexString = messageHexString,
Timestamps = timestamps,
2025-04-02 17:46:33 +08:00
};
2025-04-07 16:44:25 +08:00
await _iotDBProvider.InsertAsync(meter);
2025-04-02 17:46:33 +08:00
}
2025-04-16 17:36:46 +08:00
/// <summary>
/// 测试Session切换
/// </summary>
/// <returns></returns>
2025-04-07 10:56:40 +08:00
[HttpGet]
2025-04-07 16:44:25 +08:00
public async Task UseTableSessionPool()
2025-04-07 10:56:40 +08:00
{
2025-04-11 11:56:23 +08:00
ElectricityMeter meter2 = new ElectricityMeter()
{
SystemName = "energy",
DeviceId = "402440506",
DeviceType = "Ammeter",
Current = 10,
MeterModel = "DDZY-1980",
ProjectCode = "10059",
Voltage = 10,
Timestamps = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
};
await _iotDBProvider.InsertAsync(meter2);
_dbContext.UseTableSessionPool = true;
2025-04-07 10:56:40 +08:00
2025-04-07 16:44:25 +08:00
ElectricityMeter meter = new ElectricityMeter()
2025-04-07 10:56:40 +08:00
{
2025-04-07 16:44:25 +08:00
SystemName = "energy",
DeviceId = "402440506",
DeviceType = "Ammeter",
Current = 10,
MeterModel = "DDZY-1980",
ProjectCode = "10059",
Voltage = 10,
Timestamps = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
};
await _iotDBProvider.InsertAsync(meter);
2025-04-07 10:56:40 +08:00
}
/// <summary>
/// 测试设备分组均衡控制算法
/// </summary>
/// <param name="deviceCount"></param>
/// <returns></returns>
[HttpGet]
public async Task TestDeviceGroupBalanceControl(int deviceCount = 200000)
{
//var deviceList = new List<string>();
//for (int i = 0; i < deviceCount; i++)
//{
// deviceList.Add($"Device_{Guid.NewGuid()}");
//}
//// 初始化缓存
//DeviceGroupBalanceControl.InitializeCache(deviceList);
var timeDensity = "15";
//获取缓存中的电表信息
2025-04-16 17:36:46 +08:00
var redisKeyList = $"{string.Format(RedisConst.CacheMeterInfoHashKey, "Energy", "JiSheCollectBus", MeterTypeEnum.Ammeter.ToString(), timeDensity)}*";
var oneMinutekeyList = await FreeRedisProvider.Instance.KeysAsync(redisKeyList);
var meterInfos = await GetMeterRedisCacheListData<AmmeterInfo>(oneMinutekeyList, "Energy", "JiSheCollectBus", timeDensity, MeterTypeEnum.Ammeter);
List<string> focusAddressDataLista = new List<string>();
foreach (var item in meterInfos)
{
focusAddressDataLista.Add(item.FocusAddress);
}
DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista);
// 打印分布统计
DeviceGroupBalanceControl.PrintDistributionStats();
await Task.CompletedTask;
}
/// <summary>
/// 测试设备分组均衡控制算法获取分组Id
/// </summary>
/// <param name="deviceAddress"></param>
/// <returns></returns>
[HttpGet]
public async Task TestGetDeviceGroupBalanceControl(string deviceAddress)
{
var groupId = DeviceGroupBalanceControl.GetDeviceGroupId(deviceAddress);
Console.WriteLine(groupId);
await Task.CompletedTask;
}
2025-04-07 10:56:40 +08:00
2025-04-11 11:56:23 +08:00
/// <summary>
/// 测试单个测点数据项
/// </summary>
/// <param name="measuring"></param>
/// <returns></returns>
[HttpGet]
2025-04-11 14:50:46 +08:00
public async Task TestSingleMeasuringAFNData(string measuring, string value)
2025-04-11 11:56:23 +08:00
{
2025-04-11 14:50:46 +08:00
var meter = new SingleMeasuringAFNDataEntity<string>()
2025-04-11 11:56:23 +08:00
{
SystemName = "energy",
DeviceId = "402440506",
DeviceType = "Ammeter",
ProjectCode = "10059",
Timestamps = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
2025-04-11 14:50:46 +08:00
SingleMeasuring = new Tuple<string, string>(measuring, value)
2025-04-11 11:56:23 +08:00
};
await _iotDBProvider.InsertAsync(meter);
}
2025-04-16 17:36:46 +08:00
/// <summary>
/// 测试Redis批量读取10万条数据性能
2025-04-16 17:36:46 +08:00
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task TestRedisCacheGetAllPagedData()
{
var timeDensity = "15";
string SystemType = "Energy";
2025-04-16 17:36:46 +08:00
string ServerTagName = "JiSheCollectBus2";
var redisCacheMeterInfoHashKeyTemp = $"{string.Format(RedisConst.CacheMeterInfoHashKey, SystemType, ServerTagName, MeterTypeEnum.Ammeter, timeDensity)}";
var redisCacheMeterInfoSetIndexKeyTemp = $"{string.Format(RedisConst.CacheMeterInfoSetIndexKey, SystemType, ServerTagName, MeterTypeEnum.Ammeter, timeDensity)}";
var redisCacheMeterInfoZSetScoresIndexKeyTemp = $"{string.Format(RedisConst.CacheMeterInfoZSetScoresIndexKey, SystemType, ServerTagName, MeterTypeEnum.Ammeter, timeDensity)}";
var timer1 = Stopwatch.StartNew();
2025-04-16 17:36:46 +08:00
decimal? cursor = null;
string member = null;
bool hasNext;
List<AmmeterInfo> meterInfos = new List<AmmeterInfo>();
do
{
var page = await _redisDataCacheService.GetAllPagedData<AmmeterInfo>(
redisCacheMeterInfoHashKeyTemp,
redisCacheMeterInfoZSetScoresIndexKeyTemp,
pageSize: 1000,
lastScore: cursor,
lastMember: member);
2025-04-16 17:36:46 +08:00
meterInfos.AddRange(page.Items);
cursor = page.HasNext ? page.NextScore : null;
member = page.HasNext ? page.NextMember : null;
2025-04-16 17:36:46 +08:00
hasNext = page.HasNext;
} while (hasNext);
timer1.Stop();
_logger.LogError($"读取数据更花费时间{timer1.ElapsedMilliseconds}毫秒");
2025-04-17 17:23:20 +08:00
List<string> focusAddressDataLista = new List<string>();
foreach (var item in meterInfos)
{
focusAddressDataLista.Add(item.FocusAddress);
}
DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista);
// 打印分布统计
DeviceGroupBalanceControl.PrintDistributionStats();
await Task.CompletedTask;
2025-04-16 17:36:46 +08:00
}
2025-04-11 11:56:23 +08:00
2024-12-19 16:07:07 +08:00
public Task<SampleDto> GetAsync()
{
return Task.FromResult(
new SampleDto
{
Value = 42
}
);
}
[Authorize]
public Task<SampleDto> GetAuthorizedAsync()
{
return Task.FromResult(
new SampleDto
{
Value = 42
}
);
}
[AllowAnonymous]
public async Task<List<Vi_BaseAmmeterInfo>> Test()
{
var ammeterList = await SqlProvider.Instance.Change(DbEnum.PrepayDB).Select<Vi_BaseAmmeterInfo>().Where(d => d.TB_CustomerID == 5).Take(10).ToListAsync();
return ammeterList;
}
2025-04-11 17:12:29 +08:00
[AllowAnonymous]
public bool GetTestProtocol()
{
var aa = LazyServiceProvider.GetKeyedService<IProtocolPlugin>("TestProtocolPlugin");
return aa == null;
}
2025-04-15 11:15:42 +08:00
2025-04-17 15:06:30 +08:00
[KafkaSubscribe(ProtocolConst.TESTTOPIC)]
2025-04-15 11:15:42 +08:00
2025-04-15 18:58:38 +08:00
public async Task<ISubscribeAck> KafkaSubscribeAsync(object obj)
2025-04-15 15:49:22 +08:00
{
_logger.LogWarning($"收到订阅消息: {obj}");
2025-04-15 16:45:10 +08:00
return SubscribeAck.Success();
2025-04-15 15:49:22 +08:00
}
2024-12-19 16:07:07 +08:00
}
2025-04-15 18:03:51 +08:00