master
Martin Ger 2017-11-17 16:48:11 +01:00
rodzic bdfa7b0825
commit ffceccf9c3
6 zmienionych plików z 137 dodań i 4 usunięć

Wyświetl plik

@ -54,8 +54,9 @@ In general, scripts conform to the following BNF:
system <expr> |
<action> <action>
<expr> ::= <val> | <val> <op> <expr> | (<expr>) | not (<expr>) | |
retained_topic(<expr>) | json_parse (<expr>,<expr>)
<expr> ::= <val> | <val> <op> <expr> | (<expr>) | not (<expr>) |
retained_topic(<expr>) | substr(<expr>,<num>,<num>) |
json_parse (<expr>,<expr>)
<op> := '=' | '>' | gte | str_ge | str_gte | '+' | '-' | '*' | '|' | div
@ -205,6 +206,11 @@ retained_topic(<expr>)
```
Interpretes the argument as topic name (incl. wildcards) and searches the first local retained topic that matches this name. The stored value of this topic is returned (empty, if nothing found). Can be used to check the status of the system synchronously without the need to subscribe for that retained topic, wait for status changes and store them in a variable.
```
substr(<expr>,<num>,<num>)
```
Extracts characters from a string. The two constant numbers give the starting position (first is postion 0) and the length. If the starting position is negative (write it with colons as e.g. "-2"), it counts backwards from the end of the string.
```
json_parse (<expr>,<expr>)
```

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -1,2 +1,2 @@
11cc1192f65e4ee3c3b1c447b216d63f59c3a990 0x00000.bin
9c255dfd53c2790e94718e94fb47774791831f3f 0x10000.bin
327db48ec28bee48fd33c356c77c8caa6c1ddd0e 0x00000.bin
43bb3f6fbf04f4bdf9b736998fd9c9fd192093c1 0x10000.bin

Wyświetl plik

@ -0,0 +1,69 @@
% Config params, overwrite any previous settings from the commandline
% Nothing here
% Now the events, checked whenever something happens
% Now the initialization, this is done once after booting
on init
do
% Enter your local coordinates here:
setvar $service_url = "https://api.sunrise-sunset.org/json?lat=50.734579&lng=7.090007&date=today&formatted=0"
% Daily lookup time
setalarm 3 01:00:00
on wificonnect
do
println $service_url
http_get $service_url
% Retry on no response
settimer 1 20000
on http_response
do
println "return code: " | $this_http_code
if not($this_http_code = 200) then
% Retry on failue
settimer 1 20000
else
settimer 1 0
println $this_http_body
setvar $sunrise = substr(json_parse("results.sunrise", $this_http_body), 11, 8)
setvar $sunset = substr(json_parse("results.sunset", $this_http_body), 11, 8)
println "Sunrise: " | $sunrise
println "Sunset: " | $sunset
setalarm 1 $sunset
setalarm 2 $sunrise
endif
% Retry timer
on timer 1
do
println "Retry: " | $service_url
http_get $service_url
% Switch on
on alarm 1
do
publish local /time/sunset $sunset
println "Sunset - switch on at " | $timestamp
gpio_out 2 0
% Switch off
on alarm 2
do
publish local /time/sunrise $sunrise
println "Sunrise - switch off at " | $timestamp
gpio_out 2 1
% Get new values
on alarm 3
do
println $service_url
http_get $service_url

Wyświetl plik

@ -1131,6 +1131,64 @@ int ICACHE_FLASH_ATTR parse_expression(int next_token, char **data, int *data_le
}
}
}
else if (is_token(next_token, "substr")) {
lang_debug("val substr\r\n");
len_check(7);
if (syn_chk && !is_token(next_token+1, "("))
return syntax_error(next_token+1, "expected '('");
char *str_data;
int str_data_len;
Value_Type str_data_type;
// parse path string
if ((next_token = parse_expression(next_token + 2, &str_data, &str_data_len, &str_data_type, doit)) == -1)
return -1;
if (!doit)
str_data_len = 0;
char str[str_data_len+1];
if (doit)
os_strcpy(str, str_data);
if (syn_chk && !is_token(next_token, ","))
return syntax_error(next_token, "expected ','");
next_token++;
int16_t from = atoi(my_token[next_token]);
// if as string const
if (my_token[next_token][0] == '"')
from = atoi(&my_token[next_token][1]);
next_token++;
if (syn_chk && !is_token(next_token, ","))
return syntax_error(next_token, "expected ','");
next_token++;
uint16_t len = atoi(my_token[next_token]);
next_token++;
if (syn_chk && !is_token(next_token, ")"))
return syntax_error(next_token, "expected ')'");
next_token++;
if (doit) {
if (from < 0) {
from = str_data_len+from;
if (from < 0)
from = 0;
}
if (len+1 > sizeof(tmp_buffer))
len = sizeof(tmp_buffer)-1;
os_strncpy(tmp_buffer, &str[from], len);
tmp_buffer[len] = '\0';
*data_len = len;
*data = tmp_buffer;
*data_type = STRING_T;
}
}
#ifdef GPIO
else if (is_token(next_token, "gpio_in")) {
lang_debug("val gpio_in\r\n");