-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
179 lines (141 loc) · 3.78 KB
/
server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from flask import Flask, request
from flask_restful import Resource, Api, reqparse
from gm_api import GM_Api
from utils import get_value
app = Flask(__name__)
api = Api(app)
# add request parser for post requests
parser = reqparse.RequestParser()
parser.add_argument('action', required=True, help='Need an action')
class Vehicle_Name(Resource):
URL = '/getVehicleInfoService'
'''
{
"vin": "1213231",
"color": "Metallic Silver",
"doorCount": 4,
"driveTrain": "v8"
}
'''
def body(id):
return {'id': id}
def parse_response(data):
json = {}
json['vin'] = get_value(data, 'vin')
json['color'] = get_value(data, 'color')
json['doorCount'] = 4 if get_value(data, 'fourDoorSedan') else 2
json['driveTrain'] = get_value(data, 'driveTrain')
return json
def get(self, id):
payload = GM_Api.post(self.URL, Vehicle_Name.body(id))
if payload.get('error'):
return payload
return Vehicle_Name.parse_response(payload.get('data'))
class Vehicle_Doors(Resource):
URL = '/getSecurityStatusService'
'''
[
{
"location": "frontLeft",
"locked": true
},
{
"location": "frontRight",
"locked": true
},
{
"location": "backLeft",
"locked": true
},
{
"location": "backRight",
"locked": false
}
]
'''
def body(id):
return {'id': id}
def parse_response(data):
values = data.get('doors').get('values')
doors = {}
for door in values:
doors[get_value(door, 'location')] = get_value(door, 'locked')
json = []
for location in ['frontLeft', 'frontRight', 'backLeft', 'backRight']:
if location not in doors:
continue
json.append({
'location': location,
'locked': doors[location],
})
return json
def get(self, id):
payload = GM_Api.post(self.URL, Vehicle_Doors.body(id))
if payload.get('error'):
return payload
return Vehicle_Doors.parse_response(payload.get('data'))
class Vehicle_Fuel(Resource):
URL = '/getEnergyService'
'''
{
"percent": 30.2
}
'''
def body(id):
return {'id': id}
def parse_response(data):
json = {}
json['percent'] = get_value(data, 'tankLevel')
return json
def get(self, id):
payload = GM_Api.post(self.URL, Vehicle_Fuel.body(id))
if payload.get('error'):
return payload
return Vehicle_Fuel.parse_response(payload.get('data'))
class Vehicle_Battery(Resource):
URL = '/getEnergyService'
'''
{
"percent": 50.3
}
'''
def body(id):
return {'id': id}
def parse_response(data):
json = {}
json['percent'] = get_value(data, 'batteryLevel')
return json
def get(self, id):
payload = GM_Api.post(self.URL, Vehicle_Battery.body(id))
if payload.get('error'):
return payload
return Vehicle_Battery.parse_response(payload.get('data'))
class Vehicle_Engine(Resource):
URL = '/actionEngineService'
'''
{
"status": "success|error"
}
'''
def body(id, action):
return ({
'id': id,
'command': 'START_VEHICLE' if action == 'START' else 'STOP_VEHICLE',
})
def parse_response(data):
json = {}
json['status'] = 'success' if data.get('status') == 'EXECUTED' else 'error'
return json
def post(self, id):
args = parser.parse_args()
payload = GM_Api.post(self.URL, Vehicle_Engine.body(id, args.get('action')))
if payload.get('error'):
return payload
return Vehicle_Engine.parse_response(payload.get('actionResult'))
api.add_resource(Vehicle_Name, '/vehicles/<id>')
api.add_resource(Vehicle_Doors, '/vehicles/<id>/doors')
api.add_resource(Vehicle_Fuel, '/vehicles/<id>/fuel')
api.add_resource(Vehicle_Battery, '/vehicles/<id>/battery')
api.add_resource(Vehicle_Engine, '/vehicles/<id>/engine')
if __name__ == '__main__':
app.run()