#
Junjie
4 天以前 2468af88e9ee9901f1d4d6f1cfdb6361c7400730
src/main/java/com/zy/asrs/task/NotifyScheduler.java
@@ -1,15 +1,11 @@
package com.zy.asrs.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.domain.NotifyDto;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.HttpRequestLog;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.asrs.service.HttpRequestLogService;
import com.zy.asrs.service.NotifyAsyncService;
import com.zy.asrs.utils.NotifyUtils;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.SlaveType;
import com.zy.system.entity.Config;
@@ -19,7 +15,6 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Component
@@ -33,12 +28,12 @@
    @Autowired
    private ConfigService configService;
    @Autowired
    private HttpRequestLogService httpRequestLogService;
    @Autowired
    private DeviceConfigService deviceConfigService;
    @Autowired
    private NotifyAsyncService notifyAsyncService;
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyShuttle(){
    public synchronized void notifyShuttle() {
        List<DeviceConfig> deviceList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Crn)));
        for (DeviceConfig device : deviceList) {
@@ -47,7 +42,7 @@
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyForkLift(){
    public synchronized void notifyForkLift() {
        List<DeviceConfig> deviceList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Rgv)));
        for (DeviceConfig device : deviceList) {
@@ -56,23 +51,32 @@
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyTask(){
    public synchronized void notifyStation() {
        List<DeviceConfig> deviceList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Devp)));
        for (DeviceConfig device : deviceList) {
            notifyMsg(String.valueOf(SlaveType.Devp), device.getDeviceNo());
        }
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyTask() {
        notifyMsg("task", 1);
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyCrn(){
        notifyMsg("crn", 1);
    public synchronized void notifyCrn() {
        notifyMsg(String.valueOf(SlaveType.Crn), 1);
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyDualCrn(){
        notifyMsg("dualCrn", 1);
    public synchronized void notifyDualCrn() {
        notifyMsg(String.valueOf(SlaveType.DualCrn), 1);
    }
    private synchronized void notifyMsg(String notifyType, Integer device) {
        Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable"));
        if(notifyEnableConfig == null){
        if (notifyEnableConfig == null) {
            return;
        }
        String notifyEnable = notifyEnableConfig.getValue();
@@ -81,19 +85,19 @@
        }
        Config notifyUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUri"));
        if(notifyUriConfig == null){
        if (notifyUriConfig == null) {
            return;
        }
        String notifyUri = notifyUriConfig.getValue();
        Config notifyUriPathConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUriPath"));
        if(notifyUriPathConfig == null){
        if (notifyUriPathConfig == null) {
            return;
        }
        String notifyUriPath = notifyUriPathConfig.getValue();
        List<String> keys = notifyUtils.takeKeys(notifyType, device);
        if(keys == null){
        if (keys == null) {
            return;
        }
@@ -112,48 +116,8 @@
                continue;
            }
            HttpRequestLog httpRequestLog = new HttpRequestLog();
            httpRequestLog.setName(notifyUri + notifyUriPath);
            httpRequestLog.setRequest(JSON.toJSONString(notifyDto));
            httpRequestLog.setCreateTime(new Date());
            try {
                //触发通知
                String response = new HttpHandler.Builder()
                        .setUri(notifyUri)
                        .setPath(notifyUriPath)
                        .setJson(JSON.toJSONString(notifyDto))
                        .build()
                        .doPost();
                httpRequestLog.setResponse(response);
                JSONObject jsonObject = JSON.parseObject(response);
                Integer code = jsonObject.getInteger("code");
                if(code == 200){
                    //通知成功
                    redisUtil.del(key);
                    continue;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                //保存记录
                httpRequestLogService.insert(httpRequestLog);
            }
            //通知失败
            int times = notifyDto.getRetryTimes() + 1;
            if (times >= notifyDto.getFailTimes()) {
                //超过次数
                redisUtil.del(key);
                continue;
            }
            notifyDto.setLastRetryTime(System.currentTimeMillis());
            notifyDto.setRetryTimes(times);
            redisUtil.set(key, notifyDto);
            continue;
            // 异步发送通知,避免阻塞定时器线程
            notifyAsyncService.sendNotifyAsync(notifyUri, notifyUriPath, key, notifyDto);
        }
    }