Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work/non template container #47

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6c0aec4
Create a template-free alternative to EmbAJAXContainer
tfry-git Jan 12, 2020
6aa6335
This EmbAJAXElementList c'tor looks fairly promising, but I'll have t…
tfry-git Jan 19, 2020
790e6ad
Make variadic c'tor type-safe
tfry-git Jan 19, 2020
57643ae
Adds some more constness. This can help save some bytes
tfry-git Jan 19, 2020
54cb2bf
More work on EmbAJAXElementList
tfry-git Jan 10, 2022
c3938ee
Remove obsolete EmbAJAXPageBase class
tfry-git Jan 10, 2022
501b7b2
Merge branch 'master' into work/non-template-container
tfry-git Jan 10, 2022
de150cc
Deprecate obsolete classes / functions
tfry-git Jan 10, 2022
1e630d0
Add convenience constructors to page and hideable container.
tfry-git Jan 10, 2022
ba1823d
Update examples
tfry-git Jan 10, 2022
2745014
- Fix crash due to array of group button pointers going out of scope.
tfry-git Jan 12, 2022
a52813c
Fix comment.
tfry-git Jan 12, 2022
37e187d
Continue work on non-templated containers
tfry-git Mar 12, 2023
d77a4b4
Elaborate comment
tfry-git Mar 12, 2023
55cf808
Merge branch 'master' into work/non-template-container
tfry-git Apr 29, 2023
556d8e0
New draft for EmbAJAXElementList c'tor; small refinements
tfry-git Apr 30, 2023
0fb7c7a
Merge branch 'master' into work/non-template-container
tfry-git Apr 30, 2023
03274bd
Tweak constructors some more
tfry-git Apr 30, 2023
a701a3f
Avoid reorder (fix compilation on overly pedantic compiler)
tfry-git Apr 30, 2023
8b0e770
More constexpr constructors
tfry-git May 2, 2023
592e376
Convert radio group to non-templated (with compatiblity layer, for now)
tfry-git May 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions EmbAJAX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ void EmbAJAXConnectionIndicator::print() const {

////////////////////////////// EmbAJAXElement /////////////////////////////

/** @param id: The id for the element. Note that the string is not copied. Do not use a temporary string in this place. Also, do keep it short. */
EmbAJAXElement::EmbAJAXElement(const char* id) : EmbAJAXBase() {
_id = id;
_flags = 1 << EmbAJAXBase::Visibility | 1 << EmbAJAXBase::Enabledness;
revision = 1;
}

bool EmbAJAXElement::sendUpdates(uint16_t since, bool first) {
if (!changed(since)) return false;
if (!first) _driver->printContent(",\n");
Expand Down Expand Up @@ -209,21 +202,21 @@ void EmbAJAXElement::printTextInput(size_t SIZE, const char* _value) const {

//////////////////////// EmbAJAXContainer ////////////////////////////////////

void EmbAJAXBase::printChildren(EmbAJAXBase** _children, size_t NUM) const {
void EmbAJAXBase::printChildren(EmbAJAXBase* const* _children, size_t NUM) const {
for (size_t i = 0; i < NUM; ++i) {
_children[i]->print();
}
}

bool EmbAJAXBase::sendUpdates(EmbAJAXBase** _children, size_t NUM, uint16_t since, bool first) {
bool EmbAJAXBase::sendUpdates(EmbAJAXBase* const* _children, size_t NUM, uint16_t since, bool first) {
for (size_t i = 0; i < NUM; ++i) {
bool sent = _children[i]->sendUpdates(since, first);
if (sent) first = false;
}
return !first;
}

EmbAJAXElement* EmbAJAXBase::findChild(EmbAJAXBase** _children, size_t NUM, const char*id) const {
EmbAJAXElement* EmbAJAXBase::findChild(EmbAJAXBase* const* _children, size_t NUM, const char*id) const {
for (size_t i = 0; i < NUM; ++i) {
EmbAJAXElement* child = _children[i]->toElement();
if (child) {
Expand Down Expand Up @@ -529,12 +522,13 @@ void EmbAJAXOptionSelectBase::updateFromDriverArg(const char* argname) {

//////////////////////// EmbAJAXPage /////////////////////////////

void EmbAJAXBase::printPage(EmbAJAXBase** _children, size_t NUM, const char* _title, const char* _header_add, uint16_t _min_interval) const {
void EmbAJAXPage::print() const {
#if EMBAJAX_DEBUG > 2
time_t start = millis();
#endif

_driver->printHeader(true);
_driver->printFormatted("<!DOCTYPE html>\n<HTML><HEAD><TITLE>", PLAIN_STRING(_title), "</TITLE>\n<SCRIPT>\n"
_driver->printFormatted("<!DOCTYPE html>\n<HTML><HEAD><TITLE>", PLAIN_STRING(p.title), "</TITLE>\n<SCRIPT>\n"

"var serverrevision = 0;\n"
"var request_queue = [];\n" // requests waiting to be sent
Expand All @@ -553,7 +547,7 @@ void EmbAJAXBase::printPage(EmbAJAXBase** _children, size_t NUM, const char* _ti
"var prev_request = 0;\n"
"function sendQueued() {\n"
" var now = new Date().getTime();\n"
" if (num_waiting > 0 || (now - prev_request < ", INTEGER_VALUE(_min_interval), ")) return;\n"
" if (num_waiting > 0 || (now - prev_request < ", INTEGER_VALUE(p.min_interval), ")) return;\n"
" var e = request_queue.shift();\n"
" if (!e && (now - prev_request < 1000)) return;\n"
" if (!e) e = {id: '', value: ''};\n" //Nothing in queue, but last request more than 1000 ms ago? Send a ping to query for updates
Expand All @@ -573,7 +567,7 @@ void EmbAJAXBase::printPage(EmbAJAXBase** _children, size_t NUM, const char* _ti
" req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');\n"
" req.send('id=' + e.id + '&value=' + encodeURIComponent(e.value) + '&revision=' + serverrevision);\n"
"}\n"
"window.setInterval(sendQueued, ", INTEGER_VALUE(_min_interval/2+1), ");\n"
"window.setInterval(sendQueued, ", INTEGER_VALUE(p.min_interval/2+1), ");\n"

"function doUpdates(response) {\n"
" serverrevision = response.revision;\n"
Expand All @@ -595,7 +589,7 @@ void EmbAJAXBase::printPage(EmbAJAXBase** _children, size_t NUM, const char* _ti
" }\n"
"}\n"

"</SCRIPT>\n", PLAIN_STRING(_header_add),
"</SCRIPT>\n", PLAIN_STRING(p.header_add),
"</HEAD>\n<BODY><FORM autocomplete=\"off\" onSubmit=\"return false;\">\n");
// NOTE: The nasty thing about autocomplete is that it does not trigger onChange() functions, but also the
// "restore latest settings after client reload" is questionable in our use-case.
Expand All @@ -611,7 +605,8 @@ void EmbAJAXBase::printPage(EmbAJAXBase** _children, size_t NUM, const char* _ti
#endif
}

void EmbAJAXBase::handleRequest(EmbAJAXBase** _children, size_t NUM, void (*change_callback)()) {
void EmbAJAXPage::handleRequest(void (*change_callback)()) {
_latest_ping = millis();
char conversion_buf[EMBAJAX_MAX_ID_LEN];

// handle value changes sent from client
Expand Down Expand Up @@ -657,7 +652,7 @@ void EmbAJAXBase::handleRequest(EmbAJAXBase** _children, size_t NUM, void (*chan
// then relay value changes that have occured in the server (possibly in response to those sent)
_driver->printHeader(false);
_driver->printFormatted("{\"revision\": ", INTEGER_VALUE(_driver->revision()), ",\n\"updates\": [\n");
sendUpdates(_children, NUM, client_revision, true);
sendUpdates(client_revision, true);
_driver->printContent("\n]}\n");

/* Explanation on revision handling:
Expand Down
Loading