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.

106 lines
4.1 KiB

using Autofac;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Redis;
using log4net;
using Newtonsoft.Json;
using Repository.Station;
using Service.Cloud.Msg;
using Service.Cloud.Msg.Host.Req;
using Service.Init;
namespace Service.Cloud.Client;
[Scope("SingleInstance")]
public class CloudClientMgr
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CloudClientMgr));
public static CloudClient? CloudClient { get; set; }
private static DateTime _dateTime = DateTime.Now.AddSeconds(-30);
public static 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();
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()
{
sn = StaticStationInfo.StationNo,
en = data.ChargerNo,
sd = "A"+int.Parse(data.ChargerNo),
mtp = StaticStationInfo.ChargePower,
mcr = 1,
hb = binInfo?.Exists??0,
el = 0,
cno = int.Parse(data.ChargerNo),
cs = binInfo.ChargeStatus??0,
fs = 0,
@as = 0,
//fc = data.,
//st = data.,
ct = data.ChargingTime,
//ssoc = data.,
csoc = data.CurrentSoc,
//ssoe = data.,
//csoe = data.,
cvot = data.BmsChargingVoltage,
ccur = data.BmsChargingCurrent,
nvot = data.BmsNeedVoltage,
ncur = data.BmsNeedCurrent,
lsv = data.SingleBatteryMinVoltage,
hsv = data.SingleBatteryMaxVoltage,
lst = data.MinBatteryTemp,
hst = data.MaxBatteryTemp,
ws = 0xFF,
it = 0xFF,
ot = 0xFF,
bt = DateTime.Now
};
if ((DateTime.Now - _dateTime).TotalSeconds <= 30)
{
return;
}
_dateTime = DateTime.Now;
CloudClient?.SendChargeDevDataInfo(req);
}
}
}
catch (Exception e)
{
Log.Info("error", e);
}
});
}
}