#- autocong module for Berry -# #- -# #- To solidify: -# #- # load only persis_module and persist_module.init import autoconf solidify.dump(autoconf_module) # copy and paste into `be_autoconf_lib.c` -# #- # For external compile: display = module("display") self = nil tasmota = nil def load() end -# var autoconf_module = module("autoconf") autoconf_module.init = def (m) class Autoconf var _archive var _error def init() import path import string var dir = path.listdir("/") var entry tasmota.add_driver(self) var i = 0 while i < size(dir) if string.find(dir[i], ".autoconf") > 0 # does the file contain '*.autoconf', >0 to skip `.autoconf` if entry != nil # we have multiple configuration files, not allowed print(string.format("CFG: multiple autoconf files found, aborting ('%s' + '%s')", entry, dir[i])) self._error = true return nil end entry = dir[i] end i += 1 end if entry == nil tasmota.log("CFG: no '*.autoconf' file found", 2) return nil end self._archive = entry end # #################################################################################################### # Manage first time marker # #################################################################################################### def is_first_time() import path return !path.exists("/.autoconf") end def set_first_time() var f = open("/.autoconf", "w") f.close() end def clear_first_time() import path path.remove("/.autoconf") end # #################################################################################################### # Delete all autoconfig files present # #################################################################################################### def delete_all_configs() import path import string var dir = path.listdir("/") for d:dir if string.find(d, ".autoconf") > 0 # does the file contain '*.autoconf' path.remove(d) end end end # #################################################################################################### # Get current module # contains the name of the archive without leading `/`, ex: `M5Stack_Fire.autoconf` # or `nil` if none # #################################################################################################### def get_current_module_path() return self._archive end def get_current_module_name() return self._archive[0..-10] end # #################################################################################################### # Load templates from Github # #################################################################################################### def load_templates() import string import json try var url = string.format("https://raw.githubusercontent.com/tasmota/autoconf/main/%s_manifest.json", tasmota.arch()) tasmota.log(string.format("CFG: loading '%s'", url), 3) # load the template var cl = webclient() cl.begin(url) var r = cl.GET() if r != 200 tasmota.log(string.format("CFG: return_code=%i", r), 2) return nil end var s = cl.get_string() cl.close() # convert to json var j = json.load(s) tasmota.log(string.format("CFG: loaded '%s'", str(j)), 3) var t = j.find("files") if isinstance(t, list) return t end return nil except .. as e, m tasmota.log(string.format("CFG: exception '%s' - '%s'", e, m), 2) return nil end end # #################################################################################################### # Init web handlers # #################################################################################################### # Displays a "Autocong" button on the configuration page def web_add_config_button() import webserver webserver.content_send("
") end # This HTTP GET manager controls which web controls are displayed def page_autoconf_mgr() import webserver import string if !webserver.check_privileged_access() return nil end webserver.content_start('Auto-configuration') webserver.content_send_style() webserver.content_send("(This feature requires an internet connection)
") var cur_module = self.get_current_module_path() var cur_module_display = cur_module ? string.tr(self.get_current_module_name(), "_", " ") : self._error ? "<Error: apply new or remove>" : "<None>" webserver.content_send("") webserver.content_send("") webserver.content_button(webserver.BUTTON_CONFIGURATION) webserver.content_stop() end # #################################################################################################### # Web controller # # Applies the changes and restart # #################################################################################################### # This HTTP POST manager handles the submitted web form data def page_autoconf_ctl() import webserver import string import path if !webserver.check_privileged_access() return nil end try if webserver.has_arg("reapply") tasmota.log("CFG: removing first time marker", 2); # print("CFG: removing first time marker") self.clear_first_time() #- and force restart -# webserver.redirect("/?rst=") elif webserver.has_arg("zip") # remove any remaining autoconf file tasmota.log("CFG: removing autoconf files", 2); # print("CFG: removing autoconf files") self.delete_all_configs() # get the name of the configuration file var arch_name = webserver.arg("zip") if arch_name != "reset" var url = string.format("https://raw.githubusercontent.com/tasmota/autoconf/main/%s/%s.autoconf", tasmota.arch(), arch_name) tasmota.log(string.format("CFG: downloading '%s'", url), 2); var local_file = string.format("%s.autoconf", arch_name) # download file and write directly to file system var cl = webclient() cl.begin(url) var r = cl.GET() if r != 200 raise "connection_error", string.format("return code=%i", r) end cl.write_file(local_file) cl.close() end # remove marker to reapply template self.clear_first_time() #- and force restart -# webserver.redirect("/?rst=") else raise "value_error", "Unknown command" end except .. as e, m print(string.format("CFG: Exception> '%s' - %s", e, m)) #- display error page -# webserver.content_start("Parameter error") #- title of the web page -# webserver.content_send_style() #- send standard Tasmota styles -# webserver.content_send(string.format("Exception:
'%s'
%s