added vars as topics

pull/16/head
Martin Ger 2017-08-17 08:46:19 +02:00
rodzic 3117ba6b53
commit 4d941993f5
4 zmienionych plików z 70 dodań i 41 usunięć

Wyświetl plik

@ -434,27 +434,35 @@ int ICACHE_FLASH_ATTR parse_event(int next_token, bool * happend) {
} }
if (is_token(next_token, "topic")) { if (is_token(next_token, "topic")) {
lang_debug("event topic\r\n"); char *topic;
int topic_len;
Value_Type topic_type;
int lr_token = next_token + 1;
lang_debug("event topic\r\n");
in_topic_statement = true; in_topic_statement = true;
len_check(2); len_check(2);
if (is_token(next_token + 1, "remote")) { if ((next_token = parse_value(next_token + 2, &topic, &topic_len, &topic_type)) == -1)
return -1;
if (is_token(lr_token, "remote")) {
if (interpreter_status != TOPIC_REMOTE) if (interpreter_status != TOPIC_REMOTE)
return next_token + 3; return next_token;
} else if (is_token(next_token + 1, "local")) { } else if (is_token(lr_token, "local")) {
if (interpreter_status != TOPIC_LOCAL) if (interpreter_status != TOPIC_LOCAL)
return next_token + 3; return next_token;
} else { } else {
return syntax_error(next_token + 1, "'local' or 'remote' expected"); return syntax_error(next_token + 1, "'local' or 'remote' expected");
} }
*happend = Topics_matches(my_token[next_token + 2], true, interpreter_topic); *happend = Topics_matches(topic, true, interpreter_topic);
if (*happend) if (*happend)
lang_info("topic %s %s %s match\r\n", my_token[next_token + 1], lang_info("topic %s %s match\r\n", my_token[lr_token],
my_token[next_token + 2], interpreter_topic); topic, interpreter_topic);
return next_token + 3; return next_token;
} }
if (is_token(next_token, "timer")) { if (is_token(next_token, "timer")) {
@ -589,50 +597,59 @@ int ICACHE_FLASH_ATTR parse_action(int next_token, bool doit) {
} }
else if (is_token(next_token, "subscribe")) { else if (is_token(next_token, "subscribe")) {
char *topic;
int topic_len;
Value_Type topic_type;
bool retval; bool retval;
int rl_token = next_token + 1;
len_check(2); len_check(2);
if ((next_token = parse_value(next_token + 2, &topic, &topic_len, &topic_type)) == -1)
return -1;
#ifdef MQTT_CLIENT #ifdef MQTT_CLIENT
if (is_token(next_token + 1, "remote")) { if (is_token(rl_token, "remote")) {
if (doit && mqtt_connected) { if (doit && mqtt_connected) {
retval = MQTT_Subscribe(&mqttClient, my_token[next_token + 2], 0); retval = MQTT_Subscribe(&mqttClient, topic, 0);
lang_info("subscribe remote %s %s\r\n", my_token[next_token + 2], retval ? "success" : "failed"); lang_info("subscribe remote %s %s\r\n", topic, retval ? "success" : "failed");
} }
} else } else
#endif #endif
if (is_token(next_token + 1, "local")) { if (is_token(rl_token, "local")) {
if (doit) { if (doit) {
retval = MQTT_local_subscribe(my_token[next_token + 2], 0); retval = MQTT_local_subscribe(topic, 0);
lang_info("subscribe local %s %s\r\n", my_token[next_token + 2], retval ? "success" : "failed"); lang_info("subscribe local %s %s\r\n", topic, retval ? "success" : "failed");
} }
} else { } else {
return syntax_error(next_token + 1, "'local' or 'remote' expected"); return syntax_error(next_token + 1, "'local' or 'remote' expected");
} }
next_token += 3;
} }
else if (is_token(next_token, "unsubscribe")) { else if (is_token(next_token, "unsubscribe")) {
char *topic;
int topic_len;
Value_Type topic_type;
bool retval; bool retval;
int rl_token = next_token + 1;
len_check(2); len_check(2);
if ((next_token = parse_value(next_token + 2, &topic, &topic_len, &topic_type)) == -1)
return -1;
#ifdef MQTT_CLIENT #ifdef MQTT_CLIENT
if (is_token(next_token + 1, "remote")) { if (is_token(rl_token, "remote")) {
if (doit && mqtt_connected) { if (doit && mqtt_connected) {
retval = MQTT_UnSubscribe(&mqttClient, my_token[next_token + 2]); retval = MQTT_UnSubscribe(&mqttClient, topic);
lang_info("unsubsrcibe remote %s %s\r\n", my_token[next_token + 2], retval ? "success" : "failed"); lang_info("unsubsrcibe remote %s %s\r\n", topic, retval ? "success" : "failed");
} }
} else } else
#endif #endif
if (is_token(next_token + 1, "local")) { if (is_token(rl_token, "local")) {
if (doit) { if (doit) {
retval = MQTT_local_unsubscribe(my_token[next_token + 2]); retval = MQTT_local_unsubscribe(topic);
lang_info("unsubsrcibe local %s %s\r\n", my_token[next_token + 2], retval ? "success" : "failed"); lang_info("unsubsrcibe local %s %s\r\n", topic, retval ? "success" : "failed");
} }
} else { } else {
return syntax_error(next_token + 1, "'local' or 'remote' expected"); return syntax_error(next_token + 1, "'local' or 'remote' expected");
} }
next_token += 3;
} }
else if (is_token(next_token, "if")) { else if (is_token(next_token, "if")) {
@ -1080,3 +1097,4 @@ int ICACHE_FLASH_ATTR interpreter_topic_received(const char *topic, const char *
return parse_statement(0); return parse_statement(0);
} }

Wyświetl plik

@ -15,34 +15,40 @@ do
gpio_out 12 $1 gpio_out 12 $1
gpio_out 13 not ($1) gpio_out 13 not ($1)
publish local /martinshome/switch/1/status $1 retained
publish remote /martinshome/switch/1/status $1 retained
% $2 is blink flag % $2 is blink flag
setvar $2=0 setvar $2=0
% $3 is the command topic
setvar $3="/martinshome/switch/1/command"
% $4 is the status topic
setvar $4="/martinshome/switch/1/status"
publish local $4 $1 retained
publish remote $4 $1 retained
% local subscriptions once in 'init' % local subscriptions once in 'init'
subscribe local /martinshome/switch/1/command subscribe local $3
% Now the MQTT client init, this is done each time the client connects % Now the MQTT client init, this is done each time the client connects
on mqttconnect on mqttconnect
do do
% remote subscriptions for each connection in 'mqttconnect' % remote subscriptions for each connection in 'mqttconnect'
subscribe remote /martinshome/switch/1/command subscribe remote $3
% Now the events, checked whenever something happens % Now the events, checked whenever something happens
% Is there a remote command? % Is there a remote command?
on topic remote /martinshome/switch/1/command on topic remote $3
do do
println "Received remote command: " | $this_data println "Received remote command: " | $this_data
% republish this locally - this does the action % republish this locally - this does the action
publish local /martinshome/switch/1/command $this_data publish local $3 $this_data
% Is there a local command? % Is there a local command?
on topic local /martinshome/switch/1/command on topic local $3
do do
println "Received local command: " | $this_data println "Received local command: " | $this_data
@ -68,8 +74,8 @@ do
settimer 1 500 settimer 1 500
endif endif
publish local /martinshome/switch/1/status $1 retained publish local $4 $1 retained
publish remote /martinshome/switch/1/status $1 retained publish remote $4 $1 retained
% The local pushbutton % The local pushbutton
@ -78,7 +84,7 @@ do
println "New state GPIO 0: " | $this_gpio println "New state GPIO 0: " | $this_gpio
if $this_gpio = 0 then if $this_gpio = 0 then
setvar $2 = 0 setvar $2 = 0
publish local /martinshome/switch/1/command "toggle" publish local $3 "toggle"
endif endif
@ -86,7 +92,7 @@ do
on timer 1 on timer 1
do do
if $2 = 1 then if $2 = 1 then
publish local /martinshome/switch/1/command "toggle" publish local $3 "toggle"
settimer 1 500 settimer 1 500
endif endif
@ -95,9 +101,9 @@ do
% Switch on in the evening % Switch on in the evening
on clock 19:30:00 on clock 19:30:00
do do
publish local /martinshome/switch/1/command "on" publish local $3 "on"
% Switch off at night % Switch off at night
on clock 01:00:00 on clock 01:00:00
do do
publish local /martinshome/switch/1/command "off" publish local $3 "off"

Wyświetl plik

@ -40,7 +40,7 @@ typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_UART0, SIG_TOPIC_RECEIVE
#define MAX_SCRIPT_SIZE 0x1000 #define MAX_SCRIPT_SIZE 0x1000
#define MAX_TIMERS 4 #define MAX_TIMERS 4
#define MAX_GPIOS 3 #define MAX_GPIOS 3
#define MAX_VARS 6 #define MAX_VARS 8
#define MAX_VAR_LEN 64 #define MAX_VAR_LEN 64
#define MAX_TIMESTAMPS 6 #define MAX_TIMESTAMPS 6

Wyświetl plik

@ -365,7 +365,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
if (strcmp(tokens[0], "help") == 0) { if (strcmp(tokens[0], "help") == 0) {
os_sprintf(response, os_sprintf(response,
"show [config|stats|mqtt|script]\r\n|set [ssid|password|auto_connect|ap_ssid|ap_password|network|dns|ip|netmask|gw|ap_on|ap_open|speed|config_port|broker_user|broker_password] <val>\r\n|quit|save [config]|reset [factory]|lock [<password>]|unlock <password>"); "show [config|stats|mqtt|script]\r\n|set [ssid|password|auto_connect|ap_ssid|ap_password|network|dns|ip|netmask|gw|ap_on|ap_open|speed|config_port|config_access|broker_user|broker_password] <val>\r\n|quit|save [config]|reset [factory]|lock [<password>]|unlock <password>");
to_console(response); to_console(response);
#ifdef SCRIPTED #ifdef SCRIPTED
os_sprintf(response, "|script <port>"); os_sprintf(response, "|script <port>");
@ -962,6 +962,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
#ifdef SCRIPTED #ifdef SCRIPTED
void ICACHE_FLASH_ATTR do_command(char *t1, char *t2, char *t3) { void ICACHE_FLASH_ATTR do_command(char *t1, char *t2, char *t3) {
int len;
ringbuf_memcpy_into(console_rx_buffer, t1, os_strlen(t1)); ringbuf_memcpy_into(console_rx_buffer, t1, os_strlen(t1));
ringbuf_memcpy_into(console_rx_buffer, " ", 1); ringbuf_memcpy_into(console_rx_buffer, " ", 1);
@ -969,7 +970,11 @@ void ICACHE_FLASH_ATTR do_command(char *t1, char *t2, char *t3) {
ringbuf_memcpy_into(console_rx_buffer, " ", 1); ringbuf_memcpy_into(console_rx_buffer, " ", 1);
ringbuf_memcpy_into(console_rx_buffer, t3, os_strlen(t3)); ringbuf_memcpy_into(console_rx_buffer, t3, os_strlen(t3));
console_handle_command(0); console_handle_command(0);
ringbuf_memcpy_from(tmp_buffer, console_tx_buffer, ringbuf_bytes_used(console_tx_buffer)); len = ringbuf_bytes_used(console_tx_buffer);
if (len >= sizeof(tmp_buffer))
len = sizeof(tmp_buffer)-1;
ringbuf_memcpy_from(tmp_buffer, console_tx_buffer, len);
tmp_buffer[len] = '\0';
os_printf("%s", tmp_buffer); os_printf("%s", tmp_buffer);
} }
#endif #endif