-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.php
More file actions
100 lines (74 loc) · 2.77 KB
/
init.php
File metadata and controls
100 lines (74 loc) · 2.77 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
<?php
//error_reporting(0);
// Copyright (C) 2017, Ward Mundy & Associates LLC with MIT license
//global variables
$TESLA_API_URL = "https://owner-api.teslamotors.com/";
$API_LAST_ERROR="";
$TOKEN="";
$ID=0;
function init() {
global $TOKEN,$ID;
// test for token existing first, before trying to read it
if ( ! file_exists("token") ) {
echo "You first must generate a new token.\r\n";
echo "Run token.php and vehicle.php now.\r\n";
echo "Then try again.\r\n";
exit;
}
$tokendata=file_get_contents("token");
$tokenjson = json_decode($tokendata);
$expires=$tokenjson->created_at + $tokenjson->expires_in;
if ( time() > $expires ) {
echo "Tokens are valid 3 months. Yours has expired.\r\n";
echo "You need to generate a new token.\r\n";
echo "Run token.php and vehicle.php now.\r\n";
echo "Then try again.\r\n";
exit;
}
$TOKEN=$tokenjson->access_token;
// echo "TOKEN: ";
// echo $TOKEN;
// echo "\r\n";
if (file_exists("vehicle")) $ID = file_get_contents("vehicle");
}
//general function to query the tesla API
function queryTeslaAPI ($token, $url,$post=false,$params="") {
global $TESLA_API_URL,$API_LAST_ERROR;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $TESLA_API_URL.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, $post);
if (is_array($params)) curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
$response = curl_exec($ch);
curl_close($ch);
$response_array = json_decode ($response, true); // no worky with some long id's
// $response_array = json_decode ($response, true,512, JSON_BIGINT_AS_STRING);
$API_LAST_ERROR=$response_array;
return $response_array["response"];
}
function ReadTeslaAPI($token,$carid) {
global $TESLA_API_URL,$API_LAST_ERROR;
$climate= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/climate_state");
$drive= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/drive_state");
$charge= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/charge_state");
$state= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/vehicle_state");
$api=array();
$api["climate"]=$climate;
$api["drive"]=$drive;
$api["charge"]=$charge;
$api["state"]=$state;
$api["rated"]=(int)$charge["battery_range"];
$api["soc"]=(int)$charge["battery_level"];
$api["odometer"]=(int)$state["odometer"];
$api["ctemp"]=$climate["outside_temp"] ;
$api["temp"]=($climate["outside_temp"] * 9/5) + 32;
$api["lat"]=$drive["latitude"];
$api["lng"]=$drive["longitude"];
return $api;
}
?>