Search in sources :

Example 6 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method sendMsgToSessionActor.

private void sendMsgToSessionActor(ToDeviceSessionActorMsg response, Optional<ServerAddress> sessionAddress) {
    if (sessionAddress.isPresent()) {
        ServerAddress address = sessionAddress.get();
        logger.debug("{} Forwarding msg: {}", address, response);
        systemContext.getRpcService().tell(sessionAddress.get(), response);
    } else {
        systemContext.getSessionManagerActor().tell(response, ActorRef.noSender());
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress)

Example 7 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processRpcResponses.

void processRpcResponses(ActorContext context, ToDeviceActorMsg msg) {
    SessionId sessionId = msg.getSessionId();
    FromDeviceMsg inMsg = msg.getPayload();
    if (inMsg.getMsgType() == MsgType.TO_DEVICE_RPC_RESPONSE) {
        logger.debug("[{}] Processing rpc command response [{}]", deviceId, sessionId);
        ToDeviceRpcResponseMsg responseMsg = (ToDeviceRpcResponseMsg) inMsg;
        ToDeviceRpcRequestMetadata requestMd = rpcPendingMap.remove(responseMsg.getRequestId());
        boolean success = requestMd != null;
        if (success) {
            ToPluginRpcResponseDeviceMsg responsePluginMsg = toPluginRpcResponseMsg(requestMd.getMsg(), responseMsg.getData());
            Optional<ServerAddress> pluginServerAddress = requestMd.getMsg().getServerAddress();
            if (pluginServerAddress.isPresent()) {
                systemContext.getRpcService().tell(pluginServerAddress.get(), responsePluginMsg);
                logger.debug("[{}] Rpc command response sent to remote plugin actor [{}]!", deviceId, requestMd.getMsg().getMsg().getId());
            } else {
                context.parent().tell(responsePluginMsg, ActorRef.noSender());
                logger.debug("[{}] Rpc command response sent to local plugin actor [{}]!", deviceId, requestMd.getMsg().getMsg().getId());
            }
        } else {
            logger.debug("[{}] Rpc command response [{}] is stale!", deviceId, responseMsg.getRequestId());
        }
        if (msg.getSessionType() == SessionType.SYNC) {
            BasicCommandAckResponse response = success ? BasicCommandAckResponse.onSuccess(MsgType.TO_DEVICE_RPC_REQUEST, responseMsg.getRequestId()) : BasicCommandAckResponse.onError(MsgType.TO_DEVICE_RPC_REQUEST, responseMsg.getRequestId(), new TimeoutException());
            sendMsgToSessionActor(new BasicToDeviceSessionActorMsg(response, msg.getSessionId()), msg.getServerAddress());
        }
    }
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) SessionId(org.thingsboard.server.common.data.id.SessionId) ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class DefaultActorService method onDeviceNameOrTypeUpdate.

@Override
public void onDeviceNameOrTypeUpdate(TenantId tenantId, DeviceId deviceId, String deviceName, String deviceType) {
    log.trace("[{}] Processing onDeviceNameOrTypeUpdate event, deviceName: {}, deviceType: {}", deviceId, deviceName, deviceType);
    DeviceNameOrTypeUpdateMsg msg = new DeviceNameOrTypeUpdateMsg(tenantId, deviceId, deviceName, deviceType);
    Optional<ServerAddress> address = actorContext.getRoutingService().resolveById(deviceId);
    if (address.isPresent()) {
        rpcService.tell(address.get(), msg);
    } else {
        onMsg(msg);
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) DeviceNameOrTypeUpdateMsg(org.thingsboard.server.extensions.api.device.DeviceNameOrTypeUpdateMsg)

Example 9 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class DefaultActorService method onCredentialsUpdate.

@Override
public void onCredentialsUpdate(TenantId tenantId, DeviceId deviceId) {
    DeviceCredentialsUpdateNotificationMsg msg = new DeviceCredentialsUpdateNotificationMsg(tenantId, deviceId);
    Optional<ServerAddress> address = actorContext.getRoutingService().resolveById(deviceId);
    if (address.isPresent()) {
        rpcService.tell(address.get(), msg);
    } else {
        onMsg(msg);
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) DeviceCredentialsUpdateNotificationMsg(org.thingsboard.server.extensions.api.device.DeviceCredentialsUpdateNotificationMsg)

Example 10 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class RpcManagerActor method onMsg.

private void onMsg(RpcSessionTellMsg msg) {
    ServerAddress address = msg.getServerAddress();
    SessionActorInfo session = sessionActors.get(address);
    if (session != null) {
        log.debug("{} Forwarding msg to session actor", address);
        session.actor.tell(msg, ActorRef.noSender());
    } else {
        log.debug("{} Storing msg to pending queue", address);
        Queue<ClusterAPIProtos.ToRpcServerMessage> queue = pendingMsgs.get(address);
        if (queue == null) {
            queue = new LinkedList<>();
            pendingMsgs.put(address, queue);
        }
        queue.add(msg.getMsg());
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress)

Aggregations

ServerAddress (org.thingsboard.server.common.msg.cluster.ServerAddress)11 Subscription (org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)4 Channel (io.grpc.Channel)1 java.util (java.util)1 TimeoutException (java.util.concurrent.TimeoutException)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.springframework.util.StringUtils)1 DataConstants (org.thingsboard.server.common.data.DataConstants)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 SessionId (org.thingsboard.server.common.data.id.SessionId)1 org.thingsboard.server.common.data.kv (org.thingsboard.server.common.data.kv)1 FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)1 DeviceCredentialsUpdateNotificationMsg (org.thingsboard.server.extensions.api.device.DeviceCredentialsUpdateNotificationMsg)1 DeviceNameOrTypeUpdateMsg (org.thingsboard.server.extensions.api.device.DeviceNameOrTypeUpdateMsg)1 PluginCallback (org.thingsboard.server.extensions.api.plugins.PluginCallback)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1