-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.html
More file actions
111 lines (111 loc) · 3.79 KB
/
Copy pathTerminal.html
File metadata and controls
111 lines (111 loc) · 3.79 KB
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css"/>
<style>
div.ex1 {
margin-left: 10px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Terminal 5</h1>
<div class="ex1">
HC3 IP address:<input value="192.168.1.57" id='IP' type="url"/></br>
User: <input value="admin" id='USER' type="text"/></br>
Passsword:<input value="admin" id='PWD' type="text"/></br>
ER DeviceID:<input value="1262" id='QA' type="text"/></br>
</div>
<script>
const IP = document.getElementById('IP');
const USER = document.getElementById('USER');
const PWD = document.getElementById('PWD');
const QA = document.getElementById('QA');
$('body').terminal(function(command) {
if (command !== '') {
try {
const url = `http://${IP.value}/api/callAction?deviceID=${QA.value}&name=eval&arg1=${encodeURIComponent(command)}`
fetch(url,
{
method: "GET",
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
//credentials: "include", // include, *same-origin, omit
mode: "no-cors", //no-cors, cors, *same-origin
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Accept-language": "en",
"X-Fibaro-Version": "2",
"Authorization":'Basic ' + btoa(USER.value + ":" + PWD.value)
}
}).catch((error) => {
$('body').terminal().error(error);
})
} catch(e) {
this.error(new String(e));
}
}
}, {
greetings: 'EventRunner5 v0.1',
name: 'eventrunner',
height: 500,
prompt: 'ER> '
});
var lastTime = 0; //Math.floor(Date.now()/1000);
var lastId = 0;
setInterval(function() {
var url = `http://${IP.value}/api/debugMessages?filter=QUICKAPP${QA.value},ER${QA.value}&from=${lastTime}&last=0&offset=10`;
console.log(`lastTime2: ${lastTime} lastId: ${lastId} url: ${url}`);
fetch(url,
{
method: "GET",
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
//credentials: "include", // include, *same-origin, omit
//mode: "no-cors", //no-cors, cors, *same-origin
headers: {
"Accept": "application/json",
//"Content-Type": "application/json",
"Access-Control-Allow-Origin":"*",
"Accept-language": "en",
"X-Fibaro-Version": "2",
"Authorization":'Basic ' + btoa(USER.value + ":" + PWD.value)
}
}).then((response) => {
console.log(response);
response.json().then( data => {
var messages = data.messages;
if (messages.length == 0) {
console.log('no messages');
return;
}
lastTime = messages[0].timestamp;
var id = messages[0].id;
messages = messages.reverse();
for (var i = 0; i < messages.length; i++) {
var msg = messages[i];
//$('body').terminal().echo(`msgTime: ${msg.timestamp} lastTime: ${lastTime}`);
if (msg.id <= lastId) {
continue;
}
var out = `${new Date(1000*msg.timestamp).toLocaleTimeString()}> ${msg.message}`;
$('body').terminal().echo($(`<div>${out}</div>`));
}
//$('body').terminal().echo(`lastTime: ${lastTime} lastId: ${lastId}`);
lastId = id;
});
}).catch( error => {
$('body').terminal().error(error);
});
}, 2000);
const clearTerminal = () => {
$('body').terminal().clear();
}
</script>
<div class="ex1">
<button onclick="clearTerminal()">Clear terminal</button>
</div>
</body>
</html>