-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhass.js
73 lines (70 loc) · 2.55 KB
/
hass.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
const fetch = require("node-fetch").default;
const config = require("./config.js")
async function main() {
const busp = await fetch(`${config.hass.hctb_api_server}/api/login?` + new URLSearchParams({
user: config.hass.username,
pass: config.hass.password,
code: config.hass.school_code
}), { method: "POST" })
const bus = await busp.json()
if (!bus.success) { // We basically want to stop it if anything bad happens
console.error(`Error: ${bus.error}`)
process.exit(1)
}
// Let's implement reverse lookup if we get a lat and a lon
var street = "Unavailable"
if (bus.lat && bus.lon) {
const reversep = await fetch(`${config.hass.hctb_api_server}/api/reverse?` + new URLSearchParams({
lat: bus.lat, lon: bus.lon
}), { method: "POST" })
const reverse = reversep.json()
if (!reverse.success || !reverse.name) return // We don't need to look it up further.
street = reverse.name
}
await fetch(`${config.hass.instance_base}/api/states/hctb.street_name`, {
headers: {
"Authorization": `Bearer ${config.hass.access_token}`,
"Content-Type": `application/json`
},
body: JSON.stringify({
state: street,
entity_id: "hctb.street_name"
}),
method: "POST"
})
await fetch(`${config.hass.instance_base}/api/states/hctb.latitude`, {
headers: {
"Authorization": `Bearer ${config.hass.access_token}`,
"Content-Type": `application/json`
},
body: JSON.stringify({
state: bus.lat || "Unavailable",
entity_id: "hctb.latitude"
}),
method: "POST"
})
await fetch(`${config.hass.instance_base}/api/states/hctb.longitude`, {
headers: {
"Authorization": `Bearer ${config.hass.access_token}`,
"Content-Type": `application/json`
},
body: JSON.stringify({
state: bus.lon || "Unavailable",
entity_id: "hctb.longitude"
}),
method: "POST"
})
await fetch(`${config.hass.instance_base}/api/states/hctb.route_status`, {
headers: {
"Authorization": `Bearer ${config.hass.access_token}`,
"Content-Type": `application/json`
},
body: JSON.stringify({
state: (bus.lat && bus.lon) ? "In service": "Not running",
entity_id: "hctb.route_status"
}),
method: "POST"
})
}
main()
if (config.hass.cron.builtIn) setInterval(main, config.hass.cron.frequency || 1000 * 60)