add testServer
This commit is contained in:
parent
271e461be5
commit
8f03ad1373
|
@ -31,11 +31,12 @@ String GwConfigHandler::toString() const{
|
|||
|
||||
String GwConfigHandler::toJson() const{
|
||||
String rt;
|
||||
DynamicJsonDocument jdoc(50);
|
||||
DynamicJsonDocument jdoc(300);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
jdoc[configs[i]->getName()]=configs[i]->asString();
|
||||
jdoc[configs[i]->getName()]=configs[i]->asCString();
|
||||
}
|
||||
serializeJson(jdoc,rt);
|
||||
logger->logString("configJson: %s",rt.c_str());
|
||||
return rt;
|
||||
}
|
||||
GwConfigItem * GwConfigHandler::findConfig(const String name, bool dummy){
|
||||
|
@ -61,6 +62,7 @@ bool GwConfigHandler::loadConfig(){
|
|||
prefs.begin(PREF_NAME,true);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
String v=prefs.getString(configs[i]->getName().c_str(),configs[i]->getDefault());
|
||||
configs[i]->fromString(v);
|
||||
}
|
||||
prefs.end();
|
||||
return true;
|
||||
|
@ -68,6 +70,7 @@ bool GwConfigHandler::loadConfig(){
|
|||
bool GwConfigHandler::saveConfig(){
|
||||
prefs.begin(PREF_NAME,false);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
logger->logString("saving %s=%s",configs[i]->getName().c_str(),configs[i]->asCString());
|
||||
prefs.putString(configs[i]->getName().c_str(),configs[i]->asString());
|
||||
}
|
||||
prefs.end();
|
||||
|
@ -80,6 +83,12 @@ bool GwConfigHandler::updateValue(const char *name, const char * value){
|
|||
logger->logString("update config %s=>%s",name,value);
|
||||
i->fromString(value);
|
||||
}
|
||||
bool GwConfigHandler::updateValue(String name, String value){
|
||||
GwConfigItem *i=findConfig(name);
|
||||
if (i == NULL) return false;
|
||||
logger->logString("update config %s=>%s",name.c_str(),value.c_str());
|
||||
i->fromString(value);
|
||||
}
|
||||
bool GwConfigHandler::reset(bool save){
|
||||
logger->logString("reset config");
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
|
|
|
@ -65,6 +65,7 @@ class GwConfigHandler{
|
|||
bool loadConfig();
|
||||
bool saveConfig();
|
||||
bool updateValue(const char *name, const char * value);
|
||||
bool updateValue(String name, String value);
|
||||
bool reset(bool save);
|
||||
String toString() const;
|
||||
String toJson() const;
|
||||
|
@ -74,14 +75,14 @@ class GwConfigHandler{
|
|||
GwConfigInterface * getConfigItem(const String name, bool dummy=false) const;
|
||||
private:
|
||||
GwConfigItem* configs[5]={
|
||||
new GwConfigItem(sendUsb,true),
|
||||
new GwConfigItem (receiveUsb,false),
|
||||
new GwConfigItem (wifiClient,false),
|
||||
new GwConfigItem(sendUsb,"true"),
|
||||
new GwConfigItem (receiveUsb,"false"),
|
||||
new GwConfigItem (wifiClient,"false"),
|
||||
new GwConfigItem (wifiSSID,""),
|
||||
new GwConfigItem (wifiPass,"")
|
||||
};
|
||||
int getNumConfig() const{
|
||||
return sizeof(configs)/sizeof(GwConfigItem*);
|
||||
return 5;
|
||||
}
|
||||
};
|
||||
#endif
|
|
@ -17,7 +17,7 @@ void GwWifi::setup(){
|
|||
IPAddress AP_local_ip(192, 168, 15, 1); // Static address for AP
|
||||
IPAddress AP_gateway(192, 168, 15, 1);
|
||||
IPAddress AP_subnet(255, 255, 255, 0);
|
||||
WiFi.mode(WIFI_AP_STA); //enable both AP and client
|
||||
WiFi.mode(WIFI_MODE_APSTA); //enable both AP and client
|
||||
WiFi.softAP(AP_ssid,AP_password);
|
||||
delay(100);
|
||||
WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet);
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import shutil
|
||||
import sys
|
||||
import http
|
||||
import http.server
|
||||
import urllib.request
|
||||
import posixpath
|
||||
import os
|
||||
|
||||
class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
p=self.path
|
||||
print("path=%s"%p)
|
||||
if p.startswith("/api/"):
|
||||
apiurl=self.server.proxyUrl
|
||||
url=apiurl+p.replace("/api","")
|
||||
print("proxy to %s"%url)
|
||||
with urllib.request.urlopen(url) as response:
|
||||
self.send_response(http.HTTPStatus.OK)
|
||||
self.send_header("Content-type", response.getheader("Content-type"))
|
||||
|
||||
self.end_headers()
|
||||
shutil.copyfileobj(response,self.wfile)
|
||||
return None
|
||||
self.send_error(http.HTTPStatus.NOT_FOUND, "api not found")
|
||||
return None
|
||||
super().do_GET()
|
||||
def translate_path(self, path):
|
||||
"""Translate a /-separated PATH to the local filename syntax.
|
||||
|
||||
Components that mean special things to the local file system
|
||||
(e.g. drive or directory names) are ignored. (XXX They should
|
||||
probably be diagnosed.)
|
||||
|
||||
"""
|
||||
# abandon query parameters
|
||||
path = path.split('?',1)[0]
|
||||
path = path.split('#',1)[0]
|
||||
# Don't forget explicit trailing slash when normalizing. Issue17324
|
||||
trailing_slash = path.rstrip().endswith('/')
|
||||
try:
|
||||
path = urllib.parse.unquote(path, errors='surrogatepass')
|
||||
except UnicodeDecodeError:
|
||||
path = urllib.parse.unquote(path)
|
||||
path = posixpath.normpath(path)
|
||||
words = path.split('/')
|
||||
words = filter(None, words)
|
||||
path = self.server.baseDir
|
||||
for word in words:
|
||||
if os.path.dirname(word) or word in (os.curdir, os.pardir):
|
||||
# Ignore components that are not a simple file/directory name
|
||||
continue
|
||||
path = os.path.join(path, word)
|
||||
if trailing_slash:
|
||||
path += '/'
|
||||
return path
|
||||
|
||||
def run(baseDir,port,apiUrl,server_class=http.server.HTTPServer, handler_class=RequestHandler):
|
||||
server_address = ('', port)
|
||||
httpd = server_class(server_address, handler_class)
|
||||
httpd.proxyUrl=apiUrl
|
||||
httpd.baseDir=baseDir
|
||||
httpd.serve_forever()
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 4:
|
||||
print("usage: %s basedir port apiurl"%sys.argv[0])
|
||||
sys.exit(1)
|
||||
run(sys.argv[1],int(sys.argv[2]),sys.argv[3])
|
|
@ -21,7 +21,27 @@
|
|||
}
|
||||
})
|
||||
}
|
||||
function resetForm(ev){
|
||||
if (ev){
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
}
|
||||
fetch("/api/config")
|
||||
.then(function(resp){
|
||||
return resp.json();
|
||||
})
|
||||
.then(function(jsonData){
|
||||
for (let k in jsonData){
|
||||
let el=document.querySelector("[name='"+k+"']");
|
||||
if (el){
|
||||
let v=jsonData[k];
|
||||
el.value=v;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
window.setInterval(update,1000);
|
||||
resetForm();
|
||||
</script>
|
||||
<style type="text/css">
|
||||
#wrap
|
||||
|
@ -65,6 +85,25 @@ span.label {
|
|||
<span class="label">wifi client IP</span>
|
||||
<span class="value" id="clientIP">---</span>
|
||||
</div>
|
||||
<form action="/api/setConfig">
|
||||
<div class="row">
|
||||
<span class="label">wifiClient</span>
|
||||
<select name="wifiClient">
|
||||
<option value="true">On</option>
|
||||
<option value="false" selected="selected">Off</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span class="label">wifiClientPass</span>
|
||||
<input name="wifiPass" type="text">
|
||||
</div>
|
||||
<div class="row">
|
||||
<span class="label">wifiClientSSID</span>
|
||||
<input name="wifiSSID" type="text">
|
||||
</div>
|
||||
<button onClick="resetForm()">Reset</button>
|
||||
<button type="submit">Save&Restart</button>
|
||||
</form>
|
||||
<button id="reset" onclick="reset();">Reset</button>
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue