From d9b87bbce6b36cd5c8928cf33858dd52943326f1 Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Tue, 1 Mar 2022 12:06:02 +0100
Subject: [PATCH] selection of devices in configuration works now

---
 lib/configuration.dart | 48 ++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/lib/configuration.dart b/lib/configuration.dart
index c5da786..a0cf374 100644
--- a/lib/configuration.dart
+++ b/lib/configuration.dart
@@ -4,17 +4,40 @@ import 'package:http/http.dart' as http;
 import 'dart:convert';
 import 'datamodel.dart';
 
+Future<List<Device>> updateDevices(int collectionId) async {
+  debugPrint("Start HTTP GET Collection Devices. Collection Id: " + collectionId.toString());
+  String url = 'https://sensor.awi.de/rest/sensors/collections/getItemsOfCollection/' + collectionId.toString();
 
-Future<Collection> fetchCollection() async {
-  debugPrint("Start HTTP GET++##");
+  // Get Access to local device and current event store.
+  DeviceStoreInstance availableDevices = DeviceStoreInstance();
+  EventCurrentInstance currentEvent = EventCurrentInstance();
 
   final response = await http
-      .get(Uri.parse('https://sensor.awi.de/rest/sensors/collections/getCollection/22'));
+      .get(Uri.parse(url));
+
+  debugPrint("Got something via Sensor Rest...");
+
+  List<Device> collectionDevices = [];
 
   if (response.statusCode == 200) {
-    // If the server did return a 200 OK response,
-    // then parse the JSON.
-    return Collection.fromJson(jsonDecode(response.body));
+    // If the server did return a 200 OK response, then parse the JSON.
+    List<dynamic> data = json.decode(response.body);
+    for (var entry in data) {
+      collectionDevices.add(Device.fromJson(entry));
+    }
+
+    debugPrint("Got the following devices: ");
+    for (var device in collectionDevices){
+      debugPrint(device.toString());
+    }
+
+    /*Update to local event store*/
+    availableDevices.store = collectionDevices;
+    //Update id and urn in add event widget
+    currentEvent.store.id = collectionDevices[0].id;
+    currentEvent.store.urn = collectionDevices[0].urn;
+
+    return collectionDevices;
   } else {
     // If the server did not return a 200 OK response,
     // then throw an exception.
@@ -58,7 +81,7 @@ class _MyHomePageState extends State<Configuration> {
   @override
   void initState() {
     super.initState();
-    futureCollection = fetchCollection();
+    //futureCollection = fetchCollection();
     futureCollections = fetchCollections();
   }
   late Future<String> eventTypes;
@@ -126,16 +149,19 @@ class _MyHomePageState extends State<Configuration> {
           FloatingActionButton.extended(
             heroTag: null,
             tooltip: 'Request Collections from sensor.awi.de',
-            icon: Icon(Icons.download),
+            icon: const Icon(Icons.download),
             label: const Text("Get Collections"),
-            onPressed: null,
+            onPressed: () {
+            },
           ),
           FloatingActionButton.extended(
             heroTag: null,
             tooltip: 'Select Collection and download corresponding devices',
-            icon: Icon(Icons.save),
+            icon: const Icon(Icons.save),
             label: const Text('Select'),
-            onPressed: null,
+            onPressed: () {
+              updateDevices(activeCollection.store.id);
+            },
           ),
         ],
       ),
-- 
GitLab