captureSensie method

Future captureSensie(
  1. CaptureEvaluateSensieInput captureSensieInput
)

Implementation

Future<dynamic> captureSensie(
    CaptureEvaluateSensieInput captureSensieInput) async {
  if (captureSensieInput.userId != userId) {
    return {'message': 'User id is not valid.'};
  }

  final Map<String, StreamSubscription<dynamic>> sensors =
      startSensors(callback: captureSensieInput.onSensorData);
  final StreamSubscription<dynamic> subGyro = sensors['gyroSubscription']!;
  final StreamSubscription<dynamic> subAcc = sensors['accelSubscription']!;

  final completer = Completer();

  Timer(const Duration(milliseconds: 3000), () async {
    stopSensors(gyroSubscription: subGyro, accelSubscription: subAcc);
    roundSensorData();

    final dynamic whipData =
        await PluginSensie.whipCounter({"yaw": sensorData.gyroZ});
    final int whipCount = whipData['whipCount'];
    List<dynamic> avgFlatCrest =
        whipData['avgFlatCrest'].map((item) => item as double).toList();
    if (whipCount == 3) {
      final dynamic sensiesData = await getDataFromAsyncStorage(SENSIES);
      final dynamic sensie = {
        'whipCount': whipCount,
        'signal': avgFlatCrest,
        'sensorData': sensorData.toJson(),
      };
      final dynamic evaluation =
          await PluginSensie.evaluateSensie(sensie, sensiesData);
      final int flowing = evaluation['flowing'];

      final retSensie = Sensie(
        sensieInfo: SensieInfo(
          whips: whipCount,
          flowing: flowing,
          signal: avgFlatCrest,
          sensorData: sensorData,
        ),
        sessionInfo: SessionInfo(
          sessionId: sessionId,
          accessToken: accessToken,
        ),
      );

      final dynamic calibrationStrength =
          await PluginSensie.signalStrength(sensiesData);
      if (onEnds != null) {
        onEnds!({'calibration_strength': calibrationStrength});
      }

      resetSensorData();

      completer.complete(retSensie);
    } else {
      resetSensorData();

      completer.complete({
        'id': 'Invalid sensie',
        'whips': whipCount,
        'flowing': null,
      });
    }
  });

  return completer.future;
}