-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (40 loc) · 1.36 KB
/
index.ts
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
const shelly1IP: string = "http://192.168.33.1"; // TODO need to change a IP address for auth
// Get shelly status
async function getShellyStatus(): Promise<void> {
try {
const response = await fetch(`${shelly1IP}/status`);
const data = await response.json();
console.log("Status zaøízení:", data);
} catch (error) {
console.error("Chyba pøi získávání statusu:", error);
}
}
// Controll shelly modul
async function controlShelly(action: string): Promise<void> {
try {
const response = await fetch(`${shelly1IP}/relay/0/${action}`, { method: 'GET' });
if (!response.ok) {
console.error(`Chyba pøi pokusu o ${action} zaøízení. Status: ${response.status}`);
return;
}
else {
console.log(`Zaøízení bylo úspìšnì ${action}`); // Log success in action
setTimeout(getShellyStatus, 1000);
}
} catch (error) {
console.error(`Chyba pøi pokusu o ${action} zaøízení:`, error); // Log ERROR in action
}
}
// Just a test function
async function testShelly(): Promise<void> {
// Get Shelly status
await getShellyStatus();
// Turn On
await controlShelly('on');
// Wait 3 sec then turn off
setTimeout(async () => {
await controlShelly('off');
}, 3000);
}
// run test function
testShelly();