using System; using System.Collections.Generic; using System.Threading.Tasks; using Confluent.Kafka; using DotNetCore.CAP; using JiShe.CollectBus.Ammeters; using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.DeviceBalanceControl; using JiShe.CollectBus.Common.Helpers; using JiShe.CollectBus.FreeSql; using JiShe.CollectBus.GatherItem; using JiShe.CollectBus.IoTDBProvider; using JiShe.CollectBus.IotSystems.Devices; using JiShe.CollectBus.IotSystems.MessageIssueds; using JiShe.CollectBus.IotSystems.MeterReadingRecords; using JiShe.CollectBus.IotSystems.Watermeter; using JiShe.CollectBus.Kafka.Producer; using JiShe.CollectBus.Repository; using JiShe.CollectBus.Repository.MeterReadingRecord; using MassTransit; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Volo.Abp.Domain.Repositories; using Volo.Abp.Uow; namespace JiShe.CollectBus.ScheduledMeterReading { /// /// 能耗系统定时采集服务 /// [AllowAnonymous] //[Route($"/energy/app/scheduled")] public class EnergySystemScheduledMeterReadingService : BasicScheduledMeterReadingService { string serverTagName = string.Empty; public EnergySystemScheduledMeterReadingService(ILogger logger, ICapPublisher producerBus, IIoTDBProvider dbProvider, IMeterReadingRecordRepository meterReadingRecordRepository,IConfiguration configuration, IProducerService producerService) : base(logger, producerBus, meterReadingRecordRepository, producerService,dbProvider) { serverTagName = configuration.GetValue(CommonConst.ServerTagName)!; } public sealed override string SystemType => SystemTypeConst.Energy; public sealed override string ServerTagName => serverTagName; /// /// 获取采集项列表 /// /// public override async Task> GetGatherItemByDataTypes() { try { string sql = $"SELECT DataType,ItemCode FROM TB_GatherItem(NOLOCK) WHERE [State]=0"; return await SqlProvider.Instance.Change(DbEnum.EnergyDB) .Ado .QueryAsync(sql, null); } catch { return null; } } /// /// 获取电表信息 /// /// 采集端Code /// //[HttpGet] //[Route($"ammeter/list")] public override async Task> GetAmmeterInfoList(string gatherCode = "V4-Gather-8890") { //List ammeterInfos = new List(); //ammeterInfos.Add(new AmmeterInfo() //{ // Baudrate = 2400, // FocusAddress = "402440506", // Name = "张家祠工务(三相电表)", // FocusId = 95780, // DatabaseBusiID = 1, // MeteringCode = 1, // AmmerterAddress = "402410040506", // MeterId = 127035, // TypeName = 3, // DataTypes = "449,503,581,582,583,584,585,586,587,588,589,590,591,592,593,594,597,598,599,600,601,602,603,604,605,606,607,608,661,663,677,679", // TimeDensity = 15, //}); //ammeterInfos.Add(new AmmeterInfo() //{ // Baudrate = 2400, // FocusAddress = "542400504", // Name = "五号配(长芦二所四排)(单相电表)", // FocusId = 69280, // DatabaseBusiID = 1, // MeteringCode = 2, // AmmerterAddress = "542410000504", // MeterId = 95594, // TypeName = 1, // DataTypes = "581,589,592,597,601", // TimeDensity = 15, //}); //return ammeterInfos; string sql = $@"SELECT C.ID as MeterId,C.Name,C.FocusID as FocusId,C.SingleRate,C.MeteringCode,C.Code AS BrandType,C.Baudrate,C.Password,C.MeteringPort,C.[Address] AS AmmerterAddress,C.TypeName,C.Protocol,C.TripState,C.[State],B.[Address],B.AreaCode,B.AutomaticReport,D.DataTypes,B.TimeDensity,A.GatherCode,C.Special,C.[ProjectID],B.AbnormalState,B.LastTime,CONCAT(B.AreaCode, B.[Address]) AS FocusAddress,(select top 1 DatabaseBusiID from TB_Project where ID = B.ProjectID) AS DatabaseBusiID FROM TB_GatherInfo(NOLOCK) AS A INNER JOIN TB_FocusInfo(NOLOCK) AS B ON A.ID = B.GatherInfoID AND B.RemoveState >= 0 AND B.State>=0 INNER JOIN TB_AmmeterInfo(NOLOCK) AS C ON B.ID = C.FocusID AND C.State>= 0 AND C.State<100 INNER JOIN TB_AmmeterGatherItem(NOLOCK) AS D ON C.ID = D.AmmeterID AND D.State>=0 WHERE 1=1 and C.Special = 0 "; //TODO 记得移除特殊表过滤 //if (!string.IsNullOrWhiteSpace(gatherCode)) //{ // sql = $@"{sql} AND A.GatherCode = '{gatherCode}'"; //} return await SqlProvider.Instance.Change(DbEnum.EnergyDB) .Ado .QueryAsync(sql); } /// /// 获取水表信息 /// /// 采集端Code /// //[HttpGet] //[Route($"ammeter/list")] public override async Task> GetWatermeterInfoList(string gatherCode = "V4-Gather-8890") { string sql = $@"SELECT A.ID as MeterId, A.Name, A.FocusID as FocusId, A.MeteringCode, A.Baudrate, A.MeteringPort, A.[Address] AS MeterAddress, A.[Password], A.TypeName, A.Protocol, A.Code, A.LinkType, A.HaveValve, A.MeterType AS MeterTypeName, A.MeterBrand, A.TimesRate, A.TimeDensity, A.TripState, B.[Address], B.AreaCode, B.AutomaticReport, A.[State], C.GatherCode, A.[ProjectID], B.AbnormalState, B.LastTime, CONCAT(B.AreaCode, B.[Address]) AS FocusAddress, (select top 1 DatabaseBusiID from TB_Project where ID = b.ProjectID) AS DatabaseBusiID FROM [dbo].[TB_WatermeterInfo](NOLOCK) AS A INNER JOIN [dbo].[TB_FocusInfo](NOLOCK) AS B ON A.FocusID=B.ID AND B.RemoveState >= 0 AND B.State>=0 INNER JOIN [dbo].[TB_GatherInfo](NOLOCK) AS C ON B.GatherInfoID=C.ID WHERE A.State>=0 AND A.State<100 "; if (!string.IsNullOrWhiteSpace(gatherCode)) { sql = $@"{sql} AND C.GatherCode= '{gatherCode}'"; } return await SqlProvider.Instance.Change(DbEnum.EnergyDB) .Ado .QueryAsync(sql); } /// /// 测试设备分组均衡控制算法 /// /// /// [HttpGet] public async Task TestDeviceGroupBalanceControl(int deviceCount = 200000) { var deviceList = new List(); for (int i = 0; i < deviceCount; i++) { deviceList.Add($"Device_{Guid.NewGuid()}"); } // 初始化缓存 DeviceGroupBalanceControl.InitializeCache(deviceList); // 打印分布统计 DeviceGroupBalanceControl.PrintDistributionStats(); await Task.CompletedTask; } } }