-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (99 loc) · 3.71 KB
/
server.js
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
const express = require('express')
const app = express()
const port = 3000
app.use(express.static("public"));
const baseHAUrl = 'http://jserver.fritz.box:8123/api/';
const haToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiYzFhNDUyNTU4N2U0OTVkODJlYTM0NzU2YmMyNzA3YyIsImlhdCI6MTcxNTUxNzc5MywiZXhwIjoyMDMwODc3NzkzfQ.ZToIXAD8luAB2jdQ678wtlvdQW5SFbTPqKuPc7QRJl4'
const sensorsToGet = ['sensor.apollo_msr_1_bad7fc_co2', 'sensor.lumi_lumi_weather_temperatur', 'sensor.apollo_msr_1_bad7fc_ltr390_light', 'sensor.lumi_lumi_weather_luftfeuchtigkeit',
'sensor.tz3000_2putqrmw_ts011f_leistung_2', 'sensor.nous_steckdose_2_active_power', 'automation.wecker', 'device_tracker.a52s_j']
app.get('/api/getSensorData', (req, res) => {
getSensorData().then(returnData => {
console.log('data', returnData)
res.send(returnData)
})
})
app.get('/api/toggleBackground', (req, res) => {
fetch(baseHAUrl + 'services/light/toggle', {
method: 'POST',
headers: { 'Authorization': haToken, 'Content-Type': 'application/json'},
body: JSON.stringify({ entity_id: 'light.hintergrund' })
}).then(response => response.json()).then(data => {
console.log('data', data)
res.send(data)
}).catch(error => {
console.error('Error:', error);
});
})
app.get('/api/toggleFloalt', (req, res) => {
fetch(baseHAUrl + 'services/light/toggle', {
method: 'POST',
headers: { 'Authorization': haToken, 'Content-Type': 'application/json'},
body: JSON.stringify({ entity_id: 'light.ikea_of_sweden_floalt_panel_ws_60x60_licht' })
}).then(response => response.json()).then(data => {
console.log('data', data)
res.send(data)
}).catch(error => {
console.error('Error:', error);
});
})
app.get('/api/toggleFan', (req, res) => {
fetch(baseHAUrl + 'services/switch/toggle', {
method: 'POST',
headers: { 'Authorization': haToken, 'Content-Type': 'application/json'},
body: JSON.stringify({ entity_id: 'switch.nous3_schalter_3' })
}).then(response => response.json()).then(data => {
console.log('data', data)
res.send(data)
}).catch(error => {
console.error('Error:', error);
});
})
app.get('/api/toggleAlarm', (req, res) => {
fetch(baseHAUrl + 'services/automation/toggle', {
method: 'POST',
headers: { 'Authorization': haToken, 'Content-Type': 'application/json'},
body: JSON.stringify({ entity_id: 'automation.wecker' })
}).then(response => response.json()).then(data => {
console.log('data', data)
res.send(data)
}).catch(error => {
console.error('Error:', error);
});
})
app.get('/api/togglePCPower', (req, res) => {
fetch(baseHAUrl + 'services/switch/toggle', {
method: 'POST',
headers: { 'Authorization': haToken, 'Content-Type': 'application/json'},
body: JSON.stringify({ entity_id: 'switch.tz3000_2putqrmw_ts011f_schalter_2' })
}).then(response => response.json()).then(data => {
console.log('data', data)
res.send(data)
}).catch(error => {
console.error('Error:', error);
});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
async function getSensorData() {
var returnData = [];
var fetchPromises = sensorsToGet.map(Element => {
return fetch(baseHAUrl + 'states/' + Element, {
method: 'GET',
headers: {
'Authorization': haToken,
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
returnData.push({ 'name': data.attributes.friendly_name, 'state': data.state, 'unit_of_measurement': data.attributes.unit_of_measurement })
})
.catch(error => {
console.error('Error:', error);
});
});
await Promise.all(fetchPromises);
console.log('return Data', returnData)
return returnData;
}