Moved addChipIdToHostname method into the constructor.

This commit is contained in:
Fabian Schlenz 2019-07-18 06:34:26 +02:00
parent 2c463174f1
commit cc472c72df
1 changed files with 11 additions and 26 deletions

View File

@ -44,7 +44,6 @@ private:
const char* _mqtt_pass;
const char* _mqtt_topic;
unsigned long _startup_complete_at = 0;
bool _chip_id_added = false;
bool _initialized = false;
void _setup();
@ -67,14 +66,13 @@ private:
void _check_report_handlers();
String _get_report_handler_result(IOTReportHandler* h, bool force_update);
public:
SimpleIOT(const char* wifi_ssid, const char* wifi_pass, String hostname);
SimpleIOT(const char* wifi_ssid, const char* wifi_pass, String hostname, bool append_chip_id=false);
void setStartupDelay(uint16_t delay);
void setMQTTData(const char* mqtt_host, uint16_t mqtt_port,
const char* mqtt_user, const char* mqtt_pass,
const char* mqtt_topic);
void setMQTTData(const char* mqtt_host, uint16_t mqtt_port,
const char* mqtt_topic);
void addChipIdToHostname();
void begin();
void loop();
bool act_on(String topic, IOTActionHandlerFunction f);
@ -90,36 +88,23 @@ public:
* @param hostname The hostname this device will use to identify itself.
* It will be used as WiFi client name and for the mDNS stuff
* of the OTA daemon.
*
* @see addChipIdToHostname()
* @param append_chip_id If true, the ESP's chipID will be appended to the hostname.
* This can be useful in networks with more than one ESPs with
* the given hostname. `test` becomes `test-13D93B0A`.
*/
SimpleIOT::SimpleIOT(const char* wifi_ssid, const char* wifi_pass, String hostname) {
SimpleIOT::SimpleIOT(const char* wifi_ssid, const char* wifi_pass, String hostname, bool append_chip_id) {
Serial.begin(74880);
_wifi_ssid = wifi_ssid;
_wifi_pass = wifi_pass;
if (append_chip_id) {
char* temp = new char[9];
snprintf(temp, 9, "-%08X", ESP.getChipId());
hostname.concat(temp);
delete temp;
}
_hostname = hostname;
}
/**
* Appends the internal ID of the ESP8266 to the hostname given in the constructor.
* This makes the hostname a network with multiple ESP8266s.
*
* Example: `iot_test` becomes `iot_text-13D93B0A`.
*
* @warning This method has to be called before calling begin().
*/
void SimpleIOT::addChipIdToHostname() {
if (_initialized) return;
if (_chip_id_added) return;
char* temp = new char[9];
snprintf(temp, 9, "-%08X", ESP.getChipId());
String temp2(_hostname);
temp2.concat(temp);
delete temp;
_hostname = temp2.c_str();
_chip_id_added = true;
}
/**
* Defines a startup delay to give a chance of doing an OTA update.
* During development there are situations where a bug in code leads to quickly