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.

702 lines
23 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 _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
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>();
redisHelper?.GetSubscriber().Subscribe("UploadTelemetryData", (channel, value) =>
{
try
{
Log.Info($"receive UploadTelemetryData={value}");
if (value.HasValue)
{
UploadTelemetryData? data = JsonConvert.DeserializeObject<UploadTelemetryData>(value.ToString());
if (data != null)
{
BinInfo? binInfo = binInfoRepository.QueryByClause(it => it.ChargerNo == data.ChargerNo);
ChargeDevDataInfo req = new ChargeDevDataInfo();
req.sn = StaticStationInfo.StationNo;
req.en = StaticStationInfo.StationNo + data.ChargerNo;
req.sd = "A" + int.Parse(binInfo.No);
req.mtp = StaticStationInfo.ChargePower;
req.mcr = 1;
req.hb = binInfo?.Exists ?? 0;
req.el = 0;
req.cno = int.Parse(binInfo.No);
req.cs = binInfo.ChargeStatus ?? 0;
req.fs = 0;
req.@as = 0;
//fc = data.,
//st = data.,
req.ct = data.ChargingTime;
//ssoc = data.,
req.csoc = data.CurrentSoc;
//ssoe = data.,
//csoe = data.,
req.cvot = data.BmsChargingVoltage;
req.ccur = data.BmsChargingCurrent;
req.nvot = data.BmsNeedVoltage;
req.ncur = data.BmsNeedCurrent;
req.lsv = data.SingleBatteryMinVoltage;
req.hsv = data.SingleBatteryMaxVoltage;
req.lst = data.MinBatteryTemp;
req.hst = data.MaxBatteryTemp;
req.ws = 0xFF;
req.it = 0xFF;
req.ot = 0xFF;
req.bt = DateTime.Now;
if ((DateTime.Now - _dateTime).TotalSeconds <= 30)
{
return;
}
_dateTime = DateTime.Now;
CloudClient?.SendChargeDevDataInfo(req);
}
}
}
catch (Exception e)
{
Log.Info("error", e);
}
});
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)
{
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)
{
//Log.Info("测试111111111111111111111111111111111111111111111111111111111111111111111111111111");
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;
EquipAlarmRecordRepository equipAlarmRecordRepository = AppInfo.Container.Resolve<EquipAlarmRecordRepository>();
var EquipAlarm = equipAlarmRecordRepository.Query();
var ErrorLevel = Convert.ToInt32(EquipAlarm.Min(x => x.ErrorLevel));
StationRunStatus stationRunStatus = new()
{
rs = StaticStationInfo.StationStatus == 1 ? 1 : 2,
os = StaticStationInfo.StationStatus,
fl = ErrorLevel,
};
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,
ft = Convert.ToDateTime(exStationDayRunResult?.FristSwapTime),
sp = Convert.ToDateTime(exStationDayRunResult?.StopTime),
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);
}
}