import { NativeModules, NativeEventEmitter, Platform, Linking, Alert, EmitterSubscription, } from 'react-native'; const { BEACON_PRIORITY_FAST, BEACON_PRIORITY_OPTIMAL, BEACON_PRIORITY_SLOW, } = NativeModules.MapmyindiaIntouch.getConstants(); interface CustomConfigParams { enableAutoStartTracking?: boolean; beaconStartTimeInSec?: number; beaconEndTimeInSec?: number; timeWhileMovingInSec: number; standByTimeInMins: number; enableRequestPermissionIfMissing?: boolean; autoTrackingConfig: AutoTrackingConfig; } interface AutoTrackingConfig { endTimeConfig: WorkTiming; } interface WorkTiming { hour: number; minute: number; second: number; amPm: string; } interface InitializeApisSdkProps { mapSDKKey: string; restAPIKey: string; clientId: string; clientSecret: string; } interface GetDevicesProps { deviceIds?: Array; includeInActive?: boolean; ignoreBeacon?: boolean; lastUpdateTime: number; } interface GetDeviceProps { deviceId: number; lastUpdateTime: number; } interface GetDeviceInfoProps { deviceIds?: Array; } interface GetLocationEventsProps { deviceId: number; startTime: number; endTime: number; skipPeriod: number; } interface GetDriveProps { deviceId: number; startTime: number; endTime: number; } interface CreateGeoFenceProps { geofenceName: string; geofencePoints?: Array<[number, number]>; geofencePoint?: [number, number]; range?: number; } interface GetGeoFencesProps { ignoreGeometry: boolean; geofenceIds?: Array; geofenceId?: number; } interface UpdateGeofenceProps { geofenceId: number; geofenceName: string; geofencePoint?: [number, number]; geofencePoints?: Array<[number, number]>; buffer?: number; } interface DeleteGeoFenceProps { geofenceIds?: Array; geofenceId?: number; } interface GetGeoFenceActivity { deviceIds: Array; startTime: number; endTime: number; geofenceIds?: Array; } interface CreateTripProps { tripName: string; deviceId: number; isForceCloseEnable: boolean; scheduleEndTime?: number; tripDestination?: TripDestination; metaData: Object; tripGeofences?: Array; } interface TripDestination { metaData: Object; scheduledAt: number; radius: number; geometry: Geometry; } interface TripGeofence { radius: number; scheduledAt: number; metaData: Object; geometry: Geometry; } interface Geometry { type: string; coordinates: Array<[number, number]>; //array of lat & lng } interface GetTripsProps { deviceIds?: Array; limit?: number; status?: number; statTimeStamp?: number; endTimeStamp?: number; } interface GetTripInfoProps { tripId: String; isPositionEventsEnable?: boolean; isLocationEnable?: boolean; isPolylineEnable?: boolean; } interface CloseTripProps { tripId: string; } interface CreateAlarmProps { alarmParameters: CreateAlarmParamsProps; } interface CreateAlarmParamsProps { deviceId: Array; type?: number; duration?: number; limit?: number; geofenceId: Array; alarmType?: number; } interface GetAlarmConfigsProps { alarmIds?: Array; deviceIds?: Array; alarmTypes?: Array; } interface UpdateAlaramProps { alarmId: number; alarmParameters: UpdateAlarmParamsProps; } interface UpdateAlarmParamsProps { deviceId?: number; type?: number; duration?: number; limit?: number; geofenceId?: number; alarmType?: number; // alarmId?:number; } interface DeleteAlarmProps { alarmIds?: Array; alarmId?: number; } interface GetAlarmLogsProps { deviceIds?: Array; alarmTypes?: Array; startTime: number; endTime: number; } var MapmyIndiaIntouch = { initialize: async function ( deviceName: string, clientId: string, clientSecret: string, ) { if (deviceName && clientId && clientSecret) { return await NativeModules.MapmyindiaIntouch.initialize( deviceName, clientId, clientSecret, '', ); } else { error('please provide device Name or required keys'); } }, initializeWithDeviceId: async function ( deviceName: string, clientId: string, clientSecret: string, deviceCode: string, ) { if (deviceName && clientId && clientSecret && deviceCode) { return await NativeModules.MapmyindiaIntouch.initialize( deviceName, clientId, clientSecret, deviceCode, ); } else { error('please provide required parameters'); } }, isInitialized: async function () { const status = await NativeModules.MapmyindiaIntouch.isInitialized(); if (Platform.OS === 'ios') { return status > 0 ? true : false; } else { return status; } }, startTracking: async function ( priority = 2, enableRequestPermissionIfMissing = true, ) { NativeModules.MapmyindiaIntouch.startTracking( priority, enableRequestPermissionIfMissing, ); }, startTrackingWithCustomConfig: async function (props: CustomConfigParams) { if (props.standByTimeInMins && props.timeWhileMovingInSec) { NativeModules.MapmyindiaIntouch.startTrackingWithCustomConfig(props); } else { error('Please pass required parameters to start custom tracking'); } }, stopTracking: function () { NativeModules.MapmyindiaIntouch.stopTracking(); }, addTrackingStateListener: (listenerEvent: Function, error: Function) => { NativeModules.MapmyindiaIntouch.addTrackingStateListener(); const eventEmitter = new NativeEventEmitter( NativeModules.MapmyindiaIntouch, ); const eventListener = eventEmitter.addListener( 'TrackingStateListener', (event) => { if ( Platform.OS === 'ios' && event === 'Location services are not enabled' ) { listenerEvent('please enable location permissions and services'); iosSettingsAlert(); } else { listenerEvent(event); } }, ); var errorListener = eventEmitter.addListener( 'TrackingErrorListener', (event) => { error(event); }, ); return [eventListener, errorListener]; }, removeTrackingStateListener: () => { new NativeEventEmitter(NativeModules.MapmyindiaIntouch).removeAllListeners( 'TrackingStateListener', ); new NativeEventEmitter(NativeModules.MapmyindiaIntouch).removeAllListeners( 'TrackingErrorListener', ); NativeModules.MapmyindiaIntouch.removeTrackingStateListener(); }, isRunning: async function () { const status = await NativeModules.MapmyindiaIntouch.isRunning(); if (Platform.OS === 'ios') { return status > 0 ? true : false; } else { return status; } }, getCurrentLocationUpdate: function ( enableRequestPermissionIfMissing: boolean = true, ) { return NativeModules.MapmyindiaIntouch.getCurrentLocationUpdate( enableRequestPermissionIfMissing, ); }, //only for ios initializeApisSdk: function (props: InitializeApisSdkProps) { if (Platform.OS === 'ios') { if ( props.clientId && props.clientSecret && props.mapSDKKey && props.restAPIKey ) { return NativeModules.MapmyindiaIntouchApi.initializeApisSdk(props); } else { error('Please provide IntouchApis keys.'); } } }, getDevices: function (props: GetDevicesProps) { return NativeModules.MapmyindiaIntouchApi.getDevices(props); }, getDevice: function (props: GetDeviceProps) { return NativeModules.MapmyindiaIntouchApi.getDevice(props); }, getDevicesInfo: function (props: GetDeviceInfoProps) { return NativeModules.MapmyindiaIntouchApi.getDevicesInfo(props); }, getLocationsEvents: function (props: GetLocationEventsProps) { return NativeModules.MapmyindiaIntouchApi.getLocationsEvents(props); }, getDrive: function (props: GetDriveProps) { return NativeModules.MapmyindiaIntouchApi.getDrive(props); }, createGeoFence: function (props: CreateGeoFenceProps) { return NativeModules.MapmyindiaIntouchApi.createGeoFence(props); }, getGeoFences: function (props: GetGeoFencesProps) { return NativeModules.MapmyindiaIntouchApi.getGeoFences(props); }, updateGeoFence: function (props: UpdateGeofenceProps) { return NativeModules.MapmyindiaIntouchApi.updateGeoFence(props); }, deleteGeoFence: function (props: DeleteGeoFenceProps) { return NativeModules.MapmyindiaIntouchApi.deleteGeoFence(props); }, getGeoFenceActivity: function (props: GetGeoFenceActivity) { return NativeModules.MapmyindiaIntouchApi.getGeoFenceActivity(props); }, createTrip: function (props: CreateTripProps) { return NativeModules.MapmyindiaIntouchApi.createTrip(props); }, getTrips: function (props: GetTripsProps) { return NativeModules.MapmyindiaIntouchApi.getTrips(props); }, getTripInfo: function (props: GetTripInfoProps) { return NativeModules.MapmyindiaIntouchApi.getTripInfo(props); }, closeTrip: function (props: CloseTripProps) { return NativeModules.MapmyindiaIntouchApi.closeTrip(props); }, createAlarm: function (props: CreateAlarmProps) { return NativeModules.MapmyindiaIntouchApi.createAlarm(props); }, getAlarmConfigs: function (props: GetAlarmConfigsProps) { return NativeModules.MapmyindiaIntouchApi.getAlarmConfigs(props); }, updateAlarm: function (props: UpdateAlaramProps) { return NativeModules.MapmyindiaIntouchApi.updateAlarm(props); }, deleteAlarm: function (props: DeleteAlarmProps) { return NativeModules.MapmyindiaIntouchApi.deleteAlarm(props); }, getAlarmsLogs: function (props: GetAlarmLogsProps) { return NativeModules.MapmyindiaIntouchApi.getAlarmsLogs(props); }, BEACON_PRIORITY_FAST: BEACON_PRIORITY_FAST, BEACON_PRIORITY_SLOW: BEACON_PRIORITY_SLOW, BEACON_PRIORITY_OPTIMAL: BEACON_PRIORITY_OPTIMAL, }; function error(message: string) { console.log(message); Alert.alert(message); } //only for ios platform function iosSettingsAlert() { Alert.alert( 'Permission', 'Please enable location permission to use this feature', [{text: 'Open Settings', onPress: () => Linking.openSettings()}], {cancelable: true}, ); } export default MapmyIndiaIntouch;