fromString static method

DBCSignal fromString(
  1. String data,
  2. int lenghtOfMessage
)

When a DBC file is initially parsed each signals are constructed on a line-by-line basis

Implementation

static DBCSignal fromString(String data, int lenghtOfMessage) {
  DBCSignalSignedness signalSignedness;
  DBCSignalType signalType;
  DBCSignalMode signalMode;
  int multiplexGroup;
  int lenght;
  int start;
  List<int> mapping;
  List<int> mappingIndexes;
  double factor;
  double offset;
  double min;
  double max;
  String unit;

  signalSignedness = signednessRegex.firstMatch(data)![0]!.contains('-')
      ? DBCSignalSignedness.SIGNED
      : DBCSignalSignedness.UNSIGNED;
  signalType = signednessRegex.firstMatch(data)![0]!.contains('0')
      ? DBCSignalType.MOTOROLA
      : DBCSignalType.INTEL;

  if (multiplexGroupRegex.hasMatch(data)) {
    signalMode = DBCSignalMode.MULTIPLEX_GROUP;
    multiplexGroup =
        int.parse(multiplexGroupRegex.firstMatch(data)![0]!.substring(2, 3));
  } else {
    multiplexGroup = -1;
    signalMode = multiplexorRegex.hasMatch(data)
        ? DBCSignalMode.MULTIPLEXOR
        : DBCSignalMode.SIGNAL;
  }

  String startMatch = startbitRegex.firstMatch(data)![0]!.substring(2);
  start = int.parse(startMatch.substring(0, startMatch.length - 1));
  String lenghtMatch = lenghtRegex.firstMatch(data)![0]!.substring(1);
  lenght = int.parse(lenghtMatch.substring(0, lenghtMatch.length - 1));

  mapping = BitField.getMapping(lenght, start, signalType);
  mappingIndexes = mapping
      .asMap()
      .keys
      .toList()
      .where((element) => mapping[element] != 0)
      .toList();

  String factorMatch = factorRegex.firstMatch(data)![0]!.substring(1);
  factor = double.parse(factorMatch.substring(0, factorMatch.length - 1));
  String offsetMatch = offsetRegex.firstMatch(data)![0]!.substring(1);
  offset = double.parse(offsetMatch.substring(0, offsetMatch.length - 1));

  String minMatch = minRegex.firstMatch(data)![0]!.substring(1);
  min = double.parse(minMatch.substring(0, minMatch.length - 1));
  String maxMatch = maxRegex.firstMatch(data)![0]!.substring(1);
  max = double.parse(maxMatch.substring(0, maxMatch.length - 1));

  String unitMatch = unitRegex.firstMatch(data)![0]!.substring(3);
  unit = unitMatch.substring(0, unitMatch.length - 2);

  return DBCSignal(
      signalSignedness: signalSignedness,
      signalType: signalType,
      signalMode: signalMode,
      multiplexGroup: multiplexGroup,
      start: start,
      lenght: lenght,
      mapping: mapping,
      mappingIndexes: mappingIndexes,
      factor: factor,
      offset: offset,
      min: min,
      max: max,
      unit: unit);
}