万佳安设备数据
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.

202 lines
9.4KB

  1. using Microsoft.Extensions.Options;
  2. using TelpoDataService.Util.Entities.GpsLocationHistory;
  3. using TelpoPush.WanJiaAn.Worker.Models.CacheTemplates;
  4. using TelpoPush.WanJiaAn.Worker.Models.Config;
  5. namespace TelpoPush.WanJiaAn.Worker.Service.Cache
  6. {
  7. public class RedisUtil
  8. {
  9. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE = "TELPO#GPSDEVICE_HASH";
  10. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PERSON = "TELPO#GPSDEVICE_PERSON_HASH";
  11. private const string CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG = "TELPO#MANUFACTOR_CONFG_HASH";
  12. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS = "TELPO#GPSDEVICE_PUSH_SITTINGS_HASH";
  13. private const string CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT = "TELPO#WANJIAAN_ALARMEVENT_HASH";
  14. private readonly ILogger<RedisUtil> _logger;
  15. private readonly ServiceConfig _configService;
  16. private readonly SqlMapper _sqlMapper;
  17. public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
  18. {
  19. _configService = configService.Value;
  20. _logger = logger;
  21. optConfigRedis.Value.Prefix = "";
  22. optConfigRedis.Value.DefaultDatabase = 7;
  23. var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
  24. RedisHelper.Initialization(csredis);
  25. _sqlMapper = sqlMapper;
  26. }
  27. public async Task<DeviceInfoModel> GetGpsDevice(string imei)
  28. {
  29. if (string.IsNullOrWhiteSpace(imei)) return null;
  30. string keyCache = $"{imei}_GpsDevice";
  31. try
  32. {
  33. var objCache = MemoryCacheUtil.Get<DeviceInfoModel>(keyCache);
  34. if (objCache == null)
  35. {
  36. var obj = await RedisHelper.HGetAsync<DeviceInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei);
  37. if (obj == null)
  38. {
  39. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  40. if (deviceInfo != null)
  41. {
  42. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  43. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
  44. return deviceInfo;
  45. }
  46. }
  47. else
  48. {
  49. MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
  50. return obj;
  51. }
  52. }
  53. return objCache;
  54. }
  55. catch (Exception ex)
  56. {
  57. _logger.LogError($"GetGpsDevice,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  58. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  59. if (deviceInfo != null)
  60. {
  61. RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  62. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
  63. return deviceInfo;
  64. }
  65. }
  66. return null;
  67. }
  68. public async Task<string> GetHealthyDeviceKey(string imei)
  69. {
  70. if (string.IsNullOrWhiteSpace(imei)) return null;
  71. string HealthyDeviceKey = $"Telpol:HealthyDeviceKey:{imei}";
  72. string keyCache = $"{imei}_HealthyDeviceKey";
  73. try
  74. {
  75. var objCache = MemoryCacheUtil.Get<string>(keyCache);
  76. if (objCache == null)
  77. {
  78. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  79. if (!string.IsNullOrEmpty(deviceKey))
  80. {
  81. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  82. return deviceKey;
  83. }
  84. }
  85. return objCache;
  86. }
  87. catch (Exception ex)
  88. {
  89. _logger.LogError($"GetHealthyDeviceKey,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  90. string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
  91. if (!string.IsNullOrEmpty(deviceKey))
  92. {
  93. MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
  94. return deviceKey;
  95. }
  96. else
  97. return "";
  98. }
  99. }
  100. public async Task<SettingInfosItem> GetManufactorPushSettingHash(string imei, string manufactorId, int dataType)
  101. {
  102. if (string.IsNullOrWhiteSpace(manufactorId)) return null;
  103. string keyCache = $"{manufactorId}_{dataType}_ManufactorPushSettingHash";
  104. SettingInfosItem settingInfos = null;
  105. try
  106. {
  107. var objCache = MemoryCacheUtil.Get<SettingInfosItem>(keyCache);
  108. if (objCache == null)
  109. {
  110. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  111. if (obj != null)
  112. {
  113. if (obj.pushSettings.Any())
  114. {
  115. DateTime dt = DateTime.Now;
  116. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  117. if (settingsItem != null)
  118. {
  119. if (settingsItem.settingInfos.Any())
  120. {
  121. foreach (var item in settingsItem.settingInfos)
  122. {
  123. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  124. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  125. if (item.pushFlag && (dt > startTime && dt < endTime))
  126. {
  127. settingInfos = item;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. if (settingInfos != null)
  135. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  136. else
  137. MemoryCacheUtil.Remove(keyCache);
  138. }
  139. }
  140. else
  141. settingInfos = objCache;
  142. }
  143. catch (Exception ex)
  144. {
  145. _logger.LogError($"GetManufactorPushSettingHash(imei={imei},dataType={dataType}),key={manufactorId},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  146. var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
  147. if (obj != null)
  148. {
  149. if (obj.pushSettings.Any())
  150. {
  151. DateTime dt = DateTime.Now;
  152. var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
  153. if (settingsItem != null)
  154. {
  155. if (settingsItem.settingInfos.Any())
  156. {
  157. foreach (var item in settingsItem.settingInfos)
  158. {
  159. DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
  160. DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
  161. if (item.pushFlag && (dt > startTime && dt < endTime))
  162. {
  163. settingInfos = item;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. if (settingInfos != null)
  171. MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
  172. else
  173. MemoryCacheUtil.Remove(keyCache);
  174. }
  175. }
  176. return settingInfos;
  177. }
  178. public async Task SetWanjiaanAlarmEvent(string key, WanjiaanAlarmEvent data)
  179. {
  180. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key, data);
  181. }
  182. public async Task DelWanjiaanAlarmEvent(string key)
  183. {
  184. await RedisHelper.HDelAsync(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key);
  185. }
  186. public async Task<WanjiaanAlarmEvent> GetWanjiaanAlarmEvent(string key)
  187. {
  188. return await RedisHelper.HGetAsync<WanjiaanAlarmEvent>(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key);
  189. }
  190. }
  191. }