diff --git a/src/dallassensor.cpp b/src/dallassensor.cpp index f4db8d0d9..5cb170ff2 100644 --- a/src/dallassensor.cpp +++ b/src/dallassensor.cpp @@ -188,7 +188,7 @@ void DallasSensor::loop() { scancnt_ = 0; } else if (scancnt_ == SCAN_START + 1) { // startup firstscan_ = sensors_.size(); - LOG_DEBUG(F("Adding %d dallassensor(s) from first scan"), firstscan_); + LOG_DEBUG(F("Adding %d dallas sensor(s) from first scan"), firstscan_); } else if ((scancnt_ <= 0) && (firstscan_ != sensors_.size())) { // check 2 times for no change of sensor # scancnt_ = SCAN_START; sensors_.clear(); // restart scaning and clear to get correct numbering @@ -327,7 +327,7 @@ std::string DallasSensor::Sensor::to_string(const bool name) const { int16_t DallasSensor::Sensor::offset() const { std::string str = id_string(); - int16_t offset = 0; + int16_t offset = 0; // default value EMSESP::webSettingsService.read([&](WebSettings & settings) { for (uint8_t i = 0; i < NUM_SENSOR_NAMES; i++) { if (strcmp(settings.sensor[i].id.c_str(), str.c_str()) == 0) { @@ -339,7 +339,8 @@ int16_t DallasSensor::Sensor::offset() const { return offset; } -bool DallasSensor::add_name(const char * idstr, const char * name, int16_t offset) { +// update dallas information like name and offset +bool DallasSensor::update(const char * idstr, const char * name, int16_t offset) { bool ok = false; char id[20]; strlcpy(id, idstr, sizeof(id)); @@ -351,6 +352,7 @@ bool DallasSensor::add_name(const char * idstr, const char * name, int16_t offse strlcpy(id, sensors_[no].id_string().c_str(), sizeof(id)); } } + // check valid id if (strlen(id) != 17 || id[2] != '-' || id[7] != '-' || id[12] != '-') { LOG_WARNING(F("Invalid sensor id: %s"), id); @@ -366,27 +368,31 @@ bool DallasSensor::add_name(const char * idstr, const char * name, int16_t offse settings.sensor[i].id = ""; settings.sensor[i].name = ""; settings.sensor[i].offset = 0; - LOG_INFO(F("Deleting entry of sensor %s"), id); + LOG_INFO(F("Deleting entry for sensor %s"), id); } else { settings.sensor[i].name = (strlen(name) == 0) ? id : name; settings.sensor[i].offset = offset; - LOG_INFO(F("Setting name of sensor %s to %s"), id, name); + char result[10]; + LOG_INFO(F("Renaming sensor ID %s to %s with offset %s"), id, name, Helpers::render_value(result, offset, 10)); } ok = true; return StateUpdateResult::CHANGED; } } + // check for free place for (uint8_t i = 0; i < NUM_SENSOR_NAMES; i++) { if (settings.sensor[i].id.isEmpty()) { settings.sensor[i].id = id; settings.sensor[i].name = (strlen(name) == 0) ? id : name; settings.sensor[i].offset = offset; - LOG_INFO(F("Setting name of sensor %s to %s"), id, name); + char result[10]; + LOG_INFO(F("Renaming sensor ID %s to %s with offset %s"), id, name, Helpers::render_value(result, offset, 10)); ok = true; return StateUpdateResult::CHANGED; } } + // check if there is a unused id and overwrite it for (uint8_t i = 0; i < NUM_SENSOR_NAMES; i++) { bool found = false; @@ -399,12 +405,13 @@ bool DallasSensor::add_name(const char * idstr, const char * name, int16_t offse settings.sensor[i].id = id; settings.sensor[i].name = (strlen(name) == 0) ? id : name; settings.sensor[i].offset = offset; - LOG_INFO(F("Setting name of sensor %s to %s"), id, name); + LOG_INFO(F("Renaming sensor %s to %s"), id, name); ok = true; return StateUpdateResult::CHANGED; } } - LOG_ERROR(F("List full, remove one sensorname first")); + + LOG_ERROR(F("List full, remove a sensor first")); return StateUpdateResult::UNCHANGED; }, "local"); diff --git a/src/dallassensor.h b/src/dallassensor.h index 6d469dd04..bc352fa6c 100644 --- a/src/dallassensor.h +++ b/src/dallassensor.h @@ -88,7 +88,7 @@ class DallasSensor { dallas_format_ = dallas_format; } - bool add_name(const char * idstr, const char * name, int16_t offset); + bool update(const char * idstr, const char * name, int16_t offset); private: static constexpr uint8_t MAX_SENSORS = 20;