Skip to content

Commit

Permalink
fix Update button protection (prevent double click #527)
Browse files Browse the repository at this point in the history
optimized scheduler #515 (thx @beegee3)
  • Loading branch information
lumapu committed Dec 27, 2022
1 parent e5de251 commit ed16cff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 74 deletions.
4 changes: 4 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.63
* fix Update button protection (prevent double click #527)
* optimized scheduler #515 (thx @beegee3)

## 0.5.62
* fix MQTT `status` update
* removed MQTT `available_text` (can be deducted from `available`)
Expand Down
4 changes: 2 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class app : public IApp, public ah::Scheduler {
return mApi.getTimezoneOffset();
}

void getSchedulerInfo(uint16_t *everyMax, uint16_t *atMax) {
return getStat(everyMax, atMax);
void getSchedulerInfo(uint8_t *max) {
return getStat(max);
}

void setTimestamp(uint32_t newTime) {
Expand Down
2 changes: 1 addition & 1 deletion src/appInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IApp {
virtual void setTimestamp(uint32_t newTime) = 0;
virtual String getTimeStr(uint32_t offset) = 0;
virtual uint32_t getTimezoneOffset() = 0;
virtual void getSchedulerInfo(uint16_t *everyMax, uint16_t *atMax);
virtual void getSchedulerInfo(uint8_t *max) = 0;

virtual bool getRebootRequestState() = 0;
virtual bool getSettingsValid() = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 62
#define VERSION_PATCH 63

//-------------------------------------
typedef struct {
Expand Down
119 changes: 55 additions & 64 deletions src/utils/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,35 @@
#define __SCHEDULER_H__

#include <functional>
#include "llist.h"
#include "dbg.h"

namespace ah {
typedef std::function<void()> scdCb;

enum {SCD_SEC = 1, SCD_MIN = 60, SCD_HOUR = 3600, SCD_12H = 43200, SCD_DAY = 86400};

struct scdEvry_s {
struct sP {
scdCb c;
uint32_t timeout;
uint32_t reload;
scdEvry_s() : c(NULL), timeout(0), reload(0) {}
scdEvry_s(scdCb a, uint32_t tmt, uint32_t rl) : c(a), timeout(tmt), reload(rl) {}
bool isTimestamp;
sP() : c(NULL), timeout(0), reload(0), isTimestamp(false) {}
sP(scdCb a, uint32_t tmt, uint32_t rl, bool its) : c(a), timeout(tmt), reload(rl), isTimestamp(its) {}
};

struct scdAt_s {
scdCb c;
uint32_t timestamp;
scdAt_s() : c(NULL), timestamp(0) {}
scdAt_s(scdCb a, uint32_t ts) : c(a), timestamp(ts) {}
};
#define MAX_NUM_TICKER 30


typedef node_s<scdEvry_s, scdCb, uint32_t, uint32_t> sP;
typedef node_s<scdAt_s, scdCb, uint32_t> sPAt;
class Scheduler {
public:
Scheduler() {}

void setup() {
mUptime = 0;
mTimestamp = 0;
mMax = 0;
mPrevMillis = millis();
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++)
mTickerInUse[i] = false;
}

void loop(void) {
Expand All @@ -66,37 +61,29 @@ namespace ah {
mUptime += mDiffSeconds;
if(0 != mTimestamp)
mTimestamp += mDiffSeconds;
checkEvery();
checkAt();
checkTicker();

}

void once(scdCb c, uint32_t timeout) { mStack.add(c, timeout, 0); }
void onceAt(scdCb c, uint32_t timestamp) { mStackAt.add(c, timestamp); }
uint8_t every(scdCb c, uint32_t interval){ return mStack.add(c, interval, interval)->id; }
void once(scdCb c, uint32_t timeout) { addTicker(c, timeout, 0, false); }
void onceAt(scdCb c, uint32_t timestamp) { addTicker(c, timestamp, 0, true); }
uint8_t every(scdCb c, uint32_t interval){ return addTicker(c, interval, interval, false); }

void everySec(scdCb c) { mStack.add(c, SCD_SEC, SCD_SEC); }
void everyMin(scdCb c) { mStack.add(c, SCD_MIN, SCD_MIN); }
void everyHour(scdCb c) { mStack.add(c, SCD_HOUR, SCD_HOUR); }
void every12h(scdCb c) { mStack.add(c, SCD_12H, SCD_12H); }
void everyDay(scdCb c) { mStack.add(c, SCD_DAY, SCD_DAY); }
void everySec(scdCb c) { every(c, SCD_SEC); }
void everyMin(scdCb c) { every(c, SCD_MIN); }
void everyHour(scdCb c) { every(c, SCD_HOUR); }
void every12h(scdCb c) { every(c, SCD_12H); }
void everyDay(scdCb c) { every(c, SCD_DAY); }

virtual void setTimestamp(uint32_t ts) {
mTimestamp = ts;
}

bool resetEveryById(uint8_t id) {
sP *p = mStack.getFront();
while(NULL != p) {
if(p->id == id)
break;
p = mStack.get(p);
}
if(NULL != p) {
p->d.timeout = p->d.reload;
return true;
}
return false;
if (mTickerInUse[id] == false)
return false;
mTicker[id].timeout = mTicker[id].reload;
return true;
}

uint32_t getUptime(void) {
Expand All @@ -107,53 +94,57 @@ namespace ah {
return mTimestamp;
}

void getStat(uint16_t *everyMax, uint16_t *atMax) {
*everyMax = mStack.getMaxFill();
*atMax = mStackAt.getMaxFill();
void getStat(uint8_t *max) {
*max = mMax;
}

protected:
uint32_t mTimestamp;

private:
inline void checkEvery(void) {
sP *p = mStack.getFront();
while(NULL != p) {
if(mDiffSeconds >= p->d.timeout) { // expired
(p->d.c)();
yield();
if(0 == p->d.reload)
p = mStack.rem(p);
else {
p->d.timeout = p->d.reload;
p = mStack.get(p);
}
}
else { // not expired
p->d.timeout -= mDiffSeconds;
p = mStack.get(p);
inline uint8_t addTicker(scdCb c, uint32_t timeout, uint32_t reload, bool isTimestamp) {
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++) {
if (!mTickerInUse[i]) {
mTickerInUse[i] = true;
mTicker[i].c = c;
mTicker[i].timeout = timeout;
mTicker[i].reload = reload;
mTicker[i].isTimestamp = isTimestamp;
if(mMax == i)
mMax = i + 1;
return i;
}
}
return 0xff;
}

inline void checkAt(void) {
sPAt *p = mStackAt.getFront();
while(NULL != p) {
if((p->d.timestamp) <= mTimestamp) {
(p->d.c)();
yield();
p = mStackAt.rem(p);
inline void checkTicker(void) {
bool inUse[MAX_NUM_TICKER];
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++)
inUse[i] = mTickerInUse[i];
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++) {
if (inUse[i]) {
if (mTicker[i].timeout <= ((mTicker[i].isTimestamp) ? mTimestamp : mDiffSeconds)) { // expired
if(0 == mTicker[i].reload)
mTickerInUse[i] = false;
else
mTicker[i].timeout = mTicker[i].reload;
(mTicker[i].c)();
yield();
}
else // not expired
if (!mTicker[i].isTimestamp)
mTicker[i].timeout -= mDiffSeconds;
}
else
p = mStackAt.get(p);
}
}

llist<20, scdEvry_s, scdCb, uint32_t, uint32_t> mStack;
llist<10, scdAt_s, scdCb, uint32_t> mStackAt;
sP mTicker[MAX_NUM_TICKER];
bool mTickerInUse[MAX_NUM_TICKER];
uint32_t mMillis, mPrevMillis, mDiff;
uint32_t mUptime;
uint8_t mDiffSeconds;
uint8_t mMax;
};
}

Expand Down
7 changes: 3 additions & 4 deletions src/web/RestApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@ class RestApi {
//obj[F("littlefs_total")] = LittleFS.totalBytes();
//obj[F("littlefs_used")] = LittleFS.usedBytes();

uint16_t evry, at;
mApp->getSchedulerInfo(&evry, &at);
obj[F("schEvryMax")] = evry;
obj[F("schAtMax")] = at;
uint8_t max;
mApp->getSchedulerInfo(&max);
obj[F("schMax")] = max;
}

void getHtmlSystem(JsonObject obj) {
Expand Down
12 changes: 10 additions & 2 deletions src/web/html/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
</div>
<div id="wrapper">
<div id="content">
<form method="POST" action="/update" enctype="multipart/form-data" accept-charset="utf-8">
<input type="file" name="update"><input type="submit" value="Update">
<form id="form" method="POST" action="/update" enctype="multipart/form-data" accept-charset="utf-8">
<input type="file" name="update">
<input type="button" class="btn" value="Update" onclick="hide()">
</form>

</div>
</div>
<div id="footer">
Expand Down Expand Up @@ -54,6 +56,12 @@
}
}

function hide() {
document.getElementById("form").submit();
var e = document.getElementById("content");
e.replaceChildren(span("update started"));
}

getAjax("/api/index", parse);
</script>
</body>
Expand Down

0 comments on commit ed16cff

Please sign in to comment.