Skip to content

Commit

Permalink
#111 wip load config value
Browse files Browse the repository at this point in the history
  • Loading branch information
yakumo-saki committed Dec 13, 2022
1 parent 186ca4a commit 9b518b8
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 61 deletions.
62 changes: 31 additions & 31 deletions embed/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</head>

<body>
<h1>EnvBoyX Settings (EBXCFG.v45)</h1>
<h1>EnvBoyX Settings (settingID: <span replace="settingId">EBXCFG.vXX</span>)</h1>
<form method='post'>
<br>
<fieldset>
Expand Down Expand Up @@ -52,7 +52,7 @@ <h1>EnvBoyX Settings (EBXCFG.v45)</h1>
</label><br>

<strong>明るさ(0-255)</strong><br>
<input type='number' name='displayBrightness' placeholder='brightness 0-255' value='98'><br>
<input type='number' name='displayBrightness' placeholder='brightness 0-255' value='255'><br>

<strong>Wait for reconfigure画面</strong><br>
※ 表示しない場合、セットアップモードに入るためにはWeb APIを使用する必要があります。<br>
Expand All @@ -71,7 +71,7 @@ <h1>EnvBoyX Settings (EBXCFG.v45)</h1>
<strong>自動消灯する明るさ(暗いと判定するしきい値)</strong><br>
<span>※ 光量センサーがない場合は自動的に無効になります。</span><br>
<span>※ 自動消灯を使用しない場合は、999999を入力してください。</span><br>
<input type='number' name='displayAutoDimLux' placeholder='Lux' value='5'><br>
<input type='number' name='displayAutoDimLux' placeholder='Lux' value='999999'><br>

<strong>自動消灯判定時間</strong><br>
<span>※ しきい値未満の明るさがこの時間継続したら消灯します。</span><br>
Expand Down Expand Up @@ -134,14 +134,14 @@ <h1>EnvBoyX Settings (EBXCFG.v45)</h1>
使用する(UARTモード)
</label><br>

<input type='' name='mhz19bPwmPin' placeholder='14' class='hidden' value='14'><br>
<input type='' name='mhz19bPwmPin' placeholder='14' class='hidden' value='99'><br>

<strong>MH-Z19BのUARTが接続されているGPIOピン番号</strong><br>
※ MH-Z19Bを使用しない場合は設定不要<br> ※ ESP8266では RX 14 TX 0 で固定<br> ※ ボードによって使用可能なピンが異なるので動作しない場合は他のピンを試してください。<br>
RXピン&nbsp;
<input type='number' name='mhz19bRxPin' placeholder='16' value='32'><br>
<input type='number' name='mhz19bRxPin' placeholder='16' value='88'><br>
TXピン&nbsp;
<input type='number' name='mhz19bTxPin' placeholder='17' value='33'><br>
<input type='number' name='mhz19bTxPin' placeholder='17' value='77'><br>

