Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
speedharm-cli.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (C) 2017-2021 LEIDOS.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17
18from __future__ import print_function
19import json
20import requests
21import re
22import urlparse
23
24# Disable annoying insecure requests warnings, we're using a self-signed cert
25from requests.packages.urllib3.exceptions import InsecureRequestWarning
26requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
27
28version_id = "v0.1" # Version string for cli client
29running = True # Running flag for program will execute main loop as long as true
30base_url = "http://35.153.64.44:8081" # Base url of for AWS server, defaults to localhost.
31#base_url = "http://localhost:8081" # Base url of VM localhost.
32rel_url = ""
33
34# Constants
35vehicles_url = "/rest/vehicles"
36experiments_url = "/rest/experiments"
37algorithms_url = "/rest/algorithms"
38
39
41 try:
42 print(resp.status_code)
43 print(resp.headers)
44 print(resp.json())
45 except:
46 print("Unable to decode the rest of the response.")
47
49 print(cmd + " is not a recognized command. Try \"help\" for help.")
50
52 exec("global " + parts[1] + ";" + parts[1] + " = " + parts[2])
53
55 full_url = get_cur_url()
56 if (len(parts) > 1):
57 # A url has been specified
58 full_url = urlparse.urljoin(full_url, parts[1])
59
60 r = requests.get(full_url, verify=False)
62
64 full_url = get_cur_url()
65 if (len(parts) > 1):
66 full_url = urlparse.urljoin(full_url, parts[1])
67
68 ack = raw_input("Are you sure you want to delete " + full_url +"?\n(y/N): ")
69 if ack.lower() == "y":
70 r = requests.delete(full_url, verify=False)
72
73def cd(new_url):
74 global rel_url
75 rel_url = urlparse.urljoin(rel_url, new_url)
76
78 cd(parts[1])
79
81 data_start = 1
82 full_url = get_cur_url()
83 # if len(parts) > 2:
84 # data_start = 2
85 # full_url = urlparse.urljoin(full_url, parts[1])
86
87 data = eval(" ".join(parts[data_start:]))
88
89 headers = { "Content-Type" : "application/json" }
90
91 r = requests.post(full_url, data=json.dumps(data), headers=headers, verify=False)
93
95 uniqVehId = raw_input("Unique Vehicle ID: ")
96 description = raw_input("Vehicle Description: ")
97 full_url = urlparse.urljoin(base_url, vehicles_url)
98 headers = { "Content-Type" : "application/json" }
99 r = requests.post(full_url, data=json.dumps({"uniqVehId": uniqVehId, "description": description}), headers=headers, verify=False)
101
103 description = raw_input("Experiment Description: ")
104 location = raw_input("Experiment Location: ")
105 full_url = urlparse.urljoin(base_url, experiments_url)
106 headers = { "Content-Type" : "application/json" }
107 r = requests.post(full_url, data=json.dumps({"description": description, "location": location}), headers=headers, verify=False)
109
111 # Get the available algorithms
112 r = requests.get(urlparse.urljoin(base_url, "/rest/"), verify=False)
113 algos = r.json()["availableAlgorithms"]
114 print("Select Algorithm: ")
115 for a in zip(algos, range(len(algos))):
116 print("{}. {}".format(a[1] + 1, a[0]))
117
118 idx = input("Choice #? ") - 1
119
120 full_url = urlparse.urljoin(base_url, algorithms_url)
121 headers = { "Content-Type" : "application/json" }
122 data = json.dumps({"className": algos[idx]})
123
124 r = requests.post(full_url, data=data, headers=headers, verify=False)
126
128 if parts[1] == "vehicle":
130 elif parts[1] == "experiment":
132 elif parts[1] == "algorithm":
134 else:
135 print("Unknown entity to create: " + parts[1])
136
138 r = requests.get(urlparse.urljoin(base_url, vehicles_url), verify=False).json()
139
140 print("Select Vehicle:")
141 for veh in r:
142 print("{}. Description: {}; Unique ID: {}".format(veh["id"], veh["description"], veh["uniqVehId"]))
143
144 return raw_input("Vehicle ID#? ")
145
147 veh_id = select_vehicle()
148
149 r = requests.get(urlparse.urljoin(base_url, experiments_url), verify=False).json()
150
151 print("Select Experiment: ")
152 for exp in r:
153 print("{}. Description: {}; Location: {}".format(exp["id"], exp["description"], exp["location"]))
154 exp_id = raw_input("Experiment ID#? ")
155
156 headers = { "Content-Type" : "application/json" }
157 data = json.dumps({"id": int(veh_id)})
158
159 r = requests.post(urlparse.urljoin(base_url, experiments_url + "/" + exp_id + "/vehicles"), data=data, headers=headers, verify=False)
161
163 veh_id = select_vehicle()
164
165 r = requests.get(urlparse.urljoin(base_url, algorithms_url), verify=False).json()
166
167 print("Select Algorithm: ")
168 for exp in r:
169 print("{}. Class: {}".format(exp["id"], exp["className"]))
170 algo_id = raw_input("Algorithm ID#? ")
171
172 headers = { "Content-Type" : "application/json" }
173 data = json.dumps({"id": int(veh_id)})
174
175 r = requests.post(urlparse.urljoin(base_url, algorithms_url + "/" + algo_id + "/vehicles"), data=data, headers=headers, verify=False)
177
179 if parts[1] == "experiment":
181 elif parts[1] == "algorithm":
183 else:
184 print("Unknown relationship to assign: " + parts[1])
185
186
188 r = requests.get(urlparse.urljoin(base_url, vehicles_url), verify=False).json()
189
190 print("Vehicles:")
191 for veh in r:
192 print("{}. Description: {}; Unique ID: {}".format(veh["id"], veh["description"], veh["uniqVehId"]))
193
195 r = requests.get(urlparse.urljoin(base_url, experiments_url), verify=False).json()
196
197 print("Experiments:")
198 for veh in r:
199 print("{}. Description: {}; Location: {}".format(veh["id"], veh["description"], veh["location"]))
200
202 r = requests.get(urlparse.urljoin(base_url, algorithms_url), verify=False).json()
203
204 print("Algorithms:")
205 for veh in r:
206 print("{}. ClassName: {}".format(veh["id"], veh["className"]))
207
209 if parts[1] == "vehicles":
211 elif parts[1] == "experiments":
213 elif parts[1] == "algorithms":
215 else:
216 print("Unknown entity to list: " + parts[1])
217
219 if parts[1] == "vehicle":
220 full_url = urlparse.urljoin(base_url, vehicles_url)
221 elif parts[1] == "experiment":
222 full_url = urlparse.urljoin(base_url, experiments_url)
223 elif parts[1] == "algorithm":
224 full_url = urlparse.urljoin(base_url, algorithms_url)
225 else:
226 print("Unknown entity to fetch: " + parts[1])
227 return
228
229 r = requests.get(full_url + "/" + parts[2], verify=False)
231
232def parse_input(cmd):
233 """
234 Parse the command input and execute the appropriate action
235 """
236
237 parts = cmd.split(' ') # Split on spaces
238
239 args = {"set" : "variable value", "get" : "[url]", "cd" : "rel_url",
240 "post" : "data", "delete" : "[url]", "create" : "(vehicle | experiment | algorithm)",
241 "assign" : "(experiment | algorithm)", "list" : "(vehicle | experiment | algorithm)",
242 "fetch" : "(vehicle | experiment | algorithm) id", "help": "", "exit" : ""}
243
244 try:
245 if parts[0] == "exit":
246 global running
247 running = False
248 exit(0)
249 elif parts[0] == "set":
251 elif parts[0] == "get":
253 elif parts[0] == "cd":
254 process_cd_command(parts)
255 elif parts[0] == "post":
257 elif parts[0] == "delete":
259 elif parts[0] == "create":
261 elif parts[0] == "assign":
263 elif parts[0] == "list":
265 elif parts[0] == "fetch":
267 elif parts[0] == "help":
268 print("Commands: ")
269 for cmd, usage in args.items():
270 print("{} {}".format(cmd, usage))
271 else:
272 unknown_command(cmd)
273 except IndexError:
274 print("Incorrect usage of: " + parts[0])
275 print("Usage: " + parts[0] + " " + args[parts[0]])
276 except requests.exceptions.ConnectionError:
277 print("Unable to connect to " + base_url + ".")
278
280 return urlparse.urljoin(base_url, rel_url)
281
282def main():
283 print("Speed Harmonization Command Line Configurator " + version_id + ".")
284 while running:
285 parse_input(raw_input(get_cur_url() + ">> "))
286
287if __name__ == "__main__":
288 main()
289
def unknown_command(cmd)
def process_create_vehicle()
def cd(new_url)
def process_delete_command(parts)
def process_get_command(parts)
def process_assign_command(parts)
def process_list_vehicles()
def process_create_algorithm()
def parse_input(cmd)
def process_create_experiment()
def print_response(resp)
def process_fetch_command(parts)
def process_create_command(parts)
def process_set_command(parts)
def process_assign_algorithm()
def process_list_experiments()
def process_list_command(parts)
def process_list_algorithms()
def process_cd_command(parts)
def select_vehicle()
def process_assign_experiment()
def process_post_command(parts)