Files
MiddlePlatform/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java
2026-11-19 09:18:17 +08:00

102 lines
4.0 KiB
Java

package com.maibu.scheduled;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
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.GlobalMemory;
import com.maibu.memory.SiteMemory;
@Component
public class ScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private RedisCache redisCache;
@Autowired
private VehicleMessageMapper vehicleMessageMapper;
// 整点更新设备状态
@Scheduled(cron = "0 0 * * * ?")
// @Scheduled(cron = "0 * * * * ?")
public void executeHourlyTask() {
System.out.println("整点定时任务执行时间: " + dateFormat.format(new Date()));
String key = NettyCacheKey.vehicleMessageKey;
// Collection<String> keys = redisCache.keys(key);
Collection<String> keys = redisCache.getListKeyByPrefix(key);
if (!CollectionUtils.isEmpty(keys)) {
keys.forEach(k -> {
VehicleMessageRecordDTO recordDTO = redisCache.getCacheObject(k);
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
// recordDTO.getHistories().forEach(x -> {
// deviceStatusHistoryMapper.insert(x);
// });
vehicleMessageMapper.insertBatch(recordDTO.getHistories());
}
redisCache.deleteObject(k);
});
}
System.out.println("任务执行完成");
}
// 0点刷新任务创建状态
@Scheduled(cron = "0 0 0 * * ?")
public void refreshDevicePlanTask() {
System.out.println("0点定时刷新状态");
List<SiteMemory> siteMemories = GlobalMemory.getAllSiteMemory();
if (CollectionUtils.isEmpty(siteMemories))
return;
siteMemories.forEach(z -> {
Map<String, List<DevicePlan>> map = z.devicePlanMap;
if (!CollectionUtils.isEmpty(map.keySet())) {
map.values().forEach(x -> {
if (!CollectionUtils.isEmpty(x)) {
x.forEach(y -> {
y.setDayTriggerFlag(false);
});
}
});
}
});
System.out.println("0点定时刷新状态执行完成");
}
// public static void main(String[] args) throws DecoderException {
// 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));
// runParam.setLeftBackwardGain(0.2f);
// runParam.setLeftForwardGain(0.2f);
// runParam.setRightForwardGain(0.2f);
// runParam.setRightBackwardGain(0.2f);
// byte[] newB = runParam.toWriteBytes();
// System.out.println(Hex.encodeHexString(newB));
// }
}