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.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(); BinInfoRepository binInfoRepository = AppInfo.Container.Resolve(); redisHelper?.GetSubscriber().Subscribe("UploadTelemetryData", (channel, value) => { try { Log.Info($"receive UploadTelemetryData={value}"); if (value.HasValue) { UploadTelemetryData? data = JsonConvert.DeserializeObject(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(value.ToString()); if (data != null) { CloudClient?.SendBatDataInfo(data); } } } catch (Exception e) { Log.Info("error", e); } }); } }