From 2549e3873d043e419903bd7a688236e12dffc9d9 Mon Sep 17 00:00:00 2001 From: am0ebe <am0ebe@gmx.de> Date: Mon, 22 Jul 2024 18:46:49 +0200 Subject: [PATCH] add pingScan.sh to search and find cams when they get a new ip adress in direct connection --- src/cam.cpp | 27 +++++++++++++++++------- src/cmd/main.cpp | 2 +- src/core.cpp | 13 +++++++++--- src/pingScan.sh | 41 +++++++++++++++++++++++++++++++++++++ src/record.cpp | 53 +++++++++++++++++++++++++++++++----------------- src/record.h | 7 +++++-- 6 files changed, 111 insertions(+), 32 deletions(-) create mode 100755 src/pingScan.sh diff --git a/src/cam.cpp b/src/cam.cpp index 5d4a865..e5fb969 100644 --- a/src/cam.cpp +++ b/src/cam.cpp @@ -25,7 +25,7 @@ _id(""), _state(disconnected), _rec() { - qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; + // qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; } // Cam::Cam(const Cam& other) : IPrinter(), std::enable_shared_from_this<Cam>(other) @@ -123,20 +123,33 @@ void Cam::close() void Cam::printFeatures( QString feature) { + if(_state != opened) + return error("cant access features if cam is " + stateToString(_state) + " cam"); + printFeatures(QStringList(feature)); } void Cam::printFeatures( QStringList features) { + + if(_state != opened) + return error("cant access features if cam is " + stateToString(_state) + " cam"); + for( auto feature : features ) { try { + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; + qDebug() << feature << "| " << feature.toStdString().c_str(); + + // auto vcam = _cam->getCameraPtr(); FeaturePtr pFeature; - _cam->GetFeatureByName(feature.toStdString().c_str(), pFeature); + g( _cam->GetFeatureByName(feature.toStdString().c_str(), pFeature) ); + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; VmbFeatureDataType type; g( pFeature->GetDataType(type) ); + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; switch(type) { @@ -183,7 +196,7 @@ void Cam::printFeatures( QStringList features) catch (const std::runtime_error& xx) { qDebug() << __FUNCTION__ << "Exception caught: " << xx.what(); - continue; + break; } } } @@ -200,8 +213,8 @@ QString Cam::camInfo() try { - camInfo += name() + DELIM; - camInfo += ip() + DELIM; + // camInfo += name() + DELIM; + // camInfo += ip() + DELIM; camInfo += id() + DELIM; std::string str; @@ -214,11 +227,11 @@ QString Cam::camInfo() g(_cam->GetInterfaceID( str )); camInfo += QString::fromStdString(str) + DELIM; - camInfo += stateToString(_state); + camInfo += stateToString(_state) + DELIM; if( _state == recording) _rec->showProgress(); //xxx string to add to camInfo else - camInfo += "recdur:" + formatTime( _rec->dur().count() * 1000 ) + DELIM; + camInfo += "recdur: " + formatTime( _rec->dur().count() * 1000 ) + DELIM; } catch(const std::exception& e) diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp index 6634838..4f1e3ab 100644 --- a/src/cmd/main.cpp +++ b/src/cmd/main.cpp @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) // XXX add new func QTimer::singleShot(0, Console::getInstance(), SLOT(printVersion())); - QTimer::singleShot(2000, &core, SLOT(openCam())); // for testing + QTimer::singleShot(4000, &core, SLOT(openCam())); // for testing return a.exec(); } diff --git a/src/core.cpp b/src/core.cpp index f34195b..528f974 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -56,8 +56,8 @@ void Core::init() _cameras.push_back(pcam); } - // detectCams(); showInfo(); + detectCams(); // listCams(); } @@ -180,8 +180,11 @@ void Core::stopRecording() void Core::showRecordingStats() { - if( cam() && cam()->rec() ) + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; + if( cam() && cam()->rec() && cam()->_cam ) + { cam()->rec()->showStats(); + } } void Core::showInfo() @@ -198,7 +201,11 @@ void Core::showInfo() print("-------------------------------"); if( cam() && cam()->rec() ) - cam()->rec()->checkDirExists(); + { + print(QString("checkDirExists: %1").arg(cam()->rec()->checkDirExists())); + showRecordingStats(); + // cam()->rec()->showStats(); + } //listCams //showStats //checkDiskSpace (show progressbar) diff --git a/src/pingScan.sh b/src/pingScan.sh new file mode 100755 index 0000000..99e6142 --- /dev/null +++ b/src/pingScan.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +#scans class-B subnet 169.254.i.j +#watch CPU usage and adapt max_jobs / timeout / step_size vars to ur system + +step_size=8 +max_jobs=50 # Maximum number of concurrent pings +timeout=3 # Timeout for each ping in seconds + +for i in {0..255}; do + if [ $((i % step_size)) == 0 ]; then + read -rp "169.254.$i-$((i+step_size))" -t$timeout #wait s + echo "" + fi + + # echo $i + for j in {0..255}; do + if [[ "$i" == 255 && "$j" == 255 ]]; then + continue #ignore broadcast + fi + + ip="169.254.$i.$j" + ( + if timeout "$timeout"s ping -c1 -W1"$ip" &> /dev/null; then + echo -e "\n$ip <<<<<<<<<<<<<<<<<< !!\n" + fi + ) & + + + # Wait for jobs to finish if max_jobs limit is reached + if (( $(jobs -r | wc -l) >= max_jobs )); then + wait -n + fi + done +done + + +wait # Wait for all background pings in this step to complete before proceeding + + + # (ping "${ip}" -c 1 -W 1 &> /dev/null && echo -e "\n${ip} <<<<<<<<<<<<<<<<<< !!\n" &) ; diff --git a/src/record.cpp b/src/record.cpp index ce2fe99..a317199 100644 --- a/src/record.cpp +++ b/src/record.cpp @@ -25,21 +25,12 @@ _frames(FramePtrVector(utils::threadsPerCam())) _timer->setInterval( duration_cast<milliseconds>(utils::recDuration()) ); connect(_timer, SIGNAL(timeout()), this, SLOT(stop())); - _updateTimer->setInterval(1000); + + _updateTimer->setInterval(duration_cast<milliseconds>(updateInterval)); connect(_updateTimer, SIGNAL(timeout()), this, SLOT(showProgress())); connect(_updateTimer, SIGNAL(timeout()), this, SLOT(showStats())); } -// Record::Record() : IPrinter(), -// _cam(nullptr), -// _timer(nullptr), -// _updateTimer(nullptr), -// _dir(""), -// _payloadSize(0LL), -// _pixelFormat(""), -// _frames() -// {} - void Record::init(CamPtr cam) { _cam = cam; @@ -116,7 +107,7 @@ void Record::start() _cam->setState(Cam::recording); _timer->start(); - // _updateTimer->start(); + _updateTimer->start(); } /* Does: @@ -165,6 +156,7 @@ void Record::stop() void Record::showStats() { + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; _cam->printFeatures("StatFrameDropped"); _cam->printFeatures({ // "StatFrameDropped", //unsigned int max 4294967295 @@ -187,6 +179,7 @@ void Record::showProgress() { //TODO test //fps? + qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; info( progressBar(_timer) ); } @@ -194,28 +187,50 @@ void Record::checkDiskSpace(const QString &dir, seconds dur) { QStorageInfo storageInfo(dir); - //in bytes + // In bytes qint64 available = storageInfo.bytesAvailable(); qint64 total = storageInfo.bytesTotal(); qint64 used = calcDirSize(dir); int usedPercentage = (double(used) / total) * 100; - info( progressBar(usedPercentage) ); // << cuz of this line, the function is here not in utils... + info(progressBar(usedPercentage)); // << cuz of this line, the function is here not in utils... qint64 estimated = dur.count() * utils::fps() * utils::frameSize(); - qDebug() << "Used Space:" << used / (1024 * 1024) << "MB"; - qDebug() << "Available Space:" << available / (1024 * 1024) << "MB"; - qDebug() << "Total Space:" << total / (1024 * 1024) << "MB"; + auto formatSize = [](qint64 size) -> QString { + double displaySize = size; + QString unit = "MB"; + + const qint64 ONE_MB = 1024LL * 1024LL; + const qint64 ONE_GB = ONE_MB * 1024LL; + const qint64 ONE_TB = ONE_GB * 1024LL; + + if (size >= ONE_TB) { + displaySize = size / double(ONE_TB); + unit = "TB"; + } else if (size >= ONE_GB) { + displaySize = size / double(ONE_GB); + unit = "GB"; + } else { + displaySize = size / double(ONE_MB); + } + + return QString::number(displaySize, 'f', 2) + " " + unit; + }; + + qDebug() << "Used Space:" << formatSize(used); + qDebug() << "Available Space:" << formatSize(available); + qDebug() << "Total Space:" << formatSize(total); qDebug() << "Used Percentage:" << usedPercentage << "%"; - qDebug() << "Estimated Recording Space:" << estimated / (1024 * 1024) << "MB"; + qDebug() << "Estimated Recording Space:" << formatSize(estimated); // Check if estimated recording space exceeds available space if (estimated > available) { - warning("Estimated recording space exceeds available space!)"); + warning("Estimated recording space exceeds available space!"); } } + //calc basedir size qint64 Record::calcDirSize(const QDir &dirrr) { QDir dir(dirrr); diff --git a/src/record.h b/src/record.h index faf2ba8..66bcac6 100644 --- a/src/record.h +++ b/src/record.h @@ -4,6 +4,7 @@ #include "typeDefinitions.h" #include <QDir> +#include <chrono> class QTimer; @@ -22,8 +23,6 @@ public: void setDur(seconds); seconds dur(); - void showStats(); - void showProgress(); void checkDiskSpace(const QString &, seconds); bool checkDirExists(); @@ -35,6 +34,8 @@ public: public slots: void stop(); + void showStats(); + void showProgress(); private: @@ -48,6 +49,8 @@ private: std::string _pixelFormat; FramePtrVector _frames; + const seconds updateInterval = 2s; + }; -- GitLab