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|Ntt%GM(rxz{pE>pNS;gche3702Y2<dw#_q!wqF<QK#g6y>K+ Ie#6)R0Io|CMF0Q* delta 77 zcmcc2@`i<bIWI340|NttN73Pp+-n(4Jd&+qLW@(2ievIh@=9a;^HN=kGRsmGbQSzU h{6b?~Qj4=o@(W@Dg8bc$EDa1|ic$;mCtqf4006%78RY-~ 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 zcmbQjzLke>IWI340|NuY*-J-K8(AjuNih0NR5w;&VsK|jVQ*nb;aJARz_6MLqy&Vc zm{J&n88kUJCTKA-uAJP>=pb$CY!wq)oLW>IlUI^g8sm~$oL!P%5K~Z;pFa6N;|g9u z1_p+e3`If=3=BmAleaKgrGc3|3=9m(AR}Rz5z6>1#lXNYogtMWiZO)&WM2zI6mtqw z3qur33Ue@nCd(~8-^8Nq)S{5o+=86MlGJ2Ys7??Caw#(d1H)&#$?}Z0^(hR&3`Kk( z7qAq8lohcsFfiO=DbB1)y~UcGpOast$y&q@avEH=NE*bIVPIe=28k(vK#>4Q4CIq4 zaa7Co;^Q;(GE3s)L9Q-VVqjosV7MWn(81EfdqYyGzJsNQ?}ms(2TKp<4N<8MmL9GS zt`076U?jsl401NeCa|+Dz@D#R$O4IiaScNjTxAVI7RZAzagYu$JB4u$OE7~blV1_Y z8DK(_v523co`Inlq*(#%fm`e)`SB^4$t6`{Xr6$I%Y!_v3iiVT37I*(HzZ}}$lVZ; znBsXuRB}qx2ToQ2t`7_lf)nBkKTXymVNl?RfCy0#0ScxfIS@;%9z=qp8ytWVATEfI z1X*MRwy1#t0zWXZuu6YmfD>G7tXv-$FbQzj6@d)VWWL2$ke``X5?=yM2}Php1(quk z16d^`3nD>Q|KhO8%}*)KNwq6dU|?VXg-LPKWFzJfGYK9>t`7{D#7B_W7Z8Ca3w8hi DDn6xJ delta 206 zcmdnWGliXRIWI340|Ntt>$3x?XPGAQNidpBR5xZzVGL%_WZzh&#mJ~Rc`2iViC?l+ zOlWaxQE^OONnUA;e_pCfQD#}Hg06yJh+k-oOKNd;Nq#|0K#;$?k)?q_Oi^k<{v@Uq zyaEgi3@aIm1Q{3@iufmQWwLSsGkF*o7&Mt~@fGA}=9R>kq~;dnB$lKWfrN`hL2ALO s_!$@&esS33=BJeAq}mmUGcYhPGB7X{M@?SM8p6lG!KnBFM1V~P00?3<wEzGB 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..6b2fcc46f8cf164508bc3de6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Qr$ZS3x3NiY>9AAT=-L7EelINn(6)Nl|HXNijG)Zn382Bo=3bg9Yr@B0dHN z2Cxgjf$@vOCO1E&G$+-rNQHrc0TfEbiy0UgJ}@&fGTva|YyiU>3|tLhc!N)%C%r$v zD}P4DMLzW_eCiF{H+cDb!u#X9;%5ZS37;P`D`tiEhLVf2HdkbAF7n!5;k9kxy1~HQ t!qLv(#6N-SB7^i52I&h7(l;2?FQB3u3}P2h(FYa|Mn}dE4A@DqCjdj@SuOwo 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