Skip to content

Commit

Permalink
add timeout message
Browse files Browse the repository at this point in the history
  • Loading branch information
celesWuff committed Apr 28, 2024
1 parent 28b1498 commit 7d46b59
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h3>不支持的浏览器</h3>
</body>

<script>
const VERSION = "1.1.3";
const VERSION = "1.1.4";

// PAYLOADS
// prettier-ignore
Expand Down Expand Up @@ -148,6 +148,7 @@ <h3>不支持的浏览器</h3>
let isNewFirmware = false;
let isStarted = false;
let pendingStartEpilogueLegacy; // workaround for determining new firmware, see handleRxdNotifications
let pendingTimeoutMessage; // if we don't get a response in time, we should show an error message
const logs = [];

function bufferToHexString(array) {
Expand Down Expand Up @@ -198,6 +199,7 @@ <h3>不支持的浏览器</h3>
isNewFirmware = false;
isStarted = false;
logs.length = 0;
clearTimeout(pendingTimeoutMessage);
updateUi("standby");
}

Expand All @@ -216,9 +218,12 @@ <h3>不支持的浏览器</h3>
const dialogDebugContainer = document.getElementById("dialog-debug-container");
const dialogDebugContent = document.getElementById("dialog-debug-content");

if (e.match(/Unknown RXD dType/)) {
if (e.match(/WATERCTL INTERNAL Unknown RXD dType/)) {
dialogContent.innerText = "接收到未知数据。可能不影响使用。\n\n(这可能是一个 Bug,请截图并";
dialogContent.innerHTML += "<a href='https://github.com/celesWuff/waterctl' target='_blank'>反馈给开发者</a>。)";
} else if (e.match(/WATERCTL INTERNAL Operation timed out/)) {
dialogContent.innerText = "等待时间似乎太长了。\n\n如果该问题反复发生,这可能是一个 Bug,请截图并";
dialogContent.innerHTML += "<a href='https://github.com/celesWuff/waterctl' target='_blank'>反馈给开发者</a>。";
} else if (e.match(/User denied the browser permission/)) {
dialogContent.innerText = "蓝牙权限遭拒。\n\n请前往手机设置,授予浏览器“位置信息”权限。\n此权限不会用于定位,详情请参考源代码仓库内的";
dialogContent.innerHTML += "<a href='https://github.com/celesWuff/waterctl/blob/gh-pages/FAQ.md' target='_blank'>“疑难解答”</a>。";
Expand Down Expand Up @@ -278,6 +283,7 @@ <h3>不支持的浏览器</h3>
case 0xAF: // not implemented; AE/AF key authentication for new firmwares
break;
case 0xB2: // start ok; update ui
clearTimeout(pendingTimeoutMessage);
isStarted = true;
updateUi("ok");
break;
Expand All @@ -291,14 +297,22 @@ <h3>不支持的浏览器</h3>
case 0xBC:
break;
default:
throw new Error("Unknown RXD dType");
throw new Error("WATERCTL INTERNAL Unknown RXD dType");
break;
}
} catch (error) {
handleBluetoothError(error);
}
}

function setupTimeoutMessage() {
if (!pendingTimeoutMessage) {
pendingTimeoutMessage = setTimeout(() => {
handleBluetoothError("WATERCTL INTERNAL Operation timed out");
}, 15000);
}
}

async function start() {
try {
bluetoothDevice = await navigator.bluetooth.requestDevice({
Expand All @@ -320,6 +334,7 @@ <h3>不支持的浏览器</h3>
rxdCharacteristic.addEventListener("characteristicvaluechanged", handleRxdNotifications);

await txdCharacteristic.writeValue(startPrologue);
setupTimeoutMessage();
} catch (error) {
handleBluetoothError(error);
}
Expand All @@ -329,6 +344,7 @@ <h3>不支持的浏览器</h3>
try {
if (isNewFirmware) await txdCharacteristic.writeValue(offlinebombFix);
await txdCharacteristic.writeValue(endPrologue);
setupTimeoutMessage();
} catch (error) {
handleBluetoothError(error);
}
Expand Down

0 comments on commit 7d46b59

Please sign in to comment.