模拟量数据上报更新tdengine

This commit is contained in:
huguanghan 2024-11-18 11:50:06 +08:00
parent 5c3b2e95a5
commit 364500bcb8

View File

@ -87,7 +87,6 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
private DataServiceImpl dataService; private DataServiceImpl dataService;
@PostConstruct @PostConstruct
public void init() { public void init() {
//初始化高性能队列 //初始化高性能队列
@ -110,6 +109,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
disruptor.shutdown(); disruptor.shutdown();
} }
} }
public void pushMessage(TerminalMessage msg) { public void pushMessage(TerminalMessage msg) {
RingBuffer<TerminalMessage> ringBuffer = disruptor.getRingBuffer(); RingBuffer<TerminalMessage> ringBuffer = disruptor.getRingBuffer();
if (ringBuffer == null) { if (ringBuffer == null) {
@ -233,7 +233,13 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
JsonNode jsonNode = data.getData(); JsonNode jsonNode = data.getData();
String deviceId = jsonNode.get("deviceId").asText(); String deviceId = jsonNode.get("deviceId").asText();
JsonNode values = jsonNode.get("values"); JsonNode values = jsonNode.get("values");
Long dataTime = jsonNode.get("dataTime").asLong();
Map<String, Object> keyValueMap = new HashMap<>(); Map<String, Object> keyValueMap = new HashMap<>();
DeviceInfoCache deviceInfoCacheById = cacheService.getEquipmentCache().getDeviceInfoCacheById(Long.valueOf(deviceId));
Set<String> highKey = dataService.highIotFieldMap.get(deviceInfoCacheById.getDeviceCode()).keySet();
Set<String> lowKey = dataService.lowIotFieldMap.get(deviceInfoCacheById.getDeviceCode()).keySet();
Map<String, Object> highSpeedValueMap = new HashMap<>();
Map<String, Object> lowSpeedValueMap = new HashMap<>();
//数据入redis //数据入redis
Iterator<String> keysHigh = values.fieldNames(); Iterator<String> keysHigh = values.fieldNames();
@ -241,8 +247,30 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
String fieldName = keysHigh.next(); String fieldName = keysHigh.next();
String key = String.format("RT:%s:%s", deviceId, fieldName.toLowerCase()); String key = String.format("RT:%s:%s", deviceId, fieldName.toLowerCase());
keyValueMap.put(key, values.get(fieldName)); keyValueMap.put(key, values.get(fieldName));
if (highKey.contains(fieldName)){
highSpeedValueMap.put(fieldName,values.get(fieldName));
}
if (lowKey.contains(fieldName)){
lowSpeedValueMap.put(fieldName,values.get(fieldName));
}
} }
adminRedisTemplate.mSet(keyValueMap); adminRedisTemplate.mSet(keyValueMap);
if (jsonNode.get("isStore").asBoolean()) {
//更新td
if (!highSpeedValueMap.isEmpty()){
List<RTData> highSpeedData = new ArrayList<>();
RTData rtHighData = RTData.builder().dataTime(dataTime).deviceId(Long.valueOf(deviceId)).values(highSpeedValueMap).build();
highSpeedData.add(rtHighData);
tdEngineService.updateYCHighValues(highSpeedData,deviceInfoCacheById.getDeviceCode());
}
if (!lowSpeedValueMap.isEmpty()){
List<RTData> lowSpeedData = new ArrayList<>();
RTData rtLowData = RTData.builder().dataTime(dataTime).deviceId(Long.valueOf(deviceId)).values(lowSpeedValueMap).build();
lowSpeedData.add(rtLowData);
tdEngineService.updateYCLowValues(lowSpeedData,deviceInfoCacheById.getDeviceCode());
}
}
} }
@Override @Override
@ -336,8 +364,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
} else { } else {
deviceEventInfo.setEventText(fieldName + " 动作"); deviceEventInfo.setEventText(fieldName + " 动作");
} }
} } else {
else{
deviceEventInfo.setEventText(fieldName + eventType + ",属性值为:" + item.getAttrValue() + ",越限值为:" + item.getLimitValue()); deviceEventInfo.setEventText(fieldName + eventType + ",属性值为:" + item.getAttrValue() + ",越限值为:" + item.getLimitValue());
} }
valueList.add(deviceEventInfo); valueList.add(deviceEventInfo);
@ -392,6 +419,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
/** /**
* 收到节点Websocket报文回调函数 * 收到节点Websocket报文回调函数
*
* @param session websocket session * @param session websocket session
* @param message 报文内容 * @param message 报文内容
* @throws Exception * @throws Exception
@ -406,8 +434,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
//如果是应答报文跳过队列直接异步返回 //如果是应答报文跳过队列直接异步返回
if (responseCallback.contains(msg.getCmdId())) { if (responseCallback.contains(msg.getCmdId())) {
responseCallback.get(msg.getCmdId()).complete(msg); responseCallback.get(msg.getCmdId()).complete(msg);
} } else {
else{
//如果是主动请求报文加入队列等待处理 //如果是主动请求报文加入队列等待处理
String nodeId = session.getAttributes().get(NodeConstant.NODE_ID).toString(); String nodeId = session.getAttributes().get(NodeConstant.NODE_ID).toString();
String cmd = msg.getCmd(); String cmd = msg.getCmd();
@ -465,25 +492,27 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
/** /**
* 发送ping消息 * 发送ping消息
*
* @param session * @param session
*/ */
private void SendPingMessage(ConcurrentWebSocketSessionDecorator session) { private void SendPingMessage(ConcurrentWebSocketSessionDecorator session) {
try { try {
session.sendMessage(new PingMessage()); session.sendMessage(new PingMessage());
} catch (Exception ignore) {
} }
catch (Exception ignore){}
} }
private void closeSession(WebSocketSession session) { private void closeSession(WebSocketSession session) {
try { try {
session.close(CloseStatus.NO_CLOSE_FRAME); session.close(CloseStatus.NO_CLOSE_FRAME);
} catch (Exception ignore) {
} }
catch (Exception ignore){}
} }
/** /**
* 向指定采集节点发送指令(无返回值) * 向指定采集节点发送指令(无返回值)
*
* @param nodeId 节点ID * @param nodeId 节点ID
* @param message 指令 * @param message 指令
*/ */
@ -493,8 +522,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
try { try {
session.sendMessage(new TextMessage(message.toJsonString())); session.sendMessage(new TextMessage(message.toJsonString()));
log.info("发送的消息为:{}", message.toJsonString()); log.info("发送的消息为:{}", message.toJsonString());
} } catch (Exception exception) {
catch (Exception exception){
log.error(String.format("发送消息失败: NodeId: %s", nodeId), exception); log.error(String.format("发送消息失败: NodeId: %s", nodeId), exception);
closeSession(session); closeSession(session);
} }
@ -503,6 +531,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
/** /**
* 向指定采集节点发送指令(无返回值) * 向指定采集节点发送指令(无返回值)
*
* @param nodeId 节点ID * @param nodeId 节点ID
* @param message 指令 * @param message 指令
* @return * @return
@ -517,6 +546,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
/** /**
* 向指定采集节点发送指令(有返回值) * 向指定采集节点发送指令(有返回值)
*
* @param nodeId 节点ID * @param nodeId 节点ID
* @param message 指令 * @param message 指令
* @param timeout 超时时间 * @param timeout 超时时间