mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 13:02:56 +00:00
add open telemetry
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { CaptureLogService } from './capture-log.service';
|
||||
|
||||
describe('CaptureLogService', () => {
|
||||
let service: CaptureLogService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(CaptureLogService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
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
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// src/app/worker.worker.ts
|
||||
// addEventListener('message', ({ data }) => {
|
||||
// const response = `Worker response to ${data}`;
|
||||
// postMessage(response);
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, Subject, BehaviorSubject } from 'rxjs';
|
||||
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
|
||||
import { catchError, retryWhen, tap, delay } from 'rxjs/operators';
|
||||
|
||||
interface WebSocketMessage {
|
||||
type: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
interface WebSocketError {
|
||||
type: string;
|
||||
error: any;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SocketLog {
|
||||
private socket$: WebSocketSubject<WebSocketMessage>;
|
||||
private messageSubject$: Subject<WebSocketMessage>;
|
||||
private connectionStatus$: BehaviorSubject<boolean>;
|
||||
private reconnectAttempts = 0;
|
||||
private readonly maxReconnectAttempts = 5;
|
||||
|
||||
constructor() {
|
||||
this.messageSubject$ = new Subject<WebSocketMessage>();
|
||||
this.connectionStatus$ = new BehaviorSubject<boolean>(false);
|
||||
this.setupVisibilityChangeHandler();
|
||||
}
|
||||
|
||||
public connect(url: string) {
|
||||
this.socket$ = webSocket<WebSocketMessage>(url);
|
||||
|
||||
this.socket$.pipe(
|
||||
tap({
|
||||
error: () => {
|
||||
this.connectionStatus$.next(false);
|
||||
}
|
||||
}),
|
||||
retryWhen(errors => errors.pipe(
|
||||
tap(() => {
|
||||
this.reconnectAttempts++;
|
||||
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
||||
throw new Error('Max reconnect attempts reached');
|
||||
}
|
||||
}),
|
||||
delay(1000)
|
||||
))
|
||||
).subscribe(
|
||||
(message) => {
|
||||
this.messageSubject$.next(message);
|
||||
this.connectionStatus$.next(true);
|
||||
this.reconnectAttempts = 0;
|
||||
},
|
||||
(err) => {
|
||||
console.error('WebSocket connection error:', err);
|
||||
},
|
||||
() => {
|
||||
console.log('WebSocket connection closed');
|
||||
this.connectionStatus$.next(false);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public sendMessage(message: WebSocketMessage): Observable<void> {
|
||||
return new Observable<void>(observer => {
|
||||
this.socket$.next(message);
|
||||
observer.next();
|
||||
observer.complete();
|
||||
}).pipe(
|
||||
catchError(err => {
|
||||
console.error('Send message error:', err);
|
||||
return new Observable<never>(observer => {
|
||||
observer.error({ type: 'SEND_ERROR', error: err });
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public get messages$(): Observable<WebSocketMessage> {
|
||||
return this.messageSubject$.asObservable();
|
||||
}
|
||||
|
||||
public get connectionStatus(): Observable<boolean> {
|
||||
return this.connectionStatus$.asObservable();
|
||||
}
|
||||
|
||||
private setupVisibilityChangeHandler() {
|
||||
if (typeof document !== 'undefined' && typeof document.addEventListener === 'function') {
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
this.reconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private reconnect() {
|
||||
if (this.socket$) {
|
||||
const url = (this.socket$ as any)._config.url;
|
||||
this.establishNewConnection(url);
|
||||
}
|
||||
}
|
||||
|
||||
private establishNewConnection(url: string) {
|
||||
const newSocket$ = webSocket<WebSocketMessage>(url);
|
||||
|
||||
newSocket$.pipe(
|
||||
tap({
|
||||
error: () => {
|
||||
console.error('New WebSocket connection error');
|
||||
}
|
||||
}),
|
||||
retryWhen(errors => errors.pipe(
|
||||
tap(() => {
|
||||
this.reconnectAttempts++;
|
||||
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
||||
throw new Error('Max reconnect attempts reached');
|
||||
}
|
||||
}),
|
||||
delay(1000)
|
||||
))
|
||||
).subscribe(
|
||||
(message) => {
|
||||
this.messageSubject$.next(message);
|
||||
this.connectionStatus$.next(true);
|
||||
this.reconnectAttempts = 0;
|
||||
|
||||
// Close the old socket and replace it with the new one
|
||||
this.socket$.complete();
|
||||
this.socket$ = newSocket$;
|
||||
},
|
||||
(err) => {
|
||||
console.error('New WebSocket connection error:', err);
|
||||
},
|
||||
() => {
|
||||
console.log('New WebSocket connection closed');
|
||||
this.connectionStatus$.next(false);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user