Files
MiddlePlatform/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java

96 lines
3.8 KiB
Java
Raw Normal View History

2026-04-17 17:12:41 +08:00
package com.maibu.scheduled;
2026-04-17 10:42:16 +08:00
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
2026-11-16 13:38:56 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.maibu.constant.NettyCacheKey;
import com.maibu.core.business.DevicePlan;
import com.maibu.core.business.DeviceStatusRecordDTO;
import com.maibu.core.business.dto.VehicleMessageRecordDTO;
import com.maibu.core.redis.RedisCache;
import com.maibu.mapper.VehicleMessageMapper;
import com.maibu.memory.SiteMemory;
2026-04-17 10:42:16 +08:00
@Component
public class ScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private RedisCache redisCache;
@Autowired
2026-11-16 13:38:56 +08:00
private VehicleMessageMapper vehicleMessageMapper;
2026-04-17 10:42:16 +08:00
2026-11-16 13:38:56 +08:00
// 整点更新设备状态
2026-04-17 10:42:16 +08:00
@Scheduled(cron = "0 0 * * * ?")
2026-11-16 13:38:56 +08:00
// @Scheduled(cron = "0 * * * * ?")
2026-04-17 10:42:16 +08:00
public void executeHourlyTask() {
System.out.println("整点定时任务执行时间: " + dateFormat.format(new Date()));
2026-11-16 13:38:56 +08:00
String key = NettyCacheKey.vehicleMessageKey;
// Collection<String> keys = redisCache.keys(key);
2026-04-17 10:42:16 +08:00
Collection<String> keys = redisCache.getListKeyByPrefix(key);
if (!CollectionUtils.isEmpty(keys)) {
keys.forEach(k -> {
2026-11-16 13:38:56 +08:00
VehicleMessageRecordDTO recordDTO = redisCache.getCacheObject(k);
2026-04-17 10:42:16 +08:00
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
2026-11-16 13:38:56 +08:00
// recordDTO.getHistories().forEach(x -> {
// deviceStatusHistoryMapper.insert(x);
// });
vehicleMessageMapper.insertBatch(recordDTO.getHistories());
2026-04-17 10:42:16 +08:00
}
redisCache.deleteObject(k);
});
}
System.out.println("任务执行完成");
}
2026-11-16 13:38:56 +08:00
// 0点刷新任务创建状态
2026-04-17 10:42:16 +08:00
@Scheduled(cron = "0 0 0 * * ?")
public void refreshDevicePlanTask() {
System.out.println("0点定时刷新状态");
2026-04-30 15:25:58 +08:00
Map<String, List<DevicePlan>> map = SiteMemory.devicePlanMap;
2026-04-17 10:42:16 +08:00
if (!CollectionUtils.isEmpty(map.keySet())) {
map.values().forEach(x -> {
if (!CollectionUtils.isEmpty(x)) {
x.forEach(y -> {
y.setDayTriggerFlag(false);
});
}
});
}
System.out.println("0点定时刷新状态执行完成");
}
2026-07-29 13:08:49 +08:00
2026-10-23 09:12:53 +08:00
// public static void main(String[] args) throws DecoderException {
2026-11-16 13:38:56 +08:00
// String hex = "AB AA 05 88 4D 43 37 30 30 50 4C 55 53 2D 43 4E 2D 4A 53 2D 31
// 37 38 34 38 37 31 39 37 39 31 39 30 2D 30 30 30 30 30 30 30 41 2D 36 30 00 B1
// FB 7F FE FE FE 00 00 00 00 00 00 00 00 00 00 00 00 00 12 01 48 95 E8 03 E8 03
// 28 32 91 53 24 4D 43 37 30 30 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 31
// 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 CD CC
// A4 41 00 00 80 3F 33 33 33 3F B8 1E 05 3F 33 33 33 3F 9A 99 19 3E 00 00 80 3F
// 00 00 80 3F 00 00 80 3F 32 36 2D 34 2D 32 37 00 2D 32 37 00 50 B1 BF 00 00 AA
// AB";
// String cleanHex = hex.replaceAll("\\s+", ""); // 移除所有空白字符
// byte[] bytes = Hex.decodeHex(cleanHex);
// DeviceRunParam runParam = DeviceRunParam.fromBytes(bytes);
// System.out.println(JsonUtils.toJsonString(runParam));
2026-07-29 13:08:49 +08:00
2026-11-16 13:38:56 +08:00
// runParam.setLeftBackwardGain(0.2f);
// runParam.setLeftForwardGain(0.2f);
// runParam.setRightForwardGain(0.2f);
// runParam.setRightBackwardGain(0.2f);
2026-07-29 13:08:49 +08:00
2026-11-16 13:38:56 +08:00
// byte[] newB = runParam.toWriteBytes();
// System.out.println(Hex.encodeHexString(newB));
2026-07-29 13:08:49 +08:00
2026-10-23 09:12:53 +08:00
// }
2026-04-17 10:42:16 +08:00
}