use of org.thingsboard.server.common.data.rpc.Rpc in project thingsboard by thingsboard.
the class DeviceActorMessageProcessor method processRpcRequest.
void processRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg) {
ToDeviceRpcRequest request = msg.getMsg();
ToDeviceRpcRequestMsg rpcRequest = creteToDeviceRpcRequestMsg(request);
long timeout = request.getExpirationTime() - System.currentTimeMillis();
boolean persisted = request.isPersisted();
if (timeout <= 0) {
log.debug("[{}][{}] Ignoring message due to exp time reached, {}", deviceId, request.getId(), request.getExpirationTime());
if (persisted) {
createRpc(request, RpcStatus.EXPIRED);
}
return;
} else if (persisted) {
createRpc(request, RpcStatus.QUEUED);
}
boolean sent = false;
if (systemContext.isEdgesEnabled() && edgeId != null) {
log.debug("[{}][{}] device is related to edge [{}]. Saving RPC request to edge queue", tenantId, deviceId, edgeId.getId());
saveRpcRequestToEdgeQueue(request, rpcRequest.getRequestId());
sent = true;
} else if (isSendNewRpcAvailable()) {
sent = rpcSubscriptions.size() > 0;
Set<UUID> syncSessionSet = new HashSet<>();
rpcSubscriptions.forEach((key, value) -> {
sendToTransport(rpcRequest, key, value.getNodeId());
if (SessionType.SYNC == value.getType()) {
syncSessionSet.add(key);
}
});
log.trace("Rpc syncSessionSet [{}] subscription after sent [{}]", syncSessionSet, rpcSubscriptions);
syncSessionSet.forEach(rpcSubscriptions::remove);
}
if (persisted) {
ObjectNode response = JacksonUtil.newObjectNode();
response.put("rpcId", request.getId().toString());
systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), JacksonUtil.toString(response), null));
}
if (!persisted && request.isOneway() && sent) {
log.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId());
systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), null, null));
} else {
registerPendingRpcRequest(context, msg, sent, rpcRequest, timeout);
}
if (sent) {
log.debug("[{}] RPC request {} is sent!", deviceId, request.getId());
} else {
log.debug("[{}] RPC request {} is NOT sent!", deviceId, request.getId());
}
}
use of org.thingsboard.server.common.data.rpc.Rpc in project thingsboard by thingsboard.
the class DeviceActorMessageProcessor method sendPendingRequests.
private void sendPendingRequests(TbActorCtx context, UUID sessionId, String nodeId) {
SessionType sessionType = getSessionType(sessionId);
if (!toDeviceRpcPendingMap.isEmpty()) {
log.debug("[{}] Pushing {} pending RPC messages to new async session [{}]", deviceId, toDeviceRpcPendingMap.size(), sessionId);
if (sessionType == SessionType.SYNC) {
log.debug("[{}] Cleanup sync rpc session [{}]", deviceId, sessionId);
rpcSubscriptions.remove(sessionId);
}
} else {
log.debug("[{}] No pending RPC messages for new async session [{}]", deviceId, sessionId);
}
Set<Integer> sentOneWayIds = new HashSet<>();
if (rpcSequential) {
getFirstRpc().ifPresent(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
} else if (sessionType == SessionType.ASYNC) {
toDeviceRpcPendingMap.entrySet().forEach(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
} else {
toDeviceRpcPendingMap.entrySet().stream().findFirst().ifPresent(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
}
sentOneWayIds.stream().filter(id -> !toDeviceRpcPendingMap.get(id).getMsg().getMsg().isPersisted()).forEach(toDeviceRpcPendingMap::remove);
}
use of org.thingsboard.server.common.data.rpc.Rpc in project thingsboard by thingsboard.
the class BaseRpcControllerTest method testGetRpcsByDeviceId.
@Test
public void testGetRpcsByDeviceId() throws Exception {
Device device = createDefaultDevice();
Device savedDevice = doPost("/api/device", device, Device.class);
ObjectNode rpc = createDefaultRpc();
String result = doPostAsync("/api/rpc/oneway/" + savedDevice.getId().getId().toString(), JacksonUtil.toString(rpc), String.class, status().isOk());
String rpcId = JacksonUtil.fromString(result, JsonNode.class).get("rpcId").asText();
String url = "/api/rpc/persistent/device/" + savedDevice.getId().getId() + "?" + "page=0" + "&" + "pageSize=" + Integer.MAX_VALUE + "&" + "rpcStatus=" + RpcStatus.QUEUED;
MvcResult byDeviceResult = doGetAsync(url).andReturn();
List<Rpc> byDeviceRpcs = JacksonUtil.fromString(byDeviceResult.getResponse().getContentAsString(), new TypeReference<PageData<Rpc>>() {
}).getData();
boolean found = byDeviceRpcs.stream().anyMatch(r -> r.getUuidId().toString().equals(rpcId) && r.getDeviceId().equals(savedDevice.getId()));
Assert.assertTrue(found);
}
use of org.thingsboard.server.common.data.rpc.Rpc in project thingsboard by thingsboard.
the class BaseRpcControllerTest method testDeleteRpc.
@Test
public void testDeleteRpc() throws Exception {
Device device = createDefaultDevice();
Device savedDevice = doPost("/api/device", device, Device.class);
ObjectNode rpc = createDefaultRpc();
String result = doPostAsync("/api/rpc/oneway/" + savedDevice.getId().getId().toString(), JacksonUtil.toString(rpc), String.class, status().isOk());
String rpcId = JacksonUtil.fromString(result, JsonNode.class).get("rpcId").asText();
Rpc savedRpc = getRpcById(rpcId);
MvcResult mvcResult = removeRpcById(savedRpc.getId().getId().toString());
MvcResult res = doGet("/api/rpc/persistent/" + rpcId).andExpect(status().isNotFound()).andReturn();
JsonNode deleteResponse = JacksonUtil.fromString(res.getResponse().getContentAsString(), JsonNode.class);
Assert.assertEquals(404, deleteResponse.get("status").asInt());
String url = "/api/rpc/persistent/device/" + savedDevice.getUuidId().toString() + "?" + "page=0" + "&" + "pageSize=" + Integer.MAX_VALUE + "&" + "rpcStatus=" + RpcStatus.DELETED.name();
MvcResult byDeviceResult = doGet(url).andReturn();
JsonNode byDeviceResponse = JacksonUtil.fromString(byDeviceResult.getResponse().getContentAsString(), JsonNode.class);
Assert.assertEquals(500, byDeviceResponse.get("status").asInt());
}
use of org.thingsboard.server.common.data.rpc.Rpc in project thingsboard by thingsboard.
the class BaseController method checkRpcId.
Rpc checkRpcId(RpcId rpcId, Operation operation) throws ThingsboardException {
try {
validateId(rpcId, "Incorrect rpcId " + rpcId);
Rpc rpc = rpcService.findById(getCurrentUser().getTenantId(), rpcId);
checkNotNull(rpc, "RPC with id [" + rpcId + "] is not found");
accessControlService.checkPermission(getCurrentUser(), Resource.RPC, operation, rpcId, rpc);
return rpc;
} catch (Exception e) {
throw handleException(e, false);
}
}
Aggregations