<br> <strong>起動時のAuto Baseline Correction</strong><br>
※ MH-Z19Bを使用しない場合・MH-Z19BがUARTモード以外の場合は無視されます。<br>
Expand Down Expand Up @@ -169,70 +169,70 @@ <h1>EnvBoyX Settings (EBXCFG.v45)</h1>
<fieldset>
<legend>気温</legend>
<span class='m_caution1'>注意1</span>:&nbsp;
<input type='number' name='tempAlerts.caut1.L' class='num' value='10'>
<input type='number' name='tempAlerts.caut1.L' class='num' value='0'>
以上
<input type='number' name='tempAlerts.caut1.H' class='num' value='15'>
<input type='number' name='tempAlerts.caut1.H' class='num' value='0'>
未満<br>
<span class='m_caution1'>注意2</span>:&nbsp;
<input type='number' name='tempAlerts.caut2.L' class='num' value='28'>
<input type='number' name='tempAlerts.caut2.L' class='num' value='1'>
以上
<input type='number' name='tempAlerts.caut2.H' class='num' value='30'>
<input type='number' name='tempAlerts.caut2.H' class='num' value='1'>
未満<br>
<span class='m_caution1'>警報1</span>:&nbsp;
<input type='number' name='tempAlerts.warn1.L' class='num' value='-99'>
<input type='number' name='tempAlerts.warn1.L' class='num' value='-991'>
以上
<input type='number' name='tempAlerts.warn1.H' class='num' value='10'>
<input type='number' name='tempAlerts.warn1.H' class='num' value='100'>
未満<br>
<span class='m_caution1'>警報2</span>:&nbsp;
<input type='number' name='tempAlerts.warn2.L' class='num' value='30'>
<input type='number' name='tempAlerts.warn2.L' class='num' value='3340'>
以上
<input type='number' name='tempAlerts.warn2.H' class='num' value='99'>
<input type='number' name='tempAlerts.warn2.H' class='num' value='9339'>
未満<br>
</fieldset>
<fieldset>
<legend>湿度</legend>
<span class='m_caution1'>注意1</span>:&nbsp;
<input type='number' name='humiAlerts.caut1.L' class='num' value='20'>
<input type='number' name='humiAlerts.caut1.L' class='num' value='21110'>
以上
<input type='number' name='humiAlerts.caut1.H' class='num' value='35'>
<input type='number' name='humiAlerts.caut1.H' class='num' value='33535'>
未満<br>
<span class='m_caution1'>注意2</span>:&nbsp;
<input type='number' name='humiAlerts.caut2.L' class='num' value='65'>
<input type='number' name='humiAlerts.caut2.L' class='num' value='65555'>
以上
<input type='number' name='humiAlerts.caut2.H' class='num' value='75'>
<input type='number' name='humiAlerts.caut2.H' class='num' value='75555'>
未満<br>
<span class='m_caution1'>警報1</span>:&nbsp;
<input type='number' name='humiAlerts.warn1.L' class='num' value='0'>
<input type='number' name='humiAlerts.warn1.L' class='num' value='1102'>
以上
<input type='number' name='humiAlerts.warn1.H' class='num' value='20'>
<input type='number' name='humiAlerts.warn1.H' class='num' value='3320'>
未満<br>
<span class='m_caution1'>警報2</span>:&nbsp;
<input type='number' name='humiAlerts.warn2.L' class='num' value='75'>
<input type='number' name='humiAlerts.warn2.L' class='num' value='432475'>
以上
<input type='number' name='humiAlerts.warn2.H' class='num' value='100'>
<input type='number' name='humiAlerts.warn2.H' class='num' value='142300'>
未満<br>
</fieldset>
<fieldset>
<legend>照度</legend>
<span class='m_caution1'>注意1</span>:&nbsp;
<input type='number' name='luxAlerts.caut1.L' class='num' value='-1'>
<input type='number' name='luxAlerts.caut1.L' class='num' value='-1234'>
以上
<input type='number' name='luxAlerts.caut1.H' class='num' value='-1'>
<input type='number' name='luxAlerts.caut1.H' class='num' value='-1444'>
未満<br>
<span class='m_caution1'>注意2</span>:&nbsp;
<input type='number' name='luxAlerts.caut2.L' class='num' value='-1'>
<input type='number' name='luxAlerts.caut2.L' class='num' value='-155'>
以上
<input type='number' name='luxAlerts.caut2.H' class='num' value='-1'>
<input type='number' name='luxAlerts.caut2.H' class='num' value='-166'>
未満<br>
<span class='m_caution1'>警報1</span>:&nbsp;
<input type='number' name='luxAlerts.warn1.L' class='num' value='0'>
<input type='number' name='luxAlerts.warn1.L' class='num' value='550'>
以上
<input type='number' name='luxAlerts.warn1.H' class='num' value='1'>
<input type='number' name='luxAlerts.warn1.H' class='num' value='661'>
未満<br>
<span class='m_caution1'>警報2</span>:&nbsp;
<input type='number' name='luxAlerts.warn2.L' class='num' value='2000'>
<input type='number' name='luxAlerts.warn2.L' class='num' value='2222'>
以上
<input type='number' name='luxAlerts.warn2.H' class='num' value='99999'>
<input type='number' name='luxAlerts.warn2.H' class='num' value='4444'>
未満<br>
</fieldset>
<fieldset>
Expand Down Expand Up @@ -285,7 +285,7 @@ <h1>EnvBoyX Settings (EBXCFG.v45)</h1>
<br> <input type='submit' value='設定する'><br>
</form>
<br>
<hr>EnvBoyX Ver.45.0, Copyright 2018-2021 Yakumo Saki / ziomatrix.org.
<hr>EnvBoyX <span replace="productVer">Ver.00.0</span>, Copyright 2018-2022 Yakumo Saki / ziomatrix.org.
</body>

</html>
125 changes: 105 additions & 20 deletions embed/config.js
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();
});

3 changes: 3 additions & 0 deletions include/network/http_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ void sendHtmlHeader();

// HTTP REDIRECTヘッダを送信する。これを実行したあとは何も送れない
void sendHttpRedirectHeader(String url);

// CORS リクエストに応答する(http OPTIONS リクエスト)
void http_handle_cors();
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

[platformio]
description = "EnvBoyX"
default_envs = esp32dev

[env]
extra_scripts =
Expand Down
2 changes: 1 addition & 1 deletion src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern const String ver = "46"; // Major
extern const String minorVer = "0"; // Bugfix
extern const String ver_long = " Ver." + ver + "." + minorVer;

// Setting version
// Setting version (version number when settings changed)
extern const String SETTING_ID = "EBXCFG.v45";

// EnvBoyX Ver.53.0
Expand Down
Loading

0 comments on commit 9b518b8

Please sign in to comment.