diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java index b374e7a..87bc4b3 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java @@ -405,7 +405,7 @@ public interface IAsDeviceService extends IService /** * 更新最新的位置信息 */ - boolean updateLatestLocation(String sn); + boolean updateLatestLocation(AsDevice device); /** * 获取最新的位置信息 diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java index 9b07e5e..06eed9c 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java @@ -9,7 +9,6 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.annotation.DataScope; import com.ruoyi.common.constant.*; -import com.ruoyi.common.core.domain.entity.AsUser; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.exception.ServiceException; @@ -43,10 +42,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -158,13 +154,20 @@ public class AsDeviceServiceImpl extends ServiceImpl i device.setAreaName(etOperatingArea.getAreaName()); device.setDeptName(etOperatingArea.getDeptName()); } - EtModel model = etModelService.selectEtModelByModelId(device.getModelId()); - if(ObjectUtil.isNotNull(model)){ - device.setModel(model.getModel()); - if(StrUtil.isNotBlank(device.getVoltage())){ - device.setRemainingMileage(CommonUtil.getRemainingMileage(device.getVoltage(),model.getFullVoltage(),model.getLowVoltage(),model.getFullEndurance())); - } + pullDeviceInfo(device); + if(ObjectUtil.isNotNull(areaId) && areaId!=0){ + SysDept sysDept = wxPayService.getDeptObjByAreaId(areaId); + //https://dianche.chuantewulian.cn?sn=https://dche.ccttiot.com?sn=3000900 + device.setQrText(sysDept.getDomain()+"?sn="+device.getSn()); + device.setDeptName(sysDept.getDeptName()); + device.setDeptId(sysDept.getDeptId()); + }else{ + device.setQrText(""); } + return device; + } + + private void pullDeviceInfo(AsDevice device) { String msg = redisCache.getCacheObject(CacheConstants.CACHE_DEVICE_KEY + device.getMac()); if(StrUtil.isNotBlank(msg)){ LogEntry logEntry = JSONObject.parseObject(msg, LogEntry.class); @@ -177,22 +180,13 @@ public class AsDeviceServiceImpl extends ServiceImpl i BigDecimal lat = BigDecimal.valueOf(doubles[0]).setScale(8, RoundingMode.HALF_UP); // long 转String device.setLongitude(lon.toString()); - updateDevice2(logEntry.getAt(),value,device,lon,lat); + device.setLatitude(lat.toString()); + updateDevice2(logEntry.getAt(),value, device,lon,lat); }else{ device.setLongitude(null); device.setLatitude(null); } } - if(ObjectUtil.isNotNull(areaId) && areaId!=0){ - SysDept sysDept = wxPayService.getDeptObjByAreaId(areaId); - //https://dianche.chuantewulian.cn?sn=https://dche.ccttiot.com?sn=3000900 - device.setQrText(sysDept.getDomain()+"?sn="+device.getSn()); - device.setDeptName(sysDept.getDeptName()); - device.setDeptId(sysDept.getDeptId()); - }else{ - device.setQrText(""); - } - return device; } /** @@ -216,6 +210,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i asDeviceVO.setIsBand(true); } } + pullDeviceInfo(asDevice); return asDeviceVO; } @@ -238,33 +233,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i etOperatingArea = etOperatingAreaService.selectEtOperatingAreaByAreaId(areaId); asDevice.setAreaName(etOperatingArea.getAreaName()); } - EtModel model = etModelService.selectEtModelByModelId(asDevice.getModelId()); - if(ObjectUtil.isNotNull(model)){ - asDevice.setModel(model.getModel()); - if(StrUtil.isNotBlank(asDevice.getVoltage())){ - asDevice.setRemainingMileage(CommonUtil.getRemainingMileage(asDevice.getVoltage(),model.getFullVoltage(),model.getLowVoltage(),model.getFullEndurance())); - } - } - String msg = redisCache.getCacheObject(CacheConstants.CACHE_DEVICE_KEY + asDevice.getMac()); - if(StrUtil.isNotBlank(msg)){ - log.info("【根据sn号查询车辆实时信息】-========redis缓存中的数据:" + msg); - LogEntry logEntry = JSONObject.parseObject(msg, LogEntry.class); - log.info("【根据sn号查询车辆实时信息】============logEntry转换后的对象: logEntry---【{}】" , JSON.toJSONString(logEntry)); - LogEntry.LocationValue value = logEntry.getValue(); - if(!"0.0".equals(value.getLon()) && !"0.0".equals(value.getLat())){ - // 坐标转换 WGS84 转 GCJ02 - double[] doubles = CommonUtil.coordinateConvert(value.getLon(),value.getLat()); - BigDecimal lon = BigDecimal.valueOf(doubles[1]).setScale(8, RoundingMode.HALF_UP); - BigDecimal lat = BigDecimal.valueOf(doubles[0]).setScale(8, RoundingMode.HALF_UP); - // todo 0E-8 排除 - asDevice.setLongitude(lon.toString()); - asDevice.setLatitude(lat.toString()); - }else{ - asDevice.setLongitude(null); - asDevice.setLatitude(null); - } - asDevice.setLastLocationTime(new Date(logEntry.getAt())); - } + pullDeviceInfo(asDevice); //正在进行中的订单 EtOrder order = new EtOrder(); order.setSn(asDevice.getSn()); @@ -354,6 +323,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i }else{ asDevice1.setQrText(""); } + pullDeviceInfo(asDevice1); } return asDevices; } @@ -648,7 +618,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i log.info("设备【"+device.getSn()+"】更新版本失败"); } // 最新位置信息 - if(updateLatestLocation(device.getSn())){ + if(updateLatestLocation(device)){ log.info("设备【"+device.getSn()+"】更新位置成功"); }else{ log.info("设备【"+device.getSn()+"】更新位置失败"); @@ -678,7 +648,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i log.info("设备【"+device.getSn()+"】更新版本失败"); } // 最新位置信息 - if(updateLatestLocation(device.getSn())){ + if(updateLatestLocation(device)){ log.info("设备【"+device.getSn()+"】更新位置成功"); }else{ log.info("设备【"+device.getSn()+"】更新位置失败"); @@ -774,23 +744,8 @@ public class AsDeviceServiceImpl extends ServiceImpl i } } List asDevices = asDeviceMapper.selectList(wrapper); - for (AsDevice asDevice:asDevices){ - Long modelId = asDevice.getModelId(); - if (ObjectUtil.isNotNull(modelId)){ - EtModel model = etModelService.selectEtModelByModelId(modelId); - if(ObjectUtil.isNotNull(model)){ - asDevice.setModel(model.getModel()); - Integer remainingMileage = 0; - if(StrUtil.isNotBlank(asDevice.getVoltage())){ - remainingMileage = CommonUtil.getRemainingMileage(asDevice.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance()); - } - Integer electricQuantity = CommonUtil.getElectricQuantity(asDevice.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百 - asDevice.setRemainingMileage(remainingMileage); - asDevice.setRemainingPower(electricQuantity.toString()); - } - } - // 根据sn获取redis中的最新定位 - getLatestLocation(asDevice); + if (ObjectUtil.isNotEmpty(asDevices)) { + asDevices.forEach(this::pullDeviceInfo); } return asDevices; } @@ -1136,22 +1091,30 @@ public class AsDeviceServiceImpl extends ServiceImpl i @Override public List deviceSearch(String key, String type, String areaId) { QueryWrapper queryWrapper = new QueryWrapper<>(); - if(StrUtil.isNotBlank(areaId)){ - queryWrapper.eq("area_id",areaId); + if (StrUtil.isNotBlank(areaId)) { + queryWrapper.eq("area_id", areaId); } - switch (type){ - case "1": + + if (StrUtil.isNotBlank(type)) { + if ("1".equals(type)) { queryWrapper.like("sn", key); - return asDeviceMapper.selectList(queryWrapper); - case "2": + } else if ("2".equals(type)) { queryWrapper.like("mac", key); - return asDeviceMapper.selectList(queryWrapper); - case "3": + } else if ("3".equals(type)) { queryWrapper.like("vehicle_num", key); - return asDeviceMapper.selectList(queryWrapper); - default: - return null; + } else { + return Collections.emptyList(); + } + } else { + return Collections.emptyList(); } + + List devices = asDeviceMapper.selectList(queryWrapper); + if (ObjectUtil.isNotEmpty(devices)) { + devices.forEach(this::pullDeviceInfo); + } + + return devices; } /** @@ -2570,6 +2533,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i @Override public boolean isLowBattery(String sn) { AsDevice device = asDeviceMapper.selectAsDeviceBySn(sn); + updateLatestLocation(device); String remainingPower = device.getRemainingPower(); log.info("车辆【{}】电量为【{}】",sn,remainingPower); EtOperatingArea area = etOperatingAreaService.selectEtOperatingAreaByAreaId(device.getAreaId()); @@ -2604,10 +2568,8 @@ public class AsDeviceServiceImpl extends ServiceImpl i wrapper.eq("status","1"); List asDevices = asDeviceMapper.selectList(wrapper); // for循环 - for (AsDevice asDevice : asDevices) { - EtModel model = etModelService.selectEtModelByModelId(asDevice.getModelId()); - Integer remainingMileage = CommonUtil.getRemainingMileage(asDevice.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance()); - asDevice.setRemainingMileage(remainingMileage); + if (ObjectUtil.isNotEmpty(asDevices)) { + asDevices.forEach(this::pullDeviceInfo); } return asDevices; } @@ -2909,11 +2871,10 @@ public class AsDeviceServiceImpl extends ServiceImpl i */ @Override @SneakyThrows - public boolean updateLatestLocation(String sn) { - AsDevice device = asDeviceMapper.selectAsDeviceBySn(sn); + public boolean updateLatestLocation(AsDevice device) { if(device!=null){ String token = Token.getToken(); - DataPointRes datapoints = historyDatapoints(asDeviceMapper.selectAsDeviceBySn(sn).getMac(), token); + DataPointRes datapoints = historyDatapoints(device.getMac(), token); if(datapoints.getCode() == 0){ Data data = datapoints.getData(); List datastreams = data.getDevices(); @@ -3048,6 +3009,7 @@ public class AsDeviceServiceImpl extends ServiceImpl i Integer electricQuantity = CommonUtil.getElectricQuantity(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百 device.setRemainingMileage(remainingMileage); device.setRemainingPower(electricQuantity.toString()); + device.setModel(model.getModel()); } device.setLastTime(new Date(at)); device.setLastLocationTime(new Date(at)); @@ -3089,28 +3051,4 @@ public class AsDeviceServiceImpl extends ServiceImpl i } } - - - /** - * 判断是否靠近边界 - */ -// @Override -// public boolean isCloseToTheBoundary(String sn, EtOperatingArea area) { -// Boolean isClose = false; -// AsDevice device = asDeviceMapper.selectAsDeviceBySn(sn); -// String latitude = device.getLatitude(); -// String longitude = device.getLongitude(); -// Geometry geometry = GeoUtils.fromWkt(area.getBoundary()); -// -// double distanceToBoundary = GeoUtils.distanceToBoundary(longitude, latitude, geometry); -// if(distanceToBoundary <= 3.0) { -// log.info("车辆【{}】靠近【{}】运营边界",sn,area.getAreaName()); -// isClose = true; -// }else{ -// log.info("车辆【{}】为靠近靠近【{}】运营边界",sn,area.getAreaName()); -// } -// return isClose; -// } - - }