MqttWorkService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Autofac.Core;
  2. using LinqKit;
  3. using Microsoft.Extensions.Hosting;
  4. using Ropin.Inspection.Model.Entities;
  5. using Microsoft.Extensions.Logging;
  6. using Ropin.Inspection.Repository;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Microsoft.EntityFrameworkCore.Internal;
  12. using System.Linq;
  13. using System.Data;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Quartz.Impl.AdoJobStore.Common;
  16. using Ropin.Core.Extensions.Redis;
  17. using AutoMapper.Configuration;
  18. using Newtonsoft.Json;
  19. using System.Security.Cryptography;
  20. using log4net;
  21. namespace Ropin.Environmentally.MqttService
  22. {
  23. public class MqttWorkService : IHostedService, IDisposable
  24. {
  25. private readonly ILogger _logger;
  26. private readonly IServiceProvider _provider;
  27. private readonly IRedisBasketRepository _redisService;
  28. private readonly Func<InspectionDbContext> _dbFuncContextFactory;
  29. public readonly ITdevBoxDevSpotRepository _tdevBoxDevSpotRepository;
  30. public readonly MqttOptions _mqttOptions;
  31. private static readonly ILog log = LogManager.GetLogger(typeof(MqttWorkService));
  32. public MqttWorkService(ILogger<MqttWorkService> logger,
  33. IServiceProvider provider,
  34. MqttOptions mqttOptions,
  35. IRedisBasketRepository redisService,
  36. Func<InspectionDbContext> dbContextFactory
  37. //, ITdevBoxDevSpotRepository tdevBoxDevSpotRepository
  38. )
  39. {
  40. _logger = logger;
  41. _provider = provider;
  42. using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
  43. {
  44. _tdevBoxDevSpotRepository = scope.ServiceProvider.GetService<ITdevBoxDevSpotRepository>();
  45. };
  46. _mqttOptions = mqttOptions;
  47. _redisService = redisService;
  48. _dbFuncContextFactory = dbContextFactory;
  49. //_tdevBoxDevSpotRepository = tdevBoxDevSpotRepository;
  50. }
  51. public void Dispose()
  52. {
  53. throw new NotImplementedException();
  54. }
  55. public Task StartAsync(CancellationToken cancellationToken)
  56. {
  57. Task.Run(async () =>
  58. {
  59. while (true)
  60. {
  61. try
  62. {
  63. var bol = await _redisService.Exist("mqtt_BoxDevSpot_IsUpdate");
  64. log.Info($"[开始] MqttWorkService.【mqtt_BoxDevSpot_IsUpdate是否存在:{bol}】");
  65. if (bol)
  66. {
  67. string val = await _redisService.GetValue("mqtt_BoxDevSpot_IsUpdate");
  68. log.Info($"[开始] MqttWorkService.【mqtt_BoxDevSpot_IsUpdate是否修改值:{bol}】");
  69. if (val=="1")
  70. {
  71. IList<TDEV_BoxDevSpot> boxDevSpot = null;
  72. using (var dbContext = _dbFuncContextFactory())
  73. {
  74. boxDevSpot = dbContext.TDEV_BoxDevSpot.AsQueryable().ToList();
  75. if (boxDevSpot != null && boxDevSpot.Count > 0)
  76. {
  77. boxDevSpot = boxDevSpot.Where(t => t.C_Status == "1").ToList();
  78. }
  79. }
  80. log.Info($"[开始] MqttWorkService.TDEV_BoxDevSpot数据【{JsonConvert.SerializeObject(boxDevSpot)}】");
  81. if (boxDevSpot != null&& boxDevSpot.Count>0)
  82. {
  83. List<MqttDevicePoint> mqttDevicePoints = new List<MqttDevicePoint>();
  84. foreach (var devicePoint in boxDevSpot)
  85. {
  86. if (devicePoint!=null)
  87. {
  88. MqttDevicePoint point = new MqttDevicePoint()
  89. {
  90. Id= devicePoint.C_ID,
  91. ChineseName= devicePoint.C_ChineseName,
  92. BoxId= devicePoint.C_BoxCode,
  93. EnglishName= devicePoint.C_EnglishName,
  94. GroupName= devicePoint.C_GroupName
  95. };
  96. mqttDevicePoints.Add(point);
  97. }
  98. }
  99. MqttClientService.AddMqttClient1(_mqttOptions, mqttDevicePoints);
  100. await _redisService.Set("mqtt_BoxDevSpot_IsUpdate", "0", TimeSpan.FromDays(7));
  101. log.Info($"[修改] mqtt_BoxDevSpot_IsUpdate=0");
  102. }
  103. }
  104. }
  105. await Task.Delay(300000);//5分钟
  106. }
  107. catch (Exception ex)
  108. {
  109. log.Error("[MqttWorkService]Task1:" + ex.Message);
  110. await Task.Delay(5000);//5秒
  111. }
  112. }
  113. });
  114. return Task.CompletedTask;
  115. }
  116. public Task StopAsync(CancellationToken cancellationToken)
  117. {
  118. Dispose();
  119. _logger.LogInformation("内部任务计划结束");
  120. return Task.CompletedTask;
  121. }
  122. }
  123. }