mirror of https://github.com/arendst/Tasmota.git
Merge pull request #14867 from s-hadinger/berry_mqtt
Berry add module mqtt
This commit is contained in:
commit
4d311d7c86
|
@ -30,6 +30,7 @@ be_extern_native_module(cb);
|
|||
/* Tasmota specific */
|
||||
be_extern_native_module(python_compat);
|
||||
be_extern_native_module(re);
|
||||
be_extern_native_module(mqtt);
|
||||
be_extern_native_module(persist);
|
||||
be_extern_native_module(autoconf);
|
||||
be_extern_native_module(tapp);
|
||||
|
@ -108,6 +109,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = {
|
|||
&be_native_module(python_compat),
|
||||
&be_native_module(re),
|
||||
&be_native_module(path),
|
||||
&be_native_module(mqtt),
|
||||
&be_native_module(persist),
|
||||
#ifdef USE_AUTOCONF
|
||||
&be_native_module(autoconf),
|
||||
|
|
|
@ -760,6 +760,7 @@ extern const bcstring be_const_str_strftime;
|
|||
extern const bcstring be_const_str_string;
|
||||
extern const bcstring be_const_str_strip;
|
||||
extern const bcstring be_const_str_strptime;
|
||||
extern const bcstring be_const_str_subscribe;
|
||||
extern const bcstring be_const_str_success;
|
||||
extern const bcstring be_const_str_super;
|
||||
extern const bcstring be_const_str_sys;
|
||||
|
@ -796,6 +797,7 @@ extern const bcstring be_const_str_type;
|
|||
extern const bcstring be_const_str_type_error;
|
||||
extern const bcstring be_const_str_udp;
|
||||
extern const bcstring be_const_str_unknown_X20instruction;
|
||||
extern const bcstring be_const_str_unsubscribe;
|
||||
extern const bcstring be_const_str_update;
|
||||
extern const bcstring be_const_str_upper;
|
||||
extern const bcstring be_const_str_url_encode;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,19 @@
|
|||
#include "be_constobj.h"
|
||||
|
||||
static be_define_const_map_slots(m_libmqtt_map) {
|
||||
{ be_const_key(unsubscribe, -1), be_const_ctype_func(be_mqtt_unsubscribe) },
|
||||
{ be_const_key(subscribe, -1), be_const_ctype_func(be_mqtt_subscribe) },
|
||||
{ be_const_key(publish, 1), be_const_func(be_mqtt_publish) },
|
||||
};
|
||||
|
||||
static be_define_const_map(
|
||||
m_libmqtt_map,
|
||||
3
|
||||
);
|
||||
|
||||
static be_define_const_module(
|
||||
m_libmqtt,
|
||||
"mqtt"
|
||||
);
|
||||
|
||||
BE_EXPORT_VARIABLE be_define_const_native_module(mqtt);
|
|
@ -0,0 +1,21 @@
|
|||
/********************************************************************
|
||||
* Berry module `mqtt`
|
||||
*
|
||||
*******************************************************************/
|
||||
|
||||
#include "be_constobj.h"
|
||||
#include "be_mapping.h"
|
||||
|
||||
extern int be_mqtt_publish(bvm *vm);
|
||||
|
||||
extern void be_mqtt_subscribe(const char* topic); BE_FUNC_CTYPE_DECLARE(be_mqtt_subscribe, "", "s")
|
||||
extern void be_mqtt_unsubscribe(const char* topic); BE_FUNC_CTYPE_DECLARE(be_mqtt_unsubscribe, "", "s")
|
||||
|
||||
/* @const_object_info_begin
|
||||
module mqtt (scope: global) {
|
||||
publish, func(be_mqtt_publish)
|
||||
subscribe, ctype_func(be_mqtt_subscribe)
|
||||
unsubscribe, ctype_func(be_mqtt_unsubscribe)
|
||||
}
|
||||
@const_object_info_end */
|
||||
#include "be_fixed_mqtt.h"
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
xdrv_52_3_berry_mqtt.ino - Berry scripting language, native fucnctions
|
||||
|
||||
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Mappgin from internal light and a generic `light_state` Berry class
|
||||
|
||||
#ifdef USE_BERRY
|
||||
|
||||
#include "berry.h"
|
||||
|
||||
// Berry: `tasmota.publish(topic, payload [, retain:bool, start:int, len:int]) -> nil``
|
||||
// is_method is true if called from Tasmota class, false if called from mqtt module
|
||||
int32_t be_mqtt_publish(struct bvm *vm, bool is_method) {
|
||||
int32_t top = be_top(vm); // Get the number of arguments
|
||||
if (top >= 2+is_method && be_isstring(vm, 1+is_method) && (be_isstring(vm, 2+is_method) || be_isbytes(vm, 2+is_method))) { // 2 mandatory string arguments
|
||||
bool retain = false;
|
||||
int32_t payload_start = 0;
|
||||
int32_t len = -1; // send all of it
|
||||
if (top >= 3+is_method) { retain = be_tobool(vm, 3+is_method); }
|
||||
if (top >= 4+is_method) {
|
||||
payload_start = be_toint(vm, 4+is_method);
|
||||
if (payload_start < 0) payload_start = 0;
|
||||
}
|
||||
if (top >= 5+is_method) { len = be_toint(vm, 5+is_method); }
|
||||
const char * topic = be_tostring(vm, 1+is_method);
|
||||
const char * payload = nullptr;
|
||||
size_t payload_len = 0;
|
||||
|
||||
if (be_isstring(vm, 2+is_method)) {
|
||||
payload = be_tostring(vm, 2+is_method);
|
||||
payload_len = strlen(payload);
|
||||
} else {
|
||||
payload = (const char *) be_tobytes(vm, 2+is_method, &payload_len);
|
||||
}
|
||||
if (!payload) { be_raise(vm, "value_error", "Empty payload"); }
|
||||
|
||||
// adjust start and len
|
||||
if (payload_start >= payload_len) { len = 0; } // send empty packet
|
||||
else if (len < 0) { len = payload_len - payload_start; } // send all packet, adjust len
|
||||
else if (payload_start + len > payload_len) { len = payload_len - payload_start; } // len is too long, adjust
|
||||
// adjust start
|
||||
payload = payload + payload_start;
|
||||
|
||||
be_pop(vm, be_top(vm)); // clear stack to avoid any indirect warning message in subsequent calls to Berry
|
||||
|
||||
MqttPublishPayload(topic, payload, len, retain);
|
||||
|
||||
be_return_nil(vm); // Return
|
||||
}
|
||||
be_raise(vm, kTypeError, nullptr);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
int32_t l_publish(struct bvm *vm);
|
||||
int32_t l_publish(struct bvm *vm) {
|
||||
return be_mqtt_publish(vm, true);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t be_mqtt_publish(struct bvm *vm) {
|
||||
return be_mqtt_publish(vm, false);
|
||||
}
|
||||
|
||||
void be_mqtt_subscribe(const char* topic) {
|
||||
if (!topic) { return; }
|
||||
MqttSubscribe(topic);
|
||||
}
|
||||
|
||||
void be_mqtt_unsubscribe(const char* topic) {
|
||||
if (!topic) { return; }
|
||||
MqttUnsubscribe(topic);
|
||||
}
|
||||
|
||||
#endif // USE_BERRY
|
|
@ -68,49 +68,6 @@ const uint32_t BERRY_MAX_REPL_LOGS = 1024; // max number of print output recor
|
|||
*
|
||||
\*********************************************************************************************/
|
||||
extern "C" {
|
||||
// Berry: `tasmota.publish(topic, payload [, retain:bool, start:int, len:int]) -> nil``
|
||||
//
|
||||
int32_t l_publish(struct bvm *vm);
|
||||
int32_t l_publish(struct bvm *vm) {
|
||||
int32_t top = be_top(vm); // Get the number of arguments
|
||||
if (top >= 3 && be_isstring(vm, 2) && (be_isstring(vm, 3) || be_isbytes(vm, 3))) { // 2 mandatory string arguments
|
||||
bool retain = false;
|
||||
int32_t payload_start = 0;
|
||||
int32_t len = -1; // send all of it
|
||||
if (top >= 4) { retain = be_tobool(vm, 4); }
|
||||
if (top >= 5) {
|
||||
payload_start = be_toint(vm, 5);
|
||||
if (payload_start < 0) payload_start = 0;
|
||||
}
|
||||
if (top >= 6) { len = be_toint(vm, 6); }
|
||||
const char * topic = be_tostring(vm, 2);
|
||||
const char * payload = nullptr;
|
||||
size_t payload_len = 0;
|
||||
|
||||
if (be_isstring(vm, 3)) {
|
||||
payload = be_tostring(vm, 3);
|
||||
payload_len = strlen(payload);
|
||||
} else {
|
||||
payload = (const char *) be_tobytes(vm, 3, &payload_len);
|
||||
}
|
||||
if (!payload) { be_raise(vm, "value_error", "Empty payload"); }
|
||||
|
||||
// adjust start and len
|
||||
if (payload_start >= payload_len) { len = 0; } // send empty packet
|
||||
else if (len < 0) { len = payload_len - payload_start; } // send all packet, adjust len
|
||||
else if (payload_start + len > payload_len) { len = payload_len - payload_start; } // len is too long, adjust
|
||||
// adjust start
|
||||
payload = payload + payload_start;
|
||||
|
||||
be_pop(vm, be_top(vm)); // clear stack to avoid any indirect warning message in subsequent calls to Berry
|
||||
|
||||
MqttPublishPayload(topic, payload, len, retain);
|
||||
|
||||
be_return_nil(vm); // Return
|
||||
}
|
||||
be_raise(vm, kTypeError, nullptr);
|
||||
}
|
||||
|
||||
// Berry: `tasmota.publish_result(payload:string, subtopic:string) -> nil``
|
||||
//
|
||||
int32_t l_publish_result(struct bvm *vm);
|
||||
|
|
Loading…
Reference in New Issue