|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | 
 |  |  | import com.moral.constant.RedisConstants; | 
 |  |  | import com.moral.util.ConvertUtils; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  |  | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
 |  |  | 
 |  |  | import java.beans.PropertyDescriptor; | 
 |  |  | import java.lang.reflect.Field; | 
 |  |  | import java.lang.reflect.Method; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.LinkedHashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.lang.reflect.Type; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletRequest; | 
 |  |  | 
 |  |  |         //扩展字段 | 
 |  |  |         deviceInfo.put("extend", device.getExtend()); | 
 |  |  |  | 
 |  |  |         //乡镇街道信息 | 
 |  |  |         deviceInfo.put("town", device.getTown()); | 
 |  |  |  | 
 |  |  |         //行业 | 
 |  |  |         deviceInfo.put("professions", device.getProfessions()); | 
 |  |  |  | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData) { | 
 |  |  |         String mac = deviceData.remove("mac").toString(); | 
 |  |  |     public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData,String code) { | 
 |  |  | //        String mac = deviceData.remove("mac").toString(); | 
 |  |  |         String mac = deviceData.get("mac").toString(); | 
 |  |  |         //从redis获取校准公式 | 
 |  |  |         Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); | 
 |  |  |         if (!ObjectUtils.isEmpty(adjustFormula)) { | 
 |  |  | 
 |  |  |             if (govMpInfo.get("guid") != null) { | 
 |  |  |                 aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString()); | 
 |  |  |             } | 
 |  |  |             return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap); | 
 |  |  |             return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,code); | 
 |  |  |         } | 
 |  |  |         deviceData.remove("DataTime"); | 
 |  |  |         return deviceData; | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> adjustSpecialDeviceData(Map<String, Object> deviceData) { | 
 |  |  |         String mac = deviceData.remove("mac").toString(); | 
 |  |  | //        String mac = deviceData.remove("mac").toString(); | 
 |  |  |         String mac = deviceData.get("mac").toString(); | 
 |  |  |         //从redis获取校准公式 | 
 |  |  |         Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); | 
 |  |  |         if (!ObjectUtils.isEmpty(adjustFormula)) { | 
 |  |  | 
 |  |  |             if (ObjectUtils.isEmpty(aqiMap)) { | 
 |  |  |                 aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, cityCode); | 
 |  |  |             } | 
 |  |  |             return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap); | 
 |  |  |             return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,"0"); | 
 |  |  |         } | 
 |  |  |         deviceData.remove("DataTime"); | 
 |  |  |         return deviceData; | 
 |  |  | 
 |  |  |             String sensorCode = sensor.getCode(); | 
 |  |  |             //转换公式 | 
 |  |  |             String formula = sensor.getFormula(); | 
 |  |  |             //转换单位前因子值 | 
 |  |  |             String sensorValue = (String) deviceData.get(sensorCode); | 
 |  |  |             double value = Double.parseDouble(sensorValue); | 
 |  |  |             //单位转换 | 
 |  |  |             if (formula != null) { | 
 |  |  |                 //转换后因子值 | 
 |  |  |                 sensorValue = formula.replace("{0}", sensorValue); | 
 |  |  |                 expression = AviatorEvaluator.compile(sensorValue); | 
 |  |  |                 value = Double.parseDouble(expression.execute().toString()); | 
 |  |  |             } | 
 |  |  |             int sensorState = judgeState(list, value); | 
 |  |  |             if (sensorState > state) { | 
 |  |  |                 state = sensorState; | 
 |  |  |  | 
 |  |  |             //设备所传因子与配置型号因子不一致的处理逻辑 | 
 |  |  |             if (deviceData.get(sensorCode) != null) { | 
 |  |  |                 String sensorValue = String.valueOf(deviceData.get(sensorCode)); | 
 |  |  |                 double value = Double.parseDouble(sensorValue); | 
 |  |  |                 if (formula != null) { | 
 |  |  |                     //转换后因子值 | 
 |  |  |                     sensorValue = formula.replace("{0}", sensorValue); | 
 |  |  |                     expression = AviatorEvaluator.compile(sensorValue); | 
 |  |  |                     value = Double.parseDouble(expression.execute().toString()); | 
 |  |  |                 } | 
 |  |  |                 int sensorState = judgeState(list, value); | 
 |  |  |                 if (sensorState > state) { | 
 |  |  |                     state = sensorState; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //修改设备状态 | 
 |  |  | 
 |  |  |         deviceMapper.update(null, updateWrapper); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<Map<String, Object>> selectMonitorPiontAndDeviceByOrgId(int orgId) { | 
 |  |  |         QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>(); | 
 |  |  |         monitorPointQueryWrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         monitorPointQueryWrapper.eq("organization_id", orgId); | 
 |  |  |         List<MonitorPoint> monitorPointList = monitorPointMapper.selectList(monitorPointQueryWrapper); | 
 |  |  |         List<Map<String, Object>> resultList = new ArrayList<>(); | 
 |  |  |         for (MonitorPoint monitorPoint : monitorPointList) { | 
 |  |  |             Map<String, Object> resultMap = new HashMap<>(); | 
 |  |  |             resultMap = JSON.parseObject(JSON.toJSONString(monitorPoint), Map.class); | 
 |  |  |             int mp_id = monitorPoint.getId(); | 
 |  |  |             QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); | 
 |  |  |             deviceQueryWrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |             deviceQueryWrapper.eq("monitor_point_id", mp_id); | 
 |  |  |             List<Device> devices = new ArrayList<>(); | 
 |  |  |             devices = deviceMapper.selectList(deviceQueryWrapper); | 
 |  |  |             resultMap.put("devices", devices); | 
 |  |  |             resultList.add(resultMap); | 
 |  |  |         } | 
 |  |  |         return resultList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //根据因子值判断状态 | 
 |  |  |     private int judgeState(List<Object> levels, Double data) { | 
 |  |  |         int state = 1; | 
 |  |  |         for (int i = levels.size() - 1; i >= 0; i--) { | 
 |  |  |             Double level = Double.parseDouble(levels.get(i).toString()); | 
 |  |  |             if (data >= level) { | 
 |  |  |                 state = i + 1; | 
 |  |  |                 state = i + 2; | 
 |  |  |                 break; | 
 |  |  |             } | 
 |  |  |         } |