lizijie
2021-11-29 d8310f6bc609440fa352c0e5a9436c7c78caa287
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.moral.service.impl;
 
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.entity.Device;
 
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.HistoryHourlyMapper;
import com.moral.mapper.HistoryMapper;
import com.moral.service.DeviceService;
import com.moral.util.AlarmUtils_2;
 
@Service
public class DeviceServiceImpl implements DeviceService {
 
    public static Logger log = Logger.getLogger(DeviceServiceImpl.class);
 
    private static Double[] PM2_5 = {0d, 35d, 75d, 115d, 150d, 250d};
    private static Double[] PM10 = {0d, 50d, 150d, 250d, 350d, 420d};
    private static Double[] SO2 = {0d, 150d, 500d, 650d, 800d, 1600d};
    private static Double[] NO2 = {0d, 100d, 200d, 700d, 1200d, 2340d};
    private static Double[] CO = {0d, 5d, 10d, 35d, 60d, 90d};
    private static Double[] O3 = {0d, 160d, 200d, 300d, 400d, 800d};
    private static Double[] TVOC = {0d, 1.5d, 3d, 5d};
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Autowired
    private HistoryMapper historyMapper;
 
    @Autowired
    private HistoryHourlyMapper historyHourlyMapper;
 
    @Override
    public List<Map<String, Object>> getSensorData(Map<String, Object> parameters) {
        return historyMapper.getSensorData(parameters);
    }
 
    @Override
    public List<Map<String, Object>> getSensorDataOnce(Map<String, Object> parameters) {
        return historyMapper.getSensorDataOnce(parameters);
    }
 
    @Override
    public List<Map<String, Object>> getSensorDataByMac(Map<String, Object> parameters) {
        return historyMapper.getSensorDataByMac(parameters);
    }
 
    @Override
    public List<Map<String, Object>> getSensorDataByMacOnce(Map<String, Object> parameters) {
        return historyMapper.getSensorDataByMacOnce(parameters);
    }
 
    @Override
    public List<String> getMacs() {
        return deviceMapper.getMacs();
    }
 
    @Override
    public List<String> getMacByOrganizationid(List<Object> organizationIdList) {
        return deviceMapper.getMacByOrganizationid(organizationIdList);
    }
 
    @Override
    public List<Map<String, Object>> macAndOrganizationIdMap(List<String> macs) {
        return deviceMapper.macAndOrganizationIdMap(macs);
    }
 
    @Override
    public List<Map<String, Object>> getAllByMacList(List<String> macList) {
        return deviceMapper.getAllByMacList(macList);
    }
 
    @Override
    public Device getDeviceByID(int id) {
        Device result = deviceMapper.selectByPrimaryKey(id);
        return result;
    }
 
    @Override
    public void alarmSms(Map<String, Object> params) {
        LocalDateTime time = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("time", time);
        List<String> sensors = Arrays.asList("e1", "e2", "e11", "e16", "e10", "e15", "e17");
        hashMap.put("sensorKeys", sensors);
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String orgId = entry.getKey();
            List<String> emails = (List<String>) entry.getValue();
            List<Map<String, Object>> devices = deviceMapper.getAllDeviceByOrg(orgId);
            List<String> result = new ArrayList<>();
            for (Map<String, Object> device : devices) {
                String name = device.get("name").toString();
                String mac = device.get("mac").toString();
                hashMap.put("mac", mac);
                Map<String, Object> data = JSONObject.parseObject(historyHourlyMapper.selectDataByMac(hashMap), Map.class);
                List<String> alarmInfo = new ArrayList<>();
                Double pm25Value = Double.parseDouble(((List) data.get("e1")).get(0).toString());
                Double pm10Value = Double.parseDouble(((List) data.get("e2")).get(0).toString());
                Double so2Value = Double.parseDouble(((List) data.get("e11")).get(0).toString());
                Double no2Value = Double.parseDouble(((List) data.get("e16")).get(0).toString());
                Double coValue = Double.parseDouble(((List) data.get("e10")).get(0).toString());
                Double o3Value = Double.parseDouble(((List) data.get("e15")).get(0).toString());
                Double tvocValue = Double.parseDouble(((List) data.get("e17")).get(0).toString());
                String pm25 = alarmInfo("PM2.5", pm25Value, PM2_5);
                String pm10 = alarmInfo("PM10", pm10Value, PM10);
                String so2 = alarmInfo("SO2", so2Value, SO2);
                String no2 = alarmInfo("NO2", no2Value, NO2);
                String co = alarmInfo("CO", coValue, CO);
                String o3 = alarmInfo("O3", o3Value, O3);
                String tvoc = alarmInfo("TVOC", tvocValue, TVOC);
                if (pm25 != null) {
                    alarmInfo.add(pm25);
                }
                if (pm10 != null) {
                    alarmInfo.add(pm10);
                }
                if (so2 != null) {
                    alarmInfo.add(so2);
                }
                if (no2 != null) {
                    alarmInfo.add(no2);
                }
                if (co != null) {
                    alarmInfo.add(co);
                }
                if (o3 != null) {
                    alarmInfo.add(o3);
                }
                if (tvoc != null) {
                    alarmInfo.add(tvoc);
                }
                if (!ObjectUtils.isEmpty(alarmInfo)) {
                    result.add(name + alarmInfo);
                }
            }
 
            if (!ObjectUtils.isEmpty(result)) {
                for (String email : emails) {
                    AlarmUtils_2.sendMail(email, "设备因子浓度报警!!!", result.toString());
                }
            }
        }
    }
 
    private String alarmInfo(String sensorName, Double value, Double[] doubles) {
        int state = 1;
        for (int i = doubles.length - 1; i >= 0; i--) {
            Double aDouble = doubles[i];
            if (value >= aDouble) {
                state = i + 1;
                break;
            }
        }
        if (state != 1) {
            StringBuilder builder = new StringBuilder();
            builder.append(sensorName).append("浓度值==").append(value).append("-->超出限定值");
            if (state == 6) {
                builder.append(doubles[5]);
            } else if (state == 5) {
                builder.append(doubles[4]);
            } else if (state == 4) {
                builder.append(doubles[3]);
            } else if (state == 3) {
                builder.append(doubles[2]);
            } else if (state == 2) {
                builder.append(doubles[1]);
            }
            return builder.toString();
        }
        return null;
    }
}