Skip to content
Snippets Groups Projects
datamodel.dart 6 KiB
Newer Older
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} }';
  }
}

Maximilian Betz's avatar
Maximilian Betz committed
  int id;  // Device URN id
  String urn;
  String label;
Maximilian Betz's avatar
Maximilian Betz committed
  String type; // Event type name  TODO: this should be an EventType variable
  int typeId;
  String description;
  String status;
  String startDate;
  String endDate;
Maximilian Betz's avatar
Maximilian Betz committed
  String latitude;
  String longitude;
  String elevation;
Maximilian Betz's avatar
Maximilian Betz committed
      this.id,
      this.urn,
      this.label,
      this.type,
Maximilian Betz's avatar
Maximilian Betz committed
      this.typeId,
Maximilian Betz's avatar
Maximilian Betz committed
      this.description,
      this.status,
      this.startDate,
      this.endDate,
Maximilian Betz's avatar
Maximilian Betz committed
      [this.latitude = "",
      this.longitude = "",
      this.elevation = ""]
      );

  factory Event.fromJson(Map<String, dynamic> parsedJson){
    return Event( parsedJson['id'] as int,
Maximilian Betz's avatar
Maximilian Betz committed
      parsedJson['urn'] as String,
      parsedJson['label'] as String,
      parsedJson['type'] as String,
Maximilian Betz's avatar
Maximilian Betz committed
      parsedJson['typeId'] as int,
Maximilian Betz's avatar
Maximilian Betz committed
      parsedJson['description'] as String,
      parsedJson['status'] as String,
      parsedJson['startDate'] as String,
      parsedJson['endDate'] as String,
Maximilian Betz's avatar
Maximilian Betz committed
      "",
      "",
      "",
    );
Maximilian Betz's avatar
Maximilian Betz committed
  Map<String, dynamic> toSensorJsonOld() => {
    "\"itemID\"":"\"$id\"",
    "\"inheritToAllChildren\"":"\"false\"",
    "\"inheritToChildren\"":[],
    "\"event\"":{
      "\"startDate\"":"\"$startDate\"",
      "\"endDate\"":"\"$endDate\"",
      "\"label\"":"\"$label\"",
      "\"description\"":"\"$description\"",
      "\"eventType\"":"\"$type\"",
      "\"latitude\"":"\"$latitude\"",
      "\"longitude\"":"\"$longitude\"",
      "\"elevationInMeter\"":"\"$elevation\"",
      "\"id\"":"\"0\""
Maximilian Betz's avatar
Maximilian Betz committed
  Map<String, dynamic> toSensorJson() {
    return {
    "\"itemID\"":"\"$id\"",
    "\"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(){
Maximilian Betz's avatar
Maximilian Betz committed
    return '{ ${this.id}, '
        '${this.urn}, '
        '${this.label}, '
        '${this.type}, '
        '${this.description}, '
        '${this.startDate}, '
        '${this.endDate}, '
Maximilian Betz's avatar
Maximilian Betz committed
        '${this.latitude},'
        '${this.longitude},'
        '${this.elevation},'
        '${this.status} }';

  factory EventType.fromJson(Map<String, dynamic> parsedJson){
    return EventType(parsedJson['id'] as int, parsedJson['generalName'] as String);
class SensorLogin{
  String mail;
  String password;

  SensorLogin(
      this.mail,
      this.password,
      );

  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 = [];
Maximilian Betz's avatar
Maximilian Betz committed
  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: '');
Maximilian Betz's avatar
Maximilian Betz committed
    loginInformation = SensorLogin('admin', 'adminadmin');
class ConfigurationStoreInstance extends ConfigurationStoreBase {
  static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance
      ._internal();
  factory ConfigurationStoreInstance() {
  ConfigurationStoreInstance._internal() {
    collections = [];
    devices = [];
    eventTypes = [];
abstract class EventStoreBase{
  List<Event> events = [];
Maximilian Betz's avatar
Maximilian Betz committed
  Event currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', '');
class EventStoreInstance extends EventStoreBase {
  static final EventStoreInstance _instance = EventStoreInstance._internal();
  factory EventStoreInstance() {
  EventStoreInstance._internal() {
    events = [];
Maximilian Betz's avatar
Maximilian Betz committed
    currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', '');

  void reset(){
    events = [];
Maximilian Betz's avatar
Maximilian Betz committed
    currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', '');