-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
186ca4a
commit 9b518b8
Showing
9 changed files
with
195 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,107 @@ | ||
"use strict"; | ||
|
||
const DEBUG_MODE = true; | ||
const DEBUG_API_HOST = "127.0.0.1"; | ||
|
||
function loadConfig() { | ||
await fetch('http://10.1.0.130', { | ||
method: "GET" | ||
}) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
const DEBUG_API_HOST = "10.1.0.130"; // デバッグ時、APIを投げる先 | ||
|
||
const CONTENT_JSON = 'application/json'; | ||
const CONTENT_TEXT = 'text/plain'; | ||
|
||
function toPath(relativePath) { | ||
if (!DEBUG_MODE) { | ||
return relativePath; | ||
} | ||
|
||
var path = "http://" + DEBUG_API_HOST; | ||
if (!relativePath.startsWith("/")) { | ||
path = path + "/" | ||
} | ||
|
||
return path + relativePath; | ||
} | ||
|
||
function httpGet(relativePath, contentType = CONTENT_JSON) { | ||
if (DEBUG_MODE) { | ||
console.log("httpGet " + toPath(relativePath)); | ||
} | ||
|
||
return fetch(toPath(relativePath), { | ||
method: "GET", | ||
cache: 'no-cache', | ||
headers: { | ||
'Content-Type': contentType | ||
}, | ||
}); | ||
} | ||
|
||
function replaceVersion(replaceName, value) { | ||
const elems = document.querySelectorAll(`span[replace='${replaceName}']`) | ||
elems.forEach(element => { | ||
element.innerHTML = value; | ||
}); | ||
} | ||
|
||
async function setPageValues() { | ||
const res = await httpGet('/ping'); | ||
console.log(res); | ||
|
||
const json = await res.json(); | ||
console.log(json); | ||
|
||
replaceVersion('productVer', `ver.${json["majorVer"]}.${json["minorVer"]}`); | ||
replaceVersion('settingId', json["settingId"]); | ||
|
||
} | ||
|
||
function backupUrlToMap(url) { | ||
|
||
// curl -X POST -d "ほしい部分" http〜 のほしい部分だけを残す | ||
let urlParams = url; | ||
urlParams = urlParams.replace(/^.*?"/, ""); // 前半分を削除 | ||
urlParams = urlParams.replace(/" .*/, ""); // 後ろを削除 | ||
|
||
// params = ssid=fffd1341398g&mDNS=ebx32&opMode=always&displayFlip=no&... | ||
let paramArray = urlParams.split("&"); | ||
|
||
const ret = new Map(); | ||
for (let i = 0; i < paramArray.length; i++) { | ||
const param = paramArray[i].split("=", 2); | ||
ret.set(param[0], param[1]); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/** | ||
* configMap の値をドキュメントにセットする | ||
* @param {Map<string,string>} configMap - 文字列パラメータ | ||
*/ | ||
function setConfigValuesToPage(configMap) { | ||
|
||
configMap.forEach( (v, k) => { | ||
console.log("key", k, 'val', v); | ||
const el = document.querySelector(`input[name="${k}"]`); | ||
if (el == null) { | ||
console.log(`key ${k} is not found on document.`); | ||
return; | ||
} | ||
// 404 や 500 ステータスならここに到達する | ||
throw new Error('Network response was not ok.'); | ||
}) | ||
.then(resJson => { | ||
console.log(JSON.stringify(resJson)); | ||
}) | ||
.catch(error => { | ||
// ネットワークエラーの場合はここに到達する | ||
console.error(error); | ||
}) | ||
} | ||
|
||
el.value = v; | ||
}); | ||
|
||
} | ||
|
||
async function loadConfig() { | ||
const res = await httpGet('/config/backup', CONTENT_TEXT) | ||
const backupCmd = await res.text(); | ||
console.log(backupCmd); | ||
|
||
let paramMap = backupUrlToMap(backupCmd); | ||
setConfigValuesToPage(paramMap); | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', async (event) => { | ||
console.log('DOM fully loaded and parsed'); | ||
await setPageValues(); | ||
await loadConfig(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
[platformio] | ||
description = "EnvBoyX" | ||
default_envs = esp32dev | ||
|
||
[env] | ||
extra_scripts = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.