|
@@ -4,12 +4,14 @@ import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
|
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
|
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
|
|
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
|
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
|
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceVO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.YfDeviceDO;
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.YfDeviceDO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.TDDeviceMapper;
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.TDDeviceMapper;
|
|
|
import cn.iocoder.yudao.module.pms.service.yanfan.YfDeviceService;
|
|
import cn.iocoder.yudao.module.pms.service.yanfan.YfDeviceService;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -19,9 +21,9 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import java.sql.Timestamp;
|
|
import java.sql.Timestamp;
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.*;
|
|
|
|
|
-import java.util.function.Function;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -38,166 +40,76 @@ public class TdCronJob implements JobHandler {
|
|
|
@Override
|
|
@Override
|
|
|
@TenantIgnore
|
|
@TenantIgnore
|
|
|
public String execute(String param) throws Exception {
|
|
public String execute(String param) throws Exception {
|
|
|
|
|
+ List<String> codes = new ArrayList<>();
|
|
|
List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
|
|
List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
|
|
|
-
|
|
|
|
|
- // ① serialNumber(小写) -> YfDeviceDO,替代循环内 stream filter
|
|
|
|
|
- Map<String, YfDeviceDO> yfDeviceMap = allDevice.stream()
|
|
|
|
|
- .collect(Collectors.toMap(
|
|
|
|
|
- d -> d.getSerialNumber().toLowerCase(),
|
|
|
|
|
- Function.identity(),
|
|
|
|
|
- (a, b) -> a // 重复时保留前者
|
|
|
|
|
- ));
|
|
|
|
|
-
|
|
|
|
|
- List<String> codes = new ArrayList<>(yfDeviceMap.keySet());
|
|
|
|
|
|
|
+ allDevice.forEach(d -> {
|
|
|
|
|
+ codes.add(d.getSerialNumber());
|
|
|
|
|
+ });
|
|
|
List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
|
|
List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
|
|
|
-
|
|
|
|
|
- // ② 预取所有存在的 device 表名(一次查询,放入 Set)
|
|
|
|
|
- Set<String> existTables = deviceMapper.selectAllDeviceTables().stream()
|
|
|
|
|
- .map(String::toLowerCase)
|
|
|
|
|
- .collect(Collectors.toSet());
|
|
|
|
|
-
|
|
|
|
|
- // ③ 筛选出表存在的设备,组装子表名列表
|
|
|
|
|
- List<String> targetTableNames = allDevice.stream()
|
|
|
|
|
- .map(d -> "device_" + d.getSerialNumber().toLowerCase())
|
|
|
|
|
- .filter(existTables::contains)
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
-
|
|
|
|
|
- // ④ 超级表一次查询全部设备的最新 ts → Map<serialNumber小写, Timestamp>
|
|
|
|
|
- Map<String, Timestamp> lastTimeMap = new HashMap<>();
|
|
|
|
|
- if (CollUtil.isNotEmpty(targetTableNames)) {
|
|
|
|
|
- List<DeviceLastTimeVO> lastTimeList = deviceMapper.selectLastTimeBatch(targetTableNames);
|
|
|
|
|
- for (DeviceLastTimeVO vo : lastTimeList) {
|
|
|
|
|
- // tbname 格式为 device_xxx,去掉前缀得到 serialNumber
|
|
|
|
|
- String serialNumber = vo.getTbname().toLowerCase().replace("device_", "");
|
|
|
|
|
- lastTimeMap.put(serialNumber, vo.getTs());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
List<IotDeviceDO> deviceDOS = new ArrayList<>();
|
|
List<IotDeviceDO> deviceDOS = new ArrayList<>();
|
|
|
-
|
|
|
|
|
- // ⑤ 循环内纯内存操作,零数据库查询
|
|
|
|
|
for (IotDeviceDO device : devices) {
|
|
for (IotDeviceDO device : devices) {
|
|
|
String deviceCode = device.getDeviceCode();
|
|
String deviceCode = device.getDeviceCode();
|
|
|
- YfDeviceDO e = yfDeviceMap.get(deviceCode.toLowerCase());
|
|
|
|
|
- if (e == null) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- device.setYfDeviceId(e.getDeviceId());
|
|
|
|
|
-
|
|
|
|
|
- // 从 Map 取最新时间,替代循环内 selectLastTime
|
|
|
|
|
- Timestamp ts = lastTimeMap.get(e.getSerialNumber().toLowerCase());
|
|
|
|
|
- if (ts != null) {
|
|
|
|
|
- device.setLastInlineTime(sdf.format(ts));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (e.getStatus() == 1) { // 未激活状态
|
|
|
|
|
- if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
- device.setIfInline(DateUtils.checkIfFullDayDifference(device.getLastInlineTime()) ? 4 : 3);
|
|
|
|
|
- } else {
|
|
|
|
|
- device.setIfInline(4);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Redis 一次 hgetAll 替代三次 hget
|
|
|
|
|
- Map<Object, Object> tslv = redisTemplate.opsForHash().entries("TSLV:" + device.getDeviceCode());
|
|
|
|
|
- Object lat = tslv.get("lat");
|
|
|
|
|
- Object lng = tslv.get("lng");
|
|
|
|
|
- Object online = tslv.get("online");
|
|
|
|
|
-
|
|
|
|
|
- if (Objects.nonNull(lat)) {
|
|
|
|
|
- device.setLat(Double.valueOf((String) JSON.parseObject(lat.toString()).get("value")));
|
|
|
|
|
|
|
+ allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
|
|
|
|
|
+ device.setYfDeviceId(e.getDeviceId());
|
|
|
|
|
+ Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
|
|
|
|
|
+ if (i==1) {
|
|
|
|
|
+ List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
|
|
|
|
|
+ if (CollUtil.isNotEmpty(deviceVOS)) {
|
|
|
|
|
+ Timestamp ts = deviceVOS.get(0).getTs();
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ String format = sdf.format(ts);
|
|
|
|
|
+ device.setLastInlineTime(format);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- if (Objects.nonNull(lng)) {
|
|
|
|
|
- device.setLng(Double.valueOf((String) JSON.parseObject(lng.toString()).get("value")));
|
|
|
|
|
- }
|
|
|
|
|
- if (Objects.nonNull(online)) {
|
|
|
|
|
- String value = String.valueOf(JSON.parseObject(online.toString()).get("value"));
|
|
|
|
|
- device.setIfInline("true".equals(value) ? 3 : 4);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
- device.setIfInline(DateUtils.checkIfFullDayDifference(device.getLastInlineTime()) ? 4 : e.getStatus());
|
|
|
|
|
|
|
+ if (e.getStatus()==1) {//未激活状态
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
|
|
|
|
|
+ device.setIfInline(4);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ device.setIfInline(3);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ throw new RuntimeException(ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ device.setIfInline(4);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object lat = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lat");
|
|
|
|
|
+ if (Objects.nonNull(lat)) {
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(lat.toString());
|
|
|
|
|
+ device.setLat(Double.valueOf((String) jsonObject.get("value")));
|
|
|
|
|
+ }
|
|
|
|
|
+ Object lng = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lng");
|
|
|
|
|
+ if (Objects.nonNull(lng)) {
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(lng.toString());
|
|
|
|
|
+ device.setLng(Double.valueOf((String) jsonObject.get("value")));
|
|
|
|
|
+ }
|
|
|
|
|
+ Object online = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "online");
|
|
|
|
|
+ if (Objects.nonNull(online)) {
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(online.toString());
|
|
|
|
|
+ String value = String.valueOf(jsonObject.get("value"));
|
|
|
|
|
+ device.setIfInline("true".equals(value)?3:4);
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
- device.setIfInline(4);
|
|
|
|
|
|
|
+ if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
+ if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
|
|
|
|
|
+ device.setIfInline(4);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ device.setIfInline(e.getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ device.setIfInline(4);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- deviceDOS.add(device);
|
|
|
|
|
|
|
+// iotDeviceMapper.updateTdCron(device);
|
|
|
|
|
+ deviceDOS.add(device);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
iotDeviceMapper.updateBatch(deviceDOS);
|
|
iotDeviceMapper.updateBatch(deviceDOS);
|
|
|
return "";
|
|
return "";
|
|
|
-// List<String> codes = new ArrayList<>();
|
|
|
|
|
-// List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
|
|
|
|
|
-// allDevice.forEach(d -> {
|
|
|
|
|
-// codes.add(d.getSerialNumber());
|
|
|
|
|
-// });
|
|
|
|
|
-// List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
|
|
|
|
|
-// List<IotDeviceDO> deviceDOS = new ArrayList<>();
|
|
|
|
|
-// for (IotDeviceDO device : devices) {
|
|
|
|
|
-// String deviceCode = device.getDeviceCode();
|
|
|
|
|
-// allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
|
|
|
|
|
-// device.setYfDeviceId(e.getDeviceId());
|
|
|
|
|
-// Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
|
|
|
|
|
-// if (i==1) {
|
|
|
|
|
-// List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
|
|
|
|
|
-// if (CollUtil.isNotEmpty(deviceVOS)) {
|
|
|
|
|
-// Timestamp ts = deviceVOS.get(0).getTs();
|
|
|
|
|
-// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
-// String format = sdf.format(ts);
|
|
|
|
|
-// device.setLastInlineTime(format);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// if (e.getStatus()==1) {//未激活状态
|
|
|
|
|
-//
|
|
|
|
|
-// if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
|
|
|
|
|
-// device.setIfInline(4);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// device.setIfInline(3);
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (Exception ex) {
|
|
|
|
|
-// throw new RuntimeException(ex.getMessage());
|
|
|
|
|
-// }
|
|
|
|
|
-// } else {
|
|
|
|
|
-// device.setIfInline(4);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// Object lat = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lat");
|
|
|
|
|
-// if (Objects.nonNull(lat)) {
|
|
|
|
|
-// JSONObject jsonObject = JSON.parseObject(lat.toString());
|
|
|
|
|
-// device.setLat(Double.valueOf((String) jsonObject.get("value")));
|
|
|
|
|
-// }
|
|
|
|
|
-// Object lng = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lng");
|
|
|
|
|
-// if (Objects.nonNull(lng)) {
|
|
|
|
|
-// JSONObject jsonObject = JSON.parseObject(lng.toString());
|
|
|
|
|
-// device.setLng(Double.valueOf((String) jsonObject.get("value")));
|
|
|
|
|
-// }
|
|
|
|
|
-// Object online = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "online");
|
|
|
|
|
-// if (Objects.nonNull(online)) {
|
|
|
|
|
-// JSONObject jsonObject = JSON.parseObject(online.toString());
|
|
|
|
|
-// String value = String.valueOf(jsonObject.get("value"));
|
|
|
|
|
-// device.setIfInline("true".equals(value)?3:4);
|
|
|
|
|
-// }
|
|
|
|
|
-// } else {
|
|
|
|
|
-// if (StringUtils.isNotBlank(device.getLastInlineTime())) {
|
|
|
|
|
-// if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
|
|
|
|
|
-// device.setIfInline(4);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// device.setIfInline(e.getStatus());
|
|
|
|
|
-// }
|
|
|
|
|
-// } else {
|
|
|
|
|
-// device.setIfInline(4);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//// iotDeviceMapper.updateTdCron(device);
|
|
|
|
|
-// deviceDOS.add(device);
|
|
|
|
|
-// });
|
|
|
|
|
-// }
|
|
|
|
|
-// iotDeviceMapper.updateBatch(deviceDOS);
|
|
|
|
|
-// return "";
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|