Skip to content

Commit

Permalink
Prevent infinite loop on one time run (#20)
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
chadek authored Aug 14, 2023
1 parent aaf646a commit 423dcca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ bool cInverter::query(const char *cmd, int replysize) {

void cInverter::poll() {
int n,j;
extern const bool runOnce;

while (true) {

Expand Down Expand Up @@ -193,6 +194,10 @@ void cInverter::poll() {
}
}
if (quit_thread) return;
if (runOnce) {
ups_leave = true;
exit(0);
}
sleep(5);
}
}
Expand Down
15 changes: 8 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

bool debugFlag = false;
bool runOnce = false;
bool ups_leave = false;

cInverter *ups = NULL;

Expand Down Expand Up @@ -291,14 +292,14 @@ int main(int argc, char* argv[]) {
// Delete reply string so we can update with new data when polled again...
delete reply1;
delete reply2;

if(runOnce) {
ups->terminateThread();
// Do once and exit instead of loop endlessly
lprintf("INVERTER: All queries complete, exiting loop.");
exit(0);
}
}

}
if(ups_leave) {
ups->terminateThread();
// Do once and exit instead of loop endlessly
lprintf("INVERTER: All queries complete, exiting loop.");
exit(0);
}

sleep(1);
Expand Down
2 changes: 2 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "inverter.h"

extern bool debugFlag;
extern bool ups_leave;

extern atomic_bool ups_data_changed;

extern atomic_bool ups_status_changed;
Expand Down

0 comments on commit 423dcca

Please sign in to comment.