package com.zy.asrs.task; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.zy.asrs.domain.NotifyDto; import com.zy.asrs.entity.DeviceConfig; import com.zy.asrs.service.DeviceConfigService; import com.zy.asrs.service.NotifyAsyncService; import com.zy.asrs.utils.NotifyUtils; import com.zy.common.utils.RedisUtil; import com.zy.core.enums.SlaveType; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; @Component @Slf4j public class NotifyScheduler { @Autowired private RedisUtil redisUtil; @Autowired private NotifyUtils notifyUtils; @Autowired private ConfigService configService; @Autowired private DeviceConfigService deviceConfigService; @Autowired private NotifyAsyncService notifyAsyncService; @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyShuttle() { List deviceList = deviceConfigService.selectList(new EntityWrapper() .eq("device_type", String.valueOf(SlaveType.Crn))); for (DeviceConfig device : deviceList) { notifyMsg(String.valueOf(SlaveType.Crn), device.getDeviceNo()); } } @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyForkLift() { List deviceList = deviceConfigService.selectList(new EntityWrapper() .eq("device_type", String.valueOf(SlaveType.Rgv))); for (DeviceConfig device : deviceList) { notifyMsg(String.valueOf(SlaveType.Rgv), device.getDeviceNo()); } } @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyTask() { notifyMsg("task", 1); } @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyCrn() { notifyMsg(String.valueOf(SlaveType.Crn), 1); } @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyDualCrn() { notifyMsg(String.valueOf(SlaveType.DualCrn), 1); } private synchronized void notifyMsg(String notifyType, Integer device) { Config notifyEnableConfig = configService.selectOne(new EntityWrapper().eq("code", "notifyEnable")); if (notifyEnableConfig == null) { return; } String notifyEnable = notifyEnableConfig.getValue(); if (!notifyEnable.equals("Y")) { return; } Config notifyUriConfig = configService.selectOne(new EntityWrapper().eq("code", "notifyUri")); if (notifyUriConfig == null) { return; } String notifyUri = notifyUriConfig.getValue(); Config notifyUriPathConfig = configService.selectOne(new EntityWrapper().eq("code", "notifyUriPath")); if (notifyUriPathConfig == null) { return; } String notifyUriPath = notifyUriPathConfig.getValue(); List keys = notifyUtils.takeKeys(notifyType, device); if (keys == null) { return; } if (keys.isEmpty()) { return; } for (String key : keys) { Object object = redisUtil.get(key); if (object == null) { continue; } NotifyDto notifyDto = (NotifyDto) object; if (System.currentTimeMillis() - notifyDto.getLastRetryTime() < 1000 * notifyDto.getRetryTime()) { continue; } // 异步发送通知,避免阻塞定时器线程 notifyAsyncService.sendNotifyAsync(notifyUri, notifyUriPath, key, notifyDto); } } }