JiShe.CollectBus/services/JiShe.CollectBus.Application/ScheduledMeterReading/EnergySystemScheduledMeterReadingService.cs

193 lines
7.9 KiB
C#
Raw Normal View History

2025-03-17 14:23:48 +08:00
using System;
using System.Collections.Generic;
2025-03-14 14:38:08 +08:00
using System.Threading.Tasks;
2025-03-12 14:57:42 +08:00
using JiShe.CollectBus.Ammeters;
2025-04-16 17:36:46 +08:00
using JiShe.CollectBus.Application.Contracts;
2025-03-12 14:57:42 +08:00
using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.DeviceBalanceControl;
2025-04-10 14:12:14 +08:00
using JiShe.CollectBus.Common.Helpers;
2025-03-12 14:57:42 +08:00
using JiShe.CollectBus.FreeSql;
2025-03-14 14:24:38 +08:00
using JiShe.CollectBus.GatherItem;
using JiShe.CollectBus.IoTDB.Context;
2025-04-17 20:28:50 +08:00
using JiShe.CollectBus.IoTDB.Interface;
2025-03-21 11:48:31 +08:00
using JiShe.CollectBus.IotSystems.Devices;
2025-03-17 14:23:48 +08:00
using JiShe.CollectBus.IotSystems.MessageIssueds;
2025-03-18 22:43:24 +08:00
using JiShe.CollectBus.IotSystems.MeterReadingRecords;
2025-03-14 14:38:08 +08:00
using JiShe.CollectBus.IotSystems.Watermeter;
using JiShe.CollectBus.Kafka.Internal;
2025-04-15 15:49:51 +08:00
using JiShe.CollectBus.Kafka.Producer;
2025-03-20 16:40:27 +08:00
using JiShe.CollectBus.Repository;
using JiShe.CollectBus.Repository.MeterReadingRecord;
2025-03-12 14:57:42 +08:00
using Microsoft.AspNetCore.Authorization;
2025-04-10 14:12:14 +08:00
using Microsoft.AspNetCore.Mvc;
2025-04-09 17:29:30 +08:00
using Microsoft.Extensions.Configuration;
2025-03-13 10:51:16 +08:00
using Microsoft.Extensions.Logging;
2025-04-17 13:54:18 +08:00
using Microsoft.Extensions.Options;
2025-03-17 14:23:48 +08:00
using Volo.Abp.Domain.Repositories;
2025-03-18 13:56:38 +08:00
using Volo.Abp.Uow;
2025-03-12 14:57:42 +08:00
2025-03-14 14:38:08 +08:00
namespace JiShe.CollectBus.ScheduledMeterReading
2025-03-12 14:57:42 +08:00
{
/// <summary>
/// 能耗系统定时采集服务
/// </summary>
[AllowAnonymous]
//[Route($"/energy/app/scheduled")]
public class EnergySystemScheduledMeterReadingService : BasicScheduledMeterReadingService
{
2025-04-09 17:29:30 +08:00
string serverTagName = string.Empty;
2025-04-16 17:36:46 +08:00
public EnergySystemScheduledMeterReadingService(
ILogger<EnergySystemScheduledMeterReadingService> logger,
2025-04-21 10:17:40 +08:00
IIoTDbProvider dbProvider,
IOptions<KafkaOptionConfig> kafkaOptions,
IoTDBRuntimeContext runtimeContext,
2025-04-16 17:36:46 +08:00
IProducerService producerService,
IRedisDataCacheService redisDataCacheService)
: base(logger,
producerService,
redisDataCacheService,
2025-04-17 13:54:18 +08:00
dbProvider,
runtimeContext,
2025-04-17 13:54:18 +08:00
kafkaOptions)
2025-03-12 16:29:38 +08:00
{
2025-04-17 13:54:18 +08:00
serverTagName = kafkaOptions.Value.ServerTagName;
2025-03-12 16:29:38 +08:00
}
2025-03-12 14:57:42 +08:00
public sealed override string SystemType => SystemTypeConst.Energy;
2025-04-09 17:29:30 +08:00
public sealed override string ServerTagName => serverTagName;
2025-03-14 14:24:38 +08:00
/// <summary>
/// 获取采集项列表
/// </summary>
/// <returns></returns>
public override async Task<List<GatherItemInfo>> GetGatherItemByDataTypes()
{
try
{
string sql = $"SELECT DataType,ItemCode FROM TB_GatherItem(NOLOCK) WHERE [State]=0";
return await SqlProvider.Instance.Change(DbEnum.EnergyDB)
.Ado
.QueryAsync<GatherItemInfo>(sql, null);
}
catch
{
return null;
}
}
2025-03-12 14:57:42 +08:00
/// <summary>
/// 获取电表信息
/// </summary>
/// <param name="gatherCode">采集端Code</param>
/// <returns></returns>
//[HttpGet]
//[Route($"ammeter/list")]
public override async Task<List<AmmeterInfo>> GetAmmeterInfoList(string gatherCode = "V4-Gather-8890")
{
//List<AmmeterInfo> ammeterInfos = new List<AmmeterInfo>();
//ammeterInfos.Add(new AmmeterInfo()
//{
// Baudrate = 2400,
// FocusAddress = "402440506",
// Name = "张家祠工务(三相电表)",
2025-04-15 16:48:35 +08:00
// FocusId = 95780,
// DatabaseBusiID = 1,
// MeteringCode = 1,
// AmmerterAddress = "402410040506",
2025-04-15 16:48:35 +08:00
// 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,
2025-04-23 09:42:09 +08:00
// BrandType = "",
//});
//ammeterInfos.Add(new AmmeterInfo()
//{
// Baudrate = 2400,
// FocusAddress = "542400504",
// Name = "五号配(长芦二所四排)(单相电表)",
2025-04-15 16:48:35 +08:00
// FocusId = 69280,
// DatabaseBusiID = 1,
// MeteringCode = 2,
// AmmerterAddress = "542410000504",
2025-04-15 16:48:35 +08:00
// MeterId = 95594,
// TypeName = 1,
// DataTypes = "581,589,592,597,601",
// TimeDensity = 15,
2025-04-23 09:42:09 +08:00
// BrandType = "",
//});
//return ammeterInfos;
2025-03-24 20:54:31 +08:00
2025-04-15 16:48:35 +08:00
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
2025-03-27 08:38:19 +08:00
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 记得移除特殊表过滤
2025-03-24 20:54:31 +08:00
2025-04-23 09:42:09 +08:00
if (!string.IsNullOrWhiteSpace(gatherCode))
{
sql = $@"{sql} AND A.GatherCode = '{gatherCode}'";
}
2025-03-27 08:38:19 +08:00
return await SqlProvider.Instance.Change(DbEnum.EnergyDB)
.Ado
.QueryAsync<AmmeterInfo>(sql);
2025-03-12 14:57:42 +08:00
}
/// <summary>
/// 获取水表信息
/// </summary>
/// <param name="gatherCode">采集端Code</param>
/// <returns></returns>
//[HttpGet]
//[Route($"ammeter/list")]
public override async Task<List<WatermeterInfo>> GetWatermeterInfoList(string gatherCode = "V4-Gather-8890")
{
string sql = $@"SELECT
2025-04-15 16:48:35 +08:00
A.ID as MeterId,
2025-03-12 14:57:42 +08:00
A.Name,
2025-04-15 16:48:35 +08:00
A.FocusID as FocusId,
2025-03-12 14:57:42 +08:00
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,
2025-03-21 11:48:31 +08:00
B.LastTime,
CONCAT(B.AreaCode, B.[Address]) AS FocusAddress,
(select top 1 DatabaseBusiID from TB_Project where ID = b.ProjectID) AS DatabaseBusiID
2025-03-12 14:57:42 +08:00
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<WatermeterInfo>(sql);
2025-04-23 09:42:09 +08:00
}
2025-03-12 14:57:42 +08:00
}
}