Updated, according to changes, removed request for device, updated timeout for RPC to 1 day

This commit is contained in:
zbeacon 2022-01-14 16:38:22 +02:00
parent d1f4a025c3
commit dbbdbd80bd

View File

@ -1,12 +1,12 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,7 +21,6 @@ import com.fasterxml.jackson.databind.node.TextNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.thingsboard.common.util.JacksonUtil;
@ -31,10 +30,10 @@ import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@ -43,49 +42,45 @@ public class DefaultGatewayDeviceStateService implements GatewayDeviceStateServi
private final static String DEVICE_RENAMED_METHOD_NAME = "gateway_device_renamed";
private final static String DEVICE_DELETED_METHOD_NAME = "gateway_device_deleted";
private final DeviceService deviceService;
@Value("${server.rest.server_side_rpc.min_timeout:5000}")
protected long minTimeout;
@Value("${server.rest.server_side_rpc.default_timeout:10000}")
protected long defaultTimeout;
private final static Long rpcTimeout = TimeUnit.DAYS.toMillis(1);
@Lazy
@Autowired
private TbCoreDeviceRpcService deviceRpcService;
@Override
public void update(Device device, Device oldDevice) {
Device gatewayDevice = findGatewayDeviceByAdditionalInfoInDevice(device.getTenantId(), device.getAdditionalInfo());
if (gatewayDevice != null) {
DeviceId gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device.getAdditionalInfo());
if (gatewayDeviceId != null) {
ObjectNode renamedDeviceNode = JacksonUtil.newObjectNode();
renamedDeviceNode.put(oldDevice.getName(), device.getName());
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(gatewayDevice, renamedDeviceNode, DEVICE_RENAMED_METHOD_NAME);
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(device.getTenantId(), gatewayDeviceId, 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(), gatewayDevice.getId(), oldDevice.getName(), device.getName());
rpcRequest.getId(), gatewayDeviceId, oldDevice.getName(), device.getName());
}, null);
}
}
@Override
public void delete(Device device) {
Device gatewayDevice = findGatewayDeviceByAdditionalInfoInDevice(device.getTenantId(), device.getAdditionalInfo());
if (gatewayDevice != null) {
DeviceId gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device.getAdditionalInfo());
if (gatewayDeviceId != null) {
TextNode deletedDeviceNode = new TextNode(device.getName());
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(gatewayDevice, deletedDeviceNode, DEVICE_DELETED_METHOD_NAME);
ToDeviceRpcRequest rpcRequest = formDeviceToGatewayRPCRequest(device.getTenantId(), gatewayDeviceId, 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(), gatewayDevice.getId(), device.getName());
rpcRequest.getId(), gatewayDeviceId, device.getName());
}, null);
}
}
private ToDeviceRpcRequest formDeviceToGatewayRPCRequest(Device gatewayDevice, JsonNode deviceDataNode, String method) {
private ToDeviceRpcRequest formDeviceToGatewayRPCRequest(TenantId tenantId, DeviceId gatewayDeviceId, JsonNode deviceDataNode, String method) {
ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(method, JacksonUtil.toString(deviceDataNode));
long expTime = System.currentTimeMillis() + Math.max(minTimeout, defaultTimeout);
long expTime = System.currentTimeMillis() + rpcTimeout;
UUID rpcRequestUUID = UUID.randomUUID();
return new ToDeviceRpcRequest(rpcRequestUUID,
gatewayDevice.getTenantId(),
gatewayDevice.getId(),
tenantId,
gatewayDeviceId,
true,
expTime,
body,
@ -95,11 +90,10 @@ public class DefaultGatewayDeviceStateService implements GatewayDeviceStateServi
);
}
private Device findGatewayDeviceByAdditionalInfoInDevice(TenantId tenantId, JsonNode deviceAdditionalInfo) {
private DeviceId getGatewayDeviceIdFromAdditionalInfoInDevice(JsonNode deviceAdditionalInfo) {
if (deviceAdditionalInfo != null && deviceAdditionalInfo.has(DataConstants.LAST_CONNECTED_GATEWAY)) {
JsonNode lastConnectedGatewayIdNode = deviceAdditionalInfo.get(DataConstants.LAST_CONNECTED_GATEWAY);
DeviceId gatewayId = new DeviceId(UUID.fromString(lastConnectedGatewayIdNode.asText()));
return deviceService.findDeviceById(tenantId, gatewayId);
return new DeviceId(UUID.fromString(lastConnectedGatewayIdNode.asText()));
}
return null;
}