| | |
| | | import org.slf4j.LoggerFactory;
|
| | | import org.springframework.amqp.core.Message;
|
| | | import org.springframework.amqp.core.MessageListener;
|
| | | import org.springframework.amqp.core.MessageProperties;
|
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
| | | import org.springframework.beans.factory.annotation.Value;
|
| | | import org.springframework.core.io.ClassPathResource;
|
| | |
| | | import com.moral.monitor.dao.TaskDao;
|
| | | import com.moral.monitor.entity.History;
|
| | | import com.moral.monitor.entity.Sensor;
|
| | | import com.moral.monitor.service.RedisService;
|
| | | import com.moral.monitor.util.RedisUtil;
|
| | |
|
| | | public class TaskListener implements MessageListener {
|
| | |
| | | protected MongoTemplate mongoTemplate;
|
| | |
|
| | | private Logger logger = LoggerFactory.getLogger(TaskListener.class);
|
| | |
|
| | | @Resource
|
| | | RedisService redisService;
|
| | | @Override
|
| | | public void onMessage(Message msg) {
|
| | |
|
| | |
| | | if(StringUtils.isEmpty(ver) || StringUtils.isEmpty(mac)) {
|
| | | return;
|
| | | }
|
| | |
|
| | | Map<String, Float> adjustMap;
|
| | | String adjust_key = "adjust_" + mac;
|
| | | if(RedisUtil.hasKey(redisTemplate, adjust_key)) {
|
| | | adjustMap = JSON.parseObject(RedisUtil.get(redisTemplate, adjust_key), new TypeReference<Map<String, Float>>() {});
|
| | | } else {
|
| | | adjustMap = getAdjustData(mac);
|
| | | RedisUtil.set(redisTemplate, adjust_key, JSON.toJSONString(adjustMap));
|
| | | }
|
| | |
|
| | | //获取缓存中的校准值map
|
| | | Map<String, Float> adjustMap = redisService.getAdjustsByMac(mac);
|
| | | if(!adjustMap.isEmpty()) {
|
| | | for (Map.Entry<String, Float> entry : adjustMap.entrySet()) {
|
| | | String key = entry.getKey();
|
| | |
| | | //mongoTemplate.insert(JSON.toJSONString(histories), "data");
|
| | | mongoTemplate.insert(new_message, "data");
|
| | | }
|
| | |
|
| | | private Map<String, Float> getAdjustData(String mac) {
|
| | | List<Map<String, String>> adjusts = jobDao.findAdjustByMac(mac);
|
| | | Map<String, Float> dataMap = new HashMap<String, Float>();
|
| | | for (int i = 0; i < adjusts.size(); i++) {
|
| | | Map adjust = adjusts.get(i);
|
| | | if(adjust.get("value") != null) {
|
| | | String key = String.valueOf(adjust.get("key"));
|
| | | String data = String.valueOf(adjust.get("value"));
|
| | | BigDecimal value = new BigDecimal(data);
|
| | | value.setScale(3, BigDecimal.ROUND_HALF_UP);
|
| | | dataMap.put(key, value.floatValue());
|
| | | }
|
| | | }
|
| | | return dataMap;
|
| | | }
|
| | | //三级警报值阀值
|
| | | private static Map<String,Double[]> alarmLevles =new HashMap<String, Double[]>();
|
| | | /*
|
| | |
| | | //根据三级警报阀值,确定设备状态
|
| | | private int detEquState(Map<String,String> data) {
|
| | | int state = 0;
|
| | | Map<String, Object> equMap = new HashMap<String, Object>();
|
| | | if(data!=null) {
|
| | | for(String key:alarmLevles.keySet()) {
|
| | | String value = data.get(key);
|
| | |
| | | if(val>=arr[index]) {
|
| | | //如果当前状态级别更高就取当前状态。否则保持之前状态
|
| | | state = (index>state)?index:state;
|
| | | break;
|
| | | String e_key = "level"+Integer.toString(index);
|
| | | Object e_States = equMap.get(e_key);
|
| | | if(e_States!=null&&e_States instanceof List) {
|
| | | ((List)e_States).add(key);
|
| | | }else {
|
| | | e_States = new ArrayList<String>();
|
| | | equMap.put(e_key, e_States);
|
| | | ((List)e_States).add(key);
|
| | | |
| | | }
|
| | | equMap.put(e_key, e_States);
|
| | | break;
|
| | | }
|
| | | }
|
| | | //当前状态到达最大级别不再遍历,跳出循环
|
| | | if(state == (arr.length-1)) {
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | equMap.put("state", state);//设备状态 |
| | | redisService.setEquState(data.get("mac"),equMap);
|
| | | return state;
|
| | | }
|
| | | private String getDate() {
|
| | | Date d = new Date();
|
| | | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | return formatter.format(d);
|
| | | }
|
| | | }
|