‘liusuyi’
2023-12-05 745d5b90be7d8cdb8873b18d18b286fdc4b6913b
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
package com.ard.alarm.stealelec.service;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ard.alarm.stealelec.config.StealElecConfiguration;
import com.ard.alarm.stealelec.domain.ArdAlarmStealelec;
import com.ard.utils.http.HttpUtils;
import com.ard.utils.mqtt.MqttProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @ClassName StealElecAlarmServiceImpl
 * @Description:盗电警报服务实现类
 * @Author 刘苏义
 * @Date 2022/12/26 16:34
 * @Version 1.0
 */
@Slf4j(topic = "stealAlarm")
@Service
public class StealElecAlarmService {
 
@Resource
    StealElecConfiguration stealElecConfig;
    List<String> tempList = new ArrayList<>();
 
    /**
     * @描述 获取api数据推送mqtt
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/6/14 9:45
     * @修改人和其它信息
     */
    @Async("alarm")
    @Scheduled(cron = "0/5 * * * * ?")
    public void alarmHandler() {
        try {
            if (!stealElecConfig.getEnabled()) {
                return;
            }
            String allAlarmData = HttpUtils.sendGet(stealElecConfig.getUrl());
            //剔除所有\符号
            String message = allAlarmData.replaceAll("\\\\", "");
            //剔除整个字符串首尾双引号
            if (message.startsWith("\"")) {
                message = message.substring(1);
            }
            if (message.endsWith("\"")) {
                message = message.substring(0, message.length() - 1);
            }
            Map<String, Object> result = (Map<String, Object>) JSON.parse(message);
            //获取到json对象list集合
            List<JSONObject> list = (List<JSONObject>) result.get("data");
            if (list.size() > 0) {
                for (JSONObject object : list) {
                    String JSONStr = JSON.toJSONString(object);
                    //转成实体对象
                    ArdAlarmStealelec wd = JSON.parseObject(JSONStr, ArdAlarmStealelec.class);
                    if (tempList.contains(wd.getId())) {
                        continue;
                    }
                    MqttProducer.publish(2, false, "stealelec", JSON.toJSONString(wd));
                    tempList.add(wd.getId());
                }
            }
        } catch (Exception ex) {
            log.error("stealelec报警数据推送异常:" + ex.getMessage());
        }
    }
}