#include <softbus_daemon/command/CommandDispatcher.h>

#include <softbus_core/logging/Logger.h>

namespace softbus::daemon {

CommandDispatcher::CommandDispatcher(softbus::transport::TcpUplinkServer* uplink)
    : uplink_(uplink)
{
}

bool CommandDispatcher::dispatch(const softbus::api::ExecuteCommand& command)
{
    std::string error;
    if (!command.validate(&error)) {
        SB_LOG_ERROR_TRACE("CommandDispatcher", command.traceId,
            "Invalid executeCommand: ", error);
        return false;
    }

    if (!uplink_) {
        return false;
    }

    const bool ok = uplink_->sendCommand(command);
    SB_LOG_INFO_TRACE("CommandDispatcher", command.traceId,
        "executeCommand dispatched to Edge objectRef=", command.objectRef,
        " command=", command.command, " success=", ok ? "true" : "false");
    return ok;
}

} // namespace softbus::daemon
