-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.html
127 lines (111 loc) · 4.59 KB
/
user.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8" />
<title> RTIO Demo </title>
<h2>RTIO Demo: Printer Remote Control Simulation </h2>
</head>
<body>
<div>
<label for="bt_start">start</label>
<button id="bt_start" class="bt_start"> >| </button>
</div>
<div>
<label for="ob_pg">progress</label>
<progress id="ob_pg" class="ob_pg" max="100" value="0"> </progress>
<output name="result" for="ob_pg"></output>
</div>
<br>
<div>
<label for="logs">run logs<br /></label>
<textarea id="logs" rows="32" cols="48" readonly></textarea>
</div>
<script type="text/javascript">
var bt_start = document.getElementById("bt_start");
bt_start.style.color = 'green';
function logShow(log) {
var t = document.getElementById("logs");
t.value = t.value + log + '\n'
t.scrollTop = t.scrollHeight
}
async function* makeTextFileLineIterator(url) {
const utf8Decoder = new TextDecoder("utf-8");
const response = await fetch(url);
const reader = response.body.getReader();
let { value: chunk, done: readerDone } = await reader.read();
chunk = chunk ? utf8Decoder.decode(chunk) : "";
const newline = /\r?\n/gm;
let startIndex = 0;
let result;
while (true) {
const result = newline.exec(chunk);
if (!result) {
if (readerDone) break;
const remainder = chunk.substr(startIndex);
({ value: chunk, done: readerDone } = await reader.read());
chunk = remainder + (chunk ? utf8Decoder.decode(chunk) : "");
startIndex = newline.lastIndex = 0;
continue;
}
yield chunk.substring(startIndex, result.index);
startIndex = newline.lastIndex;
}
if (startIndex < chunk.length) {
// Last line didn't end in a newline char
yield chunk.substr(startIndex);
}
}
async function observe() {
var pg = document.getElementById("ob_pg");
logShow('\nGET Status From Device as A Stream')
logShow('URL: http://101.200.39.65:17317/cfa09baa-4913-4ad7-a936-2e26f9671b04/obget_handler?uri=%2Fprinter%2Fstatus&id=12334"')
for await (const line of makeTextFileLineIterator("http://101.200.39.65:17317/cfa09baa-4913-4ad7-a936-2e26f9671b04/obget_handler?uri=%2Fprinter%2Fstatus&id=12334")) {
console.log(line);
const jsonObj = JSON.parse(line);
if (!jsonObj.hasOwnProperty("result")) {
console.log("Stream Error", line);
return
}
logShow("RESP: " + JSON.stringify(jsonObj["result"]))
if (jsonObj["result"]["code"] != "CODE_CONTINUE") {
bt_start.style.color = 'green';
}
const data = atob(jsonObj["result"]["data"])
if (data.length != 0) {
console.log(data);
logShow("RESP data: " + data)
pg.value = data.replace(/[^0-9]/ig, '')
}
}
}
bt_start.onclick = function () {
if (bt_start.style.color == 'red') {
console.log("click invalid");
logShow("click invalid")
return;
}
console.log("click and start...");
bt_start.style.color = 'red';
logShow("\nPOST To Device")
logShow("URL: http://101.200.39.65:17317/cfa09baa-4913-4ad7-a936-2e26f9671b04/post_handler")
logShow('BODY: {"uri":"/printer/action","id":12667,"data":"c3RhcnQ="}"')
logShow('BODY data: start')
fetch('http://101.200.39.65:17317/cfa09baa-4913-4ad7-a936-2e26f9671b04/post_handler', {
method: 'POST',
body: '{"uri":"/printer/action","id":12667,"data":"c3RhcnQ="}'
})
.then(response => response.json())
.then(data => {
console.log(data);
logShow('RESP: ' + JSON.stringify(data))
observe();
});
}
window.onload = (event) => {
console.log('page loaded, observing');
observe();
};
</script>
</body>
</html>