package com.sandu.ximon.admin.quartz; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.sandu.common.util.SpringContextHolder; import com.sandu.common.util.ThrowableUtil; import com.sandu.ximon.admin.service.*; import com.sandu.ximon.dao.domain.LightTask; import com.sandu.ximon.dao.domain.LightTaskQuartz; import com.sandu.ximon.dao.domain.LightTaskQuartzLog; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import java.util.List; /** * @author chenjiantian * @date 2021/12/15 10:28 * 设置定时控灯的定时任务 */ public class LightTimerJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("执行任务" + Thread.currentThread().toString()); long quartzId = context.getMergedJobDataMap().getLong("quartzId"); String taskName = context.getMergedJobDataMap().getString("taskName"); long taskId = context.getMergedJobDataMap().getLong("taskId"); String cron = context.getMergedJobDataMap().getString("cron"); LightTaskQuartzLog log = new LightTaskQuartzLog(); log.setCronExpression(cron); log.setTaskId(taskId); log.setTaskName(taskName); log.setQuartzId(quartzId); PoleService poleService = SpringContextHolder.getBean(PoleService.class); LightTaskPoleRelationService lightTaskPoleRelationService = SpringContextHolder.getBean(LightTaskPoleRelationService.class); LightTaskQuartzService lightTaskQuartzService = SpringContextHolder.getBean(LightTaskQuartzService.class); LightTaskService lightTaskService = SpringContextHolder.getBean(LightTaskService.class); LightTaskQuartzLogService lightTaskQuartzLogService = SpringContextHolder.getBean(LightTaskQuartzLogService.class); long startTime = System.currentTimeMillis(); try { // 任务绑定的灯杆 List poleIdList = lightTaskPoleRelationService.listPoleIdByTaskId(taskId); String lightAdress = lightTaskService.getOne(Wrappers.lambdaQuery(LightTask.class).eq(LightTask::getTaskId, taskId)).getLightAdress(); // 灯杆的设备码 List deviceCodeList = poleService.listDeviceCodeByIds(poleIdList); if (CollectionUtil.isNotEmpty(deviceCodeList)) { LightTaskQuartz lightTaskQuartz = lightTaskQuartzService.getById(quartzId); if (lightTaskQuartz != null) { for (String deviceCode : deviceCodeList) { // 定时给每个设备吗发送灯控请求 lightTaskService.sendTimeRRpc(lightTaskQuartz.getFramePayload(), deviceCode, lightAdress); } log.setFramePayload(lightTaskQuartz.getFramePayload()); } } long times = System.currentTimeMillis() - startTime; log.setTime(times); } catch (Exception e) { log.setExceptionDetail(ThrowableUtil.getStackTrace(e)); long times = System.currentTimeMillis() - startTime; log.setTime(times); } finally { lightTaskQuartzLogService.save(log); } } }