Fix server-side RPC requests handling
This commit is contained in:
parent
c905f20224
commit
0f5fd481fc
@ -1253,21 +1253,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement
|
||||
log.trace("[{}] Received RPC command to device", sessionId);
|
||||
try {
|
||||
if (sparkplugSessionHandler != null) {
|
||||
/**
|
||||
* NCMD {"metricName":"MyNodeMetric05_String","value":"MyNodeMetric05_String_Value"}
|
||||
* NCMD {"metricName":"MyNodeMetric02_LongInt64","value":2814119464032075444}
|
||||
* NCMD {"metricName":"MyNodeMetric03_Double","value":6336935578763180333}
|
||||
* NCMD {"metricName":"MyNodeMetric04_Float","value":413.18222}
|
||||
* NCMD {"metricName":"Node Control/Rebirth","value":false}
|
||||
* NCMD {"metricName":"MyNodeMetric06_Json_Bytes", "value":[40,47,-49]}
|
||||
*/
|
||||
SparkplugMessageType messageType = SparkplugMessageType.parseMessageType(rpcRequest.getMethodName());
|
||||
if (messageType == null) {
|
||||
this.sendErrorRpcResponse(deviceSessionCtx.getSessionInfo(), rpcRequest.getRequestId(),
|
||||
ThingsboardErrorCode.INVALID_ARGUMENTS, "Unsupported SparkplugMessageType: " + rpcRequest.getMethodName() + rpcRequest.getParams());
|
||||
return;
|
||||
handleToSparkplugDeviceRpcRequest(rpcRequest);
|
||||
} else {
|
||||
String baseTopic = rpcSubTopicType.getRpcRequestTopicBase();
|
||||
MqttTransportAdaptor adaptor = deviceSessionCtx.getAdaptor(rpcSubTopicType);
|
||||
adaptor.convertToPublish(deviceSessionCtx, rpcRequest, baseTopic)
|
||||
.ifPresent(payload -> sendToDeviceRpcRequest(payload, rpcRequest, deviceSessionCtx.getSessionInfo()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.trace("[{}] Failed to convert device RPC command to MQTT msg", sessionId, e);
|
||||
this.sendErrorRpcResponse(deviceSessionCtx.getSessionInfo(), rpcRequest.getRequestId(),
|
||||
ThingsboardErrorCode.INVALID_ARGUMENTS,
|
||||
"Failed to convert device RPC command to MQTT msg: " + rpcRequest.getMethodName() + rpcRequest.getParams());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleToSparkplugDeviceRpcRequest(TransportProtos.ToDeviceRpcRequestMsg rpcRequest) throws ThingsboardException {
|
||||
SparkplugMessageType messageType = SparkplugMessageType.parseMessageType(rpcRequest.getMethodName());
|
||||
SparkplugRpcRequestHeader header;
|
||||
if (StringUtils.isNotEmpty(rpcRequest.getParams())) {
|
||||
header = JacksonUtil.fromString(rpcRequest.getParams(), SparkplugRpcRequestHeader.class);
|
||||
} else {
|
||||
header = new SparkplugRpcRequestHeader();
|
||||
}
|
||||
SparkplugRpcRequestHeader header = JacksonUtil.fromString(rpcRequest.getParams(), SparkplugRpcRequestHeader.class);
|
||||
header.setMessageType(messageType.name());
|
||||
TransportProtos.TsKvProto tsKvProto = getTsKvProto(header.getMetricName(), header.getValue(), new Date().getTime());
|
||||
if (sparkplugSessionHandler.getNodeBirthMetrics().containsKey(tsKvProto.getKv().getKey())) {
|
||||
@ -1282,18 +1290,6 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement
|
||||
ThingsboardErrorCode.BAD_REQUEST_PARAMS, "Failed send To Node Rpc Request: " +
|
||||
rpcRequest.getMethodName() + ". This node does not have a metricName: [" + tsKvProto.getKv().getKey() + "]");
|
||||
}
|
||||
} else {
|
||||
String baseTopic = rpcSubTopicType.getRpcRequestTopicBase();
|
||||
MqttTransportAdaptor adaptor = deviceSessionCtx.getAdaptor(rpcSubTopicType);
|
||||
adaptor.convertToPublish(deviceSessionCtx, rpcRequest, baseTopic)
|
||||
.ifPresent(payload -> sendToDeviceRpcRequest(payload, rpcRequest, deviceSessionCtx.getSessionInfo()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.trace("[{}] Failed to convert device RPC command to MQTT msg", sessionId, e);
|
||||
this.sendErrorRpcResponse(deviceSessionCtx.getSessionInfo(), rpcRequest.getRequestId(),
|
||||
ThingsboardErrorCode.INVALID_ARGUMENTS,
|
||||
"Failed to convert device RPC command to MQTT msg: " + rpcRequest.getMethodName() + rpcRequest.getParams());
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToDeviceRpcRequest(MqttMessage payload, TransportProtos.ToDeviceRpcRequestMsg rpcRequest, TransportProtos.SessionInfoProto sessionInfo) {
|
||||
@ -1315,7 +1311,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement
|
||||
} else if (rpcRequest.getPersisted()) {
|
||||
transportService.process(sessionInfo, rpcRequest, RpcStatus.SENT, TransportServiceCallback.EMPTY);
|
||||
}
|
||||
if (sparkplugSessionHandler != null) {
|
||||
this.sendSuccessRpcResponse(sessionInfo, rpcRequest.getRequestId(), ResponseCode.CONTENT, "Success: " + rpcRequest.getMethodName());
|
||||
}
|
||||
} else {
|
||||
log.trace("[{}] Failed send To Device Rpc Request [{}]", sessionId, rpcRequest.getMethodName());
|
||||
this.sendErrorRpcResponse(sessionInfo, rpcRequest.getRequestId(),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user