diff --git a/src/mqtt/AmsMqttHandler.h b/lib/AmsMqttHandler/include/AmsMqttHandler.h similarity index 100% rename from src/mqtt/AmsMqttHandler.h rename to lib/AmsMqttHandler/include/AmsMqttHandler.h diff --git a/lib/DomoticzMqttHandler/include/.gitignore b/lib/DomoticzMqttHandler/include/.gitignore new file mode 100644 index 00000000..1907c991 --- /dev/null +++ b/lib/DomoticzMqttHandler/include/.gitignore @@ -0,0 +1 @@ +json/*.h diff --git a/src/mqtt/DomoticzMqttHandler.h b/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h similarity index 100% rename from src/mqtt/DomoticzMqttHandler.h rename to lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h diff --git a/lib/ClassicUi/html/domoticz.json b/lib/DomoticzMqttHandler/json/domoticz.json similarity index 100% rename from lib/ClassicUi/html/domoticz.json rename to lib/DomoticzMqttHandler/json/domoticz.json diff --git a/lib/DomoticzMqttHandler/scripts/generate_includes.py b/lib/DomoticzMqttHandler/scripts/generate_includes.py new file mode 100644 index 00000000..43f41f5d --- /dev/null +++ b/lib/DomoticzMqttHandler/scripts/generate_includes.py @@ -0,0 +1,76 @@ +import os +import re +import shutil +import subprocess + +try: + from css_html_js_minify import js_minify +except: + from SCons.Script import ( + ARGUMENTS, + COMMAND_LINE_TARGETS, + DefaultEnvironment, + ) + env = DefaultEnvironment() + + env.Execute( + env.VerboseAction( + '$PYTHONEXE -m pip install "css_html_js_minify" ', + "Installing Python dependencies", + ) + ) + try: + from css_html_js_minify import js_minify + except: + print("WARN: Unable to load minifier") + + +webroot = "lib/DomoticzMqttHandler/json" +srcroot = "lib/DomoticzMqttHandler/include/json" + +version = os.environ.get('GITHUB_TAG') +if version == None: + try: + result = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, check=False) + if result.returncode == 0: + version = result.stdout.decode('utf-8').strip() + else: + version = "SNAPSHOT" + except: + version = "SNAPSHOT" + +if os.path.exists(srcroot): + shutil.rmtree(srcroot) + os.mkdir(srcroot) +else: + os.mkdir(srcroot) + +for filename in os.listdir(webroot): + basename = re.sub("[^0-9a-zA-Z]+", "_", filename) + + srcfile = webroot + "/" + filename + dstfile = srcroot + "/" + basename + ".h" + + varname = basename.upper() + + with open(srcfile, encoding="utf-8") as f: + content = f.read().replace("${version}", version) + + try: + if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"): + content = js_minify(content) + except: + print("WARN: Unable to minify") + + with open(dstfile, "w") as dst: + dst.write("static const char ") + dst.write(varname) + dst.write("[] PROGMEM = R\"==\"==(") + dst.write(content) + dst.write(")==\"==\";\n") + dst.write("const int "); + dst.write(varname) + dst.write("_LEN PROGMEM = "); + dst.write(str(len(content))) + dst.write(";"); + \ No newline at end of file diff --git a/src/mqtt/DomoticzMqttHandler.cpp b/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp similarity index 98% rename from src/mqtt/DomoticzMqttHandler.cpp rename to lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp index 90cbb182..3d68d3c0 100644 --- a/src/mqtt/DomoticzMqttHandler.cpp +++ b/lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp @@ -1,5 +1,5 @@ #include "DomoticzMqttHandler.h" -#include "root/domoticz_json.h" +#include "json/domoticz_json.h" bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) { bool ret = false; diff --git a/lib/HomeAssistantMqttHandler/include/.gitignore b/lib/HomeAssistantMqttHandler/include/.gitignore new file mode 100644 index 00000000..1907c991 --- /dev/null +++ b/lib/HomeAssistantMqttHandler/include/.gitignore @@ -0,0 +1 @@ +json/*.h diff --git a/src/mqtt/HomeAssistantMqttHandler.h b/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h similarity index 100% rename from src/mqtt/HomeAssistantMqttHandler.h rename to lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h diff --git a/src/mqtt/HomeAssistantStatic.h b/lib/HomeAssistantMqttHandler/include/HomeAssistantStatic.h similarity index 100% rename from src/mqtt/HomeAssistantStatic.h rename to lib/HomeAssistantMqttHandler/include/HomeAssistantStatic.h diff --git a/lib/ClassicUi/html/ha1.json b/lib/HomeAssistantMqttHandler/json/ha1.json similarity index 100% rename from lib/ClassicUi/html/ha1.json rename to lib/HomeAssistantMqttHandler/json/ha1.json diff --git a/lib/ClassicUi/html/ha2.json b/lib/HomeAssistantMqttHandler/json/ha2.json similarity index 100% rename from lib/ClassicUi/html/ha2.json rename to lib/HomeAssistantMqttHandler/json/ha2.json diff --git a/lib/ClassicUi/html/ha3.json b/lib/HomeAssistantMqttHandler/json/ha3.json similarity index 100% rename from lib/ClassicUi/html/ha3.json rename to lib/HomeAssistantMqttHandler/json/ha3.json diff --git a/lib/ClassicUi/html/hadiscover.json b/lib/HomeAssistantMqttHandler/json/hadiscover.json similarity index 100% rename from lib/ClassicUi/html/hadiscover.json rename to lib/HomeAssistantMqttHandler/json/hadiscover.json diff --git a/lib/HomeAssistantMqttHandler/scripts/generate_includes.py b/lib/HomeAssistantMqttHandler/scripts/generate_includes.py new file mode 100644 index 00000000..5829cb81 --- /dev/null +++ b/lib/HomeAssistantMqttHandler/scripts/generate_includes.py @@ -0,0 +1,76 @@ +import os +import re +import shutil +import subprocess + +try: + from css_html_js_minify import js_minify +except: + from SCons.Script import ( + ARGUMENTS, + COMMAND_LINE_TARGETS, + DefaultEnvironment, + ) + env = DefaultEnvironment() + + env.Execute( + env.VerboseAction( + '$PYTHONEXE -m pip install "css_html_js_minify" ', + "Installing Python dependencies", + ) + ) + try: + from css_html_js_minify import js_minify + except: + print("WARN: Unable to load minifier") + + +webroot = "lib/HomeAssistantMqttHandler/json" +srcroot = "lib/HomeAssistantMqttHandler/include/json" + +version = os.environ.get('GITHUB_TAG') +if version == None: + try: + result = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, check=False) + if result.returncode == 0: + version = result.stdout.decode('utf-8').strip() + else: + version = "SNAPSHOT" + except: + version = "SNAPSHOT" + +if os.path.exists(srcroot): + shutil.rmtree(srcroot) + os.mkdir(srcroot) +else: + os.mkdir(srcroot) + +for filename in os.listdir(webroot): + basename = re.sub("[^0-9a-zA-Z]+", "_", filename) + + srcfile = webroot + "/" + filename + dstfile = srcroot + "/" + basename + ".h" + + varname = basename.upper() + + with open(srcfile, encoding="utf-8") as f: + content = f.read().replace("${version}", version) + + try: + if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"): + content = js_minify(content) + except: + print("WARN: Unable to minify") + + with open(dstfile, "w") as dst: + dst.write("static const char ") + dst.write(varname) + dst.write("[] PROGMEM = R\"==\"==(") + dst.write(content) + dst.write(")==\"==\";\n") + dst.write("const int "); + dst.write(varname) + dst.write("_LEN PROGMEM = "); + dst.write(str(len(content))) + dst.write(";"); + \ No newline at end of file diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp similarity index 97% rename from src/mqtt/HomeAssistantMqttHandler.cpp rename to lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp index 1fe9ead0..7566309d 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp @@ -3,12 +3,12 @@ #include "hexutils.h" #include "Uptime.h" #include "version.h" -#include "root/ha1_json.h" -#include "root/ha2_json.h" -#include "root/ha3_json.h" -#include "root/jsonsys_json.h" -#include "root/jsonprices_json.h" -#include "root/hadiscover_json.h" +#include "json/ha1_json.h" +#include "json/ha2_json.h" +#include "json/ha3_json.h" +#include "json/jsonsys_json.h" +#include "json/jsonprices_json.h" +#include "json/hadiscover_json.h" bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) { if(topic.isEmpty() || !mqtt->connected()) diff --git a/lib/JsonMqttHandler/include/.gitignore b/lib/JsonMqttHandler/include/.gitignore new file mode 100644 index 00000000..1907c991 --- /dev/null +++ b/lib/JsonMqttHandler/include/.gitignore @@ -0,0 +1 @@ +json/*.h diff --git a/src/mqtt/JsonMqttHandler.h b/lib/JsonMqttHandler/include/JsonMqttHandler.h similarity index 100% rename from src/mqtt/JsonMqttHandler.h rename to lib/JsonMqttHandler/include/JsonMqttHandler.h diff --git a/lib/ClassicUi/html/json1.json b/lib/JsonMqttHandler/json/json1.json similarity index 100% rename from lib/ClassicUi/html/json1.json rename to lib/JsonMqttHandler/json/json1.json diff --git a/lib/ClassicUi/html/json2.json b/lib/JsonMqttHandler/json/json2.json similarity index 100% rename from lib/ClassicUi/html/json2.json rename to lib/JsonMqttHandler/json/json2.json diff --git a/lib/ClassicUi/html/json3.json b/lib/JsonMqttHandler/json/json3.json similarity index 100% rename from lib/ClassicUi/html/json3.json rename to lib/JsonMqttHandler/json/json3.json diff --git a/lib/ClassicUi/html/json4.json b/lib/JsonMqttHandler/json/json4.json similarity index 100% rename from lib/ClassicUi/html/json4.json rename to lib/JsonMqttHandler/json/json4.json diff --git a/lib/ClassicUi/html/jsonprices.json b/lib/JsonMqttHandler/json/jsonprices.json similarity index 100% rename from lib/ClassicUi/html/jsonprices.json rename to lib/JsonMqttHandler/json/jsonprices.json diff --git a/lib/ClassicUi/html/jsonsys.json b/lib/JsonMqttHandler/json/jsonsys.json similarity index 100% rename from lib/ClassicUi/html/jsonsys.json rename to lib/JsonMqttHandler/json/jsonsys.json diff --git a/lib/JsonMqttHandler/scripts/generate_includes.py b/lib/JsonMqttHandler/scripts/generate_includes.py new file mode 100644 index 00000000..285e8fb7 --- /dev/null +++ b/lib/JsonMqttHandler/scripts/generate_includes.py @@ -0,0 +1,76 @@ +import os +import re +import shutil +import subprocess + +try: + from css_html_js_minify import js_minify +except: + from SCons.Script import ( + ARGUMENTS, + COMMAND_LINE_TARGETS, + DefaultEnvironment, + ) + env = DefaultEnvironment() + + env.Execute( + env.VerboseAction( + '$PYTHONEXE -m pip install "css_html_js_minify" ', + "Installing Python dependencies", + ) + ) + try: + from css_html_js_minify import js_minify + except: + print("WARN: Unable to load minifier") + + +webroot = "lib/JsonMqttHandler/json" +srcroot = "lib/JsonMqttHandler/include/json" + +version = os.environ.get('GITHUB_TAG') +if version == None: + try: + result = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, check=False) + if result.returncode == 0: + version = result.stdout.decode('utf-8').strip() + else: + version = "SNAPSHOT" + except: + version = "SNAPSHOT" + +if os.path.exists(srcroot): + shutil.rmtree(srcroot) + os.mkdir(srcroot) +else: + os.mkdir(srcroot) + +for filename in os.listdir(webroot): + basename = re.sub("[^0-9a-zA-Z]+", "_", filename) + + srcfile = webroot + "/" + filename + dstfile = srcroot + "/" + basename + ".h" + + varname = basename.upper() + + with open(srcfile, encoding="utf-8") as f: + content = f.read().replace("${version}", version) + + try: + if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"): + content = js_minify(content) + except: + print("WARN: Unable to minify") + + with open(dstfile, "w") as dst: + dst.write("static const char ") + dst.write(varname) + dst.write("[] PROGMEM = R\"==\"==(") + dst.write(content) + dst.write(")==\"==\";\n") + dst.write("const int "); + dst.write(varname) + dst.write("_LEN PROGMEM = "); + dst.write(str(len(content))) + dst.write(";"); + \ No newline at end of file diff --git a/src/mqtt/JsonMqttHandler.cpp b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp similarity index 97% rename from src/mqtt/JsonMqttHandler.cpp rename to lib/JsonMqttHandler/src/JsonMqttHandler.cpp index 8408b226..de7a51c0 100644 --- a/src/mqtt/JsonMqttHandler.cpp +++ b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp @@ -2,12 +2,12 @@ #include "version.h" #include "hexutils.h" #include "Uptime.h" -#include "root/json1_json.h" -#include "root/json2_json.h" -#include "root/json3_json.h" -#include "root/json4_json.h" -#include "root/jsonsys_json.h" -#include "root/jsonprices_json.h" +#include "json/json1_json.h" +#include "json/json2_json.h" +#include "json/json3_json.h" +#include "json/json4_json.h" +#include "json/jsonsys_json.h" +#include "json/jsonprices_json.h" bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) { if(topic.isEmpty() || !mqtt->connected()) diff --git a/src/mqtt/RawMqttHandler.h b/lib/RawMqttHandler/include/RawMqttHandler.h similarity index 100% rename from src/mqtt/RawMqttHandler.h rename to lib/RawMqttHandler/include/RawMqttHandler.h diff --git a/src/mqtt/RawMqttHandler.cpp b/lib/RawMqttHandler/src/RawMqttHandler.cpp similarity index 100% rename from src/mqtt/RawMqttHandler.cpp rename to lib/RawMqttHandler/src/RawMqttHandler.cpp diff --git a/platformio.ini b/platformio.ini index b4d27b19..8268ca24 100755 --- a/platformio.ini +++ b/platformio.ini @@ -2,10 +2,13 @@ extra_configs = platformio-user.ini [common] -lib_deps = EEPROM, LittleFS, DNSServer, 256dpi/MQTT@2.5.0, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, EntsoePriceApi, EnergyAccounting, ClassicUi +lib_deps = EEPROM, LittleFS, DNSServer, 256dpi/MQTT@2.5.0, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, EntsoePriceApi, EnergyAccounting, AmsMqttHandler, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, ClassicUi lib_ignore = OneWire extra_scripts = pre:scripts/addversion.py + lib/JsonMqttHandler/scripts/generate_includes.py + lib/DomoticzMqttHandler/scripts/generate_includes.py + lib/HomeAssistantMqttHandler/scripts/generate_includes.py lib/ClassicUi/scripts/generate_includes.py [esp32] diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 69f4a5f7..667fe7cb 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -54,11 +54,11 @@ ADC_MODE(ADC_VCC); #include "AmsWebServer.h" #include "AmsConfiguration.h" -#include "mqtt/AmsMqttHandler.h" -#include "mqtt/JsonMqttHandler.h" -#include "mqtt/RawMqttHandler.h" -#include "mqtt/DomoticzMqttHandler.h" -#include "mqtt/HomeAssistantMqttHandler.h" +#include "AmsMqttHandler.h" +#include "JsonMqttHandler.h" +#include "RawMqttHandler.h" +#include "DomoticzMqttHandler.h" +#include "HomeAssistantMqttHandler.h" #include "Uptime.h"