Skip to content
Snippets Groups Projects
Commit 16f47677 authored by Hoa Ben The Nguyen's avatar Hoa Ben The Nguyen
Browse files

add: method for adding new measurement and test for it

parent 5c80820d
No related branches found
No related tags found
No related merge requests found
Showing
with 133 additions and 21 deletions
No preview for this file type
...@@ -187,12 +187,14 @@ This endpoint is only for production, and is not implemented in the application. ...@@ -187,12 +187,14 @@ This endpoint is only for production, and is not implemented in the application.
### New lidar data ### New lidar data
``` ```
Method: POST Method: GET
Path: new_lidar_data?lake=* Path: new_lidar_data?lake=*
Paramters: Paramters:
- lake (required) - lake (required)
``` ```
The endpoint will update the current data from ```*_lidar_data.json```,
with the content from folder ```./server/lidar_data/*```. Should be used after the
```./server/lidar_data/*``` has been updated.
## Database ## Database
This project requires SQLite3. Download the precompiled binary for your operating system. This project requires SQLite3. Download the precompiled binary for your operating system.
......
No preview for this file type
File added
File added
File added
No preview for this file type
No preview for this file type
File added
No preview for this file type
No preview for this file type
No preview for this file type
import json
import os
from datetime import datetime
from server.consts import LIDAR_DATA_PATH, LAKE_RELATIONS_PATH
def add_new_lidar_measurement_handler(self, lake_name: str, lat: float, lon: float):
status_code, updated_content = add_new_lidar_measurement(lake_name, lat, lon)
self.send_response(status_code)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write content data to response object
self.wfile.write(json.dumps(updated_content).encode('utf-8'))
def add_new_lidar_measurement(lake_name: str, lat: float, lon: float):
"""
Adds a new lidar measurement in another location of a lake
:param self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
:param lake_name (str): The name of the requested file/lake
:param lat (float): The latitude of the measurement
:param lon (float): The longitude of the measurement
:return (int, str): A tuple containing the status code and all lidar measurements in one body of water
"""
try:
# find out if lake exist in our database
# sjekk hvordan sara sjekker dette - se i sqlDB eller mapper eller ...
lake_data_path = os.path.join(LIDAR_DATA_PATH + lake_name)
if not os.path.isdir(lake_data_path):
return 404, f"lake does not exist: {lake_name}"
# read json data with path to data for specific water body
file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json')
# time last updated data
time_now = datetime.now().utcnow().replace(microsecond=0)
content = []
# check if water body data exist, else create
if os.path.exists(file_path):
# get the areas from the map's data
with open(file_path) as data:
measurement_data = json.load(data)
# create an empty container for new measurement
new_measurement_data = {
'MeasurementID': len(measurement_data) - 1,
'TimeMeasured': str(time_now),
'CenterLat': lat,
'CenterLon': lon,
'Sensor': {
'SensorId': 2,
'SensorType': "LiDar",
"Active": True,
},
'Subdivisions': [],
}
measurement_data.append(new_measurement_data)
# convert data to json format
content = json.dumps(measurement_data, indent=4)
else:
# create an empty container for new measurement
new_measurement_data = {
'MeasurementID': 0,
'TimeMeasured': str(time_now),
'CenterLat': lat,
'CenterLon': lon,
'Sensor': {
'SensorId': 2,
'SensorType': "LiDar",
"Active": True,
},
'Subdivisions': [],
}
content = json.dumps(new_measurement_data, indent=4)
# write to file
with open(file_path, "w") as file:
file.write(content)
# send response
return 200, content
# error handling
except Exception as e:
return 500, f"An error occurred: {e} g".encode("utf-8")
print(add_new_lidar_measurement("mjøsa", 60.9, 10.9))
\ No newline at end of file
...@@ -23,6 +23,8 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): ...@@ -23,6 +23,8 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str):
:param cursor (cursor): An Sqlite3 cursor object that points to the database :param cursor (cursor): An Sqlite3 cursor object that points to the database
:param sensorId: Id of the sensor used to measure the data :param sensorId: Id of the sensor used to measure the data
:param lake_name (str): The name of the requested file/lake :param lake_name (str): The name of the requested file/lake
:return: (int, str): A tuple containing the status code and the meassurement data of the lidar files and where it
was taken on the map
""" """
try: try:
# print("name=",lake_name) # print("name=",lake_name)
...@@ -32,14 +34,14 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): ...@@ -32,14 +34,14 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str):
# read json data with path to data for specific water body # read json data with path to data for specific water body
file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json') file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json')
# map of water body area exist # check if water body data exist
if not os.path.exists(file_path): if not os.path.exists(file_path):
# Just temporary, not gonna use: return 404, f"no data for {lake_name} found"
# with open(file_path, "w") as file:
# file.write( # Just temporary, not gonna use:
# "[{\"MeasurementID\": 1,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []},{\"MeasurementID\": 2,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []}]") # Writing an empty JSON object # with open(file_path, "w") as file:
print("File not found") # file.write(
return 404, f"no {lake_name} exist on this map" # "[{\"MeasurementID\": 1,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []},{\"MeasurementID\": 2,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []}]") # Writing an empty JSON object
# get the areas from the map's data # get the areas from the map's data
with open(file_path) as data: with open(file_path) as data:
...@@ -83,7 +85,6 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): ...@@ -83,7 +85,6 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str):
# Calculate each area's ice thickness based on the area coordinates given to the calculation model # Calculate each area's ice thickness based on the area coordinates given to the calculation model
areas_data = calculate_area_data((latitude, longitude), lake_name, laz_file_path) areas_data = calculate_area_data((latitude, longitude), lake_name, laz_file_path)
print("somehow works")
if (areas_data): if (areas_data):
# store lidar data in json format # store lidar data in json format
# calculate data for each zone within the area # calculate data for each zone within the area
...@@ -121,28 +122,27 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): ...@@ -121,28 +122,27 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str):
else: else:
print('No data found') print('No data found')
print("went past")
measurement_data = { measurement_data = {
'MeasurementID': measurement_id, 'MeasurementID': measurement_id,
'TimeMeasured': str(time_now), 'TimeMeasured': str(time_now),
'CenterLat': latitude, 'CenterLat': latitude,
'CenterLon': longitude, 'CenterLon': longitude,
'Sensor': { 'Sensor': {
'SensorId': 2, 'SensorId': sensorId,
'SensorType': "LiDar", 'SensorType': "LiDar",
"Active": True, "Active": True,
}, },
'Subdivisions': subdiv_json_data, 'Subdivisions': subdiv_json_data,
} }
print("went past to append")
lidar_json_data.append(measurement_data) lidar_json_data.append(measurement_data)
print("went past to append")
# send the changes to the database # send the changes to the database
cursor.connection.commit() cursor.connection.commit()
print("jovid") # container for content, might not be needed
content = None
# check if there are ice in area # check if there are ice in area
if len(lidar_json_data) > 0: if len(lidar_json_data) > 0:
if os.path.exists(file_path): if os.path.exists(file_path):
...@@ -154,16 +154,13 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): ...@@ -154,16 +154,13 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str):
# write to file # write to file
with open(file_path, "w") as file: with open(file_path, "w") as file:
file.write(content) file.write(content)
print("jovid2")
# Send response
return 200, content
else: else:
# return empty list, since there were no ice in area scanned # return empty list, since there were no ice in area scanned
content = json.dumps([]) content = json.dumps([])
print('No ice found in area') print('No ice found in current area')
return 200, content
# send response
return 200, content
# error handling # error handling
except Exception as e: except Exception as e:
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
from server.data_processing.add_new_lidar_measurement import add_new_lidar_measurement
def test_add_new_lidar_measurement_invalid_lake() -> None:
lake_name = 'test_lake'
lat = 0.0
lon = 0.0
actual, _ = add_new_lidar_measurement(lake_name, lat, lon)
expected = 404
assert actual == expected
def test_add_new_lidar_measurement_valid_lake() -> None:
lake_name = 'mjøsa'
lat = 0.0
lon = 0.0
actual, _ = add_new_lidar_measurement(lake_name, lat, lon)
expected = 200
assert actual == expected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment