Untitled

mail@pastecode.io avatar
unknown
dart
a year ago
2.4 kB
1
Indexable
Never
import 'package:user_log/utils/objectkey_value_extractor.dart';

import '../services/aws.dart';

class AnalogSampleSpot {
  final String? timestamp;
  final String? value;

  AnalogSampleSpot({required this.timestamp, required this.value});
}

class AnalogChannel {
  final String? type;
  final String? name;
  final String? unit;
  final List<AnalogSampleSpot> values;

  AnalogChannel(
      {required this.type,
      required this.name,
      required this.unit,
      List<AnalogSampleSpot>? values})
      : values = values ?? [];
}

class AnalogLog {
  final String? machineSerial;
  final List<AnalogChannel>? channels;

  AnalogLog({required this.machineSerial, required this.channels});

  factory AnalogLog.fromCsv(List<dynamic> csv, String serial) {
    List<AnalogChannel> createdChannels = [];
    for (int i = 1; i < csv.length; i++) {
      int index = 0;
      String name = "";
      String unit = "";
      String type = "";
      String timestamp = "";
      String value = "";
      for (var val in csv[i][0].split(";")) {
        switch (index) {
          case 0:
            timestamp = val;
            break;
          case 1:
            type = val;
            break;
          case 2:
            name = val;
            break;
          case 3:
            value = val;
            break;
          case 4:
            unit = val;
            break;
        }

        if (createdChannels.any((element) => element.name == name)) {
          var existingChannel = createdChannels.where((element) => element.name == name).first;
          existingChannel.values.add(AnalogSampleSpot(timestamp: timestamp, value: value));
        } else {
          AnalogChannel newChannel = AnalogChannel(type: type, name: name, unit: unit)..values.add(AnalogSampleSpot(timestamp: timestamp, value: value));
          createdChannels.add(newChannel);
        }
      }
    }
    return AnalogLog(machineSerial: serial, channels: createdChannels);
  }

  static Future<AnalogLog> fromKey(String key) async {
    List<dynamic> result = await _getAnalogFromKey(key);
    return AnalogLog.fromCsv(
        result, ObjectKeyValueExtractor.instance.getMachineName(key));
  }

  static Future<List<dynamic>> _getAnalogFromKey(String key) async {
    var result = await AWSManager.instance.getFileAsDataFromKeyAsCsv(key);
    return result;
  }
}