Created
August 25, 2019 09:27
-
-
Save nivleshc/5f0a73c750cb0828dc409546e967e9bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_vehicle_position_webpage(map_url, body, s3Bucket, s3ImageKey, imagefilename, mainHtmlfilename, today): | |
global dict_pushpinLabel_to_desc_map | |
global dict_vehicleId_to_pushpinLabel_map | |
global lastpushpinLabel | |
global total_vehicles_found | |
#get a handle on the s3 bucket | |
s3 = boto3.resource('s3') | |
imgObject = s3.Object(s3Bucket,s3ImageKey+imagefilename) | |
map_webrequest = requests.post(map_url, data=body) | |
#check if the maps were successfully obtained. | |
if map_webrequest.status_code != 200: | |
#there was an error. show the error | |
print("generate_vehicle_position_webpage:Bing Map request failed") | |
print("generate_vehicle_position_webpage:Reason:", map_webrequest.reason) | |
print("generate_vehicle_position_webpage:ErrorMessage:", map_webrequest.text) | |
print("generate_vehicle_position_webpage:RequestBody:", body) | |
#as there has been an error generating the map, display an image denoting that there has been an error | |
map_webrequest = requests.get(maps_error_imageURL) | |
else: | |
print("generate_vehicle_position_webpage:Bing Map request was successful. Status code:",map_webrequest.status_code) | |
#upload the image file to S3 bucket, set it for public read and ensure content-type is image/jpeg | |
print("generate_vehicle_position_webpage:Uploading map to s3 bucket") | |
upload_img_result = imgObject.put(Body=map_webrequest.content,ACL='public-read',ContentType='image/jpeg') | |
print("generate_vehicle_position_webpage:Generating landing html page:",mainHtmlfilename,end="...") | |
#Generate the label description using all the pushpin labels for the day. The first entry will be the header field | |
pushpinLabels = "<li>Label - Description <font color=" + legend_vehicle_displayed_on_map_color + ">[blue label descriptions show currently displayed vehicles]</font></li>" | |
for index in range(1, lastpushpinLabel + 1): | |
pushpinLabel_desc = dict_pushpinLabel_to_desc_map[str(index)]['desc'] | |
#the isDisplayedOnMap attribute was added later to DynamoDB so there might be items that don't have it. This ensures that a call to get this | |
#attribute will not break the program | |
try: | |
isDisplayedOnMap = dict_pushpinLabel_to_desc_map[str(index)]['isDisplayedOnMap'] | |
except: | |
isDisplayedOnMap = False | |
#for all vehicles currently displayed on map, show their label description in a different color. This makes it easy to differentiate between a vehicle | |
#that is currently displayed and one that was previously displayed today however it is now not mapped. | |
if isDisplayedOnMap: | |
pushpinLabels += "<font color=" + legend_vehicle_displayed_on_map_color + "><li>" + str(index) + " - " + str(pushpinLabel_desc) + "</li></font>" | |
else: | |
pushpinLabels += "<li>" + str(index) + " - " + str(pushpinLabel_desc) + "</li>" | |
htmlObject = s3.Object(s3Bucket,mainHtmlfilename) | |
htmlContent= """<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style> | |
body { | |
font-family: Arial; | |
color: white; | |
} | |
.split { | |
height: 100%; | |
width: 50%; | |
position: fixed; | |
z-index: 1; | |
top: 0; | |
overflow-x: hidden; | |
padding-top: 20px; | |
} | |
.left { | |
left: 0; | |
width: 70%; | |
background-color: #111; | |
} | |
.right { | |
right: 0; | |
width: 30%; | |
background-color: black; | |
} | |
.centered { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
text-align: center; | |
} | |
</style> | |
<script> | |
<!-- | |
function timedRefresh(timeoutPeriod) { | |
setTimeout("location.reload(true);",timeoutPeriod); | |
} | |
window.onload = timedRefresh(60000); | |
// --> | |
</script> | |
</head>""" | |
htmlContent +=""" | |
<body> | |
<div class="split left"> | |
<div class="centered"> | |
<img src={} alt="Vehicle Location Map" width="1250" height="1000"> | |
<p> image source {}</p> | |
</div> | |
</div> | |
<div class="split right"> | |
<p>Map last updated at {} [updated every 5 min]. | |
<br>Total vehicles found {} (at most, only the first 100 will be mapped) | |
<br> | |
<br>Icon Description | |
<br>Blue = active vehicles (location reported within last {} minutes) | |
<br>Red = inactive vehicles (location last reported at least {} minutes ago) | |
</p> | |
<ul style="list-style-type:square;">""".format(s3ImageKey+imagefilename, imagefilename, imagefilename[:-4], total_vehicles_found, inactive_vehicle_datetime_limit, inactive_vehicle_datetime_limit) | |
htmlContent += pushpinLabels | |
htmlContent +=""" | |
</ul> | |
</div> | |
</body> | |
</html>""" | |
upload_html_result = htmlObject.put(Body=htmlContent,ACL='public-read',ContentType='text/html') | |
print("done") | |
print("generate_vehicle_position_webpage:Uploaded map:",imagefilename," UploadResult:",upload_img_result) | |
print("generate_vehicle_position_webpage:Uploaded htmlfile:",mainHtmlfilename," UploadResult:",upload_html_result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment