Added basic Logger with 4 modes.

This commit is contained in:
2025-01-26 18:02:39 +02:00
parent 7dd6f81264
commit 516eeab751
8 changed files with 126 additions and 107 deletions

View File

@@ -24,36 +24,35 @@ public:
isPlaying = true;
hardStop = false;
startTime = loopStartTime = millis();
Serial.println("Plbck: PLAY");
LOG_DEBUG("Plbck: PLAY");
}
void forceStop() {
hardStop = true;
isPlaying = false;
Serial.println("Plbck: FORCE STOP");
LOG_DEBUG("Plbck: FORCE STOP");
}
void stop() {
hardStop = false;
isPlaying = false;
Serial.println("Plbck: STOP");
LOG_DEBUG("Plbck: STOP");
}
void pause() {
isPaused = true;
Serial.println("Plbck: PAUSE");
LOG_DEBUG("Plbck: PAUSE");
}
void unpause() {
isPaused = false;
loopStartTime = millis();
Serial.println("Plbck: RESUME");
LOG_DEBUG("Plbck: RESUME");
}
// Handles Incoming Commands to PLAY or STOP
void command(char * command){
Serial.print("INCOMING COMMAND: ");
Serial.println(command);
LOG_DEBUG("Incoming Command: %s",command);
if (command[0] == '1') {
play();
PublishMqtt("OK - PLAY");
@@ -88,7 +87,7 @@ public:
loop_duration = doc["loop_dur"].as<uint32_t>();
}
// Print Just for Debugging Purposes
Serial.printf("Name: %s, ID: %d, Total Duration: %lu, Loop Duration: %lu, Interval: %d, Speed: %d, Inf: %s\n",
LOG_DEBUG("Name: %s, ID: %d, Total Duration: %lu, Loop Duration: %lu, Interval: %d, Speed: %d, Inf: %s\n",
name.c_str(),
id,
duration,
@@ -102,13 +101,12 @@ public:
// Loads the Selected melody from a .bin file, into RAM
void loadMelodyInRAM(std::vector<uint16_t> &melody_steps) {
LOG_INFO("Loading Melody.");
std::string filePath = "/" + name + ".bin";
Serial.println("New Melody Selected !!!");
Serial.println("Reading data from file...");
File bin_file = SPIFFS.open(filePath.c_str(), "r");
if (!bin_file) {
Serial.println("Failed to Open File");
LOG_ERROR("Failed to Open File");
return;
}
@@ -116,22 +114,14 @@ public:
size_t steps = fileSize / 2;
melody_steps.resize(steps);
Serial.print("Opened File ! Size: ");
Serial.print(fileSize);
Serial.print(" Steps: ");
Serial.println(steps);
LOG_DEBUG("Opened File, size: %zu - Steps: %zu",fileSize,steps)
for (size_t i=0; i<steps; i++){
melody_steps[i] = bin_file.read() << 8 | bin_file.read();
LOG_DEBUG("Current Step: %03d // HEX Value: 0x%04X", i, melody_steps[i]);
}
for (size_t i=0; i<steps; i++){
Serial.print("Current Step: ");
Serial.printf("%03d // ", i);
Serial.print(" HEX Value: ");
Serial.printf("0x%04X\n", melody_steps[i]);
}
Serial.println("Closing File");
LOG_DEBUG("Closing File");
bin_file.close();
// closing the file
@@ -173,32 +163,34 @@ private:
std::string filePath = "/" + getMonth() + ".json";
File file = SPIFFS.open(filePath.c_str(), "r");
if (!file) {
Serial.printf("Failed to open file: %s\n", filePath.c_str());
LOG_ERROR("Failed to open file: %s\n", filePath.c_str());
return;
}
Serial.println("Opened daily schedule file");
LOG_INFO("Opened daily schedule file");
StaticJsonDocument<8192> doc; // Adjust size based on expected JSON complexity
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error) {
Serial.printf("Failed to parse JSON: %s\n", error.c_str());
LOG_ERROR("Failed to parse JSON: %s\n", error.c_str());
return;
}
Serial.println("- - - SERIALIZE JSON - - -");
serializeJsonPretty(doc, Serial);
Serial.println("- - - - END JSON - - - -");
if (LOG_LEVEL_ENABLED(LOG_LEVEL_DEBUG)){
String jsonString;
serializeJsonPretty(doc, jsonString);
LOG_DEBUG("Serialized JSON: /n%s",jsonString.c_str());
}
// Extract entries for the current day
std::string currentDay = getDay();
dailySchedule.clear(); // Clear previous day's schedule
Serial.printf("Current Day: %s\n", currentDay.c_str());
LOG_DEBUG("Current Day: %s\n", currentDay.c_str());
if (doc.containsKey(currentDay.c_str())) {
Serial.println("doc contains key!");
LOG_DEBUG("Doc contains key!");
// Check if the current day contains a single object or an array
JsonVariant dayVariant = doc[currentDay.c_str()];
@@ -215,8 +207,8 @@ private:
schedule.duration = entry["duration"];
dailySchedule.push_back(schedule);
Serial.printf("Added Entry - Melody: %s, Hour: %d, Minute: %d, Speed: %d, Duration: %d\n",
schedule.mel.c_str(), schedule.hour, schedule.minute, schedule.speed, schedule.duration);
LOG_INFO("Added Entry - Melody: %s, Hour: %d, Minute: %d, Speed: %d, Duration: %d\n",
schedule.mel.c_str(), schedule.hour, schedule.minute, schedule.speed, schedule.duration);
}
} else if (dayVariant.is<JsonObject>()) {
// Handle case where the day contains a single object
@@ -229,13 +221,13 @@ private:
schedule.duration = entry["duration"];
dailySchedule.push_back(schedule);
Serial.printf("Added Single Entry - Melody: %s, Hour: %d, Minute: %d, Speed: %d, Duration: %d\n",
LOG_INFO("Added Single Entry - Melody: %s, Hour: %d, Minute: %d, Speed: %d, Duration: %d\n",
schedule.mel.c_str(), schedule.hour, schedule.minute, schedule.speed, schedule.duration);
} else {
Serial.println("Invalid data format for the current day.");
LOG_WARNING("Invalid data format for the current day.");
}
} else {
Serial.println("No schedule found for today.");
LOG_INFO("No schedule found for today.");
}
}
@@ -249,12 +241,12 @@ private:
if (!alreadyTriggered) {
// First time at midnight, trigger the event
alreadyTriggered = true;
Serial.println("New day detected, returning true.");
LOG_DEBUG("New day detected, returning true.");
loadDailySchedule();
return true;
} else {
// It's still midnight, but we've already triggered
Serial.println("Already triggered for today, returning false.");
LOG_DEBUG("Already triggered for today, returning false.");
return false;
}
} else {
@@ -291,7 +283,7 @@ public:
// Prints the time, NOW.
void printTimeNow(){
Serial.printf("Current Time: %s %s, %u - %02u:%02u:%02u\n",
LOG_INFO("Current Time: %s %s, %u - %02u:%02u:%02u\n",
getMonth().c_str(), // Month as a string
getDay().c_str(), // Day as a string
getYear(), // Year as an integer
@@ -306,18 +298,17 @@ public:
}
void checkAndRunSchedule(Player & player) {
LOG_DEBUG("Running daily schedule check");
for (auto it = dailySchedule.begin(); it != dailySchedule.end(); ) {
Serial.println("Running daily schedule check");
if (now.hour == it->hour && now.minute == it->minute) {
Serial.printf("Entry Exists, returning True!");
LOG_DEBUG("Entry Exists, Calling program.");
StaticJsonDocument<200> jsonDoc;
jsonDoc["name"] = it->mel.c_str();
jsonDoc["speed"] = it->speed;
jsonDoc["duration"] = it->duration;
jsonDoc["loop_dur"] = 0;
jsonDoc["internal"] = 0;
Serial.printf("name: %s speed: %d duration: %d",it->mel.c_str(), it->speed, it->duration);
//LOG_INFO("Entry Found. Name: %s Speed: %d Duration: %d",it->mel.c_str(), it->speed, it->duration);
if (!player.isPlaying){
player.setMelodyAttributes(jsonDoc);
player.loadMelodyInRAM(melody_steps);
@@ -326,7 +317,7 @@ public:
}
}
else {
Serial.printf("No Entry, returning False!");
LOG_DEBUG("Entry's time doesn't match. Skipping.");
++it;
}
}