-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.py
More file actions
244 lines (206 loc) · 7.42 KB
/
api.py
File metadata and controls
244 lines (206 loc) · 7.42 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
Cloud connected autonomous RC car.
Copyright 2016 Visible Energy Inc. All Rights Reserved.
"""
__license__ = """
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import subprocess
import pdb
import utils
import streamer
from runtime import Runtime
# JSON-RPC errors
JSON_RPC_PARSE_ERROR = '{"jsonrpc": "2.0","error":{"code":-32700,"message":"Parse error"},"id": null}'
JSON_RPC_INVALID_REQUEST = '{"jsonrpc": "2.0","error":{"code":-32600,"message":"Invalid Request"},"id":null}'
JSON_RPC_METHOD_NOTFOUND_FMT_STR = '{"jsonrpc":"2.0","error":{"code": -32601,"message":"Method not found"},"id": %s}'
JSON_RPC_METHOD_NOTFOUND_FMT_NUM = '{"jsonrpc":"2.0","error":{"code": -32601,"message":"Method not found"},"id": %d}'
JSON_RPC_INVALID_PARAMS_FMT_STR = '{"jsonrpc":"2.0","error":{"code": -32602,"message":"Method not found"},"id": %s}'
JSON_RPC_INVALID_PARAMS_FMT_NUM = '{"jsonrpc":"2.0","error":{"code": -32602,"message":"Method not found"},"id": %d}'
JSON_RPC_INTERNAL_ERROR_FMT_STR = '{"jsonrpc":"2.0","error":{"code": -32603,"message":"Method not found"},"id": %s}'
JSON_RPC_INTERNAL_ERROR_FMT_NUM = '{"jsonrpc":"2.0","error":{"code": -32602,"message":"Method not found"},"id": %d}'
# Vehicle object instantiated in the application module
car=None
def message_handler(msg, msg_len):
"""
The generic message handler for Cometa receive callback.
Invoked every time the Cometa object receives a JSON-RPC message for this device.
It returns the JSON-RPC result object to send back to the application that sent the request.
The rpc_methods tuple contains the mapping of names into functions.
"""
# pdb.set_trace()
try:
req = json.loads(msg)
except:
# the message is not a json object
car.log("Received JSON-RPC invalid message (parse error): %s" % msg, escape=True)
return JSON_RPC_PARSE_ERROR
# check the message is a proper JSON-RPC message
ret,id = utils.check_rpc_msg(req)
if not ret:
if id and utils.isanumber(id):
return JSON_RPC_INVALID_PARAMS_FMT_NUM % id
if id and isinstance(id, str):
return JSON_RPC_INVALID_PARAMS_FMT_STR % id
else:
return JSON_RPC_PARSE_ERROR
car.log("JSON-RPC: %s" % msg, escape=True)
method = req['method']
func = None
# check if the method is in the registered list
for m in rpc_methods:
if m['name'] == method:
func = m['function']
break
if func == None:
return JSON_RPC_INVALID_REQUEST
# call the method
try:
result = func(req['params'])
except Exception as e:
print e
return JSON_RPC_INTERNAL_ERROR_FMT_STR % str(id)
# build the response object
reply = {}
reply['jsonrpc'] = "2.0"
reply['result'] = result
reply['id'] = req['id']
return json.dumps(reply)
# --------------------
#
# RPC Methods
def _rexec(params):
"""Start a subprocess shell to execute the specified command and return its output.
params - a one element list ["/bin/cat /etc/hosts"]
"""
# check that params is a list
if not isinstance(params, list) or len(params) == 0:
return "Parameter must be a not empty list"
command = params[0]
try:
subprocess.check_call(command,shell=True)
out = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()
return '\n' + out.decode()
except Exception, e:
print e
return "{\"msg\":\"Invalid command.\"}"
def _video_devices(params):
"""List available video devices (v4l)."""
vdevices = Runtime.list_camera_devices()
ret = {}
ret['devices'] = vdevices[0]
ret['names'] = vdevices[1]
return ret
def _set_telemetry_period(params):
"""Set telemetry period in seconds.
params - JSON object {'period':5}
"""
if type(params) is not dict or 'period' not in params.keys():
return {"success": False}
if params['period'] <= 0:
return {"success": False}
car.telemetry_period=params['period']
return {"success": True}
def _get_config(params):
""" Get configuration object """
config = Runtime.read_config()
config['app_params']['verbose']=car.verbose
config['app_params']['telemetry_period']=car.telemetry_period
return config
def _get_status(params):
ret = {}
ret['state'] = car.state
ret['mode'] = car.mode
ret['steering'] = car.steering
ret['throttle'] = car.throttle
ret['GPS'] = {}
try:
ret['GPS']['lat'] = car.readings['lat']
ret['GPS']['lon'] = car.readings['lon']
except:
pass
return ret
def _set_throttle(params):
""" Set the throttle """
if type(params) is not dict or 'value' not in params.keys():
return {"success": False}
val = params['value']
# only values in the [0.180] range
if val < 0 or 180 < val:
return {"success": False}
# TODO: call autonomoia.set_throttle(val)
car.throttle=val
return {"success": True}
def _set_steering(params):
""" Set the steering """
if type(params) is not dict or 'value' not in params.keys():
return {"success": False}
val = params['value']
# only values in the [0.180] range
if val < 0 or 180 < val:
return {"success": False}
#car.cur_steering = val
car.steering=val
return {"success": True}
def _set_mode(params):
""" Set the vehicle running mode"""
if type(params) is not dict or 'value' not in params.keys():
return {"success": False}
val = params['value']
if val not in ("AUTO","TRAINING","REMOTE"):
return {"success": False}
if val == "AUTO":
car.mode2auto()
elif val == "TRAINING":
car.mode2training()
elif val == "REMOTE":
car.mode2remote()
return {"success": True}
def _stop(params):
car.state2idle()
return {"success": True}
def _start(params):
car.state2run()
return {"success": True}
def _video_stop(params):
streamer.video_stop()
return {"success": True}
def _video_start(params):
if type(params) is not dict or 'telem' not in params.keys():
return {"success": False}
#pdb.set_trace()
telem = params['telem']
streamer.video_start(telem)
return {"success": True}
def _load_model(params):
if type(params) is not dict or 'path' not in params.keys():
return {"success": False}
ret = car.load_model(params['path'])
if ret:
return {"success": True}
else:
return {"success": False}
global rpc_methods
rpc_methods = ({'name':'rexec','function':_rexec},
{'name':'video_devices','function':_video_devices},
{'name':'set_telemetry_period','function':_set_telemetry_period},
{'name':'get_config','function':_get_config},
{'name':'get_status','function':_get_status},
{'name':'set_throttle','function':_set_throttle},
{'name':'set_steering','function':_set_steering},
{'name':'set_mode','function':_set_mode},
{'name':'stop','function':_stop},
{'name':'start','function':_start},
{'name':'video_start','function':_video_start},
{'name':'video_stop','function':_video_stop},
{'name':'load_model','function':_load_model},
)