-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoff.html
192 lines (175 loc) · 7.06 KB
/
sonoff.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<title>智慧插座燒錄工具</title>
<style>
textarea {
width: 40rem;
min-height: 20rem;
display: block;
font-size: 1.2rem;
font-family: Consolas, monaco, monospace;
}
.item-left {
flex: 1;
}
</style>
</head>
<body>
<h1>智慧插座燒錄工具</h1>
<button type="button" class="btn btn-primary" id="getUSB">選擇USB</button>
<button type="button" class="btn btn-secondary" id="eraseFirmware" disabled>清除韌體</button>
<br>
<p>
<h2>ssid 輸入後按燒錄韌體</h2>
</p>
<p>
</p>
<p>SSID:
<input type="text" id="ssid">
<button type="button" class="btn btn-success" id="burn" disabled>燒錄韌體</button>
</p>
<div class="progress">
<div id="progress" class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0"
aria-valuemin="0" aria-valuemax="100">0%</div>
</div>
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">提示</h5>
</div>
<div id="notifyMessage" class="modal-body">
</div>
<div class="modal-footer">
<button type="button" id="ok" class="btn btn-warning" data-bs-dismiss="modal">OK</button>
<button type="button" id="reload" class="btn btn-success" data-bs-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
<div class="item-left">
<h2>response</h2>
<textarea id="resp" style="width: 800px; height: 600px"></textarea>
</div>
<script src="lib/WebSerial.js"></script>
<script src="lib/esptool/pako/pako_deflate.js"></script>
<script src="lib/esptool/slip.js"></script>
<script src="lib/esptool/md5.js"></script>
<script src="lib/esptool/esptool.js"></script>
<script>
class Log {
constructor(textElement) {
this.ele = textElement;
}
log(msg) {
this.ele.value = this.ele.value + msg + "\r\n";
this.ele.scrollTop = this.ele.scrollHeight;
}
}
let esp
let logger = new Log(document.getElementById("resp"));
let getUSB = document.getElementById("getUSB")
let eraseFirmware = document.getElementById("eraseFirmware")
let burn = document.getElementById("burn")
let ssid = document.getElementById("ssid")
let progress = document.getElementById("progress")
let notify = document.getElementById("staticBackdrop");
let notifyMessage = document.getElementById("notifyMessage")
let okButton = document.getElementById("ok")
let reloadButton = document.getElementById("reload")
const firmwareVersionLink = `https://webduino.s3.ap-northeast-2.amazonaws.com/burn-firmware/sonoff/version.last`;
function showNotify(ok, reload, message) {
okButton.style = ok ? "display: block;" : "display: none;"
reloadButton.style = reload ? "display: block;" : "display: none;"
notifyMessage.innerHTML = message
notify.classList.add("show");
notify.style = "display: block;"
}
getUSB.onclick = async () => {
esp = new ESP({
baudrate: 115200 * 2 /* 為燒錄速度 */
}, logger)
await esp.init()
getUSB.disabled = false
eraseFirmware.disabled = false
burn.disabled = false
}
eraseFirmware.onclick = async () => {
eraseFirmware.disabled = true
burn.disabled = true
await esp.erase()
.then((result) => {
console.log(result)
})
.catch((error) => {
console.log(error)
showNotify(true, false, "清除失敗,請再試一次")
});
eraseFirmware.disabled = false
burn.disabled = false
}
async function checkFirmware(firmwareLink) {
const resp = await fetch(firmwareLink);
if (resp.status !== 200) {
return false
}
return true
}
burn.onclick = async () => {
let success = -1
let firmwareExists = false
let ssidValue = ssid.value
eraseFirmware.disabled = true
burn.disabled = true
const firmwareVersion = await fetch(firmwareVersionLink).then((resp) => resp.text())
const firmwareLink = `https://webduino.s3.ap-northeast-2.amazonaws.com/burn-firmware/sonoff/${firmwareVersion}/${ssidValue}.bin`;
firmwareExists = await checkFirmware(firmwareLink)
if (firmwareExists) {
await esp.burn(firmwareLink, (percentage) => {
progress.innerText = percentage + "%"
progress.ariaValueNow = percentage + "%"
progress.style.width = percentage + "%"
})
.then((result) => {
success = result
})
.catch((error) => {
success = error
});
}
if (success == -1) {
eraseFirmware.disabled = false
burn.disabled = false
showNotify(true, false, "序號不存在或其他異常,請 RD 協助檢查")
}
else if (success == 1) {
showNotify(false, true, "燒錄完成!")
} else if (success == 0) {
eraseFirmware.disabled = false
burn.disabled = false
progress.innerText = "0%"
progress.ariaValueNow = "0%"
progress.style.width = "0%"
showNotify(true, false, "燒錄失敗,請再試一次")
}
}
okButton.onclick = function () {
notify.classList.remove("show");
notify.style = "display: none;"
}
reloadButton.onclick = function () {
window.location.reload()
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ"
crossorigin="anonymous"></script>
</body>
</html>