18from __future__
import print_function
23from datetime
import datetime
28from requests.packages.urllib3.exceptions
import InsecureRequestWarning
29requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
32base_url =
"http://35.153.64.44:8081"
35vehicles_url =
"/rest/vehicles"
36experiments_url =
"/rest/experiments"
37algorithms_url =
"/rest/algorithms"
38default_algorithm =
"gov.dot.fhwa.saxton.speedharm.executive.algorithms.ReduceSpeedAlgorithm"
39sleep_duration_millis = 1000
42 cur_datetime = datetime.utcnow()
43 unix_ts = calendar.timegm(cur_datetime.utctimetuple())
44 print(
"[" + str(unix_ts) +
"] " + str(msg))
52 r = requests.get(urlparse.urljoin(base_url, experiments_url), verify=
False).json()
54 if exp[
"description"].find(
"AUTO-CONFIG") > -1:
55 return urlparse.urljoin(base_url, experiments_url +
"/" + exp[
"id"])
60 full_url = urlparse.urljoin(base_url, experiments_url)
61 headers = {
"Content-Type" :
"application/json" }
62 r = requests.post(full_url, data=json.dumps({
"description": desc,
"location": loc}), headers=headers, verify=
False)
64 return r.headers[
"Location"]
67 headers = {
"Content-Type" :
"application/json" }
68 data = json.dumps({
"id": int(veh_id)})
69 r = requests.post(urlparse.urljoin(experiment_url +
"/vehicles",
""), data=data, headers=headers, verify=
False)
72 r = requests.get(urlparse.urljoin(base_url, vehicles_url), verify=
False)
77 full_url = urlparse.urljoin(base_url, algorithms_url)
78 headers = {
"Content-Type" :
"application/json" }
79 data = json.dumps({
"className": algo_name})
81 r = requests.post(full_url, data=data, headers=headers, verify=
False)
83 return r.headers[
"Location"]
86 headers = {
"Content-Type" :
"application/json" }
87 data = json.dumps({
"id": int(veh_id)})
88 r = requests.post(urlparse.urljoin(algo_url +
"/vehicles",
""), data=data, headers=headers, verify=
False)
92 log(
"Speed Harmonization Auto Configurator " + version_id +
".")
93 log(
"Connecting to " + base_url +
"...")
96 if not "expId" in veh:
98 log(
"Detected new vehicle ID=" + str(veh[
"id"]) +
" with no experiment, assigning to experiment.")
100 if not active_experiment_url:
101 cur_datetime = datetime.utcnow()
102 unix_ts = calendar.timegm(cur_datetime.utctimetuple())
103 active_experiment_url =
create_experiment(
"AUTO-CONFIG Experiment Generated @ " + str(unix_ts),
"UNSPECIFIED")
104 log(
"Created new experiment at " + active_experiment_url)
106 log(
"Discovered active experiment at " + active_experiment_url)
108 log(
"Assigned vehicle ID=" + str(veh[
"id"]) +
" to experiment " + active_experiment_url)
110 log(
"Detected new vehicle ID=" + str(veh[
"id"]) +
" with no algorithm, assigning to algorithm.")
112 log(
"Created new algorithm of type " + default_algorithm +
" at " + algo_url)
114 log(
"Assigned vehicle ID=" + str(veh[
"id"]) +
" to algorithm " + algo_url)
115 log(
"Sleeping " + str(sleep_duration_millis) +
"ms...")
116 time.sleep(sleep_duration_millis / 1000.0)
118if __name__ ==
"__main__":