1. 心跳的逻辑移到redis中

This commit is contained in:
邱贞招 2024-08-29 15:18:01 +08:00
parent d2bc7617a6
commit 05af4cecba

View File

@ -16,6 +16,7 @@ import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.map.GeoUtils;
import com.ruoyi.common.utils.map.GpsCoordinateUtils;
import com.ruoyi.common.utils.onenet.LogEntry;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
@ -33,10 +34,7 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
@ -502,55 +500,86 @@ public class EtTask {
}
/**
* 更新设备的定位和电压 5秒一次
* cron: 0 5 0 * * ?
* 更新设备的定位和电压 10秒一次
* cron: 0 10 0 * * ?
*/
public void updateLocation(){
log.info("-------------------【定时任务】更新设备的定位和电压-------------------");
List<Object> cacheList = redisCache.getCacheList(CacheConstants.CACHE_DEVICE_KEY + "*");
for(Object object:cacheList){
String msg =(String)object;
public void updateLocation10(){
log.info("-------------------【定时任务10秒一次】更新设备的定位和电压-----开始--------------");
Collection<String> keys = redisCache.keys(CacheConstants.CACHE_DEVICE_KEY + "*");
log.info("redis缓存中的数据" + JSON.toJSONString(keys));
for(String key:keys){
String msg = redisCache.getCacheObject(key);
log.info("redis缓存中的数据" + msg);
LogEntry logEntry = JSONObject.parseObject(msg, LogEntry.class);
log.info("logEntry转换后的对象: logEntry---【{}】" , JSON.toJSONString(logEntry));
LogEntry.LocationValue value = logEntry.getValue();
AsDevice device = asDeviceMapper.selectAsDeviceByMac(logEntry.getDevName());
// 坐标转换 WGS84 GCJ02
double[] doubles = coordinateConvert(value);
BigDecimal lat = new BigDecimal(doubles[0]).setScale(8, RoundingMode.HALF_UP);
BigDecimal lon = new BigDecimal(doubles[1]).setScale(8, RoundingMode.HALF_UP);
if(ServiceConstants.LOCK_STATUS_OPEN.equals(device.getLockStatus())){
updateLocationHandle(msg, logEntry, value, device);
}
}
log.info("-------------------【定时任务10秒一次】更新设备的定位和电压----结束---------------");
}
asynchronousSaveLog(msg,logEntry.getAt(),logEntry.getDevName(), lat, lon,device);
/**
* 更新设备的定位和电压 5分钟一次
* cron: 0 20 0 * * ?
*/
public void updateLocation300(){
log.info("-------------------【定时任务5分钟一次】更新设备的定位和电压-----开始--------------");
Collection<String> keys = redisCache.keys(CacheConstants.CACHE_DEVICE_KEY + "*");
log.info("redis缓存中的数据" + JSON.toJSONString(keys));
for(String key:keys){
String msg = redisCache.getCacheObject(key);
log.info("redis缓存中的数据" + msg);
LogEntry logEntry = JSONObject.parseObject(msg, LogEntry.class);
log.info("logEntry转换后的对象: logEntry---【{}】" , JSON.toJSONString(logEntry));
LogEntry.LocationValue value = logEntry.getValue();
AsDevice device = asDeviceMapper.selectAsDeviceByMac(logEntry.getDevName());
if(ServiceConstants.LOCK_STATUS_CLOSE.equals(device.getLockStatus())){
updateLocationHandle(msg, logEntry, value, device);
}
}
log.info("-------------------【定时任务5分钟一次】更新设备的定位和电压----结束---------------");
}
BigDecimal divide = new BigDecimal(value.getBat()).divide(new BigDecimal(10));
device.setVoltage(divide.toString());//电压
EtModel model = etModelService.selectEtModelByModelId(device.getModelId());
device.setLastTime(DateUtils.getNowDate());
device.setSignalStrength(value.getCsq());
device.setQuality(value.getQ());
if(ObjectUtil.isNotNull(model)){
Integer remainingMileage = 0;
if(StrUtil.isNotBlank(device.getVoltage())){
remainingMileage = CommonUtil.getRemainingMileage(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance());
}
Integer electricQuantity = CommonUtil.getElectricQuantity(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百
device.setRemainingMileage(remainingMileage);
device.setRemainingPower(electricQuantity.toString());
}
if(BigDecimal.ZERO.compareTo(lon) != 0 && BigDecimal.ZERO.compareTo(lat) != 0){
device.setLatitude(lat.toString());
device.setLongitude(lon.toString());
device.setLastLocationTime(DateUtils.getNowDate());
device.setGps("1");
// 信号强度
device.setSatellites(value.getS());
}else{
device.setGps("0");
device.setSatellites(0);
}
int i = asDeviceService.updateLocation(device);
if(i>0){
log.info("到定位===============保存电压等数值成功===========>" + logEntry.getDevName());
private void updateLocationHandle(String msg, LogEntry logEntry, LogEntry.LocationValue value, AsDevice device) {
// 坐标转换 WGS84 GCJ02
double[] doubles = coordinateConvert(value);
BigDecimal lat = new BigDecimal(doubles[0]).setScale(8, RoundingMode.HALF_UP);
BigDecimal lon = new BigDecimal(doubles[1]).setScale(8, RoundingMode.HALF_UP);
asynchronousSaveLog(msg, logEntry.getAt(), logEntry.getDevName(), lat, lon, device);
BigDecimal divide = new BigDecimal(value.getBat()).divide(new BigDecimal(10));
device.setVoltage(divide.toString());//电压
EtModel model = etModelService.selectEtModelByModelId(device.getModelId());
device.setLastTime(DateUtils.getNowDate());
device.setSignalStrength(value.getCsq());
device.setQuality(value.getQ());
if(ObjectUtil.isNotNull(model)){
Integer remainingMileage = 0;
if(StrUtil.isNotBlank(device.getVoltage())){
remainingMileage = CommonUtil.getRemainingMileage(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance());
}
Integer electricQuantity = CommonUtil.getElectricQuantity(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百
device.setRemainingMileage(remainingMileage);
device.setRemainingPower(electricQuantity.toString());
}
if(BigDecimal.ZERO.compareTo(lon) != 0 && BigDecimal.ZERO.compareTo(lat) != 0){
device.setLatitude(lat.toString());
device.setLongitude(lon.toString());
device.setLastLocationTime(DateUtils.getNowDate());
device.setGps("1");
// 信号强度
device.setSatellites(value.getS());
}else{
device.setGps("0");
device.setSatellites(0);
}
int i = asDeviceService.updateLocation(device);
if(i>0){
log.info("===============更新设备信息成功===========>" + logEntry.getDevName());
}
}