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> getSensorData(Map parameters) { return historyMapper.getSensorData(parameters); } @Override public List> getSensorDataOnce(Map parameters) { return historyMapper.getSensorDataOnce(parameters); } @Override public List> getSensorDataByMac(Map parameters) { return historyMapper.getSensorDataByMac(parameters); } @Override public List> getSensorDataByMacOnce(Map parameters) { return historyMapper.getSensorDataByMacOnce(parameters); } @Override public List getMacs() { return deviceMapper.getMacs(); } @Override public List getMacByOrganizationid(List organizationIdList) { return deviceMapper.getMacByOrganizationid(organizationIdList); } @Override public List> macAndOrganizationIdMap(List macs) { return deviceMapper.macAndOrganizationIdMap(macs); } @Override public List> getAllByMacList(List macList) { return deviceMapper.getAllByMacList(macList); } @Override public Device getDeviceByID(int id) { Device result = deviceMapper.selectByPrimaryKey(id); return result; } @Override public void alarmSms(Map params) { LocalDateTime time = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS); Map hashMap = new HashMap<>(); hashMap.put("time", time); List sensors = Arrays.asList("e1", "e2", "e11", "e16", "e10", "e15", "e17"); hashMap.put("sensorKeys", sensors); for (Map.Entry entry : params.entrySet()) { String orgId = entry.getKey(); List emails = (List) entry.getValue(); List> devices = deviceMapper.getAllDeviceByOrg(orgId); List result = new ArrayList<>(); for (Map device : devices) { String name = device.get("name").toString(); String mac = device.get("mac").toString(); hashMap.put("mac", mac); Map data = JSONObject.parseObject(historyHourlyMapper.selectDataByMac(hashMap), Map.class); List 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; } }