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

94 lines
3.9 KiB
Java
Raw Normal View History

2026-04-17 17:12:41 +08:00
package com.maibu.scheduled;
import com.maibu.common.NettyCacheKey;
2026-04-30 15:25:58 +08:00
import com.maibu.core.business.DevicePlan;
2026-07-29 13:08:49 +08:00
import com.maibu.core.business.DeviceRunParam;
2026-04-30 15:25:58 +08:00
import com.maibu.core.business.DeviceStatusRecordDTO;
2026-04-17 17:12:41 +08:00
import com.maibu.core.redis.RedisCache;
import com.maibu.mapper.DeviceStatusHistoryMapper;
2026-04-30 15:25:58 +08:00
import com.maibu.memory.SiteMemory;
2026-07-29 13:08:49 +08:00
import com.maibu.utils.json.JsonUtils;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
2026-04-17 10:42:16 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
2026-04-21 09:20:55 +08:00
import org.springframework.util.CollectionUtils;
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;
@Component
public class ScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private RedisCache redisCache;
@Autowired
private DeviceStatusHistoryMapper deviceStatusHistoryMapper;
//整点更新设备状态
@Scheduled(cron = "0 0 * * * ?")
// @Scheduled(cron = "0 * * * * ?")
public void executeHourlyTask() {
System.out.println("整点定时任务执行时间: " + dateFormat.format(new Date()));
String key = NettyCacheKey.deviceRunningStatusKey;
// Collection<String> keys = redisCache.keys(key);
Collection<String> keys = redisCache.getListKeyByPrefix(key);
if (!CollectionUtils.isEmpty(keys)) {
keys.forEach(k -> {
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(k);
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
// recordDTO.getHistories().forEach(x -> {
// deviceStatusHistoryMapper.insert(x);
// });
2026-06-17 09:14:42 +08:00
2026-04-17 10:42:16 +08:00
deviceStatusHistoryMapper.insertBatch(recordDTO.getHistories());
}
redisCache.deleteObject(k);
});
}
System.out.println("任务执行完成");
}
//0点刷新任务创建状态
@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
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));
}
2026-04-17 10:42:16 +08:00
}