|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.googlecode.aviator.AviatorEvaluator; | 
|---|
|  |  |  | import com.googlecode.aviator.Expression; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.util.ConvertUtils; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  | import com.sun.javafx.collections.MappingChange; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | 
|---|
|  |  |  | //设备信息存入redis | 
|---|
|  |  |  | setDeviceInfoToRedis(mac, deviceInfo); | 
|---|
|  |  |  | //刷新deviceInfo缓存 | 
|---|
|  |  |  | CacheUtils.refreshDeviceAlarmInfo(); | 
|---|
|  |  |  | CacheUtils.refreshDeviceAlarmInfo(mac); | 
|---|
|  |  |  | //操作日志记录 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); | 
|---|
|  |  |  | StringBuilder content = new StringBuilder(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return deviceInfo; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private  HistoryHourlyMapper historyHourlyMapper; | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData,String code) { | 
|---|
|  |  |  | 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 (govMpInfo.get("guid") != null) { | 
|---|
|  |  |  | aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (code.equals("2")){ | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(aqiMap.get("a21005")) || ObjectUtils.isEmpty(aqiMap.get("a21026")) | 
|---|
|  |  |  | || ObjectUtils.isEmpty(aqiMap.get("a21004")) || ObjectUtils.isEmpty(aqiMap.get("a34002")) | 
|---|
|  |  |  | || ObjectUtils.isEmpty(aqiMap.get("a34004")) || ObjectUtils.isEmpty(aqiMap.get("a05024")) | 
|---|
|  |  |  | || ObjectUtils.isEmpty(aqiMap)){ | 
|---|
|  |  |  | HashMap<String, Object> result = new HashMap<>(); | 
|---|
|  |  |  | String dataTime = deviceData.get("DataTime").toString(); | 
|---|
|  |  |  | Date time = DateUtils.getDate(dataTime, DateUtils.yyyyMMddHHmmss_EN); | 
|---|
|  |  |  | String yearAndMonth = DateUtils.dateToDateString(DateUtils.addHours(time, -2), DateUtils.yyyyMM_EN); | 
|---|
|  |  |  | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); | 
|---|
|  |  |  | String format = simpleDateFormat.format(DateUtils.addHours(time, -2)); | 
|---|
|  |  |  | result.put("timeUnits",yearAndMonth); | 
|---|
|  |  |  | result.put("mac",mac); | 
|---|
|  |  |  | result.put("time",format); | 
|---|
|  |  |  | Map<String, Object> map = historyHourlyMapper.selectHistoryHourly(result); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(map)){ | 
|---|
|  |  |  | return JSON.parseObject(map.get("value").toString(),  Map.class); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void judgeDeviceState(Map<String, Object> deviceData) { | 
|---|
|  |  |  | String mac = deviceData.remove("mac").toString(); | 
|---|
|  |  |  | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //修改设备状态 | 
|---|
|  |  |  | UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>(); | 
|---|
|  |  |  | updateWrapper.eq("id", device.getId()).set("state", String.valueOf(state)); | 
|---|
|  |  |  | deviceMapper.update(null, updateWrapper); | 
|---|
|  |  |  | Device device1 = new Device(); | 
|---|
|  |  |  | device1.setId(device.getId()); | 
|---|
|  |  |  | device1.setState(String.valueOf(state)); | 
|---|
|  |  |  | deviceMapper.updateById(device1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | organizationUnitAlarmMapper.update(null, deleteWrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void setRedisDevice() { | 
|---|
|  |  |  | List<Device> list = lambdaQuery().eq(Device::getIsDelete,0).list(); | 
|---|
|  |  |  | for(Device d : list){ | 
|---|
|  |  |  | redisTemplate.opsForHash().delete(RedisConstants.DEVICE, d.getMac()); | 
|---|
|  |  |  | Map<String, Object> deviceInfo = selectDeviceInfoById(d.getId()); | 
|---|
|  |  |  | setDeviceInfoToRedis(d.getMac(), deviceInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|