From cd04860349e9883311d7db44661f8ccc3b172366 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Thu, 8 Feb 2024 14:15:03 +0100 Subject: [PATCH] fix: update_map endpoint --- app/lib/main.dart | 80 ++++++++++++------ server/__pycache__/consts.cpython-311.pyc | Bin 620 -> 595 bytes .../__pycache__/data_structs.cpython-311.pyc | Bin 916 -> 1589 bytes server/data_structs.py | 14 ++- server/main.py | 15 ++-- .../__pycache__/get_markers.cpython-311.pyc | Bin 0 -> 1444 bytes server/map/get_markers.py | 6 ++ 7 files changed, 77 insertions(+), 38 deletions(-) create mode 100644 server/map/__pycache__/get_markers.cpython-311.pyc diff --git a/app/lib/main.dart b/app/lib/main.dart index bff14402..06166efb 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -1,8 +1,11 @@ +import 'dart:js_interop'; + import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; // Import LatLng class from the latlong package import 'dart:async'; import 'dart:io'; +import 'dart:convert'; const String port = "8443"; @@ -27,23 +30,6 @@ class App extends StatelessWidget { } } -// fetchData requests data from the update_map endpoint -Future<void> fetchData() async { - try { - // Custom HTTP client - HttpClient client = HttpClient() - ..badCertificateCallback = // NB: temporary disable SSL certificate validation - (X509Certificate cert, String host, int port) => true; - - var request = await client.getUrl(Uri.parse(serverURI+mapEndpoint)); - var response = await request.close(); - - print('Response status: ${response.statusCode}'); - } catch (e) { - print('Failed to connect to the server: $e'); - } -} - class DefaultPage extends StatefulWidget { const DefaultPage({Key? key}) : super(key: key); @@ -59,26 +45,68 @@ class MarkerData { MarkerData({required this.location, required this.size, required this.color}); } +// parseMarkerData parses jsonData into an object of type MakerData +List<MarkerData> parseMarkerData(String jsonString) { + final parsed = json.decode(jsonString); + return List<MarkerData>.from(parsed.map((data) => MarkerData( + location: LatLng(data['latitude'], data['longitude']), + size: data['size'].toDouble(), + color: parseColor(data['color']), + ))); +} + +Color parseColor(String colorString) { + switch (colorString) { + case 'blue': + return Colors.blue; + case 'red': + return Colors.red; + case 'green': + return Colors.green; + default: + return Colors.black; // Default color if unrecognized + } +} + class _DefaultPageState extends State<DefaultPage> { late Timer _timer; - final List<MarkerData> markerList = [ - MarkerData(location: LatLng(60.7266, 10.9771), size: 50.0, color: Colors.blue), - MarkerData(location: LatLng(60.8366, 10.8171), size: 70.0, color: Colors.red), - MarkerData(location: LatLng(60.7366, 10.8471), size: 60.0, color: Colors.green), - ]; + List<MarkerData> markerList = []; + + // fetchMarkerData requests data from the update_map endpoint + Future<void> fetchMarkerData() async { + try { + // Custom HTTP client + HttpClient client = HttpClient() + ..badCertificateCallback = // NB: temporary disable SSL certificate validation + (X509Certificate cert, String host, int port) => true; + + var request = await client.getUrl(Uri.parse(serverURI+mapEndpoint)); + var response = await request.close(); + + // Parse json response to list of MarkerData objects if request is ok + if (response.statusCode == 200) { + var responseBody = await response.transform(utf8.decoder).join(); + markerList = parseMarkerData(responseBody); + } else { + print('Request failed with status: ${response.statusCode}'); + } + } catch (e) { + print('Failed to connect to the server: $e'); + } + } // Timer initializer @override void initState() { super.initState(); - // Call fetchData when the widget is first created - fetchData(); + // Call fetchMarkerData when the widget is first created + fetchMarkerData(); - // Schedule fetchData to run periodically based on fetchInterval const + // Schedule fetchMarkerData to run periodically based on fetchInterval const const Duration fiveMinutes = Duration(minutes: fetchInterval); _timer = Timer.periodic(fiveMinutes, (timer) { - fetchData(); + fetchMarkerData(); }); } diff --git a/server/__pycache__/consts.cpython-311.pyc b/server/__pycache__/consts.cpython-311.pyc index 3de30302111c2cd3d3561aa908cd47ab136cdbed..9ff52d124944e7e6b5fd6348a75357356a3572d9 100644 GIT binary patch delta 52 zcmaFEa+!sDIWI340}!ZeJ+hH|Eu*x)vsFxJacWU<OkPP|X^cy1adt_5K}<nWe){A$ Gj12&;OA<u@ delta 77 zcmcc2@`i<bIWI340}yx=9p1>jmeIr`*(xTqIJKxaCa)x~G{!$K)ukx2ELA~Q!7s!w fG{z;hIJ+djASNKl-`&X4z#yh5wIF};WyS^ozVaF6 diff --git a/server/__pycache__/data_structs.cpython-311.pyc b/server/__pycache__/data_structs.cpython-311.pyc index 637f0e2f98054a92e7adcb5e251233a6d0ad1dbd..8dea98bcfe20ac3a6dcfb4290cc498c0b28974ca 100644 GIT binary patch delta 828 zcmZuv&1(};5TCanrU}U!YZEt3KSDty9z0YODOv;t@n9~kmk?OXzE@eBX2aX<B?yVq zLqRWP1HI&|22X<i2VT8+TK43h5UD3GI<uQBYMo_&?>F;4elzplF!L#Iye$-RfcE9< zXYoQiHcZwyzH+^$zysi!L*R376oB{mC)QLr+k$%b!;`Cwje}S0ZuQ2!jn-pNNUzoL zI|r@%!rSxRZmTQZr@=4wu5bZ&jzP}jAdA6AWn(v{dGzh%&;;4kIbB3@0s@+h1L++? zQ{@Vx##PkTBID7H+!JzJ?04HczOeN_j-gCd{7*N7Rd%P(VGE>z{#cW=Olk;`<~cnP z>9*T;WnGu&X(FAKRdSaQJc`xGWRYT2N$=9%;@2$8={UY+QP$oX!T>_EcBH*7gq5{E zjz+j(;*bqXi#X(t@Qa&a&kDvpKuU4iw~(B`b9hG4*pR0yk*8OIoOZ;<*+|3IC=E$z z#878gS08ht%}D`K#&<35*nY44PZ87jDlYXdBMji1xirEGmPad?{_tT~stlWxtX|Ab zAc1UBMFabHku(<wN(9s<xk6Az{n^bEGiOji;;kbL(r=<@dUXOR<j(1+brzC`L?_kN z$mqIG$G80HOOW)a5|uKs`DH@7yPtEn_g#L_7Mrq$F7?EF8LX?@H_bdlowM+rVn6;f H5hw5)Dn6xJ delta 206 zcmdnWGliXRIWI340}!}AJCJ&oX(FElqsc^dW408=U<OV0ja6EVjGB{|GCG*}C0oUW z7N-^!$K;jdmB#qzrMeVlmZd7_D)@!?g~qs~7H5~_7sLbv`MVog8W_YBr55B*Vp_o~ z05p3gLy;hmD&n8KmC4El%;W*`HJNVl7362;mBg2%<`(26mZTPego{K$1VjUn^^3zM hH$SB`C)KV<9LQw^;^L^ut64+%7&sUeKY$3ZVgLwYG_?Q# diff --git a/server/data_structs.py b/server/data_structs.py index b9b83589..6e289e54 100644 --- a/server/data_structs.py +++ b/server/data_structs.py @@ -21,9 +21,17 @@ class DataPoint: self.accuracy = accuracy +# Template for map marker data class MarkerTemplate: - def __init__(self, longitude, latitude, size, color): - self.longitude = longitude + def __init__(self, latitude, longitude, size, color): self.latitude = latitude + self.longitude = longitude self.size = size - self.color = color \ No newline at end of file + self.color = color + def to_dict(self): + return { + 'latitude': self.latitude, + 'longitude': self.longitude, + 'size': self.size, + 'color': self.color + } \ No newline at end of file diff --git a/server/main.py b/server/main.py index 06f19b32..97253949 100644 --- a/server/main.py +++ b/server/main.py @@ -3,8 +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.extract_coordinates import extract_coordinates -from data_structs import DataPoint +from map.get_markers import get_markers import atexit import ssl @@ -32,16 +31,14 @@ class IceHTTP(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(b"Root path hit!") elif self.path == '/update_map': - # Get response from process_data_point - response, status_code = extract_coordinates() - - # Send the response - self.send_response(status_code) + # Send the response with a status code (e.g., 200 for success) + self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() - # Write response data to the response - self.wfile.write(response.get_data()) + # Write the JSON data directly to the response + markers_data = get_markers() + self.wfile.write(markers_data.encode('utf-8')) elif self.path == '/test_endpoint': self.send_response(200) diff --git a/server/map/__pycache__/get_markers.cpython-311.pyc b/server/map/__pycache__/get_markers.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6b2fcc46f8cf164508bc3de6dd6af98c1ca63601 GIT binary patch literal 1444 zcmaJ=U1%dk9G}hZCex%zs-6i_$`w&U5JK_6kCN*}EXVm^LDN_Xd$;WEOt)$Gi<!;A z)w?4~K`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$lpx+U@`!cT`<`LrEYxmK%pBS P2ND!s6Ar-Rbp+yHL*iL3 literal 0 HcmV?d00001 diff --git a/server/map/get_markers.py b/server/map/get_markers.py index dc632c00..f59ca2b4 100644 --- a/server/map/get_markers.py +++ b/server/map/get_markers.py @@ -1,4 +1,10 @@ +import os +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 MarkerTemplate +from flask import json def get_markers(): testData = [ -- GitLab