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.

138 lines
4.9 KiB

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>();
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>();
SwapOrderBatteryRepository swapOrderBatteryRepository = AppInfo.Container.Resolve<SwapOrderBatteryRepository>();
redisHelper?.GetSubscriber().Subscribe("BatteryStatus", (channel, value) =>
{
try
{
Log.Info($"receive BatteryStatus={value}");
if (value.HasValue)
{
BatteryStatus? data = JsonConvert.DeserializeObject<BatteryStatus>(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<BatteryStatus>(value.ToString());
if (data != null)
{
string jsonString = JsonConvert.SerializeObject(data, Formatting.Indented);
CloudClientMgr.Send(CloudConst.charging, jsonString);
}
}
}
catch (Exception e)
{
Log.Info("error", e);
}
});
}
/// <summary>
/// 统一发送调用
/// </summary>
/// <param name="topic">主题</param>
/// <param name="payload">内容</param>
public static bool Send(string topic, string payload)
{
if (CloudClient != null)
{
if (CloudClient.Connected)
{
CloudClient.Send(topic, payload);
return true;
}
}
return false;
}
}