moved generated into extra dir, prepare for more embedded files
This commit is contained in:
parent
b1f3af3797
commit
b5aa62ff49
|
@ -3,4 +3,4 @@
|
||||||
.vscode/c_cpp_properties.json
|
.vscode/c_cpp_properties.json
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/ipch
|
.vscode/ipch
|
||||||
web/*gz
|
generated/*
|
|
@ -2,10 +2,27 @@ print("running extra...")
|
||||||
import gzip
|
import gzip
|
||||||
import shutil
|
import shutil
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import inspect
|
||||||
|
GEN_DIR='generated'
|
||||||
FILES=['web/index.html']
|
FILES=['web/index.html']
|
||||||
|
|
||||||
|
def outPath():
|
||||||
|
#see: https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined
|
||||||
|
return os.path.join(os.path.dirname(inspect.getfile(lambda: None)),GEN_DIR)
|
||||||
|
|
||||||
|
def checkDir():
|
||||||
|
dn=outPath()
|
||||||
|
if not os.path.exists(dn):
|
||||||
|
os.makedirs(dn)
|
||||||
|
if not os.path.isdir(dn):
|
||||||
|
print("unable to create %s"%dn)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def compressFile(inFile):
|
def compressFile(inFile):
|
||||||
outfile=inFile+".gz"
|
outfile=os.path.basename(inFile)+".gz"
|
||||||
|
outfile=os.path.join(outPath(),outfile)
|
||||||
if os.path.exists(outfile):
|
if os.path.exists(outfile):
|
||||||
otime=os.path.getmtime(outfile)
|
otime=os.path.getmtime(outfile)
|
||||||
itime=os.path.getmtime(inFile)
|
itime=os.path.getmtime(inFile)
|
||||||
|
@ -16,6 +33,8 @@ def compressFile(inFile):
|
||||||
with gzip.open(outfile, 'wb') as f_out:
|
with gzip.open(outfile, 'wb') as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
|
|
||||||
|
if not checkDir():
|
||||||
|
sys.exit(1)
|
||||||
for f in FILES:
|
for f in FILES:
|
||||||
print("compressing %s"%f)
|
print("compressing %s"%f)
|
||||||
compressFile(f)
|
compressFile(f)
|
|
@ -18,18 +18,24 @@ lib_deps =
|
||||||
bblanchon/ArduinoJson@^6.18.5
|
bblanchon/ArduinoJson@^6.18.5
|
||||||
ottowinter/ESPAsyncWebServer-esphome@^2.0.1
|
ottowinter/ESPAsyncWebServer-esphome@^2.0.1
|
||||||
board_build.embed_files =
|
board_build.embed_files =
|
||||||
web/index.html.gz
|
generated/index.html.gz
|
||||||
extra_scripts = extra_script.py
|
extra_scripts = extra_script.py
|
||||||
|
build_flags=
|
||||||
|
-Igenerated
|
||||||
|
|
||||||
[env:m5stack-atom]
|
[env:m5stack-atom]
|
||||||
board = m5stack-atom
|
board = m5stack-atom
|
||||||
lib_deps =
|
lib_deps =
|
||||||
${env.lib_deps}
|
${env.lib_deps}
|
||||||
build_flags = -D BOARD_M5ATOM
|
build_flags =
|
||||||
|
-D BOARD_M5ATOM
|
||||||
|
${env.build_flags}
|
||||||
upload_port = /dev/esp32
|
upload_port = /dev/esp32
|
||||||
|
|
||||||
[env:m5stack-atom-canunit]
|
[env:m5stack-atom-canunit]
|
||||||
board = m5stack-atom
|
board = m5stack-atom
|
||||||
lib_deps =
|
lib_deps =
|
||||||
${env.lib_deps}
|
${env.lib_deps}
|
||||||
build_flags = -D BOARD_M5ATOM_CANUNIT
|
build_flags =
|
||||||
|
-D BOARD_M5ATOM_CANUNIT
|
||||||
|
${env.build_flags}
|
||||||
|
|
44
src/main.cpp
44
src/main.cpp
|
@ -12,7 +12,7 @@
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define VERSION "0.1.0"
|
#define VERSION "0.1.1"
|
||||||
#include "GwHardware.h"
|
#include "GwHardware.h"
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
@ -135,11 +135,38 @@ void handleAsyncWebRequest(AsyncWebServerRequest *request, RequestMessage *msg,
|
||||||
}
|
}
|
||||||
|
|
||||||
#define JSON_OK "{\"status\":\"OK\"}"
|
#define JSON_OK "{\"status\":\"OK\"}"
|
||||||
//embedded files
|
|
||||||
extern const uint8_t indexFile[] asm("_binary_web_index_html_gz_start");
|
|
||||||
extern const uint8_t indexFileEnd[] asm("_binary_web_index_html_gz_end");
|
|
||||||
extern const uint8_t indexFileLen[] asm("_binary_web_index_html_gz_size");
|
|
||||||
|
|
||||||
|
class EmbeddedFile;
|
||||||
|
static std::map<String,EmbeddedFile*> embeddedFiles;
|
||||||
|
class EmbeddedFile {
|
||||||
|
public:
|
||||||
|
const uint8_t *start;
|
||||||
|
int len;
|
||||||
|
EmbeddedFile(String name,const uint8_t *start,int len){
|
||||||
|
this->start=start;
|
||||||
|
this->len=len;
|
||||||
|
embeddedFiles[name]=this;
|
||||||
|
}
|
||||||
|
} ;
|
||||||
|
#define EMBED_GZ_FILE(fileName, fileExt) \
|
||||||
|
extern const uint8_t fileName##_##fileExt##_File[] asm("_binary_generated_" #fileName "_" #fileExt "_gz_start"); \
|
||||||
|
extern const uint8_t fileName##_##fileExt##_FileLen[] asm("_binary_generated_" #fileName "_" #fileExt "_gz_size"); \
|
||||||
|
const EmbeddedFile fileName##_##fileExt##_Config(#fileName "." #fileExt,(const uint8_t*)fileName##_##fileExt##_File,(int)fileName##_##fileExt##_FileLen);
|
||||||
|
|
||||||
|
EMBED_GZ_FILE(index,html)
|
||||||
|
|
||||||
|
void sendEmbeddedFile(String name,String contentType,AsyncWebServerRequest *request){
|
||||||
|
std::map<String,EmbeddedFile*>::iterator it=embeddedFiles.find(name);
|
||||||
|
if (it != embeddedFiles.end()){
|
||||||
|
EmbeddedFile* found=it->second;
|
||||||
|
AsyncWebServerResponse *response=request->beginResponse_P(200,contentType,found->start,found->len);
|
||||||
|
response->addHeader(F("Content-Encoding"), F("gzip"));
|
||||||
|
request->send(response);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
request->send(404, "text/plain", "Not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String js_status(){
|
String js_status(){
|
||||||
int numPgns=nmea0183Converter->numPgns();
|
int numPgns=nmea0183Converter->numPgns();
|
||||||
|
@ -221,9 +248,10 @@ void setup() {
|
||||||
|
|
||||||
// Start Web Server
|
// Start Web Server
|
||||||
webserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
webserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
AsyncWebServerResponse *response=request->beginResponse_P(200,"text/html",(const uint8_t *)indexFile,(int)indexFileLen);
|
sendEmbeddedFile("index.html","text/html",request);
|
||||||
response->addHeader(F("Content-Encoding"), F("gzip"));
|
});
|
||||||
request->send(response);
|
webserver.on("/config.json", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
sendEmbeddedFile("config.json","application/json",request);
|
||||||
});
|
});
|
||||||
webserver.on("/api/reset", HTTP_GET,[](AsyncWebServerRequest *request){
|
webserver.on("/api/reset", HTTP_GET,[](AsyncWebServerRequest *request){
|
||||||
logger.logDebug(GwLog::LOG,"Reset Button");
|
logger.logDebug(GwLog::LOG,"Reset Button");
|
||||||
|
|
Loading…
Reference in New Issue