Пример #1
0
bool PubSubClient::_process_message(MQTT::Message* msg, uint8_t match_type, uint16_t match_pid) {
  lastInActivity = millis();
  if (msg->type() == match_type) {
    if (match_pid)
      return msg->packet_id() == match_pid;
    return true;
  }

  switch (msg->type()) {
  case MQTTPUBLISH:
    {
      auto pub = (MQTT::Publish*)msg;

      if (_callback)
	_callback(*pub);

      if (pub->qos() == 1) {
	MQTT::PublishAck puback(pub->packet_id());
	puback.send(_client);
	lastOutActivity = millis();

      } else if (pub->qos() == 2) {
	uint8_t retries = 0;

	{
	  MQTT::PublishRec pubrec(pub->packet_id());
	  if (!send_reliably(&pubrec))
	    return false;
	}

	{
	  MQTT::PublishComp pubcomp(pub->packet_id());
	  pubcomp.send(_client);
	  lastOutActivity = millis();
	}
      }
    }
    break;

  case MQTTPINGREQ:
    {
      MQTT::PingResp pr;
      pr.send(_client);
      lastOutActivity = millis();
    }
    break;

  case MQTTPINGRESP:
    pingOutstanding = false;
  }

  return false;
}
Пример #2
0
void PubSubClient::_process_message(MQTT::Message* msg) {
  switch (msg->type()) {
  case MQTT::PUBLISH:
    {
      MQTT::Publish *pub = static_cast<MQTT::Publish*>(msg);	// RTTI is disabled on embedded, so no dynamic_cast<>()

      if (_callback)
	_callback(*pub);

      if (pub->qos() == 1) {
	MQTT::PublishAck puback(pub->packet_id());
	puback.send(*_client);
	lastOutActivity = millis();

      } else if (pub->qos() == 2) {
	uint8_t retries = 0;

	{
	  MQTT::PublishRec pubrec(pub->packet_id());
	  if (!_send_reliably(&pubrec))
	    return;
	}

	{
	  MQTT::PublishComp pubcomp(pub->packet_id());
	  pubcomp.send(*_client);
	  lastOutActivity = millis();
	}
      }
    }
    break;

  case MQTT::PINGREQ:
    {
      MQTT::PingResp pr;
      pr.send(*_client);
      lastOutActivity = millis();
    }
    break;

  case MQTT::PINGRESP:
    pingOutstanding = false;
  }
}