void ConnectNode::addCmdDataBuffer()

in nlsCppSdk/transport/connectNode.cpp [1483:1571]


void ConnectNode::addCmdDataBuffer(CmdType type, const char *message) {
  char *cmd = NULL;

  LOG_DEBUG("Request(%p) Node(%p) get command type: %s with %s", _request, this,
            getCmdTypeString(type).c_str(),
            getConnectNodeStatusStringLocked().c_str());

  if (_request == NULL) {
    LOG_ERROR("The rquest of node(%p) is nullptr.", this);
    return;
  }
  if (_request->getRequestParam() == NULL) {
    LOG_ERROR("The requestParam of request(%p) node(%p) is nullptr.", _request,
              this);
    return;
  }

  switch (type) {
    case CmdStart:
      if (_reconnection.state == NodeReconnection::TriggerReconnection) {
        // setting tw_time_offset and tw_index_offset
        Json::Value root;
        Json::FastWriter writer;
        root["tw_time_offset"] =
            Json::UInt64(_reconnection.interruption_timestamp_ms -
                         _reconnection.first_audio_timestamp_ms);
        root["tw_index_offset"] = (Json::UInt64)_reconnection.tw_index_offset;
        std::string buf = writer.write(root);
        _request->getRequestParam()->setPayloadParam(buf.c_str());
      }
      cmd = (char *)_request->getRequestParam()->getStartCommand();
      if (_reconnection.state == NodeReconnection::TriggerReconnection) {
        // cleaning tw_time_offset and tw_index_offset
        _request->getRequestParam()->removePayloadParam("tw_time_offset");
        _request->getRequestParam()->removePayloadParam("tw_index_offset");
      }
      _reconnection.state = NodeReconnection::NewReconnectionStarting;
      break;
    case CmdStControl:
      cmd = (char *)_request->getRequestParam()->getControlCommand(message);
      break;
    case CmdStop:
      cmd = (char *)_request->getRequestParam()->getStopCommand();
      break;
    case CmdTextDialog:
      cmd = (char *)_request->getRequestParam()->getExecuteDialog();
      break;
    case CmdWarkWord:
      cmd = (char *)_request->getRequestParam()->getStopWakeWordCommand();
      break;
    case CmdCancel:
      LOG_DEBUG("Node(%p) add cancel command, do nothing.", this);
      return;
    case CmdSendText:
      cmd = (char *)_request->getRequestParam()->getRunFlowingSynthesisCommand(
          message);
      break;
    case CmdSendPing:
      cmd = (char *)"{ping}";
      break;
    case CmdSendFlush:
      cmd = (char *)_request->getRequestParam()->getFlushFlowingTextCommand(
          message);
      break;
    default:
      LOG_WARN("Node(%p) add unknown command, do nothing.", this);
      return;
  }

  if (cmd) {
    std::string buf_str;
    LOG_INFO("Node(%p) get command: %s, and add into evbuffer.", this,
             utility::TextUtils::securityDisposalForLog(cmd, &buf_str,
                                                        "appkey\":\"", 4, 'Z'));

    uint8_t *frame = NULL;
    size_t frameSize = 0;
    if (type == CmdSendPing) {
      _webSocket.pingFrame(&frame, &frameSize);
    } else {
      _webSocket.textFrame((uint8_t *)cmd, strlen(cmd), &frame, &frameSize);
    }

    evbuffer_add(_cmdEvBuffer, (void *)frame, frameSize);

    if (frame) free(frame);
    frame = NULL;
  }
}