|
@@ -1,6 +1,5 @@
|
|
|
package cn.iocoder.yudao.module.pms.job;
|
|
package cn.iocoder.yudao.module.pms.job;
|
|
|
|
|
|
|
|
-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;
|
|
@@ -10,6 +9,9 @@ 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 com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
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;
|
|
@@ -17,11 +19,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
-import java.sql.Timestamp;
|
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
-import java.util.function.Function;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -32,114 +30,68 @@ public class TdCronJob implements JobHandler {
|
|
|
private IotDeviceMapper iotDeviceMapper;
|
|
private IotDeviceMapper iotDeviceMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private TDDeviceMapper deviceMapper;
|
|
private TDDeviceMapper deviceMapper;
|
|
|
- @Autowired
|
|
|
|
|
- private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@TenantIgnore
|
|
@TenantIgnore
|
|
|
public String execute(String param) throws Exception {
|
|
public String execute(String param) throws Exception {
|
|
|
|
|
+ List<String> codes = new ArrayList<>();
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
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<>();
|
|
|
-
|
|
|
|
|
- // ⑤ 循环内纯内存操作,零数据库查询
|
|
|
|
|
|
|
+ List<String> allTableNames = deviceMapper.getAllTableNames();
|
|
|
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;
|
|
|
|
|
|
|
+ if ("YF6660388".equals(deviceCode)) {
|
|
|
|
|
+ System.out.println("************************");
|
|
|
}
|
|
}
|
|
|
|
|
+ allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
|
|
|
|
|
+ device.setYfDeviceId(e.getDeviceId());
|
|
|
|
|
+// Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
|
|
|
|
|
+ if (allTableNames.contains("device_"+e.getSerialNumber().toLowerCase())) {//如果存在表
|
|
|
|
|
+ Map<Object, Object> hashAll = stringRedisTemplate.opsForHash().entries("TSLV:"+e.getProductId()+"_"+e.getSerialNumber());
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
|
|
+ String timestamp = "";
|
|
|
|
|
+ for (Map.Entry<Object, Object> entry : hashAll.entrySet()) {
|
|
|
|
|
+ String paramId = entry.getKey().toString(); // "1003"
|
|
|
|
|
+ String jsonStr = entry.getValue().toString(); // "{\"id\":\"1003\",\"ts\":\"...\"}"
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ JsonNode node = objectMapper.readTree(jsonStr);
|
|
|
|
|
+ if (node.isTextual()) {
|
|
|
|
|
+ node = objectMapper.readTree(node.asText()); // 处理双重转义
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (node.has("ts")) {
|
|
|
|
|
+ String ts = node.get("ts").asText();
|
|
|
|
|
+ timestamp = ts;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception f) {
|
|
|
|
|
+ f.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.isNotBlank(timestamp)) {
|
|
|
|
|
+ device.setLastInlineTime(timestamp);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- 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")));
|
|
|
|
|
- }
|
|
|
|
|
- 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());
|
|
|
|
|
- } else {
|
|
|
|
|
- device.setIfInline(4);
|
|
|
|
|
|
|
+// 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);
|
|
|
|
|
+// }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- deviceDOS.add(device);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- iotDeviceMapper.updateBatch(deviceDOS);
|
|
|
|
|
- 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) {
|
|
// if (i==1) {
|
|
|
// List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
|
|
// List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
|
|
|
// if (CollUtil.isNotEmpty(deviceVOS)) {
|
|
// if (CollUtil.isNotEmpty(deviceVOS)) {
|
|
@@ -149,60 +101,61 @@ public class TdCronJob implements JobHandler {
|
|
|
// device.setLastInlineTime(format);
|
|
// 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 "";
|
|
|
|
|
|
|
+ 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 = stringRedisTemplate.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 = stringRedisTemplate.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 = stringRedisTemplate.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 "";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
- String abc = "2026-01-21 10:25:40";
|
|
|
|
|
|
|
+ System.out.println(new Date().toString());
|
|
|
|
|
+ String abc = "2026-09-03 18:48:38.42";
|
|
|
System.out.println(DateUtils.checkIfFullDayDifference(abc));
|
|
System.out.println(DateUtils.checkIfFullDayDifference(abc));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|