Updated, according to changes, removed request for device, updated timeout for RPC to 1 day
This commit is contained in:
parent
d1f4a025c3
commit
dbbdbd80bd
@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright © 2016-2021 The Thingsboard Authors
|
* Copyright © 2016-2021 The Thingsboard Authors
|
||||||
*
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* 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.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
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.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
|
import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
|
||||||
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
|
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
|
||||||
import org.thingsboard.server.dao.device.DeviceService;
|
|
||||||
import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
|
import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@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_RENAMED_METHOD_NAME = "gateway_device_renamed";
|
||||||
private final static String DEVICE_DELETED_METHOD_NAME = "gateway_device_deleted";
|
private final static String DEVICE_DELETED_METHOD_NAME = "gateway_device_deleted";
|
||||||
private final DeviceService deviceService;
|
private final static Long rpcTimeout = TimeUnit.DAYS.toMillis(1);
|
||||||
@Value("${server.rest.server_side_rpc.min_timeout:5000}")
|
|
||||||
protected long minTimeout;
|
|
||||||
@Value("${server.rest.server_side_rpc.default_timeout:10000}")
|
|
||||||
protected long defaultTimeout;
|
|
||||||
@Lazy
|
@Lazy
|
||||||
@Autowired
|
@Autowired
|
||||||
private TbCoreDeviceRpcService deviceRpcService;
|
private TbCoreDeviceRpcService deviceRpcService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Device device, Device oldDevice) {
|
public void update(Device device, Device oldDevice) {
|
||||||
Device gatewayDevice = findGatewayDeviceByAdditionalInfoInDevice(device.getTenantId(), device.getAdditionalInfo());
|
DeviceId gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device.getAdditionalInfo());
|
||||||
if (gatewayDevice != null) {
|
if (gatewayDeviceId != null) {
|
||||||
ObjectNode renamedDeviceNode = JacksonUtil.newObjectNode();
|
ObjectNode renamedDeviceNode = JacksonUtil.newObjectNode();
|
||||||
renamedDeviceNode.put(oldDevice.getName(), device.getName());
|
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 -> {
|
deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> {
|
||||||
log.trace("Device renamed RPC with id: [{}] processed to gateway device with id: [{}], old device name: [{}], new device name: [{}]",
|
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);
|
}, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(Device device) {
|
public void delete(Device device) {
|
||||||
Device gatewayDevice = findGatewayDeviceByAdditionalInfoInDevice(device.getTenantId(), device.getAdditionalInfo());
|
DeviceId gatewayDeviceId = getGatewayDeviceIdFromAdditionalInfoInDevice(device.getAdditionalInfo());
|
||||||
if (gatewayDevice != null) {
|
if (gatewayDeviceId != null) {
|
||||||
TextNode deletedDeviceNode = new TextNode(device.getName());
|
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 -> {
|
deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> {
|
||||||
log.trace("Device deleted RPC with id: [{}] processed to gateway device with id: [{}], deleted device name: [{}]",
|
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);
|
}, 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));
|
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();
|
UUID rpcRequestUUID = UUID.randomUUID();
|
||||||
return new ToDeviceRpcRequest(rpcRequestUUID,
|
return new ToDeviceRpcRequest(rpcRequestUUID,
|
||||||
gatewayDevice.getTenantId(),
|
tenantId,
|
||||||
gatewayDevice.getId(),
|
gatewayDeviceId,
|
||||||
true,
|
true,
|
||||||
expTime,
|
expTime,
|
||||||
body,
|
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)) {
|
if (deviceAdditionalInfo != null && deviceAdditionalInfo.has(DataConstants.LAST_CONNECTED_GATEWAY)) {
|
||||||
JsonNode lastConnectedGatewayIdNode = deviceAdditionalInfo.get(DataConstants.LAST_CONNECTED_GATEWAY);
|
JsonNode lastConnectedGatewayIdNode = deviceAdditionalInfo.get(DataConstants.LAST_CONNECTED_GATEWAY);
|
||||||
DeviceId gatewayId = new DeviceId(UUID.fromString(lastConnectedGatewayIdNode.asText()));
|
return new DeviceId(UUID.fromString(lastConnectedGatewayIdNode.asText()));
|
||||||
return deviceService.findDeviceById(tenantId, gatewayId);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user