From 188d6c86c1781b0ca113cca0e4d2d369264356f1 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 1 Dec 2021 13:52:48 +0100 Subject: [PATCH] Improve set_power_handler --- tasmota/xdrv_52_3_berry_tasmota.ino | 15 +++++++++++++-- tasmota/xdrv_52_9_berry.ino | 6 +++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tasmota/xdrv_52_3_berry_tasmota.ino b/tasmota/xdrv_52_3_berry_tasmota.ino index 83de43424..ee65d27a1 100644 --- a/tasmota/xdrv_52_3_berry_tasmota.ino +++ b/tasmota/xdrv_52_3_berry_tasmota.ino @@ -92,6 +92,7 @@ extern "C" { retain = be_tobool(vm, 4); } if (!payload) { be_raise(vm, "value_error", "Empty payload"); } + be_pop(vm, be_top(vm)); MqttPublishPayload(topic, payload, payload_len, retain); be_return_nil(vm); // Return } @@ -122,7 +123,7 @@ extern "C" { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isstring(vm, 2)) { // only 1 argument of type string accepted const char * command = be_tostring(vm, 2); - be_pop(vm, 2); // clear the stack before calling, because of re-entrant call to Berry in a Rule + be_pop(vm, top); // clear the stack before calling, because of re-entrant call to Berry in a Rule ExecuteCommand(command, SRC_BERRY); be_return_nil(vm); // Return } @@ -390,6 +391,7 @@ extern "C" { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isstring(vm, 2)) { const char *msg = be_tostring(vm, 2); + be_pop(vm, top); // avoid Error be_top is non zero message ResponseAppend_P(PSTR("%s"), msg); be_return_nil(vm); // Return nil when something goes wrong } @@ -402,6 +404,7 @@ extern "C" { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isstring(vm, 2)) { const char *msg = be_tostring(vm, 2); + be_pop(vm, top); // avoid Error be_top is non zero message WSContentSend_P(PSTR("%s"), msg); be_return_nil(vm); // Return nil when something goes wrong } @@ -414,6 +417,7 @@ extern "C" { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isstring(vm, 2)) { const char *msg = be_tostring(vm, 2); + be_pop(vm, top); // avoid Error be_top is non zero message WSContentSend_PD(PSTR("%s"), msg); be_return_nil(vm); // Return nil when something goes wrong } @@ -423,9 +427,14 @@ extern "C" { // get power int32_t l_getpower(bvm *vm); int32_t l_getpower(bvm *vm) { + power_t pow = TasmotaGlobal.power; + int32_t top = be_top(vm); // Get the number of arguments + if (top == 2 && be_isint(vm, 2)) { + pow = be_toint(vm, 2); + } be_newobject(vm, "list"); for (uint32_t i = 0; i < TasmotaGlobal.devices_present; i++) { - be_pushbool(vm, bitRead(TasmotaGlobal.power, i)); + be_pushbool(vm, bitRead(pow, i)); be_data_push(vm, -2); be_pop(vm, 1); } @@ -440,6 +449,7 @@ extern "C" { int32_t idx = be_toint(vm, 2); bool power = be_tobool(vm, 3); if ((idx >= 0) && (idx < TasmotaGlobal.devices_present)) { + be_pop(vm, top); // avoid Error be_top is non zero message ExecuteCommandPower(idx + 1, (power) ? POWER_ON : POWER_OFF, SRC_BERRY); be_pushbool(vm, power); be_return(vm); // Return @@ -473,6 +483,7 @@ extern "C" { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isint(vm, 2)) { int32_t index = be_toint(vm, 2); + be_pop(vm, top); // avoid Error be_top is non zero message bool enabled = I2cEnabled(index); be_pushbool(vm, enabled); be_return(vm); // Return diff --git a/tasmota/xdrv_52_9_berry.ino b/tasmota/xdrv_52_9_berry.ino index bee52522e..6596782c1 100644 --- a/tasmota/xdrv_52_9_berry.ino +++ b/tasmota/xdrv_52_9_berry.ino @@ -214,7 +214,7 @@ int32_t callBerryEventDispatcher(const char *type, const char *cmd, int32_t idx, be_pushstring(vm, type != nullptr ? type : ""); be_pushstring(vm, cmd != nullptr ? cmd : ""); be_pushint(vm, idx); - be_pushstring(vm, payload != nullptr ? payload : "{}"); // empty json + be_pushstring(vm, payload != nullptr ? payload : ""); // empty json BrTimeoutStart(); if (data_len > 0) { be_pushbytes(vm, payload, data_len); // if data_len is set, we also push raw bytes @@ -811,8 +811,8 @@ bool Xdrv52(uint8_t function) case FUNC_EVERY_SECOND: callBerryEventDispatcher(PSTR("every_second"), nullptr, 0, nullptr); break; - case FUNC_SET_POWER: - callBerryEventDispatcher(PSTR("set_power_handler"), nullptr, XdrvMailbox.index, nullptr); + case FUNC_SET_DEVICE_POWER: + result = callBerryEventDispatcher(PSTR("set_power_handler"), nullptr, XdrvMailbox.index, nullptr); break; #ifdef USE_WEBSERVER case FUNC_WEB_ADD_CONSOLE_BUTTON: