|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using GpsCardGatewayPosition.Model.Config;
- using GpsCardGatewayPosition.Model.Enum;
- using GpsCardGatewayPosition.Service.Cache;
- using GpsCardGatewayPosition.Service.MqProducer;
- using GpsCardGatewayPosition.Service.Resolver.Property.Dto;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TelpoDataService.Util.Clients;
- using TelpoDataService.Util.Entities.GpsLocationHistory;
-
- namespace GpsCardGatewayPosition.Service.Biz.Health
- {
- public class HealthLogic
- {
- public enum TemperatureStatusType : int
- {
-
-
-
- NotExisted = 0,
-
-
-
- FilledContent = 1,
-
-
-
- FilledAddress = 2,
-
-
-
- Complete = 3,
-
-
-
- Reserved
- }
-
- private const string KEY_CACHE_TEMPERATURE_STATUS = "Temperature_Status";
- private const int KEY_CACHE_SECONDS = 600;
-
- private readonly ServiceConfig _configService;
- private readonly MqProcessLogic _serviceMqProcess;
- private readonly PersonCacheManager _personCacheMgr;
- private readonly DeviceCacheManager _deviceCacheMgr;
-
- private readonly GpsLocationHistoryAccessorClient<HisGpsTemperature> _hisTemperatureApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsStep> _hisStepApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsHeartRate> _hisHeartApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsSpo2> _hisSpo2ApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsBloodPress> _hisBloodPressApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsDrownReport> _hisDrownReportApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsWearStatus> _hisWearStatusApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsSportResult> _hisSportResultApiClient;
- private readonly ILogger<HealthLogic> _logger;
-
-
-
- public HealthLogic(IOptions<ServiceConfig> optConfigService, MqProcessLogic serviceMqProcess,
- // StepCacheManager stepCacheMgr,
- PersonCacheManager personCacheMgr,
- DeviceCacheManager deviceCacheMgr,
- GpsLocationHistoryAccessorClient<HisGpsTemperature> temperatureApiClient,
- GpsLocationHistoryAccessorClient<HisGpsStep> stepApiClient,
- GpsLocationHistoryAccessorClient<HisGpsHeartRate> hisHeartApiClient,
- GpsLocationHistoryAccessorClient<HisGpsSpo2> hisSpo2ApiClient,
- GpsLocationHistoryAccessorClient<HisGpsBloodPress> hisBloodPressApiClient,
- GpsLocationHistoryAccessorClient<HisGpsDrownReport> hisDrownReportApiClient,
- GpsLocationHistoryAccessorClient<HisGpsWearStatus> hisWearStatusApiClient,
- GpsLocationHistoryAccessorClient<HisGpsSportResult> hisSportResultApiClient,
- ILogger<HealthLogic> logger)
- {
- _configService = optConfigService.Value;
- _serviceMqProcess = serviceMqProcess;
-
- _deviceCacheMgr = deviceCacheMgr;
- _personCacheMgr = personCacheMgr;
-
- _hisTemperatureApiClient = temperatureApiClient;
- _hisStepApiClient = stepApiClient;
- _logger = logger;
- _hisHeartApiClient = hisHeartApiClient;
- _hisSpo2ApiClient = hisSpo2ApiClient;
- _hisDrownReportApiClient = hisDrownReportApiClient;
- _hisWearStatusApiClient = hisWearStatusApiClient;
- _hisBloodPressApiClient = hisBloodPressApiClient;
- _hisSportResultApiClient = hisSportResultApiClient;
- }
-
- #region 体温相关
-
-
-
-
-
-
-
-
-
-
- public async Task<bool> HandleTemperatureContent(string messageId, string tempId,
- Func<HisGpsTemperature> factoryToAdd, Func<HisGpsTemperature, HisGpsTemperature> factoryToUpdate,
- Action handleFollowup = null, LocationType type = default)
- {
- if (factoryToAdd == null || factoryToUpdate == null) throw new ArgumentNullException();
-
- HisGpsTemperature temperature = null;
-
- var status = await GetTemperatureStatusAsync(tempId).ConfigureAwait(false);
- if (status == TemperatureStatusType.Complete || status == TemperatureStatusType.FilledContent)
- {
- _logger.LogWarning($"TEMPERATURE事件[{tempId}]已处理(内容部分)|事件");
- return false;
- }
-
- if (status == TemperatureStatusType.NotExisted)
- {
- temperature = factoryToAdd.Invoke();
- await AddTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.FilledContent);
-
- Thread thread = new Thread(async () =>
- {
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}] 无地址测温推送延时60s,判断入库后gps_temperature表中address是否为空,若为空也推送");
- Task.Delay(TimeSpan.FromSeconds(60)).Wait();
- var temperatureLate = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (string.IsNullOrEmpty(temperatureLate.Address))
- {
- PushWxTemperature(messageId, temperatureLate);
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}]延时60s,无地址已推送");
- }
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}]延时60s,在HandleTemperatureAddressAsync地址已推送");
- });
- thread.Start();
-
- }
- else
- {
- temperature = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (temperature != null)
- {
- temperature = factoryToUpdate.Invoke(temperature);
- await UpdateTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.Complete);
-
-
- await PushWxTemperature(messageId, temperature, type);
- }
- }
-
- handleFollowup?.Invoke();
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public async Task<bool> HandleTemperatureAddressAsync(string messageId, string tempId,
- Func<HisGpsTemperature> factoryToAdd, Func<HisGpsTemperature, HisGpsTemperature> factoryToUpdate,
- Action handleFollowup = null, LocationType type = default)
- {
- if (factoryToAdd == null || factoryToUpdate == null) throw new ArgumentNullException();
- HisGpsTemperature temperature = null;
-
- var status = await GetTemperatureStatusAsync(tempId).ConfigureAwait(false);
- if (status == TemperatureStatusType.Complete || status == TemperatureStatusType.FilledAddress)
- {
- _logger.LogWarning($"TEMPERATURE事件[{tempId}]已处理(地址信息内容部分)");
- return false;
- }
-
- if (status == TemperatureStatusType.NotExisted)
- {
- temperature = factoryToAdd.Invoke();
- await AddTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.FilledAddress);
-
-
-
-
-
- }
- else
- {
- temperature = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (temperature != null)
- {
- temperature = factoryToUpdate.Invoke(temperature);
- await UpdateTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.Complete);
-
-
- PushWxTemperature(messageId, temperature, type);
- }
- }
-
- handleFollowup?.Invoke();
- return true;
- }
-
- private async Task PushWxTemperature(string messageId, HisGpsTemperature temperature, LocationType type = default)
- {
- var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(messageId, temperature.Serialno);
- if (device == null) return;
-
- string deviceName = temperature.Serialno;
- var person = await _personCacheMgr.GetPersonBySerialNoAsync(messageId, temperature.Serialno).ConfigureAwait(false);
- if (person != null) deviceName = person.NickName;
- MethodType method = MethodType.Manual;
-
-
- if (temperature.Method == 1) method = MethodType.Period;
- await _serviceMqProcess.ProcessWxTemperatureAsync(messageId, new TemperatureInfoModel
- {
- DeviceId = device.DeviceId,
- DeviceName = deviceName,
- TempId = temperature.TempId,
- TempTime = temperature.LastUpdate,
- Imei = temperature.Serialno,
- Temperature = temperature.Temperature,
- Province = temperature.Province,
- City = temperature.City,
- District = temperature.District,
- Address = temperature.Address
- }, type, method);
- }
-
- private async Task<TemperatureStatusType> GetTemperatureStatusAsync(string tempId)
- {
- var key = $"{KEY_CACHE_TEMPERATURE_STATUS}_{tempId}";
- var result = await RedisHelper.GetAsync<TemperatureStatusType?>(key);
- if (result == null) return TemperatureStatusType.NotExisted;
- if (result > TemperatureStatusType.Reserved || result < TemperatureStatusType.NotExisted) return TemperatureStatusType.NotExisted;
-
- return result.Value;
- }
-
- private void SetTemperatureStatus(string sosId, TemperatureStatusType status)
- {
- var key = $"{KEY_CACHE_TEMPERATURE_STATUS}_{sosId}";
- RedisHelper.SetAsync(key, (int)status, KEY_CACHE_SECONDS);
- }
-
- public async Task<HisGpsTemperature> GetTemperatureAsync(string messageId, string temperatureId)
- {
- try
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var temperature = await _hisTemperatureApiClient.GetByIdAsync(temperatureId, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return temperature;
- }
- catch (Exception ex)
- {
- _logger.LogError($"根据{temperatureId}获取指定测温信息失败, {ex.Message}, {ex.StackTrace}");
- return null;
- }
- }
-
- public async Task<bool> AddTemperatureAsync(string messageId, HisGpsTemperature temperature)
- {
- try
- {
-
- var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(messageId, temperature.Serialno).ConfigureAwait(false);
- var persnId = string.Empty;
- if (person != null && person.Person != null) persnId = person.Person.PersonId;
- temperature.PersonId = persnId;
-
- await _hisTemperatureApiClient.AddAsync(temperature, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return true;
- }
- catch (Exception ex)
- {
- _logger.LogError($"新增体温信息发生异常:{ex.Message}, {ex.StackTrace}");
- }
-
- return false;
- }
-
- public async Task<bool> UpdateTemperatureAsync(string messageId, HisGpsTemperature temperature)
- {
- try
- {
- await _hisTemperatureApiClient.UpdateAsync(temperature, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return true;
- }
- catch (Exception ex)
- {
- _logger.LogError($"更新体温信息发生异常:{ex.Message}, {ex.StackTrace}");
- }
-
- return false;
- }
-
- #endregion
-
- }
- }
|