import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class Collection { int id; String description; String collectionName; Collection({ required this.id, required this.description, required this.collectionName, }); factory Collection.fromJson(Map<String, dynamic> json) { return Collection( id: json['id'], collectionName: json['collectionName'], description: json['description'], ); } @override String toString(){ return collectionName; } } class Device{ int id; String urn; Device(this.id, this.urn); factory Device.fromJson(Map<String, dynamic> parsedJson){ return Device( parsedJson['id'] as int, parsedJson['urn'] as String); } @override String toString(){ return '{ ${this.id}, ${this.urn} }'; } } class Event{ int id; String urn; String label; String type; String description; String status; String startDate; String endDate; String latitude; String longitude; String elevation; Event( this.id, this.urn, this.label, this.type, this.description, this.status, this.startDate, this.endDate, [this.latitude = "", this.longitude = "", this.elevation = ""] ); factory Event.fromJson(Map<String, dynamic> parsedJson){ return Event( parsedJson['id'] as int, parsedJson['urn'] as String, parsedJson['label'] as String, parsedJson['type'] as String, parsedJson['description'] as String, parsedJson['status'] as String, parsedJson['startDate'] as String, parsedJson['endDate'] as String, "", "", "", ); } @override String toString(){ return '{ ${this.id}, ' '${this.urn}, ' '${this.label}, ' '${this.type}, ' '${this.description}, ' '${this.latitude},' '${this.longitude},' '${this.elevation},' '${this.status} }'; } } class EventType{ int id; String name; EventType( this.id, this.name ); factory EventType.fromJson(Map<String, dynamic> parsedJson){ return EventType(parsedJson['id'] as int, parsedJson['generalName'] as String); } } class SensorLogin{ String mail; String password; String token; SensorLogin( this.mail, this.password, this.token //REST API access Token ); String toJsonString() { return '{ "username": $mail, "authPassword": $password }'; } Map<String, dynamic> toJson() => { "username": mail, "authPassword": password }; } // Storage classes // ConfigurationStoreBase for Collection, devices, event types, login information // EventStoreBase for created event information. abstract class ConfigurationStoreBase { List<Collection> collections = []; Collection currentCollection = Collection(id: -1, description: '', collectionName: ''); List<Device> devices = []; List<EventType> eventTypes = []; SensorLogin loginInformation = SensorLogin('', '', ''); Collection getCollectionFromName(String name) { for (var collection in collections) { if (collection.collectionName == name) { return collection; } } throw Exception('Event with name :' + name + ' was not found.'); } int getDeviceIdFromUrn(String urn) { for (var device in devices) { if (device.urn == urn) { return device.id; } } throw Exception('Device with urn:' + urn + ' was not found.'); } int getEventIdFromName(String name) { for (var eventType in eventTypes) { if (eventType.name == name) { return eventType.id; } } throw Exception('Event with name :' + name + ' was not found.'); } void reset(){ collections = []; devices = []; eventTypes = []; currentCollection = Collection(id: -1, description: '', collectionName: ''); loginInformation = SensorLogin('', '', ''); } } class ConfigurationStoreInstance extends ConfigurationStoreBase { static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance ._internal(); factory ConfigurationStoreInstance() { return _instance; } ConfigurationStoreInstance._internal() { collections = []; devices = []; eventTypes = []; } } abstract class EventStoreBase{ List<Event> events = []; Event currentEvent = Event(0, 'urn0', '', '', '', 'PENDING', '', ''); } class EventStoreInstance extends EventStoreBase { static final EventStoreInstance _instance = EventStoreInstance._internal(); factory EventStoreInstance() { return _instance; } EventStoreInstance._internal() { events = []; currentEvent = Event(0, 'urn0', '', '', '', 'PENDING', '', ''); } void reset(){ events = []; currentEvent = Event(0, 'urn0', '', '', '', 'PENDING', '', ''); } }