Cleanup old fingerprints in report loop
This commit is contained in:
parent
c06480e631
commit
5f839c66a1
|
@ -24,6 +24,7 @@ jobs:
|
|||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install --upgrade platformio
|
||||
pio platform update
|
||||
pio run -t clean
|
||||
- name: Set env
|
||||
run: echo "PLATFORMIO_BUILD_FLAGS=-DVERSION='\"${GITHUB_REF#refs/*/}\"'" >> $GITHUB_ENV
|
||||
|
|
|
@ -13,7 +13,6 @@ default_envs = esp32
|
|||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
platform_packages = framework-arduinoespressif32 @ https://github.com/DTTerastar/arduino-esp32.git#follow-redirects-http-update
|
||||
framework = arduino
|
||||
board = esp32dev
|
||||
lib_deps =
|
||||
|
@ -31,7 +30,6 @@ build_flags =
|
|||
|
||||
[env:m5stickc]
|
||||
platform = espressif32
|
||||
platform_packages = framework-arduinoespressif32 @ https://github.com/DTTerastar/arduino-esp32.git#follow-redirects-http-update
|
||||
framework = arduino
|
||||
board = m5stick-c
|
||||
lib_deps =
|
||||
|
@ -51,7 +49,6 @@ build_flags =
|
|||
|
||||
[env:m5stickc-plus]
|
||||
platform = espressif32
|
||||
platform_packages = framework-arduinoespressif32 @ https://github.com/DTTerastar/arduino-esp32.git#follow-redirects-http-update
|
||||
framework = arduino
|
||||
board = m5stick-c
|
||||
lib_deps =
|
||||
|
@ -71,7 +68,6 @@ build_flags =
|
|||
|
||||
[env:m5atom-matrix]
|
||||
platform = espressif32
|
||||
platform_packages = framework-arduinoespressif32 @ https://github.com/DTTerastar/arduino-esp32.git#follow-redirects-http-update
|
||||
framework = arduino
|
||||
board = m5stack-atom
|
||||
lib_deps =
|
||||
|
@ -91,7 +87,6 @@ build_flags =
|
|||
|
||||
[env:verbose]
|
||||
platform = espressif32
|
||||
platform_packages = framework-arduinoespressif32 @ https://github.com/DTTerastar/arduino-esp32.git#follow-redirects-http-update
|
||||
framework = arduino
|
||||
board = m5stick-c
|
||||
lib_deps =
|
||||
|
|
70
src/main.cpp
70
src/main.cpp
|
@ -10,23 +10,6 @@ BleFingerprint *getFingerprintInternal(BLEAdvertisedDevice *advertisedDevice)
|
|||
return *it;
|
||||
}
|
||||
|
||||
if (fingerprints.size() >= MAX_MAC_ADDRESSES)
|
||||
{
|
||||
long oldestTime = LONG_MAX;
|
||||
BleFingerprint *oldest;
|
||||
for (auto it = fingerprints.begin(); it != fingerprints.end(); ++it)
|
||||
{
|
||||
long time = (*it)->getLastSeen();
|
||||
if (time < oldestTime)
|
||||
{
|
||||
oldestTime = time;
|
||||
oldest = (*it);
|
||||
}
|
||||
}
|
||||
fingerprints.remove(oldest);
|
||||
delete oldest;
|
||||
}
|
||||
|
||||
auto created = new BleFingerprint(advertisedDevice);
|
||||
auto it2 = std::find_if(fingerprints.begin(), fingerprints.end(), [created](BleFingerprint *f) { return f->getId() == created->getId(); });
|
||||
if (it2 != fingerprints.end())
|
||||
|
@ -163,11 +146,10 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
|||
|
||||
void reconnect(TimerHandle_t xTimer)
|
||||
{
|
||||
if (updateInProgress)
|
||||
return;
|
||||
Serial.println("Reconnecting...");
|
||||
|
||||
if (WiFi.isConnected() && mqttClient.connected())
|
||||
return;
|
||||
if (updateInProgress) return;
|
||||
if (WiFi.isConnected() && mqttClient.connected()) return;
|
||||
|
||||
if (reconnectTries++ > 10)
|
||||
{
|
||||
|
@ -184,7 +166,7 @@ void reconnect(TimerHandle_t xTimer)
|
|||
|
||||
void connectToMqtt()
|
||||
{
|
||||
reconnectTimer = xTimerCreate("reconnectionTimer", pdMS_TO_TICKS(15000), pdTRUE, (void *)0, reconnect);
|
||||
reconnectTimer = xTimerCreate("reconnectionTimer", pdMS_TO_TICKS(3000), pdTRUE, (void *)0, reconnect);
|
||||
Serial.printf("Connecting to MQTT %s %d\n", mqttHost.c_str(), mqttPort);
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
|
@ -276,6 +258,7 @@ void scanForDevices(void *parameter)
|
|||
|
||||
if (xSemaphoreTake(fingerprintSemaphore, 1000) != pdTRUE)
|
||||
log_e("Couldn't take semaphore!");
|
||||
cleanupOldFingerprints();
|
||||
std::list<BleFingerprint *> seen(fingerprints);
|
||||
if (xSemaphoreGive(fingerprintSemaphore) != pdTRUE)
|
||||
log_e("Couldn't give semaphore!");
|
||||
|
@ -290,45 +273,6 @@ void scanForDevices(void *parameter)
|
|||
}
|
||||
}
|
||||
|
||||
void spiffsInit()
|
||||
{
|
||||
int ledState = HIGH;
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
|
||||
#ifdef BUTTON
|
||||
|
||||
pinMode(BUTTON, INPUT);
|
||||
int flashes = 0;
|
||||
unsigned long debounceDelay = 250;
|
||||
|
||||
long lastDebounceTime = millis();
|
||||
while (digitalRead(BUTTON) == BUTTON_PRESSED)
|
||||
{
|
||||
if ((millis() - lastDebounceTime) > debounceDelay)
|
||||
{
|
||||
ledState = !ledState;
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
lastDebounceTime = millis();
|
||||
flashes++;
|
||||
|
||||
if (flashes > 10)
|
||||
{
|
||||
Serial.println(F("Resetting back to defaults..."));
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
SPIFFS.format();
|
||||
SPIFFS.begin(true);
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
SPIFFS.begin(true);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
|
@ -354,10 +298,6 @@ void setup()
|
|||
|
||||
void loop()
|
||||
{
|
||||
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
|
||||
TIMERG0.wdt_feed = 1;
|
||||
TIMERG0.wdt_wprotect = 0;
|
||||
|
||||
ArduinoOTA.handle();
|
||||
firmwareUpdate();
|
||||
}
|
||||
|
|
106
src/main.h
106
src/main.h
|
@ -1,39 +1,26 @@
|
|||
#include <rom/rtc.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <WiFi.h>
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
}
|
||||
#include "soc/timer_group_struct.h"
|
||||
#include "soc/timer_group_reg.h"
|
||||
|
||||
#include <AsyncTCP.h>
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <HTTPUpdate.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
|
||||
#include <WebServer.h>
|
||||
#include <WiFiSettings.h>
|
||||
#include <SPIFFS.h>
|
||||
|
||||
#include <NimBLEDevice.h>
|
||||
#include <HTTPUpdate.h>
|
||||
#include <NimBLEAdvertisedDevice.h>
|
||||
#include "NimBLEEddystoneURL.h"
|
||||
#include "NimBLEEddystoneTLM.h"
|
||||
#include "NimBLEBeacon.h"
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "Settings.h"
|
||||
#include <NimBLEBeacon.h>
|
||||
#include <NimBLEDevice.h>
|
||||
#include <NimBLEEddystoneTLM.h>
|
||||
#include <NimBLEEddystoneURL.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <WiFiSettings.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/timers.h>
|
||||
#include <rom/rtc.h>
|
||||
|
||||
#include "BleFingerprint.h"
|
||||
#include "Settings.h"
|
||||
|
||||
#ifdef M5STICK
|
||||
#ifdef PLUS
|
||||
|
@ -215,3 +202,64 @@ void firmwareUpdate()
|
|||
updateInProgress = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void spiffsInit()
|
||||
{
|
||||
int ledState = HIGH;
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
|
||||
#ifdef BUTTON
|
||||
|
||||
pinMode(BUTTON, INPUT);
|
||||
int flashes = 0;
|
||||
unsigned long debounceDelay = 250;
|
||||
|
||||
long lastDebounceTime = millis();
|
||||
while (digitalRead(BUTTON) == BUTTON_PRESSED)
|
||||
{
|
||||
if ((millis() - lastDebounceTime) > debounceDelay)
|
||||
{
|
||||
ledState = !ledState;
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
lastDebounceTime = millis();
|
||||
flashes++;
|
||||
|
||||
if (flashes > 10)
|
||||
{
|
||||
Serial.println(F("Resetting back to defaults..."));
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
SPIFFS.format();
|
||||
SPIFFS.begin(true);
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
SPIFFS.begin(true);
|
||||
}
|
||||
|
||||
void cleanupOldFingerprints()
|
||||
{
|
||||
if (fingerprints.size() < MAX_MAC_ADDRESSES)
|
||||
return;
|
||||
|
||||
long oldestTime = LONG_MAX;
|
||||
BleFingerprint *oldest = nullptr;
|
||||
for (auto it = fingerprints.begin(); it != fingerprints.end(); ++it)
|
||||
{
|
||||
long time = (*it)->getLastSeen();
|
||||
if (time < oldestTime)
|
||||
{
|
||||
oldestTime = time;
|
||||
oldest = (*it);
|
||||
}
|
||||
}
|
||||
if (oldest == nullptr) return;
|
||||
|
||||
fingerprints.remove(oldest);
|
||||
delete oldest;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue