-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggleSentry.ts
More file actions
48 lines (43 loc) · 1.19 KB
/
Copy pathtoggleSentry.ts
File metadata and controls
48 lines (43 loc) · 1.19 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
import axios from 'axios';
import fs from 'fs';
const TESLA_API_KEY = fs.readFileSync('.env', 'utf-8').split('=')[1].trim();
const BASE_URL = 'https://owner-api.teslamotors.com/api/1';
async function setSentryMode(vehicleId: string, on: boolean) {
try {
const response = await axios.post(
`${BASE_URL}/vehicles/${vehicleId}/command/set_sentry_mode`,
{ on },
{
headers: {
'Authorization': `Bearer ${TESLA_API_KEY}`,
},
},
);
console.log(`Sentry Mode ${on ? 'enabled' : 'disabled'}`);
} catch (error) {
console.error('Error toggling Sentry Mode:', error);
}
}
async function getVehicle() {
try {
const response = await axios.get(`${BASE_URL}/vehicles`, {
headers: {
'Authorization': `Bearer ${TESLA_API_KEY}`,
},
});
return response.data.response[0].id_s;
} catch (error) {
console.error('Error fetching vehicle data:', error);
}
}
(async () => {
const now = new Date();
const vehicleId = await getVehicle();
if (vehicleId) {
if (now.getHours() >= 22 || now.getHours() < 6) {
await setSentryMode(vehicleId, true);
} else {
await setSentryMode(vehicleId, false);
}
}
})();