|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Core;
|
|
|
|
|
using Common.Util;
|
|
|
|
|
using HybirdFrameworkCore.Autofac;
|
|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
|
using HybirdFrameworkDriver.Common;
|
|
|
|
@ -15,13 +16,13 @@ using MQTTnet.Client.Receiving;
|
|
|
|
|
using MQTTnet.Formatter;
|
|
|
|
|
using MQTTnet.Protocol;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Service.Cloud.Handler;
|
|
|
|
|
using Service.Cloud.Msg;
|
|
|
|
|
using Service.Cloud.Msg.Cloud.Req;
|
|
|
|
|
using Service.Cloud.Msg.Cloud.Resp;
|
|
|
|
|
using Service.Cloud.Msg.Host.Req;
|
|
|
|
|
using Service.Init;
|
|
|
|
|
|
|
|
|
|
namespace Service.Cloud.Client;
|
|
|
|
|
|
|
|
|
@ -38,7 +39,7 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
public string ClientId { get; set; }
|
|
|
|
|
public string? Username { get; set; }
|
|
|
|
|
public string? Password { get; set; }
|
|
|
|
|
public int KeepalivePeriod { get; set; } = 30;
|
|
|
|
|
public int KeepalivePeriod { get; set; } = 3000;
|
|
|
|
|
public int Timeout { get; set; } = 60;
|
|
|
|
|
public string Version { get; set; } = "5.0.16";
|
|
|
|
|
public bool IsCleanSession { get; set; } = false;
|
|
|
|
@ -191,7 +192,15 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
public async Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
await DoSubTopic(SubTopic);
|
|
|
|
|
Publish(new SignIn());
|
|
|
|
|
|
|
|
|
|
SendSignIn(new SignIn()
|
|
|
|
|
{
|
|
|
|
|
sn = StaticStationInfo.StationNo,
|
|
|
|
|
st = "01",
|
|
|
|
|
ss = StaticStationInfo.StationStatus,
|
|
|
|
|
en = 1,
|
|
|
|
|
cn = 7
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DoSubTopic(string topic)
|
|
|
|
@ -223,18 +232,24 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
chipherFlag = Encrypt,
|
|
|
|
|
id = GetIncrementId(),
|
|
|
|
|
sid = StationNo,
|
|
|
|
|
timeStamp = DateTime.Now.Millisecond
|
|
|
|
|
timeStamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000
|
|
|
|
|
},
|
|
|
|
|
body = data
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
model.dataSign = SignData(model);
|
|
|
|
|
|
|
|
|
|
var settings = new JsonSerializerSettings()
|
|
|
|
|
{
|
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
|
|
|
DateFormatString = "yyyy-MM-dd HH:mm:ss",
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
};
|
|
|
|
|
var appMsg = new MqttApplicationMessage
|
|
|
|
|
{
|
|
|
|
|
Topic = PubTopic,
|
|
|
|
|
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model)),
|
|
|
|
|
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
|
|
|
|
|
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model, settings)),
|
|
|
|
|
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
|
|
|
|
Retain = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -242,16 +257,20 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
var result = task.Result;
|
|
|
|
|
if (result.ReasonCode == MqttClientPublishReasonCode.Success)
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"send {JsonConvert.SerializeObject(model)} success");
|
|
|
|
|
Log.Info($"send {JsonConvert.SerializeObject(model, settings)} success");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SignData<T>(Model<T> model) where T : ICmd
|
|
|
|
|
{
|
|
|
|
|
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
|
|
|
|
|
timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
string body = JsonConvert.SerializeObject(model.body, timeConverter);
|
|
|
|
|
return body + ":" + model.Header.timeStamp + ":" + model.Header.id;
|
|
|
|
|
var settings = new JsonSerializerSettings()
|
|
|
|
|
{
|
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
|
|
|
DateFormatString = "yyyy-MM-dd HH:mm:ss",
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
};
|
|
|
|
|
string body = JsonConvert.SerializeObject(model.body, settings);
|
|
|
|
|
return MD5Util.MD5Encrypt32(body + ":" + model.Header.timeStamp + ":" + model.Header.id).ToLower();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -310,10 +329,6 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
{
|
|
|
|
|
Log.Info("cloud disconnect");
|
|
|
|
|
Connected = false;
|
|
|
|
|
if (AutoReConnect)
|
|
|
|
|
{
|
|
|
|
|
return Task.Run(Connect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
@ -358,7 +373,7 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
#region 主动发送CMD
|
|
|
|
|
|
|
|
|
|
public VehicleCertificationResp? SendVehicleCertification(VehicleCertification vehicleCertification,
|
|
|
|
|
TimeSpan timeSpan)
|
|
|
|
|
TimeSpan? timeSpan = null)
|
|
|
|
|
{
|
|
|
|
|
Log.Info(vehicleCertification);
|
|
|
|
|
this.CarAuth.Req = vehicleCertification;
|
|
|
|
@ -421,8 +436,8 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR
|
|
|
|
|
this.Publish(reportingDeviceList);
|
|
|
|
|
return ReportingDevice.GetResp(timeSpan);
|
|
|
|
|
}
|
|
|
|
|
public SignInResp? SendReportingDeviceList(SignIn signIn,
|
|
|
|
|
TimeSpan timeSpan)
|
|
|
|
|
public SignInResp? SendSignIn(SignIn signIn,
|
|
|
|
|
TimeSpan? timeSpan=null)
|
|
|
|
|
{
|
|
|
|
|
this.Sign.Req = signIn;
|
|
|
|
|
this.Publish(signIn);
|
|
|
|
|