diff --git a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs index 1a2f43d..42e12d6 100644 --- a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs +++ b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs @@ -84,56 +84,58 @@ namespace YD_AllHeartRates.Api.Mqtt return _queue.Writer.WriteAsync(msg, stoppingToken).AsTask(); }; + List studentList; + List devices; + + var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}"; + var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}"; + + studentList = _caching.Get>(studentListCacheKey); + devices = _caching.Get>(deviceListCacheKey); + + if (studentList == null || devices == null) + { + studentList = ( + from s in _smartSportsContext.Student + join c in _smartSportsContext.Class on s.ClassId equals c.Id + join d in _smartSportsContext.Device on s.StudentNo equals d.StudentNo into sd + from d in sd.DefaultIfEmpty() + where s.SchoolCode == AppSettings.SchoolCode && s.StudentStatus == 1 + select new StudentDto + { + SchoolCode = s.SchoolCode, + StudentNo = s.StudentNo, + StudentName = s.StudentName, + Sex = s.Sex, + Age = s.Age, + HeartRateId = d != null ? d.Code : "", + JumpRopeId = d != null ? d.Code : "", + ClassId = s.ClassId, + ClassName = s.ClassName, + GradeId = c.GradeId, + GradeName = c.GradeName ?? "", + DeviceType = d != null ? d.DeviceType : 0 + } + ).ToList(); + + + devices = _smartSportsContext.Device.Where(x => x.SchoolCode == AppSettings.SchoolCode && !string.IsNullOrWhiteSpace(x.StudentNo)).ToList(); + + _caching.AddObject(studentListCacheKey, studentList, 600); + _caching.AddObject(deviceListCacheKey, devices, 600); + } + + var heartRateEntities = new ConcurrentBag(); + var jumpRopeEntities = new ConcurrentBag(); + + var deviceHMap = devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo); + var deviceJMap = devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo); + + var studentMap = studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First()); + + await foreach (var batch in ReadBatchesAsync(stoppingToken)) { - List studentList; - List devices; - - var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}"; - var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}"; - - studentList = _caching.Get>(studentListCacheKey); - devices = _caching.Get>(deviceListCacheKey); - - if (studentList == null || devices == null) - { - studentList = ( - from s in _smartSportsContext.Student - join c in _smartSportsContext.Class on s.ClassId equals c.Id - join d in _smartSportsContext.Device on s.StudentNo equals d.StudentNo into sd - from d in sd.DefaultIfEmpty() - where s.SchoolCode == AppSettings.SchoolCode && s.StudentStatus == 1 - select new StudentDto - { - SchoolCode = s.SchoolCode, - StudentNo = s.StudentNo, - StudentName = s.StudentName, - Sex = s.Sex, - Age = s.Age, - HeartRateId = d != null ? d.Code : "", - JumpRopeId = d != null ? d.Code : "", - ClassId = s.ClassId, - ClassName = s.ClassName, - GradeId = c.GradeId, - GradeName = c.GradeName ?? "", - DeviceType = d != null ? d.DeviceType : 0 - } - ).ToList(); - - - devices = _smartSportsContext.Device.Where(x => x.SchoolCode == AppSettings.SchoolCode && !string.IsNullOrWhiteSpace(x.StudentNo)).ToList(); - - _caching.AddObject(studentListCacheKey, studentList, 600); - _caching.AddObject(deviceListCacheKey, devices, 600); - } - - var heartRateEntities = new ConcurrentBag(); - var jumpRopeEntities = new ConcurrentBag(); - - var deviceHMap = devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo); - var deviceJMap = devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo); - - var studentMap = studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First()); Parallel.ForEach(batch, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, msg => { diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs index 5236787..336ae75 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4af2ed6c7dde8915d38199959dc15eb0626ad29e")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a15af967e2dd98f88c113b78a7bd304a4d3c0593")] [assembly: System.Reflection.AssemblyProductAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyTitleAttribute("YD_AllHeartRates.Api")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache index 2618ff6..47a2061 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.AssemblyInfoInputs.cache @@ -1 +1 @@ -beaa3e786d777e801e865749bc1ed887c9c8bed6062702fc86efc5e999ecbaef +22e8205ab6b5676d7d793a438db64102c980db506ddb7d0babd8c83fd38fab3a diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.CoreCompileInputs.cache b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.CoreCompileInputs.cache index 155c4f2..782a37c 100644 --- a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.CoreCompileInputs.cache +++ b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -5f7fb39ffdbe64b582eda15bdc00418a6579ad165c38355ff639a59366e2f9f7 +9b82f68e122f7768a48d83e21fcb65980b61db40f12686a3b3daf32153b4fa35 diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll index 30da94d..f3464af 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.dll differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb index b3824c3..7eb93ca 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb and b/YD_AllHeartRates.Api/obj/Debug/net6.0/YD_AllHeartRates.Api.pdb differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe b/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe index f384e24..fb2d676 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe and b/YD_AllHeartRates.Api/obj/Debug/net6.0/apphost.exe differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll index 28aa0dc..a72f20b 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/ref/YD_AllHeartRates.Api.dll differ diff --git a/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll b/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll index 28aa0dc..a72f20b 100644 Binary files a/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll and b/YD_AllHeartRates.Api/obj/Debug/net6.0/refint/YD_AllHeartRates.Api.dll differ