captureSensie method

Future<Map<String, dynamic>> captureSensie(
  1. CaptureSensieInput captureSensieInput
)

Implementation

Future<Map<String, dynamic>> captureSensie(
    CaptureSensieInput captureSensieInput) async {
  canCaptureSensie = await checkCanCaptureSensie();
  if (!canCaptureSensie) {
    return {'message': "Can't capture sensie anymore"};
  }

  Map<String, StreamSubscription> sensorsSubscriptions =
      startSensors(callback: captureSensieInput.onSensorData);

  Completer<Map<String, dynamic>> completer = Completer();

  Timer(const Duration(milliseconds: 3000), () async {
    stopSensors(
      sensorsSubscriptions['gyroSubscription']!,
      sensorsSubscriptions['accelSubscription']!,
    );
    roundSensorData();

    final dynamic whipCounterRes =
        await PluginSensie.whipCounter({"yaw": sensorData.gyroZ});
    int whipCount = whipCounterRes['whipCount'];
    List<dynamic> avgFlatCrest =
        whipCounterRes['avgFlatCrest'].map((item) => item as double).toList();

    currentSensie = {
      'whipCount': whipCount,
      'signal': avgFlatCrest,
      'sensorData': sensorData.toJson(),
      'flow': captureSensieInput.flow ? 1 : -1,
    };

    canCaptureSensie = await checkCanCaptureSensie();

    if (whipCount == 3) await addSensie(sensie: currentSensie);
    if (whipCount != 0) {
      final resJson = await storeSensieRequest(
          whipCount: whipCount, flow: captureSensieInput.flow);
      final sensieId = resJson['data']['sensie']['id'];

      Map<String, dynamic> retSensie = {
        'id': sensieId,
        'whips': whipCount,
        'valid': whipCount == 3,
      };
      resetSensorData();
      completer.complete(retSensie);
    } else {
      resetSensorData();
      completer.complete({
        'id': 'Invalid Sensie with 0 whip count',
        'whips': whipCount,
        'valid': false,
      });
    }
  });

  return completer.future;
}