import { Injectable } from '@angular/core' ; import { DeviceService } from './device.service' ; import { Geolocation } from '@ionic-native/geolocation/ngx' ; import { Vibration } from '@ionic-native/vibration/ngx' ; import { NoticeService } from './notice.service' ; import { Platform } from '@ionic/angular' ; @ Injectable ( { providedIn : 'root' } ) export class NativeService { constructor ( private geolocation , private vibration , private noticeService , private platform ) { } init ( ) { } vibrate ( time = 100 ) { if ( this . platform . is ( 'cordova' ) ) this . vibration . vibrate ( time ) ; } ahrsStop ( ) { window . removeEventListener ( "deviceorientation" , this . updateAhrs ) ; } lastSendTime = 0 ; ahrsStart ( device ) { if ( device . config . mode == "mqtt" ) return ; window . addEventListener ( "deviceorientation" , this . updateAhrs . bind ( this , device ) ) ; } updateAhrs = ( e ) => { if ( new Date ( ) . getTime ( ) > this . lastSendTime ) { this . lastSendTime = new Date ( ) . getTime ( ) + 200 ; let ahrsData = `{"ahrs":[${ e . alpha . toFixed ( 0 ) },${ e . beta . toFixed ( 0 ) },${ e . gamma . toFixed ( 0 ) }]}\n` ; } } ; getLocation ( device ) { return this . geolocation . getCurrentPosition ( { enableHighAccuracy : true } ) . then ( resp => { let latitude = resp . coords . latitude . toString ( ) ; let longitude = resp . coords . longitude . toString ( ) ; let gps = `{"gps":[${ longitude },${ latitude }]}\n` ; } ) . catch ( ( error ) => { this . noticeService . showAlert ( 'openLocation' ) ; console . log ( 'Error getting location2' , error ) ; } ) ; } allStop ( device ) { if ( typeof ( this . ahrsStop ) === 'function' ) { this . ahrsStop ( ) ; device . ahrs = 'stop' ; } this . vibrate ( 0 ) ; } }