diff --git a/HybirdFrameworkDriver/Common/MsgCache.cs b/HybirdFrameworkDriver/Common/MsgCache.cs index 079743c..79dec65 100644 --- a/HybirdFrameworkDriver/Common/MsgCache.cs +++ b/HybirdFrameworkDriver/Common/MsgCache.cs @@ -36,12 +36,48 @@ public class MsgCache { _dictionary.Remove(MsgKey + key, out var value); } - - } public class MsgPair { - public TReq? Req { get; set; } - public TResp? Resp { get; set; } + private readonly ManualResetEvent _lock = new(false); + private TReq? _req; + + public TReq? Req + { + get => _req; + set + { + _req = value; + ClearResp(); + } + } + + private TResp? _resp; + + public void ClearResp() + { + _resp = default; + } + + public void SetResp(TResp? resp) + { + _resp = resp; + _lock.Set(); + _lock.Reset(); + } + + public TResp? GetResp(TimeSpan? timeSpan = default) + { + if (timeSpan != null) + { + _lock.WaitOne(timeSpan.Value); + } + else + { + _lock.WaitOne(); + } + + return _resp; + } } \ No newline at end of file diff --git a/Service/Cloud/Client/CloudClient.cs b/Service/Cloud/Client/CloudClient.cs index 1fb4783..5ddfd4c 100644 --- a/Service/Cloud/Client/CloudClient.cs +++ b/Service/Cloud/Client/CloudClient.cs @@ -347,7 +347,6 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR public void SendVehicleCertification(VehicleCertification vehicleCertification) { this.CarAuth.Req = vehicleCertification; - this.CarAuth.Resp = null; this.Publish(vehicleCertification); } diff --git a/Service/Cloud/Handler/CarAuthResHandler.cs b/Service/Cloud/Handler/CarAuthResHandler.cs index 431c485..43401fc 100644 --- a/Service/Cloud/Handler/CarAuthResHandler.cs +++ b/Service/Cloud/Handler/CarAuthResHandler.cs @@ -18,6 +18,6 @@ public class CarAuthResHandler : IBaseHandler public void Handle(string t) { VehicleCertificationResp? resp = JsonConvert.DeserializeObject(t); - CloudClientMgr.CloudClient.CarAuth.Resp = resp; + CloudClientMgr.CloudClient.CarAuth.SetResp(resp); } } \ No newline at end of file