liusuyi
2026-05-12 9f327c33730ba10cb2d89aff99b727502232e968
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.ard.work.health.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ard.common.core.utils.StringUtils;
import com.ard.work.health.domain.ArdAlarmEqHealth;
import com.ard.work.health.service.ArdAlarmEqHealthService;
import com.ard.work.health.util.Global;
import com.ard.work.health.util.LonlatConver;
import com.ard.work.websocket.utils.JKWebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
@Slf4j(topic = "task")
@RefreshScope
public class HealthTask {
 
    @Value("${health.getAlarmAPITask}")
    Boolean EqHealthGetAlarmAPITaskEnable;
 
    @Value("${health.realTask}")
    Boolean EqHealthRealTaskEnable;
 
    @Autowired
    private ArdAlarmEqHealthService ardAlarmEqHealthService;
 
    /**
     * @描述 设备健康报警api接口查询定时任务
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/5/5 11:51
     * @修改人和其它信息
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void GetAlarmEqHealthApiTask() {
        if (!EqHealthGetAlarmAPITaskEnable) {
            return;
        }
        log.debug("设备健康报警定时获取数据");
        ardAlarmEqHealthService.getAlarmDataByApi();
    }
 
    /**
     * @描述 设备健康报警上传前端定时任务
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/5/5 11:51
     * @修改人和其它信息
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void EqHealthAlarmRealTask() {
        if (!EqHealthRealTaskEnable) {
            return;
        }
        log.debug("设备健康报警定时任务开始");
        // 开始时间
        long stime = System.currentTimeMillis();
        try {
            /*获取数据库最新50条数据*/
            List<ArdAlarmEqHealth> ardAlarmEqHealths = ardAlarmEqHealthService.GetRealAlarm();
            Map<String, Object> map = new HashMap<>();
            map.put("Command", 2001);
            int count = 0;
            List<ArdAlarmEqHealth> objs = new ArrayList<>();
            for (ArdAlarmEqHealth data : ardAlarmEqHealths) {
 
                //判断DirectLE为空 赋值空字符串
                if (data.getDirectle() == null) {
                    data.setDirectle("");
                }
                //经纬度火星转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]);
                }
 
                String alarmTime = data.getAlarmTime();
                //将最新数据放入obj
                if (StringUtils.isEmpty(Global.EqHealthNewTime)) {
                    /*如果临时最新时间为空,则取最近10秒内的最新数据*/
                    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.MINUTE, -10);
                    Date time = calendar.getTime();
                    int dateFlag = alarmTime.compareTo(fmt.format(time));
                    if (dateFlag > 0) {
                        objs.add(data);
                        Global.EqHealthNewTime = alarmTime;
                    }
                } else {
                    /*否则判断当前数据的时间是否大于最新时间*/
                    int dateFlag = alarmTime.compareTo(Global.EqHealthNewTime);
                    if (dateFlag > 0) {
                        objs.add(data);
                        Global.EqHealthNewTime = alarmTime;
                    }
                }
 
                //统计所有countLE的总数
                if (StringUtils.isEmpty(data.getDirectle())) {
                    count++;
                }
                data.setId(data.getMeasureKey() + "&" + data.getCommand());
            }
            if (objs.size() > 0) {
                map.put("obj", objs);
            } else {
                map.put("obj", -1);
            }
            map.put("count", count);
            map.put("data", ardAlarmEqHealths);
            if (ardAlarmEqHealths.size() > 0) {
 
                //推送
                JKWebSocketUtils.sendMessageAll(Collections.singletonList(new TextMessage(JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect))));
 
//                for (Integer userId : SessionCollection.getClientsessionmap().keySet()) {
//
//                    List<WebSocketSession> webSocketSessions = SessionCollection.getClientsessionmap().get(userId);
//                    for (WebSocketSession webSocketSession : webSocketSessions) {
//                        if (webSocketSession != null && webSocketSession.isOpen()) {
//                            synchronized (webSocketSession) {
//                                try {
//                                    webSocketSession.sendMessage(new TextMessage(JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect)));
//                                } catch (IOException e) {
//                                    continue;
//                                }
//
//                            }
//                        }
//                    }
//                }
            }
 
        } catch (Exception ex) {
            log.error("设备健康实时数据定时任务异常:" + ex.getMessage());
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        log.debug("设备健康报警定时任务结束:" + (etime - stime) + " 毫秒");
    }
}