🎐 Makes analytics a part of system service
This commit is contained in:
@@ -5,7 +5,6 @@ build_flags =
|
||||
-D USE_SLEEP=0
|
||||
-D USE_UPLOAD_FIRMWARE=1
|
||||
-D USE_DOWNLOAD_FIRMWARE=0
|
||||
-D USE_ANALYTICS=1
|
||||
-D USE_MOTION=1
|
||||
|
||||
; Hardware specific
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <analytics_service.h>
|
||||
#include <BatteryService.h>
|
||||
#include <filesystem.h>
|
||||
#include <firmware_download_service.h>
|
||||
@@ -93,9 +92,6 @@ class Spot {
|
||||
#if FT_ENABLED(USE_BATTERY)
|
||||
BatteryService _batteryService;
|
||||
#endif
|
||||
#if FT_ENABLED(USE_ANALYTICS)
|
||||
AnalyticsService _analyticsService;
|
||||
#endif
|
||||
#if FT_ENABLED(USE_MOTION)
|
||||
MotionService _motionService;
|
||||
#endif
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <filesystem.h>
|
||||
#include <event_socket.h>
|
||||
#include <system_service.h>
|
||||
#include <WiFi.h>
|
||||
#include <timing.h>
|
||||
|
||||
#define MAX_ESP_ANALYTICS_SIZE 2024
|
||||
#define EVENT_ANALYTICS "analytics"
|
||||
#define ANALYTICS_INTERVAL 2000
|
||||
|
||||
class AnalyticsService {
|
||||
public:
|
||||
AnalyticsService() {};
|
||||
|
||||
void loop() { EXECUTE_EVERY_N_MS(ANALYTICS_INTERVAL, updateAnalytics()); };
|
||||
|
||||
private:
|
||||
JsonDocument doc;
|
||||
char message[MAX_ESP_ANALYTICS_SIZE];
|
||||
|
||||
void updateAnalytics() {
|
||||
if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
||||
doc.clear();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
system_service::metrics(root);
|
||||
serializeJson(doc, message);
|
||||
socket.emit(EVENT_ANALYTICS, message);
|
||||
}
|
||||
};
|
||||
@@ -8,7 +8,6 @@ void features(JsonObject &root) {
|
||||
root["download_firmware"] = USE_DOWNLOAD_FIRMWARE;
|
||||
root["sleep"] = USE_SLEEP;
|
||||
root["battery"] = USE_BATTERY;
|
||||
root["analytics"] = USE_ANALYTICS;
|
||||
root["camera"] = USE_CAMERA;
|
||||
root["imu"] = USE_IMU;
|
||||
root["mag"] = USE_MAG;
|
||||
|
||||
@@ -32,11 +32,6 @@
|
||||
#define USE_BATTERY 0
|
||||
#endif
|
||||
|
||||
// ESP32 analytics on by default
|
||||
#ifndef USE_ANALYTICS
|
||||
#define USE_ANALYTICS 1
|
||||
#endif
|
||||
|
||||
// ESP32 camera off by default
|
||||
#ifndef USE_CAMERA
|
||||
#define USE_CAMERA 0
|
||||
|
||||
@@ -4,6 +4,9 @@ namespace system_service {
|
||||
|
||||
static const char *TAG = "SystemService";
|
||||
|
||||
static JsonDocument analyticsDoc;
|
||||
static char analyticsMessage[MAX_ESP_ANALYTICS_SIZE];
|
||||
|
||||
esp_err_t handleReset(PsychicRequest *request) {
|
||||
reset();
|
||||
return request->reply(200);
|
||||
@@ -132,6 +135,15 @@ void metrics(JsonObject &root) {
|
||||
}
|
||||
}
|
||||
|
||||
void emitMetrics() {
|
||||
if (!socket.hasSubscribers(EVENT_ANALYTICS)) return;
|
||||
analyticsDoc.clear();
|
||||
JsonObject root = analyticsDoc.to<JsonObject>();
|
||||
system_service::metrics(root);
|
||||
serializeJson(analyticsDoc, analyticsMessage);
|
||||
socket.emit(EVENT_ANALYTICS, analyticsMessage);
|
||||
}
|
||||
|
||||
const char *resetReason(int reason) {
|
||||
switch (reason) {
|
||||
case 1: return "Vbat power on reset";
|
||||
|
||||
@@ -5,9 +5,13 @@
|
||||
#include <PsychicHttp.h>
|
||||
#include <WiFi.h>
|
||||
#include <task_manager.h>
|
||||
#include <event_socket.h>
|
||||
#include <filesystem.h>
|
||||
#include <global.h>
|
||||
|
||||
#define MAX_ESP_ANALYTICS_SIZE 2024
|
||||
#define EVENT_ANALYTICS "analytics"
|
||||
|
||||
namespace system_service {
|
||||
esp_err_t handleReset(PsychicRequest *request);
|
||||
esp_err_t handleRestart(PsychicRequest *request);
|
||||
@@ -20,6 +24,9 @@ void restart();
|
||||
void sleep();
|
||||
void status(JsonObject &root);
|
||||
void metrics(JsonObject &root);
|
||||
|
||||
void emitMetrics();
|
||||
|
||||
const char *resetReason(int reason);
|
||||
} // namespace system_service
|
||||
|
||||
|
||||
+1
-4
@@ -184,7 +184,6 @@ void Spot::setupMDNS() {
|
||||
|
||||
void Spot::startServices() {
|
||||
_apService.begin();
|
||||
|
||||
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
||||
_uploadFirmwareService.begin();
|
||||
#endif
|
||||
@@ -209,9 +208,7 @@ void IRAM_ATTR Spot::loop() {
|
||||
while (1) {
|
||||
_wifiService.loop();
|
||||
_apService.loop();
|
||||
#if FT_ENABLED(USE_ANALYTICS)
|
||||
_analyticsService.loop();
|
||||
#endif
|
||||
EXECUTE_EVERY_N_MS(2000, system_service::emitMetrics());
|
||||
delay(20);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user