电子围栏推送服务
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.

192 line
8.3KB

  1. using Microsoft.Extensions.Options;
  2. using TelpoPush.Fence.Worker.Models.CacheTemplates;
  3. using TelpoPush.Fence.Worker.Models.Config;
  4. namespace TelpoPush.Fence.Worker.Service.Cache
  5. {
  6. public class RedisUtil
  7. {
  8. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE = "TELPO#GPSDEVICE_HASH";
  9. private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE = "TELPO#GPSDEVICE_FENCE_HASH";
  10. private const string CACHE_HASH_KEY_FENCE_LAST_TIME = "TELPO_FENCE#GPSDEVICE_LAST_TIME_HASH";
  11. private const string CACHE_HASH_KEY_FENCE_LAST_STATUS = "TELPO_FENCE#GPSDEVICE_LAST_STATUS_HASH";
  12. private readonly ILogger<RedisUtil> _logger;
  13. private readonly ServiceConfig _configService;
  14. private readonly SqlMapper _sqlMapper;
  15. public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
  16. {
  17. _configService = configService.Value;
  18. _logger = logger;
  19. optConfigRedis.Value.Prefix = "";
  20. optConfigRedis.Value.DefaultDatabase = 7;
  21. var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
  22. RedisHelper.Initialization(csredis);
  23. _sqlMapper = sqlMapper;
  24. }
  25. public async Task<DeviceInfoModel> GetGpsDevice(string imei)
  26. {
  27. if (string.IsNullOrWhiteSpace(imei)) return null;
  28. string keyCache = $"{imei}_GpsDevice";
  29. try
  30. {
  31. var objCache = MemoryCacheUtil.Get<DeviceInfoModel>(keyCache);
  32. if (objCache == null)
  33. {
  34. var obj = await RedisHelper.HGetAsync<DeviceInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei);
  35. if (obj == null)
  36. {
  37. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  38. if (deviceInfo != null)
  39. {
  40. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  41. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds);
  42. return deviceInfo;
  43. }
  44. }
  45. else
  46. {
  47. MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds);
  48. return obj;
  49. }
  50. }
  51. return objCache;
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.LogError($"GetGpsDevice,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
  56. var deviceInfo = _sqlMapper.DeviceInfo(imei);
  57. if (deviceInfo != null)
  58. {
  59. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
  60. MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds);
  61. return deviceInfo;
  62. }
  63. }
  64. return null;
  65. }
  66. public bool ClearGpsDeviceFence() {
  67. var fence = _sqlMapper.DeviceFenceAll();
  68. var status = RedisHelper.HGetAll<LastStatusInfo>(CACHE_HASH_KEY_FENCE_LAST_STATUS);
  69. var list = status.Where(s => !fence.Where(p => s.Key == p.imei + "_" + p.geofenceId).Any()).ToList();
  70. foreach (var item in list)
  71. {
  72. RedisHelper.HDel(CACHE_HASH_KEY_FENCE_LAST_STATUS, item.Key);
  73. }
  74. //int[] oldArray = { 1, 2, 3, 4, 5 };
  75. //int[] newArray = { 2, 4, 5, 7, 8, 9 };
  76. //var jiaoJi = oldArray.Intersect(newArray).ToList();//2,4,5
  77. //var oldChaJi = oldArray.Except(newArray).ToList();//1,3
  78. //var newChaJi = newArray.Except(oldArray).ToList();//7,8,9
  79. //var bingJi = oldArray.Union(newArray).ToList();//1,2,3,4,5,7,8,9
  80. return true;
  81. }
  82. public async Task<DeviceFenceModel> GetGpsDeviceFence(string imei)
  83. {
  84. if (string.IsNullOrWhiteSpace(imei)) return null;
  85. var obj = await RedisHelper.HGetAsync<DeviceFenceModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE, imei);
  86. if (obj == null)
  87. {
  88. var device = GetGpsDevice(imei).Result;
  89. if (device == null) return null;
  90. DeviceFenceModel model = new DeviceFenceModel();
  91. model.imei = device.imei;
  92. model.deviceId = device.deviceId;
  93. List<DeviceFenceList> fenceList = new List<DeviceFenceList>();
  94. var list = _sqlMapper.DeviceFencelist(imei);
  95. if (list.Count > 0)
  96. {
  97. foreach (var fence in list)
  98. {
  99. DeviceFenceList val = new DeviceFenceList();
  100. val.geofenceId = fence.geofenceId;
  101. val.geofenceName = fence.geofenceName;
  102. val.WifiInfo = _sqlMapper.DeviceFenceWifi(imei, fence.geofenceId);
  103. val.fenceType = fence.fenceType;
  104. val.isActive = fence.isActive;
  105. val.appType = fence.appType;
  106. val.address = fence.address;
  107. val.alarmType = fence.alarmType;
  108. List<pointList> pointList = new List<pointList>();
  109. if (fence.fenceType == 1)
  110. {
  111. pointList.Add(new pointList()
  112. {
  113. index = 0,
  114. radius = fence.radius,
  115. latitude = fence.latitude,
  116. longitude = fence.longitude
  117. });
  118. }
  119. else
  120. {
  121. var pointLists = _sqlMapper.DeviceFencePointlist(fence.geofenceId);
  122. foreach (var point in pointLists)
  123. {
  124. pointList.Add(new pointList()
  125. {
  126. index = point.index,
  127. radius = 0,
  128. latitude = point.latitude,
  129. longitude = point.longitude
  130. });
  131. }
  132. }
  133. val.pointList = pointList;
  134. fenceList.Add(val);
  135. }
  136. model.count = fenceList.Count;
  137. model.fenceList = fenceList;
  138. await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE, imei, model);
  139. return model;
  140. }
  141. else
  142. return null;
  143. }
  144. return obj;
  145. }
  146. public async Task<bool> GetIsDateStatus(DeviceTime model)
  147. {
  148. bool result = true;
  149. try
  150. {
  151. string file = model.imei;
  152. if (string.IsNullOrWhiteSpace(model.imei)) return false;
  153. var obj = await RedisHelper.HGetAsync<DeviceTime>(CACHE_HASH_KEY_FENCE_LAST_TIME, file);
  154. if (obj == null)
  155. await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_TIME, file, model);
  156. else
  157. {
  158. if (model.dt > obj.dt)
  159. await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_TIME, file, model);
  160. else
  161. result = false;
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. string exs = ex.Message;
  167. }
  168. return result;
  169. }
  170. public async Task SetFenceLastStatus(string fenceKey, LastStatusInfo info)
  171. {
  172. await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_STATUS, fenceKey, info);
  173. }
  174. public async Task<LastStatusInfo> GetFenceLastStatus(string fenceKey)
  175. {
  176. if (string.IsNullOrWhiteSpace(fenceKey)) return null;
  177. var status = await RedisHelper.HGetAsync<LastStatusInfo>(CACHE_HASH_KEY_FENCE_LAST_STATUS, fenceKey);
  178. return status;
  179. }
  180. }
  181. }