-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmw.py
151 lines (125 loc) · 5.6 KB
/
bmw.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env python
import requests
import json
import urllib
import boto3
import logging
import sys
import pickle
logging.basicConfig(filename='/root/bmw/bmw.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
BASE_URL = 'b2vapi.bmwgroup.us'
def getToken(BASE_URL, username, password):
try:
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "124",
"Connection": "Keep-Alive",
"Host": BASE_URL,
"Accept-Encoding": "gzip",
"Authorization": "Basic blF2NkNxdHhKdVhXUDc0eGYzQ0p3VUVQOjF6REh4NnVuNGNEanli"
"TEVOTjNreWZ1bVgya0VZaWdXUGNRcGR2RFJwSUJrN3JPSg==",
"Credentials": "nQv6CqtxJuXWP74xf3CJwUEP:1zDHx6un4cDjybLENN3kyfumX2kEYigWPcQpdvDRpIBk7rOJ",
"User-Agent": "okhttp/2.60"}
data = {
'grant_type': 'password',
'scope': 'authenticate_user vehicle_data remote_services',
'username': username,
'password': password}
data = urllib.parse.urlencode(data)
url = 'https://' + BASE_URL + '/webapi/oauth/token'
r = requests.post(url, data=data, headers=headers)
if r.status_code == 200:
logging.info('Access token acquired')
return r.json()['access_token']
else:
raise ValueError('Unable to login')
except Exception as msg:
logging.error(msg)
sys.exit(1)
def getBattery(BASE_URL, token, vin):
try:
headers = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_1_1 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 Mobile/15B150 Safari/604.1',
'Authorization': 'Bearer ' + token}
url = 'https://' + BASE_URL + '/webapi/v1/user/vehicles/' + vin + '/status'
r = requests.get(url, headers=headers)
if r.status_code == 200:
status = r.json()
chargingLevelHv = status['vehicleStatus']['chargingLevelHv']
remainingRangeElectricMls = status['vehicleStatus']['remainingRangeElectricMls']
connectionStatus = status['vehicleStatus']['connectionStatus']
chargingStatus = status['vehicleStatus']['chargingStatus']
logging.info('Battery data retreived successfully')
return chargingLevelHv, remainingRangeElectricMls, connectionStatus, chargingStatus
else:
raise ValueError('Unable to retreive battery data from vehicle')
except Exception as msg:
logging.error(msg)
sys.exit(1)
def sendSMS(mobile_number, message, aws_access_key_id, aws_secret_access_key, aws_region_name):
try:
client = boto3.client(
'sns',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region_name
)
client.publish(
PhoneNumber=mobile_number,
Message=message
)
logging.info('SMS message sent successfully')
except Exception as msg:
logging.error(msg)
sys.exit(1)
def getCredentials():
with open('/root/bmw/credentials.json', 'r') as cf:
credentials = json.load(cf)
return credentials
def getLastPercent():
try:
with open ('/root/bmw/last_percent', 'rb') as lp:
last_percent = pickle.load(lp)
logging.info(f'last_percent loaded with a value of {last_percent}')
return last_percent
except FileNotFoundError:
logging.error('last_percent file not found')
with open('/root/bmw/last_percent', 'wb') as lp:
pickle.dump(0, lp)
logging.info('last_percent file created with value of 0')
with open ('/root/bmw/last_percent', 'rb') as lp:
last_percent = pickle.load(lp)
return last_percent
def setLastPercent(chargingLevelHv):
try:
with open('/root/bmw/last_percent', 'wb') as lp:
pickle.dump(chargingLevelHv, lp)
logging.info(f'last_percent file updated with {chargingLevelHv}')
except Exception as msg:
logging.error(msg)
sys.exit(1)
try:
last_percent = int(getLastPercent())
credentials = getCredentials()
token = getToken(BASE_URL, credentials['username'], credentials['password'])
chargingLevelHv, remainingRangeElectricMls, connectionStatus, chargingStatus = getBattery(BASE_URL, token, credentials['vin'])
message = 'My 2014 BMW i3 Status:\n' + f'Battery: {chargingLevelHv}% ({remainingRangeElectricMls} mi) - {connectionStatus}/{chargingStatus}'
if chargingLevelHv > last_percent and chargingLevelHv == 100:
setLastPercent(chargingLevelHv)
logging.info(message)
sendSMS(credentials['mobile_number'], message, credentials['aws_access_key_id'], credentials['aws_secret_access_key'], credentials['aws_region_name'])
elif chargingLevelHv > last_percent and remainingRangeElectricMls == 40:
setLastPercent(chargingLevelHv)
logging.info(message)
sendSMS(credentials['mobile_number'], message, credentials['aws_access_key_id'], credentials['aws_secret_access_key'], credentials['aws_region_name'])
else:
setLastPercent(chargingLevelHv)
logging.info(message)
#sendSMS(credentials['mobile_number'], message, credentials['aws_access_key_id'], credentials['aws_secret_access_key'], credentials['aws_region_name'])
#print(message)
except Exception as msg:
logging.error(msg)
sys.exit(1)