package com.moral.monitor.listener;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.TypeReference;
|
import com.moral.monitor.dao.JobDao;
|
import com.moral.monitor.util.RedisUtil;
|
import org.slf4j.Logger;
|
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.connection.CachingConnectionFactory;
|
import org.springframework.amqp.rabbit.connection.Connection;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import javax.annotation.Resource;
|
import java.io.UnsupportedEncodingException;
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Random;
|
|
public class TaskListener implements MessageListener {
|
|
@Resource
|
RabbitTemplate rabbitTemplate;
|
|
@Resource
|
JobDao jobDao;
|
|
@Resource
|
RedisTemplate<String, String> redisTemplate;
|
|
private Logger logger = LoggerFactory.getLogger(TaskListener.class);
|
|
@Override
|
public void onMessage(Message msg) {
|
|
String message = null;
|
try {
|
message = new String(msg.getBody(), "utf-8");
|
} catch (UnsupportedEncodingException e) {
|
logger.warn(e.getMessage());
|
}
|
|
Map<String, String> msgData = JSON.parseObject(message, new TypeReference<Map<String, String>>() {});
|
String mac = msgData.get("mac");
|
|
Map<String, Float> adjustMap;
|
if(RedisUtil.hasKey(redisTemplate, mac)) {
|
adjustMap = JSON.parseObject(RedisUtil.get(redisTemplate, mac), new TypeReference<Map<String, Float>>() {});
|
} else {
|
adjustMap = getAdjustData(mac);
|
RedisUtil.set(redisTemplate, mac, JSON.toJSONString(adjustMap));
|
}
|
|
if(!adjustMap.isEmpty()) {
|
for (Map.Entry<String, Float> entry : adjustMap.entrySet()) {
|
String key = entry.getKey();
|
if(msgData.containsKey(key)) {
|
Float value = entry.getValue();
|
Float dataValue = Float.valueOf(msgData.get(key)) ;
|
msgData.put(key, String.valueOf(dataValue + value));
|
}
|
}
|
}
|
|
System.out.println(message);
|
System.out.println(JSON.toJSONString(msgData));
|
|
rabbitTemplate.convertAndSend("monitors_data_2", "", JSON.toJSONString(msgData).getBytes());
|
// rabbitTemplate.send("monitors_data_3", "", new Message(JSON.toJSONString(msgData).getBytes(), new MessageProperties()));
|
|
int state = (new Random()).nextInt(4) % 5;
|
rabbitTemplate.convertAndSend("monitors_alarm", "", "{\"mac\": \"" + mac + "\", \"state\": " + state + "}");
|
|
jobDao.updateStateByMac(mac, state);
|
}
|
|
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;
|
}
|
}
|