226 lines
6.8 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;
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-02 17:46:33 +08:00
2025-04-07 16:44:25 +08:00
public SampleAppService(IIoTDBProvider iotDBProvider, IOptions<IoTDBOptions> options,
2025-04-08 17:44:42 +08:00
IoTDBRuntimeContext dbContext, ILogger<SampleAppService> logger)
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-02 17:46:33 +08:00
}
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-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";
//获取缓存中的电表信息
var redisKeyList = $"{string.Format(RedisConst.CacheMeterInfoKey, "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);
}
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-15 15:49:22 +08:00
[KafkaSubscribe(["test-topic"])]
2025-04-15 11:15:42 +08:00
2025-04-15 15:50:30 +08:00
public async Task<bool> KafkaSubscribeAsync(string obj)
2025-04-15 15:49:22 +08:00
{
_logger.LogWarning($"收到订阅消息: {obj}");
2025-04-15 15:50:30 +08:00
return await Task.FromResult(true);
2025-04-15 15:49:22 +08:00
}
2024-12-19 16:07:07 +08:00
}