Skip to content
Snippets Groups Projects
Commit b6ad7b70 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

fix: error handling

parent cf82bed4
No related branches found
No related tags found
1 merge request!13Clhp map
......@@ -20,13 +20,18 @@ class Measurement {
});
factory Measurement.fromJson(Map<String, dynamic> json) {
if (json == null) {
throw const FormatException('Error parsing Measurement: JSON data is null');
}
try {
return Measurement(
measurementID: json['MeasurementID'] ?? 0,
timeMeasured: json['TimeMeasured'] != null
? DateTime.parse(json['TimeMeasured'])
: DateTime(0),
sensor: Sensor.fromJson(json['Sensor']),
sensor:json['Sensor'] != null
? Sensor.fromJson(json['Sensor'])
: Sensor.empty(),
bodyOfWater: json['BodyOfWater'] != null ? json['BodyOfWater'].toString() : 'nil',
center: LatLng(
json['CenterLat'] != null ? json['CenterLat'].toDouble() : 0.0,
......@@ -67,8 +72,8 @@ class SubDiv {
return SubDiv(
sub_div_id: json['SubdivID'].toString(),
groupID: json['GroupID'] ?? 0,
minThickness: json['MinThickness'] ?? 0.0,
avgThickness: json['AvgThickness'] ?? 0.0,
minThickness: json['MinThickness'].toDouble() ?? 0.0,
avgThickness: json['AvgThickness'].toDouble() ?? 0.0,
center: json['CenLatitude'] != null && json['CenLongitude'] != null
? LatLng(json['CenLatitude'], json['CenLongitude'])
: LatLng(0.0, 0.0),
......@@ -148,16 +153,13 @@ class Sensor {
required this.active,
});
Sensor.empty()
: sensorID = 0,
sensorType = '',
active = false;
factory Sensor.fromJson(Map<String, dynamic> json) {
try {
if (json == null) {
return Sensor(
sensorID: -1,
sensorType: 'Weather data only',
active: false
);
}
return Sensor(
sensorID: json['SensorID'] ?? 0,
sensorType: json['SensorType'] ?? 'nil',
......
This diff is collapsed.
......@@ -73,6 +73,19 @@ class IceHTTP(BaseHTTPRequestHandler):
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
elif self.path.startswith('/get_measurements'):
parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query)
lake_name_param = query_params.get('lake', [''])[0]
lake_name = unquote(lake_name_param) # Decode url param
if lake_name_param:
get_measurements(self, cursor, lake_name)
else:
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
elif self.path.startswith('/get_relation'):
parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query)
......
No preview for this file type
......@@ -72,10 +72,10 @@ def get_measurements(self, cursor, lake_name):
}
# Populate remaining subdivisions and create "invalid" measurement to store them
remaining_sub_divs = fillRemainingSubdivisions(lake_name, sub_div_ids)
remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids)
measurement_data[-1] = {
'MeasurementID': -1,
'TimeMeasured': datetime.now(),
'TimeMeasured': str(datetime.now()),
'CenterLat': None,
'CenterLon': None,
'Sensor': None,
......@@ -114,7 +114,7 @@ def get_measurements(self, cursor, lake_name):
# Get data for subdivisions that have not been measured by sensors, and thus are not in the database
def fillRemainingSubdivisions(lake_name: str, sub_div_ids: list):
def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list):
try:
with open(LAKE_RELATIONS_PATH + lake_name + '_div.json', 'r') as file:
data = json.load(file)
......@@ -133,8 +133,8 @@ def fillRemainingSubdivisions(lake_name: str, sub_div_ids: list):
sub_division = {
'SubdivID': sub_div_id,
'GroupID': None,
'MinThickness': 0, # NB placeholders
'AvgThickness': 0,
'MinThickness': 0.0, # NB placeholders
'AvgThickness': 0.0,
'CenLatitude': center_lat,
'CenLongitude': center_lng,
'Accuracy': None,
......
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