|
- using Microsoft.Extensions.Options;
- using TelpoPush.Fence.Worker.Models.CacheTemplates;
- using TelpoPush.Fence.Worker.Models.Config;
-
- namespace TelpoPush.Fence.Worker.Service.Cache
- {
- public class RedisUtil
- {
- private const string CACHE_HASH_KEY_TELPO_GPSDEVICE = "TELPO#GPSDEVICE_HASH";
- private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE = "TELPO#GPSDEVICE_FENCE_HASH";
- private const string CACHE_HASH_KEY_FENCE_LAST_TIME = "TELPO_FENCE#GPSDEVICE_LAST_TIME_HASH";
- private const string CACHE_HASH_KEY_FENCE_LAST_STATUS = "TELPO_FENCE#GPSDEVICE_LAST_STATUS_HASH";
- private readonly ILogger<RedisUtil> _logger;
- private readonly ServiceConfig _configService;
- private readonly SqlMapper _sqlMapper;
- public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
- {
- _configService = configService.Value;
- _logger = logger;
- optConfigRedis.Value.Prefix = "";
- optConfigRedis.Value.DefaultDatabase = 7;
- var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
- RedisHelper.Initialization(csredis);
- _sqlMapper = sqlMapper;
- }
-
- public async Task<DeviceInfoModel> GetGpsDevice(string imei)
- {
- if (string.IsNullOrWhiteSpace(imei)) return null;
- string keyCache = $"{imei}_GpsDevice";
- try
- {
- var objCache = MemoryCacheUtil.Get<DeviceInfoModel>(keyCache);
- if (objCache == null)
- {
- var obj = await RedisHelper.HGetAsync<DeviceInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei);
- if (obj == null)
- {
- var deviceInfo = _sqlMapper.DeviceInfo(imei);
- if (deviceInfo != null)
- {
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
- MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds);
- return deviceInfo;
- }
- }
- else
- {
- MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds);
- return obj;
- }
- }
- return objCache;
- }
- catch (Exception ex)
- {
- _logger.LogError($"GetGpsDevice,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
- var deviceInfo = _sqlMapper.DeviceInfo(imei);
- if (deviceInfo != null)
- {
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
- MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds);
- return deviceInfo;
- }
- }
- return null;
- }
-
- public bool ClearGpsDeviceFence() {
-
- var fence = _sqlMapper.DeviceFenceAll();
- var status = RedisHelper.HGetAll<LastStatusInfo>(CACHE_HASH_KEY_FENCE_LAST_STATUS);
-
- var list = status.Where(s => !fence.Where(p => s.Key == p.imei + "_" + p.geofenceId).Any()).ToList();
-
- foreach (var item in list)
- {
- RedisHelper.HDel(CACHE_HASH_KEY_FENCE_LAST_STATUS, item.Key);
- }
- //int[] oldArray = { 1, 2, 3, 4, 5 };
- //int[] newArray = { 2, 4, 5, 7, 8, 9 };
- //var jiaoJi = oldArray.Intersect(newArray).ToList();//2,4,5
- //var oldChaJi = oldArray.Except(newArray).ToList();//1,3
- //var newChaJi = newArray.Except(oldArray).ToList();//7,8,9
- //var bingJi = oldArray.Union(newArray).ToList();//1,2,3,4,5,7,8,9
-
- return true;
- }
-
-
- public async Task<DeviceFenceModel> GetGpsDeviceFence(string imei)
- {
- if (string.IsNullOrWhiteSpace(imei)) return null;
- var obj = await RedisHelper.HGetAsync<DeviceFenceModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE, imei);
- if (obj == null)
- {
- var device = GetGpsDevice(imei).Result;
- if (device == null) return null;
- DeviceFenceModel model = new DeviceFenceModel();
- model.imei = device.imei;
- model.deviceId = device.deviceId;
- List<DeviceFenceList> fenceList = new List<DeviceFenceList>();
- var list = _sqlMapper.DeviceFencelist(imei);
- if (list.Count > 0)
- {
- foreach (var fence in list)
- {
- DeviceFenceList val = new DeviceFenceList();
- val.geofenceId = fence.geofenceId;
- val.geofenceName = fence.geofenceName;
- val.WifiInfo = _sqlMapper.DeviceFenceWifi(imei, fence.geofenceId);
- val.fenceType = fence.fenceType;
- val.isActive = fence.isActive;
- val.appType = fence.appType;
- val.address = fence.address;
- val.alarmType = fence.alarmType;
- List<pointList> pointList = new List<pointList>();
- if (fence.fenceType == 1)
- {
- pointList.Add(new pointList()
- {
- index = 0,
- radius = fence.radius,
- latitude = fence.latitude,
- longitude = fence.longitude
- });
- }
- else
- {
- var pointLists = _sqlMapper.DeviceFencePointlist(fence.geofenceId);
- foreach (var point in pointLists)
- {
- pointList.Add(new pointList()
- {
- index = point.index,
- radius = 0,
- latitude = point.latitude,
- longitude = point.longitude
- });
- }
- }
- val.pointList = pointList;
- fenceList.Add(val);
- }
- model.count = fenceList.Count;
- model.fenceList = fenceList;
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE_FENCE, imei, model);
- return model;
- }
- else
- return null;
- }
- return obj;
- }
- public async Task<bool> GetIsDateStatus(DeviceTime model)
- {
- bool result = true;
- try
- {
- string file = model.imei;
- if (string.IsNullOrWhiteSpace(model.imei)) return false;
- var obj = await RedisHelper.HGetAsync<DeviceTime>(CACHE_HASH_KEY_FENCE_LAST_TIME, file);
- if (obj == null)
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_TIME, file, model);
- else
- {
- if (model.dt > obj.dt)
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_TIME, file, model);
- else
- result = false;
- }
- }
- catch (Exception ex)
- {
- string exs = ex.Message;
- }
- return result;
- }
- public async Task SetFenceLastStatus(string fenceKey, LastStatusInfo info)
- {
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_FENCE_LAST_STATUS, fenceKey, info);
- }
- public async Task<LastStatusInfo> GetFenceLastStatus(string fenceKey)
- {
- if (string.IsNullOrWhiteSpace(fenceKey)) return null;
- var status = await RedisHelper.HGetAsync<LastStatusInfo>(CACHE_HASH_KEY_FENCE_LAST_STATUS, fenceKey);
- return status;
- }
-
- }
- }
|