‘liusuyi’
2023-06-14 37f8d15dd8954e04ad09445184c04a58773af17a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.ruoyi.quartz.task;
 
import com.ruoyi.alarm.globalAlarm.service.IGlobalAlarmService;
import com.ruoyi.alarm.stealAlarm.domain.ArdAlarmStealelec;
import com.ruoyi.alarm.stealAlarm.service.IStealElecAlarmService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.LonlatConver;
import com.ruoyi.common.websocket.util.WebSocketUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.ruoyi.common.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
 
/**
 * @ClassName: AlarmTask
 * @Description:
 * @Author: Administrator
 * @Date: 2023年03月06日 11:13
 * @Version: 1.0
 **/
@Component("AlarmTask")
@Slf4j(topic = "AlarmTask")
public class AlarmTask {
 
    @Resource
    IStealElecAlarmService IStealElecAlarmService;
    @Resource
    ISysUserService sysUserService;
    @Resource
    IGlobalAlarmService globalAlarmService;
 
    /**
     * @描述 盗电报警实时推送任务
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/3/6 15:10
     * @修改人和其它信息
     */
    public void stealElecAlarmPush() {
        log.info("盗电报警推送开始");
        // 开始时间
        long stime = System.currentTimeMillis();
        try {
            for (String userId : ONLINE_USER_SESSIONS.keySet()) {
                SysUser sysUser = sysUserService.selectUserById(userId);
                List<ArdAlarmStealelec> alarms = IStealElecAlarmService.getRealAlarm(sysUser.getDeptId());
                Map<String, Object> map = new HashMap<>();
                List<ArdAlarmStealelec> objs = new ArrayList<>();
                for (ArdAlarmStealelec data : alarms) {
 
                    //经纬度火星转84
                    if (data.getLongitude() != 0 && data.getLatitude() != 0) {
                        double[] wgs84 = LonlatConver.gcj02_To_Wgs84(data.getLongitude(), data.getLatitude());//火星转84
                        data.setLongitude(wgs84[0]);
                        data.setLatitude(wgs84[1]);
                    }
 
                    //判断当前数据是否10S内
                    Date startTime = data.getStartTime();
                    SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//HH是24小时制,12小时用hh
                    Calendar calendar = Calendar.getInstance();
                    //从当前时间减去10秒
                    calendar.add(Calendar.MINUTE, -30);
                    String currentTime = formatTime.format(calendar.getTime());
                    int dateFlag = formatTime.format(startTime).compareTo(currentTime);
                    if (dateFlag > 0) {
                        objs.add(data);
                    }
                    data.setId(data.getDescribe() + "&" + data.getCommand());
                }
                if (objs.size() > 0) {
                    map.put("obj", objs);
                } else {
                    map.put("obj", -1);
                }
                map.put("Command", 8000);
                map.put("count", objs.size());
                map.put("total", alarms.size());
                map.put("data", alarms);
                if (alarms.size() > 0) {
                    WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(userId), map);
                }
            }
        } catch (Exception ex) {
            log.error("盗电报警推送异常:" + ex.getMessage());
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        log.info("盗电报警推送结束:" + (etime - stime) + " 毫秒");
    }
 
    /**
     * @描述 盗电报警api拉取任务
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/2/28 11:51
     * @修改人和其它信息
     */
    public void stealElecAlarmPull() {
        log.info("盗电数据拉取开始");
        // 开始时间
        long stime = System.currentTimeMillis();
        try {
            // String url = "http://iot.zhdk.net:8090/Warning/GetWarning?userName=cy4oil";
            IStealElecAlarmService.getAlarmByApi();
        } catch (Exception ex) {
            log.error("盗电数据拉取异常:" + ex.getMessage());
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        log.info("盗电数据拉取结束:" + (etime - stime) + " 毫秒");
    }
 
    public void globalAlarmCountPush() {
        Map<String, Integer> stringIntegerMap = globalAlarmService.selectAlarmLogsCount();
        if (ONLINE_USER_SESSIONS.size() > 0) {
                WebSocketUtils.sendMessageAll(stringIntegerMap);
        }
    }
}