Memory leak fixes in api and nfc, location tag fix
Fixes multiple potential memory leaks in API and NFC. Also fixes an issue in the new locaiton tag feature that could lead to multiple parallel API requests. This could cause memory leak issues but also result in wrong weights being registered for a spool.
This commit is contained in:
26
src/api.cpp
26
src/api.cpp
@@ -2,6 +2,7 @@
|
|||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "commonFS.h"
|
#include "commonFS.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
volatile spoolmanApiStateType spoolmanApiState = API_INIT;
|
volatile spoolmanApiStateType spoolmanApiState = API_INIT;
|
||||||
//bool spoolman_connected = false;
|
//bool spoolman_connected = false;
|
||||||
@@ -87,6 +88,8 @@ JsonDocument fetchSingleSpoolInfo(int spoolId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sendToApi(void *parameter) {
|
void sendToApi(void *parameter) {
|
||||||
|
HEAP_DEBUG_MESSAGE("sendToApi begin");
|
||||||
|
|
||||||
spoolmanApiState = API_TRANSMITTING;
|
spoolmanApiState = API_TRANSMITTING;
|
||||||
SendToApiParams* params = (SendToApiParams*)parameter;
|
SendToApiParams* params = (SendToApiParams*)parameter;
|
||||||
|
|
||||||
@@ -131,8 +134,8 @@ void sendToApi(void *parameter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||||
doc.clear();
|
|
||||||
}
|
}
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
||||||
@@ -145,8 +148,9 @@ void sendToApi(void *parameter) {
|
|||||||
|
|
||||||
// Speicher freigeben
|
// Speicher freigeben
|
||||||
delete params;
|
delete params;
|
||||||
vTaskDelete(NULL);
|
HEAP_DEBUG_MESSAGE("sendToApi end");
|
||||||
spoolmanApiState = API_IDLE;
|
spoolmanApiState = API_IDLE;
|
||||||
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateSpoolTagId(String uidString, const char* payload) {
|
bool updateSpoolTagId(String uidString, const char* payload) {
|
||||||
@@ -169,6 +173,8 @@ bool updateSpoolTagId(String uidString, const char* payload) {
|
|||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
// Update Payload erstellen
|
// Update Payload erstellen
|
||||||
JsonDocument updateDoc;
|
JsonDocument updateDoc;
|
||||||
updateDoc["extra"]["nfc_id"] = "\""+uidString+"\"";
|
updateDoc["extra"]["nfc_id"] = "\""+uidString+"\"";
|
||||||
@@ -207,6 +213,7 @@ bool updateSpoolTagId(String uidString, const char* payload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolWeight begin");
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId + "/measure";
|
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId + "/measure";
|
||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
@@ -241,11 +248,14 @@ uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
updateDoc.clear();
|
updateDoc.clear();
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolWeight end");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t updateSpoolLocation(String spoolId, String location){
|
uint8_t updateSpoolLocation(String spoolId, String location){
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolLocation begin");
|
||||||
|
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId;
|
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId;
|
||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
@@ -281,6 +291,7 @@ uint8_t updateSpoolLocation(String spoolId, String location){
|
|||||||
|
|
||||||
updateDoc.clear();
|
updateDoc.clear();
|
||||||
|
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolLocation end");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,6 +356,10 @@ bool updateSpoolBambuData(String payload) {
|
|||||||
|
|
||||||
String updatePayload;
|
String updatePayload;
|
||||||
serializeJson(updateDoc, updatePayload);
|
serializeJson(updateDoc, updatePayload);
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
updateDoc.clear();
|
||||||
|
|
||||||
Serial.print("Update Payload: ");
|
Serial.print("Update Payload: ");
|
||||||
Serial.println(updatePayload);
|
Serial.println(updatePayload);
|
||||||
|
|
||||||
@@ -511,6 +526,7 @@ bool checkSpoolmanExtraFields() {
|
|||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
doc.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,6 +573,8 @@ bool checkSpoolmanInstance(const String& url) {
|
|||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
return strcmp(status, "healthy") == 0;
|
return strcmp(status, "healthy") == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
|
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
|
||||||
@@ -584,6 +602,8 @@ bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octoWh, const
|
|||||||
octoUrl = octoWh;
|
octoUrl = octoWh;
|
||||||
octoToken = octoTk;
|
octoToken = octoTk;
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,6 +620,8 @@ String loadSpoolmanUrl() {
|
|||||||
return doc["url"].as<String>();
|
return doc["url"].as<String>();
|
||||||
}
|
}
|
||||||
Serial.println("Keine gültige Spoolman-URL gefunden.");
|
Serial.println("Keine gültige Spoolman-URL gefunden.");
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -212,14 +212,14 @@ void loop() {
|
|||||||
lastWeight = weight;
|
lastWeight = weight;
|
||||||
|
|
||||||
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
||||||
if (spoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS) {
|
if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS) {
|
||||||
oledShowIcon("loading");
|
oledShowIcon("loading");
|
||||||
if (updateSpoolWeight(spoolId, weight))
|
if (updateSpoolWeight(activeSpoolId, weight))
|
||||||
{
|
{
|
||||||
oledShowIcon("success");
|
oledShowIcon("success");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
weightSend = 1;
|
weightSend = 1;
|
||||||
autoSetToBambuSpoolId = spoolId.toInt();
|
autoSetToBambuSpoolId = activeSpoolId.toInt();
|
||||||
|
|
||||||
if (octoEnabled)
|
if (octoEnabled)
|
||||||
{
|
{
|
||||||
|
17
src/nfc.cpp
17
src/nfc.cpp
@@ -14,7 +14,8 @@ Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
|
|||||||
TaskHandle_t RfidReaderTask;
|
TaskHandle_t RfidReaderTask;
|
||||||
|
|
||||||
JsonDocument rfidData;
|
JsonDocument rfidData;
|
||||||
String spoolId = "";
|
String activeSpoolId = "";
|
||||||
|
String lastSpoolId = "";
|
||||||
String nfcJsonData = "";
|
String nfcJsonData = "";
|
||||||
volatile bool pauseBambuMqttTask = false;
|
volatile bool pauseBambuMqttTask = false;
|
||||||
|
|
||||||
@@ -64,6 +65,8 @@ void payloadToJson(uint8_t *data) {
|
|||||||
Serial.print("deserializeJson() failed: ");
|
Serial.print("deserializeJson() failed: ");
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Kein gültiger JSON-Inhalt gefunden oder fehlerhafte Formatierung.");
|
Serial.println("Kein gültiger JSON-Inhalt gefunden oder fehlerhafte Formatierung.");
|
||||||
//writeJsonToTag("{\"version\":\"1.0\",\"protocol\":\"NFC\",\"color_hex\":\"#FFFFFF\",\"type\":\"Example\",\"min_temp\":10,\"max_temp\":30,\"brand\":\"BrandName\"}");
|
//writeJsonToTag("{\"version\":\"1.0\",\"protocol\":\"NFC\",\"color_hex\":\"#FFFFFF\",\"type\":\"Example\",\"min_temp\":10,\"max_temp\":30,\"brand\":\"BrandName\"}");
|
||||||
@@ -221,14 +224,15 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
|||||||
if (doc.containsKey("sm_id") && doc["sm_id"] != "")
|
if (doc.containsKey("sm_id") && doc["sm_id"] != "")
|
||||||
{
|
{
|
||||||
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
|
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
|
||||||
spoolId = doc["sm_id"].as<String>();
|
activeSpoolId = doc["sm_id"].as<String>();
|
||||||
|
lastSpoolId = activeSpoolId;
|
||||||
}
|
}
|
||||||
else if(doc.containsKey("location") && doc["location"] != "")
|
else if(doc.containsKey("location") && doc["location"] != "")
|
||||||
{
|
{
|
||||||
Serial.println("Location Tag found!");
|
Serial.println("Location Tag found!");
|
||||||
String location = doc["location"].as<String>();
|
String location = doc["location"].as<String>();
|
||||||
if(spoolId != ""){
|
if(lastSpoolId != ""){
|
||||||
updateSpoolLocation(spoolId, location);
|
updateSpoolLocation(lastSpoolId, location);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -240,12 +244,14 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Keine SPOOL-ID gefunden.");
|
Serial.println("Keine SPOOL-ID gefunden.");
|
||||||
spoolId = "";
|
activeSpoolId = "";
|
||||||
oledShowMessage("Unknown Spool");
|
oledShowMessage("Unknown Spool");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,6 +452,7 @@ void scanRfidTask(void * parameter) {
|
|||||||
nfcReaderState = NFC_IDLE;
|
nfcReaderState = NFC_IDLE;
|
||||||
//uidString = "";
|
//uidString = "";
|
||||||
nfcJsonData = "";
|
nfcJsonData = "";
|
||||||
|
activeSpoolId = "";
|
||||||
Serial.println("Tag entfernt");
|
Serial.println("Tag entfernt");
|
||||||
if (!autoSendToBambu) oledShowWeight(weight);
|
if (!autoSendToBambu) oledShowWeight(weight);
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,8 @@ void startWriteJsonToTag(const char* payload);
|
|||||||
|
|
||||||
extern TaskHandle_t RfidReaderTask;
|
extern TaskHandle_t RfidReaderTask;
|
||||||
extern String nfcJsonData;
|
extern String nfcJsonData;
|
||||||
extern String spoolId;
|
extern String activeSpoolId;
|
||||||
|
extern String lastSpoolId;
|
||||||
extern volatile nfcReaderStateType nfcReaderState;
|
extern volatile nfcReaderStateType nfcReaderState;
|
||||||
extern volatile bool pauseBambuMqttTask;
|
extern volatile bool pauseBambuMqttTask;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user