From 8f678ee1ad4b7736055770169e392d2db4334c83 Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Thu, 20 Jan 2022 12:46:01 +0100
Subject: [PATCH] corrected urn<=>id handling.

---
 lib/addevent.dart  |  1 +
 lib/datamodel.dart | 30 ++++++++++++++++++++++++++++--
 lib/main.dart      | 18 ++++++++++++------
 3 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/lib/addevent.dart b/lib/addevent.dart
index 7c88504..64b9bfd 100644
--- a/lib/addevent.dart
+++ b/lib/addevent.dart
@@ -75,6 +75,7 @@ class AddEvent extends StatelessWidget {
                 }).toList(),
                 onChanged: (value) {
                   currentEvent.store.urn = value.toString();
+                  currentEvent.store.id = availableDevice.getDeviceIdFromUrn(value.toString());
                 }
             ),
             TextFormField(
diff --git a/lib/datamodel.dart b/lib/datamodel.dart
index 86496ae..04c07e9 100644
--- a/lib/datamodel.dart
+++ b/lib/datamodel.dart
@@ -64,8 +64,21 @@ class EventType{
 }
 
 
-abstract class DeviceStoreBase{
+abstract class DeviceStoreBase {
   List<Device> store = [];
+
+  int getDeviceIdFromUrn(String urn) {
+    for (var device in store) {
+      if (device.urn == urn) {
+        return device.id;
+      }
+    }
+    throw Exception('Device with urn:' + urn + ' was not found.');
+  }
+
+  void reset(){
+    store = [];
+  }
 }
 
 abstract class EventStoreBase{
@@ -90,6 +103,10 @@ class EventStoreInstance extends EventStoreBase {
   EventStoreInstance._internal() {
     store = [];
   }
+
+  void reset(){
+    store = [];
+  }
 }
 
 class EventCurrentInstance extends EventCurrentBase {
@@ -100,7 +117,11 @@ class EventCurrentInstance extends EventCurrentBase {
   }
 
   EventCurrentInstance._internal(){
-    store = Event(0, 'urn0', '', '', '', 'PENDING');
+    store = Event(-1, 'urn0', '', '', '', 'PENDING');
+  }
+
+  void reset(){
+    store = Event(-1, 'urn0', '', '', '', 'PENDING');
   }
 }
 
@@ -114,6 +135,11 @@ class EventTypeStoreInstance extends EventTypeStoreBase{
   EventTypeStoreInstance._internal(){
     store = [];
   }
+
+
+  void reset(){
+    store = [];
+  }
 }
 
 
diff --git a/lib/main.dart b/lib/main.dart
index 01284fb..df29534 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -12,20 +12,26 @@ void main() {
   EventStoreInstance events = EventStoreInstance();
   EventTypeStoreInstance eventTypes = EventTypeStoreInstance();
   EventCurrentInstance currentEvent = EventCurrentInstance();
-  DeviceStoreInstance availableDevice = DeviceStoreInstance();
+  DeviceStoreInstance availableDevices = DeviceStoreInstance();
+
+  //Reset old data
+  events.reset();
+  eventTypes.reset();
+  currentEvent.reset();
+  availableDevices.reset();
+
 
-  
   // Add some dummy devices
   // TODO: load from shared preferences.
   // TODO: this shall be requested from sensor.awi.de after selecting a collection in configuration.
-  availableDevice.store.add(Device.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1'}));
-  availableDevice.store.add(Device.fromJson({'id': 4086, 'urn':'acoustic_backscatter_sensor:test'}));
-  availableDevice.store.add(Device.fromJson({'id': 1393, 'urn':'vessel:polarstern:hydrosweep_ds3'}));
+  availableDevices.store.add(Device.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1'}));
+  availableDevices.store.add(Device.fromJson({'id': 4086, 'urn':'acoustic_backscatter_sensor:test'}));
+  availableDevices.store.add(Device.fromJson({'id': 1393, 'urn':'vessel:polarstern:hydrosweep_ds3'}));
 
   // Fill the textboxes in addevent with some usefull data.
   // TODO: store this in shared preferences so that last entered data reappears when restarting the app.
   currentEvent.store.urn = 'acoustic_backscatter_sensor:test';
-  currentEvent.store.id = 1234;
+  currentEvent.store.id = 4086;
   currentEvent.store.description = 'blabla';
   currentEvent.store.label = 'PS129_ABC';
   currentEvent.store.type = 'Deployment';
-- 
GitLab