You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

740 lines
24 KiB

using Autofac;
5 months ago
using Entity.Ammeter;
using Entity.Constant;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Redis;
using log4net;
using Newtonsoft.Json;
5 months ago
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Repository.Station;
5 months ago
using Service.Ammeter;
using Service.Cloud.Msg;
5 months ago
using Service.Cloud.Msg.Cloud.Resp;
using Service.Cloud.Msg.Host.Req;
5 months ago
using Service.Execute.Api;
using Service.FireControl.Client;
using Service.Init;
5 months ago
using Service.Station;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Service.Cloud.Client;
[Scope("SingleInstance")]
public class CloudClientMgr
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CloudClientMgr));
public static CloudClient? CloudClient { get; set; }
5 months ago
static EmeterDayEnergyService emeterDayEnergyService = AppInfo.Container.Resolve<EmeterDayEnergyService>();
static EmeterHourEnergyService emeterHourEnergyService = AppInfo.Container.Resolve<EmeterHourEnergyService>();
private static DateTime _dateTime = DateTime.Now.AddSeconds(-30);
5 months ago
private static DateTime _dateTime1 = DateTime.Now.AddSeconds(-30);
5 months ago
private static DateTime _dateTime2 = DateTime.Now.AddSeconds(-10);
private static DateTime _dateTime3 = DateTime.Now.AddSeconds(-10);
private static DateTime _dateTime4 = DateTime.Now.AddSeconds(-10);
private static DateTime _dateTime5 = DateTime.Now.AddMinutes(-15);//15分钟
private static DateTime _dateTime6 = DateTime.Now.AddMinutes(-15);//15分钟
private static DateTime _dateTime7 = DateTime.Today.AddHours(DateTime.Now.Hour-1);//小时
private static DateTime _dateTime8 = DateTime.Now.AddDays(-1).Date;//天
private static DateTime _dateTime9 = DateTime.Today.AddHours(DateTime.Now.Hour - 1);//小时
private static DateTime _dateTime10 = DateTime.Now.AddDays(-1).Date;//天
private static DateTime _dateTime11 = DateTime.Now.AddDays(-1).Date;//天
private const int TimeSpan = 5;
5 months ago
private static ChargeDevDataInfo req = new ChargeDevDataInfo();
5 months ago
public static async void Init()
{
CloudClient = AppInfo.Container.Resolve<CloudClient>();
CloudClient.MqttVersion = StaticStationInfo.CloudServerMqttVersion;
CloudClient.ServerIp = StaticStationInfo.CloudServerIp;
CloudClient.ServerPort = StaticStationInfo.CloudServerPort;
CloudClient.ClientId = StaticStationInfo.CloudClientId;
CloudClient.Username = StaticStationInfo.CloudUsername;
CloudClient.Password = StaticStationInfo.CloudPassword;
CloudClient.SubTopic = StaticStationInfo.CloudSubTopic;
CloudClient.PubTopic = StaticStationInfo.CloudPubTopic;
CloudClient.StationNo = StaticStationInfo.StationNo;
CloudClient.AutoReConnect = true;
CloudClient.InitHandler();
5 months ago
var ddddd = CloudClient.Connected;
Task.Run(() => CloudClient.Connect());
RedisHelper? redisHelper = AppInfo.Container.Resolve<RedisHelper>();
BinInfoRepository binInfoRepository = AppInfo.Container.Resolve<BinInfoRepository>();
5 months ago
#region 遥测合并上报
req.datainfo = new List<DataInfo>();
redisHelper?.GetSubscriber().Subscribe("UploadTelemetryData", (channel, value) =>
{
try
{
5 months ago
if (value.HasValue)
{
5 months ago
UploadTelemetryData? data = JsonConvert.DeserializeObject<UploadTelemetryData>(value.ToString());
if (data != null)
{
5 months ago
BinInfo? binInfo = binInfoRepository.QueryByClause(it => it.ChargerNo == data.ChargerNo);
5 months ago
//ChargeDevDataInfo req = new ChargeDevDataInfo();
req.sn = StaticStationInfo.StationNo;
5 months ago
DataInfo dataInfo = new DataInfo();
dataInfo.en = StaticStationInfo.StationNo + data.ChargerNo;
var areNotEqual = !dataInfo.en.Equals(
req.datainfo.Select(d => d.en)
.ToList()
.Any(x => x == dataInfo.en));
if (areNotEqual)//如果没有重复的
{
dataInfo.sd = "A" + int.Parse(binInfo.No);
dataInfo.mtp = StaticStationInfo.ChargePower;
dataInfo.mcr = 1;
dataInfo.hb = binInfo?.Exists ?? 0;
dataInfo.el = 0;
dataInfo.cno = int.Parse(binInfo.No);
dataInfo.cs = binInfo.ChargeStatus ?? 0;
dataInfo.fs = 0;
dataInfo.@as = 0;
//fc = data.,
//st = data.,
dataInfo.ct = data.ChargingTime;
//ssoc = data.,
dataInfo.csoc = data.CurrentSoc;
//ssoe = data.,
//csoe = data.,
dataInfo.cvot = data.BmsChargingVoltage;
dataInfo.ccur = data.BmsChargingCurrent;
dataInfo.nvot = data.BmsNeedVoltage;
dataInfo.ncur = data.BmsNeedCurrent;
dataInfo.lsv = data.SingleBatteryMinVoltage;
dataInfo.hsv = data.SingleBatteryMaxVoltage;
dataInfo.lst = data.MinBatteryTemp;
dataInfo.hst = data.MaxBatteryTemp;
dataInfo.ws = 0xFF;
dataInfo.it = 0xFF;
dataInfo.ot = 0xFF;
dataInfo.bt = DateTime.Now;
if (req.datainfo.Count < 7)
{
req.datainfo.Add(dataInfo);
}
}
if ((DateTime.Now - _dateTime).TotalSeconds <= 30)
{
return;
}
_dateTime = DateTime.Now;
5 months ago
if (req.datainfo.Count > 0)
{
CloudClient?.SendChargeDevDataInfo(req);
req = new ChargeDevDataInfo();
req.datainfo = new List<DataInfo>();
}
Log.Info($"receive UploadTelemetryData={value}");
}
}
}
catch (Exception e)
{
Log.Info("error", e);
}
});
5 months ago
#endregion
var sv = StaticStationInfo.StationSftVer.ToString();
redisHelper?.GetSubscriber().Subscribe("BatteryInfoUploadTask", (channel, value) =>
{
try
{
Log.Info($"receive BatteryInfoUploadTask={value}");
if (value.HasValue)
{
BatDataInfo? data = JsonConvert.DeserializeObject<BatDataInfo>(value.ToString());
if (data != null)
{
5 months ago
if ((DateTime.Now - _dateTime1).TotalSeconds <= 30)
{
return;
}
_dateTime1 = DateTime.Now;
CloudClient?.SendBatDataInfo(data);
}
}
}
catch (Exception e)
{
Log.Info("error", e);
}
});
5 months ago
StartAsyncLoop();
}
private static async Task StartAsyncLoop()
{
await Task.Run(() =>
{
try
{
while (CloudClient!=null)
{
if (CloudClient.Connected)
{
CloudApi.SendStateLog(0);
TaskHostStatusReported();
TaskAirConditioningData();
TaskTemperatureHumidityData();
TaskPowerTotal();
TaskStaChargingTota();
TaskMeterEnergyKwh();
TaskMeterDayEnergyVal();
TaskStaHourEnergyVal();
TaskStaDayEnergyVal();
TaskStaDayOpeEnergyVal();
TaskStaHourAmountVal();
}
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Log.Error(ex);
}
});
}
static int os = 0;
/// <summary>
/// 上传换电站状态 10s
/// </summary>
private static void TaskHostStatusReported()
{
if (os != StaticStationInfo.StationStatus)
{
if ((DateTime.Now - _dateTime2).TotalSeconds <= 30)
{
return;
}
_dateTime2 = DateTime.Now;
5 months ago
//EquipAlarmRecordRepository equipAlarmRecordRepository = AppInfo.Container.Resolve<EquipAlarmRecordRepository>();
//var EquipAlarm = equipAlarmRecordRepository.Query();
//var ErrorLevel = Convert.ToInt32(EquipAlarm.Min(x => x.ErrorLevel));
5 months ago
StationRunStatus stationRunStatus = new()
{
5 months ago
rs = StaticStationInfo.Sevstatus,
5 months ago
os = StaticStationInfo.StationStatus,
5 months ago
fl =Convert.ToInt32( StaticStationInfo.Faultlevel),
5 months ago
};
CloudApi.SendHostStatusReported(stationRunStatus);
}
os = StaticStationInfo.StationStatus;
}
/// <summary>
/// 空调数据上报 10s
/// </summary>
private static void TaskAirConditioningData()
{
if ((DateTime.Now - _dateTime3).TotalSeconds <= 60)
{
return;
}
_dateTime3 = DateTime.Now;
AcDataInfo airConditioningData = new()
{
sn = StaticStationInfo.StationNo,
ad = "",
an = "",
au = 0,
am = 3,
ap = 0,
bt = DateTime.Now,
};
CloudApi.SendAirConditioningData(airConditioningData);
}
/// <summary>
/// 温湿度 10s
/// </summary>
private static void TaskTemperatureHumidityData()
{
if ((DateTime.Now - _dateTime4).TotalSeconds <= 10)
{
return;
}
_dateTime4 = DateTime.Now;
EvmDataInfo evmDataInfo = new()
{
sn = StaticStationInfo.StationNo,
ea = "电池仓",
et = FireControlMgr.Temperature,
eh = FireControlMgr.Humidity,
bt = DateTime.Now,
};
CloudApi.SendEvmDataInfo(evmDataInfo);
}
/// <summary>
/// 电能表累计值信息 15m
/// </summary>
private static void TaskPowerTotal()//Desc记得改回来
{
if ((DateTime.Now - _dateTime5).TotalMinutes <= 15)
{
return;
}
_dateTime5 = DateTime.Now;
EmeterEnergyService emeterEnergyService = AppInfo.Container.Resolve<EmeterEnergyService>();
EmeterEnergyChangeService emeterEnergyChangeService = AppInfo.Container.Resolve<EmeterEnergyChangeService>();
List<EmeterEnergy> emeterEnergy = emeterEnergyService.QueryListByClause(u=>u.UploadFlag==0, 7,i => i.UploadTime,
OrderByType.Desc);
5 months ago
emeterEnergy = emeterEnergy.OrderBy(p => p.Code).ToList();
var emeterEnergyChange = emeterEnergyChangeService.QueryListByClause(u => u.UploadFlag == 0, 7, i => i.UploadTime,
OrderByType.Desc);
5 months ago
emeterEnergyChange = emeterEnergyChange.OrderBy(p => p.Code).ToList();
PowerTotal powerTotal = new()
{
sn = StaticStationInfo.StationNo,
};
powerTotal.meter = new List<meter>();
for (int i = 0; i < 7; i++)
{
meter meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓直流表",
ms = emeterEnergy[i].Code,
mk = emeterEnergy[i].Value,
};
powerTotal.meter.Add(meter);
}
for (int i = 0;i < 7; i++)
{
meter meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓交流表",
ms = emeterEnergyChange[i].Code,
mk = emeterEnergyChange[i].Value,
};
powerTotal.meter.Add(meter);
}
CloudApi.SendPowerTotal(powerTotal);
}
/// <summary>
/// 电能表变化值信息 15m
/// </summary>
private static void TaskStaChargingTota()
{
if ((DateTime.Now - _dateTime6).TotalMinutes <= 15)
{
return;
}
_dateTime6 = DateTime.Now;
EmeterEnergyService emeterEnergyService = AppInfo.Container.Resolve<EmeterEnergyService>();
EmeterEnergyChangeService emeterEnergyChangeService = AppInfo.Container.Resolve<EmeterEnergyChangeService>();
List<EmeterEnergy> emeterEnergy = emeterEnergyService.QueryListByClause(u => u.UploadFlag == 0, 7, i => i.UploadTime,
OrderByType.Desc);
5 months ago
emeterEnergy = emeterEnergy.OrderBy(p => p.Code).ToList();
var emeterEnergyChange = emeterEnergyChangeService.QueryListByClause(u => u.UploadFlag == 0, 7, i => i.UploadTime,
OrderByType.Desc);
5 months ago
emeterEnergyChange = emeterEnergyChange.OrderBy(p => p.Code).ToList();
StaChargingTotal staChargingTotal = new()
{
sn = StaticStationInfo.StationNo,
};
staChargingTotal.meter = new List<meter2>();
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓直流表",
ms = emeterEnergy[i].Code,
mk = emeterEnergy[i].Value,
st = DateTime.Now.AddMinutes(-15),
et = DateTime.Now,
};
staChargingTotal.meter.Add(meter);
}
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓交流表",
ms = emeterEnergyChange[i].Code,
mk = emeterEnergyChange[i].Value,
st = DateTime.Now.AddMinutes(-15),
et = DateTime.Now,
};
staChargingTotal.meter.Add(meter);
}
CloudApi.SendStaChargingTotal(staChargingTotal);
}
/// <summary>
/// 电能表小时能耗值信息 1h
/// </summary>
private static void TaskMeterEnergyKwh()
{
if ((DateTime.Now - _dateTime7).TotalHours <= 1)
{
return;
}
_dateTime7 = DateTime.Now;
var emeterEnergy = emeterHourEnergyService.QueryListByClause(u => u.UploadFlag == 0, 14, i => i.Time,
OrderByType.Desc);
5 months ago
emeterEnergy = emeterEnergy.OrderBy(p => p.Code).ToList();
var emeterEnergy1= emeterEnergy.Where(p => p.Type==1).ToList();
var emeterEnergy2= emeterEnergy.Where(p => p.Type==2).ToList();
MeterEnergyKwh meterEnergyKwh = new()
{
sn = StaticStationInfo.StationNo,
};
meterEnergyKwh.meter = new List<meter2>();
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓直流表",
ms = emeterEnergy1[i].Code,
mk = emeterEnergy1[i].Value,
//st = DateTime.Now.AddMinutes(-60),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy1[i].Time),
et = Convert.ToDateTime(emeterEnergy1[i].Time).AddHours(1),
};
meterEnergyKwh.meter.Add(meter);
}
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓交流表",
ms = emeterEnergy2[i].Code,
mk = emeterEnergy2[i].Value,
//st = DateTime.Now.AddMinutes(-15),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy1[i].Time),
et = Convert.ToDateTime(emeterEnergy1[i].Time).AddHours(1),
};
meterEnergyKwh.meter.Add(meter);
}
CloudApi.SendMeterEnergyKwh(meterEnergyKwh);
for (int i = 0; i < emeterEnergy.Count; i++)
{
emeterEnergy[i].UploadFlag = 1;
}
emeterHourEnergyService.Update(emeterEnergy);
}
/// <summary>
/// 电能表天能耗值信息 1d
/// </summary>
private static void TaskMeterDayEnergyVal()
{
if ((DateTime.Now - _dateTime8).TotalDays < 1)
{
return;
}
_dateTime8 = DateTime.Now;
EmeterDayEnergyService emeterDayEnergyService = AppInfo.Container.Resolve<EmeterDayEnergyService>();
var emeterEnergy = emeterDayEnergyService.QueryListByClause(u => u.UploadFlag == 0, 14, i => i.Time,
OrderByType.Desc);
5 months ago
emeterEnergy = emeterEnergy.OrderBy(p => p.Code).ToList();
var emeterEnergy1 = emeterEnergy.Where(p => p.Type == 1).ToList();
var emeterEnergy2 = emeterEnergy.Where(p => p.Type == 2).ToList();
MeterDayEnergyVal meterEnergyKwh = new()
{
sn = StaticStationInfo.StationNo,
};
meterEnergyKwh.meter = new List<meter2>();
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓直流表",
ms = emeterEnergy1[i].Code,
mk = emeterEnergy1[i].Value,
//st = DateTime.Now.AddDays(-1),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy1[i].Time).AddDays(-1),
et = Convert.ToDateTime(emeterEnergy1[i].Time),
};
meterEnergyKwh.meter.Add(meter);
}
for (int i = 0; i < 7; i++)
{
meter2 meter = new()
{
addr = i + 1,
desc = (i + 2).ToString() + "号仓交流表",
ms = emeterEnergy2[i].Code,
mk = emeterEnergy2[i].Value,
//st = DateTime.Now.AddDays(-1),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy1[i].Time).AddDays(-1),
et = Convert.ToDateTime(emeterEnergy1[i].Time),
};
meterEnergyKwh.meter.Add(meter);
}
CloudApi.SendMeterDayEnergyVal(meterEnergyKwh);
for (int i = 0; i < emeterEnergy.Count; i++)
{
emeterEnergy[i].UploadFlag = 1;
}
emeterDayEnergyService.Update(emeterEnergy);
}
/// <summary>
/// 换电站分时用电统计信息 1h
/// </summary>
private static void TaskStaHourEnergyVal()
{
if ((DateTime.Now - _dateTime9).TotalHours <= 1)
{
return;
}
_dateTime9 = DateTime.Now;
var emeterEnergy = emeterHourEnergyService.QueryListByClause(u => u.Type == 1, 7, i => i.Time,
OrderByType.Desc);
5 months ago
float hc = emeterEnergy.Sum(e => e.Value);
StaHourEnergyVal meterEnergyKwh = new()
{
sn = StaticStationInfo.StationNo,
ht= hc,
hc = hc,
hs =0,
ho=0,
//st = DateTime.Now.AddHours(-1),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy?.First().Time),
et = Convert.ToDateTime(emeterEnergy?.First().Time).AddHours(1),
};
CloudApi.SendStaHourEnergyVal(meterEnergyKwh);
}
/// <summary>
/// 换电站每天用电统计信息 1d
/// </summary>
private static void TaskStaDayEnergyVal()
{
if ((DateTime.Now - _dateTime10).TotalDays < 1)
{
return;
}
_dateTime10 = DateTime.Now;
var emeterEnergy = emeterDayEnergyService.QueryListByClause(u => u.Type == 1, 7, i => i.Time,
OrderByType.Desc);
5 months ago
float dc = emeterEnergy.Sum(e => e.Value);
StaDayEnergyVal staDayEnergyVal = new()
{
sn = StaticStationInfo.StationNo,
ht = dc,
hc = dc,
hs = 0,
ho = 0,
//st = DateTime.Now.AddDays(-1),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy?.First().Time).AddDays(-1),
et = Convert.ToDateTime(emeterEnergy?.First().Time),
};
CloudApi.SendStaDayEnergyVal(staDayEnergyVal);
}
/// <summary>
/// 换电站日运行统计结果 1d
/// </summary>
private static void TaskStaDayOpeEnergyVal()
{
if ((DateTime.Now - _dateTime11).TotalDays < 1)
{
return;
}
_dateTime11 = DateTime.Now;
ExStationDayRunResultService exStationDayRunResultService = AppInfo.Container.Resolve<ExStationDayRunResultService>();
var exStationDayRunResult = exStationDayRunResultService.QueryByClause(u => u.Id !=0, i => i.CreatedTime,
OrderByType.Desc);
5 months ago
StaDayOpeEnergyVal staDayOpeEnergyVal = new()
{
sn = StaticStationInfo.StationNo,
sm = StaticStationInfo.StationName,
st = Convert.ToDateTime(exStationDayRunResult?.RunStartTime),
et = Convert.ToDateTime(exStationDayRunResult?.RunEndTime),
tt = Convert.ToInt32(exStationDayRunResult?.ToltalTimeCount),
//tt = 24 * 60,
5 months ago
ft = exStationDayRunResult?.FristSwapTime,
sp = exStationDayRunResult?.StopTime,
5 months ago
ts = Convert.ToSingle(exStationDayRunResult?.ToltalSwapAllTime * 60),
tc = exStationDayRunResult.ToltalSwapCount,
cc = exStationDayRunResult.ChgCount,
os = Convert.ToByte(exStationDayRunResult.State),
at = Convert.ToSingle(exStationDayRunResult?.AvgRepTime),
ct = Convert.ToSingle(exStationDayRunResult?.AvgChgTime),
dt = Convert.ToDateTime(exStationDayRunResult?.SwapDate),
};
CloudApi.SendStaDayOpeEnergyVal(staDayOpeEnergyVal);
}
/// <summary>
/// 换电站分时用电统计信息
/// </summary>
private static void TaskStaHourAmountVal()
{
if ((DateTime.Now - _dateTime2).TotalSeconds <= 30)
{
return;
}
_dateTime2 = DateTime.Now;
var emeterEnergy = emeterDayEnergyService.QueryListByClause(u => u.Type == 1, 7, i => i.Time,
OrderByType.Desc);
5 months ago
float hc = emeterEnergy.Sum(e => e.Value);
ElecPriceModelVersionDetailServices elecPriceModelVersionDetailServices = AppInfo.Container.Resolve<ElecPriceModelVersionDetailServices>();
var elecPriceModelVersionDetail= elecPriceModelVersionDetailServices.QueryListByClause(u=>u.Version==1);
var timeList = elecPriceModelVersionDetail.Select(u => u.StartHour).ToList();
int targetTime = DateTime.Now.Hour;
int index1 = timeList.FindIndex(time => time > targetTime)-1;
index1 = index1<0? timeList.Count-1: index1;
int index2 = timeList.FindIndex(time => time == targetTime);
int index3 = index2 == -1 ? index1 : index2;
var price = elecPriceModelVersionDetail[index3].Price;
StaHourAmountVal staHourAmountVal = new()
{
sn = StaticStationInfo.StationNo,
ht = Convert.ToSingle(hc * price),
hc = Convert.ToSingle(hc * price),
hs = 0,
ho = 0,
//st = DateTime.Now.AddHours(-1),
//et = DateTime.Now,
st = Convert.ToDateTime(emeterEnergy?.First().Time),
et = Convert.ToDateTime(emeterEnergy?.First().Time).AddHours(1),
};
CloudApi.SendStaHourAmountVal(staHourAmountVal);
}
}