import 'dart:convert'; import 'package:flutter/cupertino.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'], description: json['description'], collectionName: json['collectionName'], ); } Map<String, dynamic> toJson() => { 'id': id, 'description': description, 'collectionName' : collectionName }; @override String toString(){ return collectionName; } } class Device{ int id; String urn; Device(this.id, this.urn); factory Device.fromJson(Map<String, dynamic> json){ return Device( json['id'] as int, json['urn'] as String); } Map<String, dynamic> toJson() => { 'id': id, 'urn': urn }; @override String toString(){ return urn; } } class Event{ int id; //This shall be the mysql primary key database id int urnId; String urn; String label; String type; // Event type name TODO: this should be an EventType variable int typeId; String description; String status; String startDate; String endDate; String latitude; String longitude; String elevation; Event({ required this.id, required this.urnId, required this.urn, required this.label, required this.type, required this.typeId, required this.description, required this.status, required this.startDate, required this.endDate, required this.latitude, required this.longitude, required this.elevation, }); factory Event.fromJson(Map<String, dynamic> json){ return Event( id: json['id'], urnId: json['urnId'], urn: json['urn'], label: json['label'], type: json['type'], typeId: json['typeId'], description: json['description'], status: json['status'], startDate: json['startDate'], endDate: json['endDate'], latitude: json['latitude'], longitude: json['longitude'], elevation: json['elevation'], ); } Map<String, dynamic> toMap() { return { 'id' : id, 'urnId': urnId, 'urn': urn, 'label': label, 'type' : type, 'typeId' : typeId, 'description' : description, 'status' : status, 'startDate' : startDate, 'endDate' : endDate, 'latitude' : latitude, 'longitude' : longitude, 'elevation' : elevation }; } Map<String, dynamic> toMapNoId() { return { 'urnId': urnId, 'urn': urn, 'label': label, 'type' : type, 'typeId' : typeId, 'description' : description, 'status' : status, 'startDate' : startDate, 'endDate' : endDate, 'latitude' : latitude, 'longitude' : longitude, 'elevation' : elevation }; } Map<String, dynamic> toJson() => { 'id' : id, 'urnId': urnId, 'urn': urn, 'label': label, 'type' : type, 'typeId' : typeId, 'description' : description, 'status' : status, 'startDate' : startDate, 'endDate' : endDate, 'latitude' : latitude, 'longitude' : longitude, 'elevation' : elevation }; Map<String, dynamic> toSensorJson() { return { "\"itemID\"":"\"$urnId\"", "\"inheritToAllChildren\"":"\"false\"", "\"inheritToChildren\"":[], "\"event\"":{ "\"startDate\"":"\"$startDate\"", "\"endDate\"":"\"$endDate\"", "\"label\"":"\"$label\"", "\"description\"":"\"$description\"", "\"eventType\"":"\"$typeId\"", "\"latitude\"":"\"$latitude\"", "\"longitude\"":"\"$longitude\"", "\"elevationInMeter\"":"\"$elevation\"", "\"id\"":"\"0\"" } }; } @override String toString(){ return '{ ' '${this.id}, ' '${this.urnId}, ' '${this.urn}, ' '${this.label}, ' '${this.type}, ' '${this.description}, ' '${this.startDate}, ' '${this.endDate}, ' '${this.latitude},' '${this.longitude},' '${this.elevation},' '${this.status} }'; } } class EventType{ int id; String name; EventType({ required this.id, required this.name }); EventType.fromSensorJson(Map<String, dynamic> json) : id = json['id'], name = json['generalName']; EventType.fromJson(Map<String, dynamic> json) : id = json['id'], name = json['name']; Map<String, dynamic> toJson() => { 'id': id, 'name': name, }; @override String toString(){ return name; } } class SensorLogin{ String mail; String password; SensorLogin( this.mail, this.password, ); Map<String, dynamic> toJson() => { "mail": mail, "password": password }; SensorLogin.fromJson(Map<String, dynamic> json) : mail = json['mail'], password = json['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('', ''); bool initialized = false; 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('admin', 'adminadmin'); initialized = false; } } 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( id: 0, urnId:-1, urn:'urn0', label:'', type:'', typeId:-1, description:'', status:'PENDING', startDate:'', endDate: '', latitude: '', longitude: '', elevation: '' ); /* int getPendingEventCount(List<Event> events){ int count = 0; for (var event in events) { if (event.status == 'PENDING') { count++; } } return count; } */ //String getEventDump(List<Event> events){ // String ev = '['; // for (var event in events) { // ev += jsonEncode(event); // ev += ','; // } // ev += ']'; // return ev; //} //fromEventDump(dump){ // events = []; // for (var entry in dump) { // debugPrint('Added Event from Storage: ' + entry.toString()); // events.add(Event.fromJson(entry)); // } //} } class EventStoreInstance extends EventStoreBase { static final EventStoreInstance _instance = EventStoreInstance._internal(); factory EventStoreInstance() { return _instance; } EventStoreInstance._internal() { //events = []; currentEvent = Event( id:0, urnId:-1, urn:'urn0', label:'', type:'', typeId:-1, description:'', status:'PENDING', startDate:'', endDate: '', latitude: '', longitude: '', elevation: '' );; } void reset(){ //events = []; currentEvent = Event( id:0, urnId:-1, urn:'urn0', label:'', type:'', typeId:-1, description:'', status:'PENDING', startDate:'', endDate: '', latitude: '', longitude: '', elevation: '' ); } }