From 04245e112cdc374b4d73d4e87cb041a5deab058c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 25 Apr 2019 15:55:19 +0200 Subject: [PATCH] ConfigGenerator stuff --- .../ConfigDataStructure.py-example | 17 ++ ConfigGenerator/configGen.py | 40 +++++ ConfigGenerator/configGen.sh | 12 ++ ConfigGenerator/configuration_c.tmpl | 153 ++++++++++++++++++ ConfigGenerator/configuration_h.tmpl | 17 ++ 5 files changed, 239 insertions(+) create mode 100644 ConfigGenerator/ConfigDataStructure.py-example create mode 100644 ConfigGenerator/configGen.py create mode 100755 ConfigGenerator/configGen.sh create mode 100644 ConfigGenerator/configuration_c.tmpl create mode 100644 ConfigGenerator/configuration_h.tmpl diff --git a/ConfigGenerator/ConfigDataStructure.py-example b/ConfigGenerator/ConfigDataStructure.py-example new file mode 100644 index 0000000..b13e782 --- /dev/null +++ b/ConfigGenerator/ConfigDataStructure.py-example @@ -0,0 +1,17 @@ + +configItems = [ + {"label":"_", "key":"magic", "type":"I", "default": ""}, + {"label":"Config Username", "key":"confUser", "type":"C", "length":16, "default":"admin"}, + {"label":"Config Password", "key":"confPasswd", "type":"C", "length":16, "default":"geheim123"}, + {"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"}, + {"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"}, + {"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":32, "default":"broker.hottis.de"}, + {"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"RgbLed1"}, + {"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"}, + {"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"}, + {"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883}, + {"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"}, + {"label":"MQTT Topic Command", "key":"mqttTopicCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/Command"}, + {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"}, + {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0} +] diff --git a/ConfigGenerator/configGen.py b/ConfigGenerator/configGen.py new file mode 100644 index 0000000..1d53fa1 --- /dev/null +++ b/ConfigGenerator/configGen.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +from Cheetah.Template import Template +from ConfigDataStructure import configItems + + + +#configItems = [ +# {"label":"_", "key":"magic", "type":"I", "default": ""}, +# {"label":"Config Username", "key":"confUser", "type":"C", "length":16, "default":"admin"}, +# {"label":"Config Password", "key":"confPasswd", "type":"C", "length":16, "default":"geheim123"}, +# {"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"}, +# {"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"}, +# {"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":32, "default":"broker.hottis.de"}, +# {"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"RgbLed1"}, +# {"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"}, +# {"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"}, +# {"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883}, +# {"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"}, +# {"label":"MQTT Topic Command", "key":"mqttTopicCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/Command"}, +# {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"}, +# {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0} +#] + + +magic = 0xC0DE0005 +appName = "ESP8266 based TwoLedSignal" +confWifiSsid = "espconfig" + +params = { + "magic":magic, + "appName":appName, + "confWifiSsid":confWifiSsid, + "configItems":configItems +} + +h_file = Template(file="configuration_h.tmpl", searchList=[params]) +open('configuration.h','w').write(str(h_file)) +c_file = Template(file="configuration_c.tmpl", searchList=[params]) +open('configuration.cpp','w').write(str(c_file)) diff --git a/ConfigGenerator/configGen.sh b/ConfigGenerator/configGen.sh new file mode 100755 index 0000000..46b3c18 --- /dev/null +++ b/ConfigGenerator/configGen.sh @@ -0,0 +1,12 @@ +#!/bin/bash + + +export PROJECTDIR=$PWD/.. + +export PYTHONPATH=$PYTHONPATH:$PROJECTDIR + +python -B configGen.py + +mv configuration.cpp configuration.h $PROJECTDIR + + diff --git a/ConfigGenerator/configuration_c.tmpl b/ConfigGenerator/configuration_c.tmpl new file mode 100644 index 0000000..9bf61a9 --- /dev/null +++ b/ConfigGenerator/configuration_c.tmpl @@ -0,0 +1,153 @@ +#raw +#include + +#include +#include +#include + +#include "defines.h" +#include "configuration.h" +#end raw + + +tConfigBlock configBlock; +const uint32_t MAGIC = $magic; +const char* CONFIG_SSID = "$confWifiSsid"; +extern ESP8266WebServer webServer; + +bool configSaved = false; + + +static bool checkAuthentication() { + Serial.print("User: "); Serial.println(configBlock.confUser); + Serial.print("Pass: "); Serial.println(configBlock.confPasswd); + return webServer.authenticate(configBlock.confUser, configBlock.confPasswd); +} + +void configServeIndex() { + bool configValid = (configBlock.magic == MAGIC); + + if (! configValid) { + configBlock.magic = MAGIC; + #for $configItem in $configItems + #if $configItem.label != "_" + #if $configItem.type == "C" + strcpy(configBlock.$configItem.key, "$configItem.default"); + #else if $configItem.type == "I" + configBlock.$configItem.key = $configItem.default; + #end if + #end if + #end for + } + + if (! checkAuthentication()) { + return webServer.requestAuthentication(); + } + + + String buffer = + "" + " " + " $appName" + " " + " " + "

$appName - ESP8266 Configuration Page

"; + + if (configSaved) { + configSaved = false; + buffer += "

Configuration saved

"; + } + + buffer += + "
" + " " + #for $configItem in $configItems + #if $configItem.label != "_" + " " + " " + " " + #end if + #end for + " " + " " + " " + "
" + " " + " " + " " + "
" + " " + "
" + "
" + " " + ""; + + webServer.send(200, "text/html", buffer); + + +#ifdef DEBUG + Serial.println("indexHtml request served"); +#endif +} + +void configServeGetConfiguration() { + if (! checkAuthentication()) { + return webServer.requestAuthentication(); + } + + String arg; + + #for $configItem in $configItems + #if $configItem.label != "_" + arg = webServer.arg("$configItem.key"); + #if $configItem.type == "C" + strcpy(configBlock.$configItem.key, arg.c_str()); + #else if $configItem.type == "I" + configBlock.$configItem.key = atoi(arg.c_str()); + #end if + #end if + #end for + + configBlock.magic = MAGIC; + + showConfiguration(); + + EEPROM.begin(512); + EEPROM.put(EEPROM_ADDR, configBlock); + EEPROM.commit(); + + Serial.println("EEPROM saved"); + + configSaved = true; + webServer.sendHeader("Location", String("/"), true); + webServer.send(302, "text/plain", ""); + //webServer.send(200, "text/html", "configuration saved"); +} + +void showConfiguration() { + Serial.println("Configuration is"); + + #for $configItem in $configItems + Serial.print("$configItem.key = <"); + Serial.print(configBlock.$configItem.key); + Serial.println(">"); + + #end for + + Serial.println("---"); +} \ No newline at end of file diff --git a/ConfigGenerator/configuration_h.tmpl b/ConfigGenerator/configuration_h.tmpl new file mode 100644 index 0000000..e528f14 --- /dev/null +++ b/ConfigGenerator/configuration_h.tmpl @@ -0,0 +1,17 @@ +typedef struct { +#for $configItem in $configItems +#if $configItem.type == 'C' + char ${configItem.key}[$configItem.length]; +#else if $configItem.type == 'I' + uint32_t $configItem.key; +#end if +#end for +} tConfigBlock; + +extern const uint32_t MAGIC; +extern tConfigBlock configBlock; +extern const char* CONFIG_SSID; + +void configServeIndex(); +void configServeGetConfiguration(); +void showConfiguration(); \ No newline at end of file