Merge pull request #16588 from s-hadinger/urlfetch

Command ``UrlFetch <url>`` to download a file to filesystem
This commit is contained in:
s-hadinger 2022-09-19 19:41:12 +02:00 committed by GitHub
commit af039838b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1504 additions and 1269 deletions

View File

@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- Support of optional file calib.dat on ADE7953 based energy monitors like Shelly EM (#16486)
- Command ``SetOption46 0..255`` to add 0..255 * 10 milliseconds power on delay before initializing I/O (#15438)
- Zigbee support for decimal Voltage/Current/Power on power metering plugs
- Command ``UrlFetch <url>`` to download a file to filesystem
### Changed
- ESP32 Increase number of button GPIOs from 8 to 28 (#16518)

View File

@ -151,6 +151,8 @@ class be_class_tasmota (scope: global, name: Tasmota) {
load, closure(Tasmota_load_closure)
wire_scan, closure(Tasmota_wire_scan_closure)
time_str, closure(Tasmota_time_str_closure)
urlfetch, closure(Tasmota_urlfetch_closure)
urlfetch_cmd, closure(Tasmota_urlfetch_cmd_closure)
add_cron, closure(Tasmota_add_cron_closure)
run_cron, closure(Tasmota_run_cron_closure)

View File

@ -35,6 +35,8 @@ class Tasmota
self._debug_present = true
except ..
end
# declare `UrlFetch` command
self.add_cmd('UrlFetch', def (cmd, idx, payload, payload_json) self.urlfetch_cmd(cmd, idx, payload, payload_json) end)
end
# check that the parameter is not a method, it would require a closure instead
@ -751,4 +753,44 @@ class Tasmota
return (r << 16) | (g << 8) | b
end
# urlfetch - add a Tasmota command to download a file from a URL to file-system
def urlfetch(url,file)
if file==nil
import string
file = string.split(url,'/').pop()
if size(file) == 0
file = 'index.html' # fallback in case you fetch a root file
end
end
var wc = webclient()
wc.begin(url)
var st = wc.GET()
if st != 200
raise 'connection_error','status: '+str(st)
end
var ret = wc.write_file(file)
wc.close()
self.log('BRY: Fetched '+str(ret), 3)
return st
end
def urlfetch_cmd(cmd, idx, payload, payload_json)
import string
if string.find(payload, "http") != 0
self.resp_cmnd_str("URL must start with 'http(s)'")
return
end
try
var ret = self.urlfetch(payload)
if ret < 0
self.resp_cmnd_failed()
return
end
except .. as e, m
self.resp_cmnd_failed()
return
end
tasmota.resp_cmnd_done()
end
end