I am working on an Ionic project that uses the accelerometer at 100hz. I need to save the accelerometer readings to a file, and I am using ionic native file plugin. My problem - the writing time to the file is longer than the accelerometer sample time and the data in the file is not written in the needed order (newer data is written before the previous one had the chance to be written.
This is the provider that handles the accelerometer reading and the writing to local file :`import {DeviceMotion, DeviceMotionAccelerationData} from
'@ionic-native/device-motion';
import {Subject} from "rxjs/Subject";
import {Observable} from "rxjs/Observable";
import {Subscription} from "rxjs/Subscription";
import {Http} from '@angular/http';
import {File} from '@ionic-native/file';
@Injectable()
export class DeviceMotionProvider {
accelerometerata$: Subject<DeviceMotionAccelerationData> = new Subject();
subscription: Subscription;
constructor(private deviceMotion: DeviceMotion, private http: Http,
private file: File) {
}
startAccelerometer() {
// Watch device acceleration
this.subscription = this.deviceMotion.watchAcceleration({frequency: 100})
.subscribe((acceleration: DeviceMotionAccelerationData) => {
this.accelerometerata$.next(acceleration);
this.file.writeFile(this.file.dataDirectory, 'flightData.txt', acceleration.timestamp + ',' +Math.sqrt(acceleration.x*acceleration.x+acceleration.y*acceleration.y+acceleration.z*acceleration.z)/10 + '\n' ,{ append:true})
});
}
getAccelerometerData(): Observable<DeviceMotionAccelerationData> {
return this.accelerometerata$.asObservable();
}
stopAccelerometer() {
this.subscription.unsubscribe();
}
}`
I couldn't find the proper way to overcome this issue. Any help will be greatly appreciated.