using Autofac; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Redis; using log4net; using Newtonsoft.Json; using Repository.Station; using Service.Cloud.Common; 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; } 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(); SwapOrderBatteryRepository swapOrderBatteryRepository = AppInfo.Container.Resolve(); redisHelper?.GetSubscriber().Subscribe("BatteryStatus", (channel, value) => { try { Log.Info($"receive BatteryStatus={value}"); if (value.HasValue) { BatteryStatus? data = JsonConvert.DeserializeObject(value.ToString()); if (data != null) { //BinInfo? binInfo = binInfoRepository.QueryByClause(it => it.ChargerNo == data.sn); var swapOrderBattery = swapOrderBatteryRepository.QueryListByClause(u => u.DownBatteryNo == data.sn || u.UpBatteryNo == data.sn); BatteryStatus batteryStatus = new BatteryStatus() { sn = data.sn, soc = data.soc, soh = data.soh, energy = data.energy, temperature = data.temperature, voltage_min = data.voltage_max, voltage_max = data.voltage_max, network_status = data.network_status, alarmCode = data.alarmCode, status = data.status, isVehicle = "2", longitude = StaticStationInfo.Longitude, latitude = StaticStationInfo.Latitude, battery_no = swapOrderBattery.Count.ToString(), }; //if ((DateTime.Now - _dateTime).TotalSeconds <= 30) //{ // return; //} //_dateTime = DateTime.Now; string jsonString = JsonConvert.SerializeObject(batteryStatus, Formatting.Indented); CloudClientMgr.Send(CloudConst.batteryInfo, jsonString); } } } catch (Exception e) { Log.Info("error", e); } }); redisHelper?.GetSubscriber().Subscribe("ChargeData", (channel, value) => { try { Log.Info($"receive ChargeData={value}"); if (value.HasValue) { BatteryStatus? data = JsonConvert.DeserializeObject(value.ToString()); if (data != null) { string jsonString = JsonConvert.SerializeObject(data, Formatting.Indented); CloudClientMgr.Send(CloudConst.charging, jsonString); } } } catch (Exception e) { Log.Info("error", e); } }); } /// /// 统一发送调用 /// /// 主题 /// 内容 public static bool Send(string topic, string payload) { if (CloudClient != null) { if (CloudClient.Connected) { CloudClient.Send(topic, payload); return true; } } return false; } }