-
Notifications
You must be signed in to change notification settings - Fork 80
Add custom OT requests #138
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,30 @@ class OpenThermTask : public Task { | |
| delete this->instance; | ||
| } | ||
|
|
||
| struct ReadResult{ | ||
| bool valid = false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Добавить поле status:
|
||
| bool parityValid = false; | ||
| bool responseMessageIdValid = false; | ||
| const char* responseType = ""; | ||
| uint16_t value = 0; | ||
| }; | ||
|
|
||
| ReadResult readRequest(byte messageId) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вместо этого добавить метод Добавить метод Внутри |
||
| ReadResult result; | ||
| OpenThermMessageID eMessageId = (OpenThermMessageID)messageId; | ||
| auto response = this->instance->sendRequest(CustomOpenTherm::buildRequest( | ||
| OpenThermRequestType::READ_DATA, | ||
| eMessageId, | ||
| 0 | ||
| )); | ||
| result.valid = CustomOpenTherm::isValidResponse(response); | ||
| result.parityValid = !CustomOpenTherm::parity(response); | ||
| result.responseMessageIdValid = CustomOpenTherm::isValidResponseId(response, eMessageId); | ||
| result.responseType = CustomOpenTherm::messageTypeToString(CustomOpenTherm::getMessageType(response)); | ||
| result.value = CustomOpenTherm::getUInt(response); | ||
| return result; | ||
| } | ||
|
|
||
| protected: | ||
| const unsigned short readyTime = 60000u; | ||
| const unsigned int resetBusInterval = 120000u; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ using namespace NetworkUtils; | |
| extern NetworkMgr* network; | ||
| extern FileData fsNetworkSettings, fsSettings, fsSensorsSettings; | ||
| extern MqttTask* tMqtt; | ||
| extern OpenThermTask* tOt; | ||
|
|
||
|
|
||
| class PortalTask : public LeanTask { | ||
|
|
@@ -177,6 +178,18 @@ class PortalTask : public LeanTask { | |
| }); | ||
| this->webServer->addHandler(upgradePage); | ||
|
|
||
| // Opentherm request page | ||
| auto openthermRequestPage = (new StaticPage("/opentherm_request.html", &LittleFS, F("/pages/opentherm_request.html"), PORTAL_CACHE)) | ||
| ->setBeforeSendCallback([this]() { | ||
| if (this->isAuthRequired() && !this->isValidCredentials()) { | ||
| this->webServer->requestAuthentication(BASIC_AUTH); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
| this->webServer->addHandler(openthermRequestPage); | ||
|
|
||
| // OTA | ||
| auto upgradeHandler = (new UpgradeHandler("/api/upgrade"))->setCanUploadCallback([this](const String& uri) { | ||
| if (this->isAuthRequired() && !this->isValidCredentials()) { | ||
|
|
@@ -841,6 +854,41 @@ class PortalTask : public LeanTask { | |
| this->bufferedWebServer->send(200, F("application/json"), doc, true); | ||
| }); | ||
|
|
||
| this->webServer->on(F("/api/opentherm_request/read"), HTTP_POST, [this]() { | ||
| if (this->isAuthRequired() && !this->isValidCredentials()) { | ||
| return this->webServer->send(401); | ||
| } | ||
|
|
||
| const String& plain = this->webServer->arg(0); | ||
| Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Request /api/opentherm_request/read %d bytes: %s"), plain.length(), plain.c_str()); | ||
|
|
||
| JsonDocument doc; | ||
| DeserializationError dErr = deserializeJson(doc, plain); | ||
|
|
||
| if (dErr != DeserializationError::Ok || doc.isNull() || !doc.size()) { | ||
| this->webServer->send(400); | ||
| return; | ||
| } | ||
|
|
||
| if (doc[FPSTR(S_MESSAGE_ID)].isNull() || !doc[FPSTR(S_MESSAGE_ID)].is<byte>()) { | ||
| this->webServer->send(400); | ||
| return; | ||
| } | ||
| auto messageId = doc[FPSTR(S_MESSAGE_ID)].as<byte>(); | ||
| doc.clear(); | ||
| doc.shrinkToFit(); | ||
|
|
||
| auto result = tOt->readRequest(messageId); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вместо этого вызывать |
||
|
|
||
| doc[FPSTR(S_VALID)] = result.valid; | ||
| doc[FPSTR(S_PARITY_VALID)] = result.parityValid; | ||
| doc[FPSTR(S_RESPONSE_MESSAGE_ID_VALID)] = result.responseMessageIdValid; | ||
| doc[FPSTR(S_RESPONSE_TYPE)] = result.responseType; | ||
| doc[FPSTR(S_VALUE)] = result.value; | ||
| doc.shrinkToFit(); | ||
|
|
||
| this->bufferedWebServer->send(200, F("application/json"), doc); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Просто вернуть пустой ответ с кодом 200 |
||
| }); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Добавить обработчик для чтения данных через |
||
|
|
||
| // not found | ||
| this->webServer->onNotFound([this]() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Переименовать в ExternalRequest