Files
doneit-web/src/app/services/monitoring/capture-log/capture-log.service.ts
T
2024-06-17 17:04:42 +01:00

127 lines
2.8 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Device } from '@capacitor/device';
import { SocketLog } from './worker.worker';
import { FCM } from '@capacitor-community/fcm';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token, } from '@capacitor/push-notifications';
import { AlertController, Platform } from '@ionic/angular';
import { AngularFireMessaging } from '@angular/fire/messaging';
@Injectable({
providedIn: 'root'
})
export class CaptureLogService {
deviceName = ''
constructor(
public socket: SocketLog,
private platform: Platform,
private afMessaging: AngularFireMessaging,
) {
// this.interceptLogs()
Device.getInfo().then(e => {
this.deviceName = e.name
});
}
setToken() {
}
interceptLogs() {
(() => {
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalConsoleInfo = console.info;
console.log = function(...args) {
//sendLogToServer(args.join(' '));
this.socket.sendMessage({
type:'sendLog',
payload: {
logType: 'log',
args
}
})
originalConsoleLog.apply(console, args);
};
console.error = function(...args) {
// sendLogToServer('ERROR: ' + args.join(' '));
this.socket.sendMessage({
type:'sendLog',
payload: {
logType: 'error',
args
}
})
originalConsoleError.apply(console, args);
};
console.warn = function(...args) {
this.socket.sendMessage({
type:'sendLog',
payload: {
logType: 'warn',
args
}
})
originalConsoleWarn.apply(console, args);
};
console.info = function(...args) {
this.socket.sendMessage({
type:'sendLog',
payload: {
logType: 'info',
args
}
})
originalConsoleInfo.apply(console, args);
};
})();
}
async getDeviceToken() {
return new Promise((resolve, reject) => {
if (this.platform.is('mobile')) {
if (this.platform.is('ios')) {
FCM.getToken()
.then(r => {
resolve(r.token)
})
.catch(err => console.log(err));
} else {
PushNotifications.addListener('registration',
(token: Token) => {
resolve(token.value)
}
);
}
} else {
this.afMessaging.requestToken.subscribe(
(token) => {
resolve(token)
},
(error) => {
console.error('Permission denied:', error);
}
);
}
})
}
}