<commit_before>#include "BootNormal.hpp"

using namespace HomieInternals;

BootNormal::BootNormal(SharedInterface* shared_interface)
: Boot("normal")
, _shared_interface(shared_interface)
, _last_wifi_reconnect_attempt(0)
, _last_mqtt_reconnect_attempt(0)
, _last_signal_sent(0)
, _flagged_for_ota(false)
, _flagged_for_reset(false)
{
  if (Config.get().mqtt.ssl) {
    this->_shared_interface->mqtt = new PubSubClient(this->_wifiClientSecure);
  } else {
    this->_shared_interface->mqtt = new PubSubClient(this->_wifiClient);
  }

  strcpy(this->_mqtt_base_topic, "devices/");
  strcat(this->_mqtt_base_topic, Helpers.getDeviceId());
}

BootNormal::~BootNormal() {
  delete this->_shared_interface->mqtt;
}

void BootNormal::_wifiConnect() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(Config.get().wifi.ssid, Config.get().wifi.password);
}

void BootNormal::_mqttConnect() {
  this->_shared_interface->mqtt->setServer(Config.get().mqtt.host, Config.get().mqtt.port);
  this->_shared_interface->mqtt->setCallback(std::bind(&BootNormal::_mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
  char topic[24 + 1];
  strcpy(topic, this->_mqtt_base_topic);
  strcat(topic, "/$online");

  char client_id[14 + 1] = "Homie-";
  strcat(client_id, Helpers.getDeviceId());

  bool connectResult;
  if (Config.get().mqtt.auth) {
    connectResult = this->_shared_interface->mqtt->connect(client_id, topic, 2, true, "false");
  } else {
    connectResult = this->_shared_interface->mqtt->connect(client_id, Config.get().mqtt.username, Config.get().mqtt.password, topic, 2, true, "false");
  }

  if (connectResult) {
    if (Config.get().mqtt.ssl && !strcmp(Config.get().mqtt.fingerprint, "")) {
      if(!this->_wifiClientSecure.verify(Config.get().mqtt.fingerprint, Config.get().mqtt.host)) {
        Logger.logln("✖ MQTT SSL certificate mismatch");
        this->_shared_interface->mqtt->disconnect();
        return;
      }
    }

    this->_mqttSetup();
  }
}

void BootNormal::_mqttSetup() {
  Logger.logln("Sending initial informations");

  char topic[27 + 1];
  strcpy(topic, this->_mqtt_base_topic);
  strcat(topic, "/$nodes");

  int nodesLength = 0;
  for (int i = 0; i < this->_shared_interface->nodes.size(); i++) {
    HomieNode node = this->_shared_interface->nodes[i];
    nodesLength += strlen(node.id);
    nodesLength += 1; // :
    nodesLength += strlen(node.type);
    nodesLength += 1; // ,<commit_msg>Fix #19<commit_after>#include "BootNormal.hpp"

using namespace HomieInternals;

BootNormal::BootNormal(SharedInterface* shared_interface)
: Boot("normal")
, _shared_interface(shared_interface)
, _last_wifi_reconnect_attempt(0)
, _last_mqtt_reconnect_attempt(0)
, _last_signal_sent(0)
, _flagged_for_ota(false)
, _flagged_for_reset(false)
{
  if (Config.get().mqtt.ssl) {
    this->_shared_interface->mqtt = new PubSubClient(this->_wifiClientSecure);
  } else {
    this->_shared_interface->mqtt = new PubSubClient(this->_wifiClient);
  }

  strcpy(this->_mqtt_base_topic, "devices/");
  strcat(this->_mqtt_base_topic, Helpers.getDeviceId());
}

BootNormal::~BootNormal() {
  delete this->_shared_interface->mqtt;
}

void BootNormal::_wifiConnect() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(Config.get().wifi.ssid, Config.get().wifi.password);
}

void BootNormal::_mqttConnect() {
  this->_shared_interface->mqtt->setServer(Config.get().mqtt.host, Config.get().mqtt.port);
  this->_shared_interface->mqtt->setCallback(std::bind(&BootNormal::_mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
  char topic[24 + 1];
  strcpy(topic, this->_mqtt_base_topic);
  strcat(topic, "/$online");

  char client_id[14 + 1] = "Homie-";
  strcat(client_id, Helpers.getDeviceId());

  bool connectResult;
  if (Config.get().mqtt.auth) {
    connectResult = this->_shared_interface->mqtt->connect(client_id, Config.get().mqtt.username, Config.get().mqtt.password, topic, 2, true, "false");
  } else {
    connectResult = this->_shared_interface->mqtt->connect(client_id, topic, 2, true, "false");
  }

  if (connectResult) {
    if (Config.get().mqtt.ssl && !strcmp(Config.get().mqtt.fingerprint, "")) {
      if(!this->_wifiClientSecure.verify(Config.get().mqtt.fingerprint, Config.get().mqtt.host)) {
        Logger.logln("✖ MQTT SSL certificate mismatch");
        this->_shared_interface->mqtt->disconnect();
        return;
      }
    }

    this->_mqttSetup();
  }
}

void BootNormal::_mqttSetup() {
  Logger.logln("Sending initial informations");

  char topic[27 + 1];
  strcpy(topic, this->_mqtt_base_topic);
  strcat(topic, "/$nodes");

  int nodesLength = 0;
  for (int i = 0; i < this->_shared_interface->nodes.size(); i++) {
    HomieNode node = this->_shared_interface->nodes[i];
    nodesLength += strlen(node.id);
    nodesLength += 1; // :
    nodesLength += strlen(node.type);
    nodesLength += 1; // ,
{'fraction_non_alphanumeric': 0.17157464212678936, 'fraction_numerical': 0.00899795501022495, 'mean_word_length': 4.878605769230769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306922', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1716, 'n_words': 249}