use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest 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.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.
the class DefaultTbCoreDeviceRpcService method forwardRpcRequestToDeviceActor.
@Override
public void forwardRpcRequestToDeviceActor(ToDeviceRpcRequestActorMsg rpcMsg) {
ToDeviceRpcRequest request = rpcMsg.getMsg();
log.trace("[{}][{}] Processing local rpc call to device actor [{}]", request.getTenantId(), request.getId(), request.getDeviceId());
UUID requestId = request.getId();
localToDeviceRpcRequests.put(requestId, rpcMsg);
actorContext.tellWithHighPriority(rpcMsg);
scheduleToDeviceTimeout(request, requestId);
}
use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.
the class DefaultTbRuleEngineRpcService method sendRpcRequestToDevice.
@Override
public void sendRpcRequestToDevice(RuleEngineDeviceRpcRequest src, Consumer<RuleEngineDeviceRpcResponse> consumer) {
ToDeviceRpcRequest request = new ToDeviceRpcRequest(src.getRequestUUID(), src.getTenantId(), src.getDeviceId(), src.isOneway(), src.getExpirationTime(), new ToDeviceRpcRequestBody(src.getMethod(), src.getBody()), src.isPersisted(), src.getRetries(), src.getAdditionalInfo());
forwardRpcRequestToDeviceActor(request, response -> {
if (src.isRestApiCall()) {
sendRpcResponseToTbCore(src.getOriginServiceId(), response);
}
consumer.accept(RuleEngineDeviceRpcResponse.builder().deviceId(src.getDeviceId()).requestId(src.getRequestId()).error(response.getError()).response(response.getResponse()).build());
});
}
use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.
the class DefaultGatewayNotificationsService method onDeviceDeleted.
@Override
public void onDeviceDeleted(Device device) {
Optional<DeviceId> gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device);
if (gatewayDeviceId.isPresent()) {
TextNode deletedDeviceNode = new TextNode(device.getName());
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(device.getTenantId(), gatewayDeviceId.get(), deletedDeviceNode, DEVICE_DELETED_METHOD_NAME);
deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> {
log.trace("Device deleted RPC with id: [{}] processed to gateway device with id: [{}], deleted device name: [{}]", rpcRequest.getId(), gatewayDeviceId, device.getName());
}, null);
}
}
use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.
the class DefaultGatewayNotificationsService method onDeviceUpdated.
@Override
public void onDeviceUpdated(Device device, Device oldDevice) {
Optional<DeviceId> gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device);
if (gatewayDeviceId.isPresent()) {
ObjectNode renamedDeviceNode = JacksonUtil.newObjectNode();
renamedDeviceNode.put(oldDevice.getName(), device.getName());
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(device.getTenantId(), gatewayDeviceId.get(), renamedDeviceNode, DEVICE_RENAMED_METHOD_NAME);
deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> {
log.trace("Device renamed RPC with id: [{}] processed to gateway device with id: [{}], old device name: [{}], new device name: [{}]", rpcRequest.getId(), gatewayDeviceId, oldDevice.getName(), device.getName());
}, null);
}
}
Aggregations