From b8adaf6fbe9bf0b943a59623f1b9da8655a14860 Mon Sep 17 00:00:00 2001 From: Jm Date: Sun, 14 Feb 2021 16:17:40 -0800 Subject: [PATCH] #671 Range test plugin --- src/plugins/Plugins.cpp | 2 +- src/plugins/RangeTestPlugin.cpp | 62 ++++++------ src/plugins/StoreForwardPlugin-old.cpp | 125 +++++++++++++++++++++++++ src/plugins/StoreForwardPlugin-old.h | 55 +++++++++++ 4 files changed, 216 insertions(+), 28 deletions(-) create mode 100644 src/plugins/StoreForwardPlugin-old.cpp create mode 100644 src/plugins/StoreForwardPlugin-old.h diff --git a/src/plugins/Plugins.cpp b/src/plugins/Plugins.cpp index 46f854106..6fe1459f2 100644 --- a/src/plugins/Plugins.cpp +++ b/src/plugins/Plugins.cpp @@ -36,6 +36,6 @@ void setupPlugins() //storeForwardPlugin = new StoreForwardPlugin(); new RangeTestPlugin(); - new StoreForwardPlugin(); + //new StoreForwardPlugin(); #endif } \ No newline at end of file diff --git a/src/plugins/RangeTestPlugin.cpp b/src/plugins/RangeTestPlugin.cpp index a1c69b101..7f99070ec 100644 --- a/src/plugins/RangeTestPlugin.cpp +++ b/src/plugins/RangeTestPlugin.cpp @@ -153,12 +153,12 @@ bool RangeTestPluginRadio::handleReceived(const MeshPacket &mp) DEBUG_MSG("mp.from %d\n", mp.from); DEBUG_MSG("mp.rx_snr %f\n", mp.rx_snr); DEBUG_MSG("mp.hop_limit %d\n", mp.hop_limit); - DEBUG_MSG("mp.decoded.position.latitude_i %d\n", mp.decoded.position.latitude_i); - DEBUG_MSG("mp.decoded.position.longitude_i %d\n", mp.decoded.position.longitude_i); + // DEBUG_MSG("mp.decoded.position.latitude_i %d\n", mp.decoded.position.latitude_i); // Depricated + // DEBUG_MSG("mp.decoded.position.longitude_i %d\n", mp.decoded.position.longitude_i); // Depricated DEBUG_MSG("---- Node Information of Received Packet (mp.from):\n"); DEBUG_MSG("n->user.long_name %s\n", n->user.long_name); DEBUG_MSG("n->user.short_name %s\n", n->user.short_name); - DEBUG_MSG("n->user.macaddr %X\n", n->user.macaddr); + // DEBUG_MSG("n->user.macaddr %X\n", n->user.macaddr); DEBUG_MSG("n->has_position %d\n", n->has_position); DEBUG_MSG("n->position.latitude_i %d\n", n->position.latitude_i); DEBUG_MSG("n->position.longitude_i %d\n", n->position.longitude_i); @@ -241,22 +241,25 @@ bool RangeTestPluginRadio::appendFile(const MeshPacket &mp) return 0; } - //--------- Write to file - File fileToWrite = SPIFFS.open("/static/rangetest.csv", FILE_WRITE); + // If the file doesn't exist, write the header. + if (!SPIFFS.exists("/static/rangetest.csv")) { + //--------- Write to file + File fileToWrite = SPIFFS.open("/static/rangetest.csv", FILE_WRITE); - if (!fileToWrite) { - DEBUG_MSG("There was an error opening the file for writing\n"); - return 0; + if (!fileToWrite) { + DEBUG_MSG("There was an error opening the file for writing\n"); + return 0; + } + + if (fileToWrite.println("time,from,sender name,sender lat,sender long,rx lat,rx long,rx snr,distance,payload")) { + DEBUG_MSG("File was written\n"); + } else { + DEBUG_MSG("File write failed\n"); + } + + fileToWrite.close(); } - if (fileToWrite.println("time,sender mac,rx snr,sender lat,sender long,rx lat,rx long,distance,payload")) { - DEBUG_MSG("File was written\n"); - } else { - DEBUG_MSG("File write failed\n"); - } - - fileToWrite.close(); - //--------- Apend content to file File fileToAppend = SPIFFS.open("/static/rangetest.csv", FILE_APPEND); @@ -278,21 +281,26 @@ bool RangeTestPluginRadio::appendFile(const MeshPacket &mp) int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN - fileToAppend.printf("%02d:%02d:%02d ", hour, min, sec); // Time + fileToAppend.printf("%02d:%02d:%02d,", hour, min, sec); // Time } else { - fileToAppend.printf("??:??:?? "); // Time + fileToAppend.printf("??:??:??,"); // Time } - fileToAppend.printf("$X,", n->user.macaddr); // Mac Address - fileToAppend.printf("%f,", mp.rx_snr); // RX SNR - fileToAppend.printf("%d,", n->position.latitude_i); // Sender Lat - fileToAppend.printf("%d,", n->position.longitude_i); // Sender Long - fileToAppend.printf("%d,", gpsStatus->getLatitude()); // RX Lat - fileToAppend.printf("%d,", gpsStatus->getLongitude()); // RX Long + fileToAppend.printf("%d,", mp.from); // From + fileToAppend.printf("%s,", n->user.long_name); // Long Name + fileToAppend.printf("%f,", n->position.latitude_i * 1e-7); // Sender Lat + fileToAppend.printf("%f,", n->position.longitude_i * 1e-7); // Sender Long + fileToAppend.printf("%f,", gpsStatus->getLatitude() * 1e-7); // RX Lat + fileToAppend.printf("%f,", gpsStatus->getLongitude() * 1e-7); // RX Long + fileToAppend.printf("%f,", mp.rx_snr); // RX SNR - float distance = latLongToMeter(n->position.latitude_i * 1e-7, n->position.longitude_i * 1e-7, - gpsStatus->getLatitude() * 1e-7, gpsStatus->getLongitude() * 1e-7); - fileToAppend.printf("%f,", distance); // Distance in meters + if (n->position.latitude_i && n->position.longitude_i && gpsStatus->getLatitude() && gpsStatus->getLongitude()) { + float distance = latLongToMeter(n->position.latitude_i * 1e-7, n->position.longitude_i * 1e-7, + gpsStatus->getLatitude() * 1e-7, gpsStatus->getLongitude() * 1e-7); + fileToAppend.printf("%f,", distance); // Distance in meters + } else { + fileToAppend.printf("0,"); + } // TODO: If quotes are found in the payload, it has to be escaped. fileToAppend.printf("\"%s\"\n", p.payload.bytes); diff --git a/src/plugins/StoreForwardPlugin-old.cpp b/src/plugins/StoreForwardPlugin-old.cpp new file mode 100644 index 000000000..3df421af6 --- /dev/null +++ b/src/plugins/StoreForwardPlugin-old.cpp @@ -0,0 +1,125 @@ +#if 0 + +#include "StoreForwardPlugin.h" +#include "MeshService.h" +#include "NodeDB.h" +#include "RTC.h" +#include "Router.h" +#include "configuration.h" +#include + +StoreForwardPlugin *storeForwardPlugin; +StoreForwardPluginRadio *storeForwardPluginRadio; + +StoreForwardPlugin::StoreForwardPlugin() : concurrency::OSThread("StoreForwardPlugin") {} + +int32_t StoreForwardPlugin::runOnce() +{ +#if 0 + +#ifndef NO_ESP32 + + /* + Uncomment the preferences below if you want to use the plugin + without having to configure it from the PythonAPI or WebUI. + */ + + radioConfig.preferences.store_forward_plugin_enabled = 0; + + if (radioConfig.preferences.store_forward_plugin_enabled) { + + if (firstTime) { + + DEBUG_MSG("Initializing Store & Forward Plugin\n"); + /* + */ + + // Router + if (radioConfig.preferences.is_router) { + if (ESP.getPsramSize()) { + if (ESP.getFreePsram() <= 1024 * 1024) { + // Do the startup here + + } else { + DEBUG_MSG("Device has less than 1M of PSRAM free. Aborting startup.\n"); + DEBUG_MSG("Store & Forward Plugin - Aborting Startup.\n"); + + return (INT32_MAX); + } + + } else { + DEBUG_MSG("Device doesn't have PSRAM.\n"); + DEBUG_MSG("Store & Forward Plugin - Aborting Startup.\n"); + + return (INT32_MAX); + } + + // Non-Router + } else { + } + // storeForwardPluginRadio = new StoreForwardPluginRadio(); + + firstTime = 0; + + } else { + // TBD + } + + return (1000); + } else { + DEBUG_MSG("Store & Forward Plugin - Disabled\n"); + + return (INT32_MAX); + } + +#endif +#endif + return (INT32_MAX); +} + +MeshPacket *StoreForwardPluginRadio::allocReply() +{ + + auto reply = allocDataPacket(); // Allocate a packet for sending + + return reply; +} + +void StoreForwardPluginRadio::sendPayload(NodeNum dest, bool wantReplies) +{ +#if 0 + MeshPacket *p = allocReply(); + p->to = dest; + p->decoded.want_response = wantReplies; + + service.sendToMesh(p); +#endif +} + +bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp) +{ + +#if 0 +#ifndef NO_ESP32 + + if (radioConfig.preferences.store_forward_plugin_enabled) { + + // auto &p = mp.decoded.data; + + if (mp.from != nodeDB.getNodeNum()) { + DEBUG_MSG("Store & Forward Plugin -- Print Start ---------- ---------- ---------- ---------- ----------\n"); + printPacket("PACKET FROM RADIO", &mp); + DEBUG_MSG("Store & Forward Plugin -- Print End ---------- ---------- ---------- ---------- ----------\n"); + } + + } else { + DEBUG_MSG("Store & Forward Plugin - Disabled\n"); + } + +#endif +#endif + + return true; // Let others look at this message also if they want +} + +#endif \ No newline at end of file diff --git a/src/plugins/StoreForwardPlugin-old.h b/src/plugins/StoreForwardPlugin-old.h new file mode 100644 index 000000000..29d11ea7d --- /dev/null +++ b/src/plugins/StoreForwardPlugin-old.h @@ -0,0 +1,55 @@ +#if 0 + +#pragma once + +#include "SinglePortPlugin.h" +#include "concurrency/OSThread.h" +#include "configuration.h" +#include +#include + +class StoreForwardPlugin : private concurrency::OSThread +{ + bool firstTime = 1; + + public: + StoreForwardPlugin(); + + protected: + virtual int32_t runOnce(); +}; + +extern StoreForwardPlugin *storeForwardPlugin; + +/* + * Radio interface for StoreForwardPlugin + * + */ +class StoreForwardPluginRadio : public SinglePortPlugin +{ + //uint32_t lastRxID; + + public: + StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_STORE_FORWARD_APP) {} + //StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_TEXT_MESSAGE_APP) {} + + /** + * Send our payload into the mesh + */ + void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); + + protected: + virtual MeshPacket *allocReply(); + + //virtual bool wantPortnum(PortNum p){return true;}; + + /** Called to handle a particular incoming message + + @return true if you've guaranteed you've handled this message and no other handlers should be considered for it + */ + virtual bool handleReceived(const MeshPacket &mp); +}; + +extern StoreForwardPluginRadio *storeForwardPluginRadio; + +#endif \ No newline at end of file