mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-04-20 19:12:13 +02:00
replace Serial.print with logger
This commit is contained in:
parent
f38dcea1f1
commit
a220b9ecc2
13 changed files with 397 additions and 562 deletions
|
|
@ -1,10 +1,13 @@
|
|||
#include <Arduino.h>
|
||||
#include <LittleFS.h>
|
||||
#include <Preferences.h>
|
||||
#include "esp_log.h"
|
||||
#include "ConfigFile.h"
|
||||
#include "Utils.h"
|
||||
#include "ConfigSettings.h"
|
||||
|
||||
static const char *TAG = "ConfigFile";
|
||||
|
||||
extern Preferences pref;
|
||||
|
||||
#define SHADE_HDR_VER 24
|
||||
|
|
@ -68,7 +71,7 @@ bool ConfigFile::writeHeader(const config_header_t &hdr) {
|
|||
bool ConfigFile::readHeader() {
|
||||
if(!this->isOpen()) return false;
|
||||
//if(this->file.position() != 0) this->file.seek(0, SeekSet);
|
||||
Serial.printf("Reading header at %u\n", this->file.position());
|
||||
ESP_LOGD(TAG, "Reading header at %u", this->file.position());
|
||||
this->header.version = this->readUInt8(this->header.version);
|
||||
this->header.length = this->readUInt8(0);
|
||||
if(this->header.version >= 19) {
|
||||
|
|
@ -93,7 +96,7 @@ bool ConfigFile::readHeader() {
|
|||
this->header.transRecordSize = this->readUInt16(this->header.transRecordSize);
|
||||
this->readString(this->header.serverId, sizeof(this->header.serverId));
|
||||
}
|
||||
Serial.printf("version:%u len:%u roomSize:%u roomRecs:%u shadeSize:%u shadeRecs:%u groupSize:%u groupRecs: %u pos:%d\n", this->header.version, this->header.length, this->header.roomRecordSize, this->header.roomRecords, this->header.shadeRecordSize, this->header.shadeRecords, this->header.groupRecordSize, this->header.groupRecords, this->file.position());
|
||||
ESP_LOGD(TAG, "version:%u len:%u roomSize:%u roomRecs:%u shadeSize:%u shadeRecs:%u groupSize:%u groupRecs: %u pos:%d", this->header.version, this->header.length, this->header.roomRecordSize, this->header.roomRecords, this->header.shadeRecordSize, this->header.shadeRecords, this->header.groupRecordSize, this->header.groupRecords, this->file.position());
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
|
|
@ -403,39 +406,34 @@ bool ShadeConfigFile::backup(SomfyShadeController *s) {
|
|||
bool ShadeConfigFile::validate() {
|
||||
this->readHeader();
|
||||
if(this->header.version < 1) {
|
||||
Serial.print("Invalid Header Version:");
|
||||
Serial.println(this->header.version);
|
||||
ESP_LOGE(TAG, "Invalid Header Version:%u", this->header.version);
|
||||
return false;
|
||||
}
|
||||
if(this->header.shadeRecordSize < 100) {
|
||||
Serial.print("Invalid Shade Record Size:");
|
||||
Serial.println(this->header.shadeRecordSize);
|
||||
ESP_LOGE(TAG, "Invalid Shade Record Size:%u", this->header.shadeRecordSize);
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
if(this->header.shadeRecords != SOMFY_MAX_SHADES) {
|
||||
Serial.print("Invalid Shade Record Count:");
|
||||
Serial.println(this->header.shadeRecords);
|
||||
ESP_LOGE(TAG, "Invalid Shade Record Count:%u", this->header.shadeRecords);
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
if(this->header.version > 10) {
|
||||
if(this->header.groupRecordSize < 100) {
|
||||
Serial.print("Invalid Group Record Size:");
|
||||
Serial.println(this->header.groupRecordSize);
|
||||
ESP_LOGE(TAG, "Invalid Group Record Size:%u", this->header.groupRecordSize);
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
if(this->header.groupRecords != SOMFY_MAX_GROUPS) {
|
||||
Serial.print("Invalid Group Record Count:");
|
||||
Serial.println(this->header.groupRecords);
|
||||
ESP_LOGE(TAG, "Invalid Group Record Count:%u", this->header.groupRecords);
|
||||
return false;
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
if(this->file.position() != this->header.length) {
|
||||
Serial.printf("File not positioned at %u end of header: %d\n", this->header.length, this->file.position());
|
||||
ESP_LOGE(TAG, "File not positioned at %u end of header: %d", this->header.length, this->file.position());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -452,7 +450,7 @@ bool ShadeConfigFile::validate() {
|
|||
fsize += (this->header.repeaterRecordSize * this->header.repeaterRecords);
|
||||
}
|
||||
if(this->file.size() != fsize) {
|
||||
Serial.printf("File size is not correct should be %d and got %d\n", fsize, this->file.size());
|
||||
ESP_LOGE(TAG, "File size is not correct should be %d and got %d", fsize, this->file.size());
|
||||
}
|
||||
// Next check to see if the records match the header length.
|
||||
uint8_t recs = 0;
|
||||
|
|
@ -461,11 +459,11 @@ bool ShadeConfigFile::validate() {
|
|||
while(recs < this->header.roomRecords) {
|
||||
uint32_t pos = this->file.position();
|
||||
if(!this->seekChar(CFG_REC_END)) {
|
||||
Serial.printf("Failed to find the room record end %d\n", recs);
|
||||
ESP_LOGE(TAG, "Failed to find the room record end %d", recs);
|
||||
return false;
|
||||
}
|
||||
if(this->file.position() - pos != this->header.roomRecordSize) {
|
||||
Serial.printf("Room record length is %d and should be %d\n", this->file.position() - pos, this->header.roomRecordSize);
|
||||
ESP_LOGE(TAG, "Room record length is %d and should be %d", this->file.position() - pos, this->header.roomRecordSize);
|
||||
return false;
|
||||
}
|
||||
recs++;
|
||||
|
|
@ -475,11 +473,11 @@ bool ShadeConfigFile::validate() {
|
|||
while(recs < this->header.shadeRecords) {
|
||||
uint32_t pos = this->file.position();
|
||||
if(!this->seekChar(CFG_REC_END)) {
|
||||
Serial.printf("Failed to find the shade record end %d\n", recs);
|
||||
ESP_LOGE(TAG, "Failed to find the shade record end %d", recs);
|
||||
return false;
|
||||
}
|
||||
if(this->file.position() - pos != this->header.shadeRecordSize) {
|
||||
Serial.printf("Shade record length is %d and should be %d\n", this->file.position() - pos, this->header.shadeRecordSize);
|
||||
ESP_LOGE(TAG, "Shade record length is %d and should be %d", this->file.position() - pos, this->header.shadeRecordSize);
|
||||
return false;
|
||||
}
|
||||
recs++;
|
||||
|
|
@ -489,12 +487,12 @@ bool ShadeConfigFile::validate() {
|
|||
while(recs < this->header.groupRecords) {
|
||||
uint32_t pos = this->file.position();
|
||||
if(!this->seekChar(CFG_REC_END)) {
|
||||
Serial.printf("Failed to find the group record end %d\n", recs);
|
||||
ESP_LOGE(TAG, "Failed to find the group record end %d", recs);
|
||||
return false;
|
||||
}
|
||||
recs++;
|
||||
if(this->file.position() - pos != this->header.groupRecordSize) {
|
||||
Serial.printf("Group record length is %d and should be %d\n", this->file.position() - pos, this->header.groupRecordSize);
|
||||
ESP_LOGE(TAG, "Group record length is %d and should be %d", this->file.position() - pos, this->header.groupRecordSize);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -504,7 +502,7 @@ bool ShadeConfigFile::validate() {
|
|||
while(recs < this->header.repeaterRecords) {
|
||||
//uint32_t pos = this->file.position();
|
||||
if(!this->seekChar(CFG_REC_END)) {
|
||||
Serial.printf("Failed to find the repeater record end %d\n", recs);
|
||||
ESP_LOGE(TAG, "Failed to find the repeater record end %d", recs);
|
||||
}
|
||||
recs++;
|
||||
|
||||
|
|
@ -534,30 +532,27 @@ bool ShadeConfigFile::restore(SomfyShadeController *s, const char *filename, res
|
|||
bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename, restore_options_t &opts) {
|
||||
bool opened = false;
|
||||
if(!this->isOpen()) {
|
||||
Serial.println("Opening shade restore file");
|
||||
ESP_LOGI(TAG, "Opening shade restore file");
|
||||
this->begin(filename, true);
|
||||
opened = true;
|
||||
}
|
||||
if(!this->validate()) {
|
||||
Serial.println("Shade restore file invalid!");
|
||||
ESP_LOGE(TAG, "Shade restore file invalid!");
|
||||
if(opened) this->end();
|
||||
return false;
|
||||
}
|
||||
if(opts.shades) {
|
||||
Serial.println("Restoring Rooms...");
|
||||
ESP_LOGI(TAG, "Restoring Rooms...");
|
||||
for(uint8_t i = 0; i < this->header.roomRecords; i++) {
|
||||
this->readRoomRecord(&s->rooms[i]);
|
||||
if(i > 0) Serial.print(",");
|
||||
Serial.print(s->rooms[i].roomId);
|
||||
ESP_LOGD(TAG, "Room %u", s->rooms[i].roomId);
|
||||
}
|
||||
Serial.println("Restoring Shades...");
|
||||
ESP_LOGI(TAG, "Restoring Shades...");
|
||||
// We should be valid so start reading.
|
||||
for(uint8_t i = 0; i < this->header.shadeRecords; i++) {
|
||||
this->readShadeRecord(&s->shades[i]);
|
||||
if(i > 0) Serial.print(",");
|
||||
Serial.print(s->shades[i].getShadeId());
|
||||
ESP_LOGD(TAG, "Shade %u", s->shades[i].getShadeId());
|
||||
}
|
||||
Serial.println("");
|
||||
if(this->header.shadeRecords < SOMFY_MAX_SHADES) {
|
||||
uint8_t ndx = this->header.shadeRecords;
|
||||
// Clear out any positions that are not in the shade file.
|
||||
|
|
@ -565,13 +560,11 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename,
|
|||
((SomfyShade *)&s->shades[ndx++])->clear();
|
||||
}
|
||||
}
|
||||
Serial.println("Restoring Groups...");
|
||||
ESP_LOGI(TAG, "Restoring Groups...");
|
||||
for(uint8_t i = 0; i < this->header.groupRecords; i++) {
|
||||
if(i > 0) Serial.print(",");
|
||||
Serial.print(s->groups[i].getGroupId());
|
||||
ESP_LOGD(TAG, "Group %u", s->groups[i].getGroupId());
|
||||
this->readGroupRecord(&s->groups[i]);
|
||||
}
|
||||
Serial.println("");
|
||||
if(this->header.groupRecords < SOMFY_MAX_GROUPS) {
|
||||
uint8_t ndx = this->header.groupRecords;
|
||||
// Clear out any positions that are not in the shade file.
|
||||
|
|
@ -581,14 +574,14 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename,
|
|||
}
|
||||
}
|
||||
else {
|
||||
Serial.println("Shade data ignored");
|
||||
ESP_LOGI(TAG, "Shade data ignored");
|
||||
// FF past the shades and groups.
|
||||
this->file.seek(this->file.position()
|
||||
+ (this->header.shadeRecords * this->header.shadeRecordSize)
|
||||
+ (this->header.groupRecords * this->header.groupRecordSize), SeekSet); // Start at the beginning of the file after the header.
|
||||
}
|
||||
if(opts.repeaters) {
|
||||
Serial.println("Restoring Repeaters...");
|
||||
ESP_LOGI(TAG, "Restoring Repeaters...");
|
||||
if(this->header.repeaterRecords > 0) {
|
||||
memset(s->repeaters, 0x00, sizeof(uint32_t) * SOMFY_MAX_REPEATERS);
|
||||
for(uint8_t i = 0; i < this->header.repeaterRecords; i++) {
|
||||
|
|
@ -632,7 +625,7 @@ bool ShadeConfigFile::readNetRecord(restore_options_t &opts) {
|
|||
if(this->header.netRecordSize > 0) {
|
||||
uint32_t startPos = this->file.position();
|
||||
if(opts.network) {
|
||||
Serial.println("Reading network settings from file...");
|
||||
ESP_LOGD(TAG, "Reading network settings from file...");
|
||||
settings.connType = static_cast<conn_types_t>(this->readUInt8(static_cast<uint8_t>(conn_types_t::unset)));
|
||||
settings.IP.dhcp = this->readBool(true);
|
||||
char ip[24];
|
||||
|
|
@ -678,7 +671,7 @@ bool ShadeConfigFile::readNetRecord(restore_options_t &opts) {
|
|||
// the ethernet phy settings.
|
||||
if(opts.network) {
|
||||
if(strncmp(settings.serverId, this->header.serverId, sizeof(settings.serverId)) == 0) {
|
||||
Serial.println("Restoring Ethernet adapter settings");
|
||||
ESP_LOGI(TAG, "Restoring Ethernet adapter settings");
|
||||
settings.Ethernet.boardType = this->readUInt8(1);
|
||||
settings.Ethernet.phyType = static_cast<eth_phy_type_t>(this->readUInt8(0));
|
||||
settings.Ethernet.CLKMode = static_cast<eth_clock_mode_t>(this->readUInt8(0));
|
||||
|
|
@ -689,7 +682,7 @@ bool ShadeConfigFile::readNetRecord(restore_options_t &opts) {
|
|||
}
|
||||
}
|
||||
if(this->file.position() != startPos + this->header.netRecordSize) {
|
||||
Serial.println("Reading to end of network record");
|
||||
ESP_LOGD(TAG, "Reading to end of network record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
}
|
||||
|
|
@ -698,7 +691,7 @@ bool ShadeConfigFile::readNetRecord(restore_options_t &opts) {
|
|||
bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) {
|
||||
if(this->header.transRecordSize > 0) {
|
||||
uint32_t startPos = this->file.position();
|
||||
Serial.println("Reading Transceiver settings from file...");
|
||||
ESP_LOGD(TAG, "Reading Transceiver settings from file...");
|
||||
cfg.enabled = this->readBool(false);
|
||||
cfg.proto = static_cast<radio_proto>(this->readUInt8(0));
|
||||
cfg.type = this->readUInt8(56);
|
||||
|
|
@ -713,7 +706,7 @@ bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) {
|
|||
cfg.deviation = this->readFloat(cfg.deviation);
|
||||
cfg.txPower = this->readInt8(cfg.txPower);
|
||||
if(this->file.position() != startPos + this->header.transRecordSize) {
|
||||
Serial.println("Reading to end of transceiver record");
|
||||
ESP_LOGD(TAG, "Reading to end of transceiver record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
|
||||
|
|
@ -723,7 +716,7 @@ bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) {
|
|||
bool ShadeConfigFile::readSettingsRecord() {
|
||||
if(this->header.settingsRecordSize > 0) {
|
||||
uint32_t startPos = this->file.position();
|
||||
Serial.println("Reading settings from file...");
|
||||
ESP_LOGD(TAG, "Reading settings from file...");
|
||||
char ver[24];
|
||||
this->readVarString(ver, sizeof(ver));
|
||||
this->readVarString(settings.hostname, sizeof(settings.hostname));
|
||||
|
|
@ -732,7 +725,7 @@ bool ShadeConfigFile::readSettingsRecord() {
|
|||
settings.ssdpBroadcast = this->readBool(false);
|
||||
if(this->header.version >= 20) settings.checkForUpdate = this->readBool(true);
|
||||
if(this->file.position() != startPos + this->header.settingsRecordSize) {
|
||||
Serial.println("Reading to end of settings record");
|
||||
ESP_LOGD(TAG, "Reading to end of settings record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
}
|
||||
|
|
@ -772,7 +765,7 @@ bool ShadeConfigFile::readGroupRecord(SomfyGroup *group) {
|
|||
|
||||
pref.end();
|
||||
if(this->file.position() != startPos + this->header.groupRecordSize) {
|
||||
Serial.println("Reading to end of group record");
|
||||
ESP_LOGD(TAG, "Reading to end of group record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -784,7 +777,7 @@ bool ShadeConfigFile::readRepeaterRecord(SomfyShadeController *s) {
|
|||
s->linkRepeater(this->readUInt32(0));
|
||||
}
|
||||
if(this->file.position() != startPos + this->header.repeaterRecordSize) {
|
||||
Serial.println("Reading to end of repeater record");
|
||||
ESP_LOGD(TAG, "Reading to end of repeater record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -795,7 +788,7 @@ bool ShadeConfigFile::readRoomRecord(SomfyRoom *room) {
|
|||
this->readString(room->name, sizeof(room->name));
|
||||
room->sortOrder = this->readUInt8(room->roomId - 1);
|
||||
if(this->file.position() != startPos + this->header.roomRecordSize) {
|
||||
Serial.println("Reading to end of room record");
|
||||
ESP_LOGD(TAG, "Reading to end of room record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -881,7 +874,7 @@ bool ShadeConfigFile::readShadeRecord(SomfyShade *shade) {
|
|||
pinMode(shade->gpioMy, OUTPUT);
|
||||
if(this->header.version >= 19) shade->roomId = this->readUInt8(0);
|
||||
if(this->file.position() != startPos + this->header.shadeRecordSize) {
|
||||
Serial.println("Reading to end of shade record");
|
||||
ESP_LOGD(TAG, "Reading to end of shade record");
|
||||
this->seekChar(CFG_REC_END);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -889,12 +882,12 @@ bool ShadeConfigFile::readShadeRecord(SomfyShade *shade) {
|
|||
bool ShadeConfigFile::loadFile(SomfyShadeController *s, const char *filename) {
|
||||
bool opened = false;
|
||||
if(!this->isOpen()) {
|
||||
Serial.println("Opening shade config file");
|
||||
ESP_LOGI(TAG, "Opening shade config file");
|
||||
this->begin(filename, true);
|
||||
opened = true;
|
||||
}
|
||||
if(!this->validate()) {
|
||||
Serial.println("Shade config file invalid!");
|
||||
ESP_LOGE(TAG, "Shade config file invalid!");
|
||||
if(opened) this->end();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -936,7 +929,7 @@ bool ShadeConfigFile::loadFile(SomfyShadeController *s, const char *filename) {
|
|||
this->readRepeaterRecord(s);
|
||||
}
|
||||
if(opened) {
|
||||
Serial.println("Closing shade config file");
|
||||
ESP_LOGI(TAG, "Closing shade config file");
|
||||
this->end();
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue