Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
27176c26
Commit
27176c26
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
Update: move code from main
parent
b5fecb19
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
server/main.py
+2
-30
2 additions, 30 deletions
server/main.py
server/map/__pycache__/get_markers.cpython-311.pyc
+0
-0
0 additions, 0 deletions
server/map/__pycache__/get_markers.cpython-311.pyc
server/map/get_markers.py
+29
-28
29 additions, 28 deletions
server/map/get_markers.py
with
31 additions
and
58 deletions
server/main.py
+
2
−
30
View file @
27176c26
...
...
@@ -3,7 +3,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler
from
pymongo
import
MongoClient
from
pymongo.server_api
import
ServerApi
from
consts
import
DB_NAME
,
COLLECTION
,
MONGO_URI
,
MONGO_CERT_PATH
,
SSL_CERT_PATH
,
SSL_KEY_PATH
,
HOST
,
PORT
from
map.get_markers
import
get_markers
from
map.get_markers
import
get_
all_
markers
from
data_structs
import
Measurement
,
MarkerTemplate
,
DateAndTime
,
Sensor
import
ssl
import
keyboard
...
...
@@ -60,35 +60,7 @@ class IceHTTP(BaseHTTPRequestHandler):
# Update_map endpoint
elif
self
.
path
==
'
/update_map
'
:
# NB: should be POST?
try
:
# Fetch marker data
marker_cursor
=
col
.
find
({})
marker_data
=
list
(
marker_cursor
)
# Remove ObjectId field from each document
for
document
in
marker_data
:
document
.
pop
(
'
_id
'
,
None
)
if
marker_data
:
# Data found in DB, convert from BSON to JSON
resp_code
=
200
marker_json_list
=
[
json_util
.
dumps
(
document
)
for
document
in
marker_data
]
marker_json
=
"
[
"
+
"
,
"
.
join
(
marker_json_list
)
+
"
]
"
else
:
# Data not found in DB
resp_code
=
404
marker_json
=
'
[]
'
except
Exception
as
e
:
print
(
f
"
An error occurred while querying MongoDB:
{
e
}
"
)
resp_code
=
500
marker_data
=
[]
# Set headers
self
.
send_response
(
resp_code
)
self
.
send_header
(
"
Content-type
"
,
"
application/json
"
)
self
.
end_headers
()
# Convert marker_data to JSON and write it to the response object
marker_json
=
json
.
dumps
(
marker_data
)
self
.
wfile
.
write
(
marker_json
.
encode
(
'
utf-8
'
))
get_all_markers
(
self
,
col
)
# Listen for pressing of q key to terminate server
def
on_key_press
(
server
,
event
):
...
...
This diff is collapsed.
Click to expand it.
server/map/__pycache__/get_markers.cpython-311.pyc
+
0
−
0
View file @
27176c26
No preview for this file type
This diff is collapsed.
Click to expand it.
server/map/get_markers.py
+
29
−
28
View file @
27176c26
...
...
@@ -3,36 +3,37 @@ import sys
current_dir
=
os
.
path
.
dirname
(
__file__
)
parent_dir
=
os
.
path
.
abspath
(
os
.
path
.
join
(
current_dir
,
'
..
'
))
sys
.
path
.
append
(
parent_dir
)
from
data_structs
import
Measurement
,
Sensor
,
MarkerTemplate
,
DateAndTime
from
flask
import
json
from
bson
import
json_util
# get_markers parses a list of MarkerTemplate objects to json, and returns either a successfully
# parsed json object with status code 200, or and error message and status code 501
def
get_markers
():
# get_markers requests all marker data from mongoDB
def
get_all_markers
(
self
,
col
):
try
:
# NB: temporary test data
sensor1
=
Sensor
(
ID
=
1
,
type
=
"
Type1
"
,
active
=
True
)
sensor2
=
Sensor
(
ID
=
2
,
type
=
"
Type2
"
,
active
=
False
)
# Fetch marker data from DB
marker_cursor
=
col
.
find
({})
marker_data
=
list
(
marker_cursor
)
# Remove ObjectId field documents to allow for easy JSON to BSON conversion
for
document
in
marker_data
:
document
.
pop
(
'
_id
'
,
None
)
datetime1
=
DateAndTime
(
2023
,
12
,
31
,
15
,
43
)
datetime2
=
DateAndTime
(
2024
,
1
,
15
,
12
,
2
)
datetime3
=
DateAndTime
(
2024
,
1
,
31
,
18
,
10
)
measurement1
=
Measurement
(
longitude
=
10.9771
,
latitude
=
60.7066
,
datetime
=
datetime1
,
sensor
=
sensor1
,
precipitation
=
0.0
,
thickness
=
0.0
,
max_weight
=
0.0
,
safety_level
=
0.0
,
accuracy
=
2.5
)
measurement2
=
Measurement
(
longitude
=
10.8171
,
latitude
=
60.6366
,
datetime
=
datetime2
,
sensor
=
sensor2
,
precipitation
=
0.0
,
thickness
=
0.0
,
max_weight
=
0.0
,
safety_level
=
0.0
,
accuracy
=
1.5
)
measurement3
=
Measurement
(
longitude
=
10.8471
,
latitude
=
60.7366
,
datetime
=
datetime3
,
sensor
=
sensor1
,
precipitation
=
0.0
,
thickness
=
0.0
,
max_weight
=
0.0
,
safety_level
=
0.0
,
accuracy
=
4.0
)
testData
=
[
MarkerTemplate
(
measurement1
,
30.0
-
measurement1
.
accuracy
,
"
Green
"
),
MarkerTemplate
(
measurement2
,
10.0
-
measurement2
.
accuracy
,
"
Red
"
),
MarkerTemplate
(
measurement3
,
20.0
-
measurement3
.
accuracy
,
"
Yellow
"
),
]
# NB: return test data as JSON
return
json
.
dumps
([
marker
.
to_dict
()
for
marker
in
testData
]),
200
if
marker_data
:
# Data found in DB, convert from BSON to JSON
resp_code
=
200
marker_json_list
=
[
json_util
.
dumps
(
document
)
for
document
in
marker_data
]
marker_json
=
"
[
"
+
"
,
"
.
join
(
marker_json_list
)
+
"
]
"
else
:
# Data not found in DB
resp_code
=
404
marker_json
=
'
[]
'
except
Exception
as
e
:
return
e
,
500
\ No newline at end of file
print
(
f
"
An error occurred while querying MongoDB:
{
e
}
"
)
resp_code
=
500
marker_data
=
[]
# Set headers
self
.
send_response
(
resp_code
)
self
.
send_header
(
"
Content-type
"
,
"
application/json
"
)
self
.
end_headers
()
# Convert marker_data to JSON and write it to the response object
marker_json
=
json
.
dumps
(
marker_data
)
self
.
wfile
.
write
(
marker_json
.
encode
(
'
utf-8
'
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment