|
@@ -1,83 +1,49 @@
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
import { ClientProxy } from '@nestjs/microservices';
|
|
|
-import { interval } from 'rxjs';
|
|
|
+import { interval, Observable, Subject } from 'rxjs';
|
|
|
import * as net from 'net'
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
+import { connectToAfisJava } from 'libs/java/afis.utils';
|
|
|
+import { FpVerificationPayload, FpVerificationResponse } from 'libs';
|
|
|
+import { v1 as uuid } from 'uuid'
|
|
|
|
|
|
@Injectable()
|
|
|
export class FisVerificationService {
|
|
|
+ private port: number
|
|
|
+ private host: string
|
|
|
+ private javaClient: net.Socket
|
|
|
+ private incomingMessageFromJava!: Subject<FpVerificationResponse>
|
|
|
+ private fpTemplate: any[] = []
|
|
|
|
|
|
constructor(
|
|
|
@Inject(`SAMPLEAPP_SERVICE`) private sampleClient: ClientProxy,
|
|
|
- @Inject(`JAVA_AFIS`) private javaClient: ClientProxy, // this one cannot work unfortunately
|
|
|
private configService: ConfigService
|
|
|
) {
|
|
|
- // logic here
|
|
|
- setTimeout(() => {
|
|
|
- // logic here
|
|
|
- }, 5000)
|
|
|
+ this.port = this.configService.get<number>('afis.tcpPort') as number
|
|
|
+ this.host = this.configService.get<string>('afis.host') as string
|
|
|
|
|
|
- interval(3000).subscribe(interval => {
|
|
|
- // this.sampleClient.emit(`message`, `Verification says HI`)
|
|
|
- // this.javaClient.emit(`message`, `Testing Message from Verification`)
|
|
|
- // this.javaClient.send(`message`, `Testing Message from Verification`)
|
|
|
+ this.javaClient = connectToAfisJava(this.host, this.port)
|
|
|
+ this.javaClient.on(`data`, (data) => {
|
|
|
+ let message: FpVerificationResponse = JSON.parse(data.toString())
|
|
|
+ console.log(message)
|
|
|
+ // this.incomingMessageFromJava.next(message)
|
|
|
})
|
|
|
-
|
|
|
- this.tryThis()
|
|
|
}
|
|
|
|
|
|
- tryThis() {
|
|
|
- const port = this.configService.get<number>('afis.tcpPort')!;
|
|
|
- const host = this.configService.get<string>('afis.host')!;
|
|
|
-
|
|
|
- const client = new net.Socket();
|
|
|
-
|
|
|
- const connectToServer = () => {
|
|
|
- client.connect(port, host, () => {
|
|
|
- console.log(`✅ Connected to Afis Java Server at ${host}:${port}`);
|
|
|
- });
|
|
|
- };
|
|
|
-
|
|
|
- // Attempt to connect initially
|
|
|
- connectToServer();
|
|
|
-
|
|
|
- // Reconnect logic: try again after delay
|
|
|
- client.on('error', (err) => {
|
|
|
- console.error(`❌ TCP Error:`, err.message);
|
|
|
- // Avoid crash on ECONNREFUSED etc.
|
|
|
- setTimeout(connectToServer, 3000);
|
|
|
- });
|
|
|
-
|
|
|
- client.on('close', (hadError) => {
|
|
|
- console.warn(`⚠️ Connection closed${hadError ? ' due to error' : ''}. Reconnecting...`);
|
|
|
- setTimeout(connectToServer, 3000);
|
|
|
- });
|
|
|
-
|
|
|
- client.on('end', () => {
|
|
|
- console.warn('⚠️ Server closed the connection.');
|
|
|
- });
|
|
|
-
|
|
|
- // Optional: handle incoming data
|
|
|
- client.on('data', (data) => {
|
|
|
- console.log('📩 Received from server:', data.toString());
|
|
|
- });
|
|
|
-
|
|
|
- // Message sending interval
|
|
|
- interval(3000).subscribe(() => {
|
|
|
- if (client.destroyed) {
|
|
|
- console.warn('⚠️ Cannot send: socket is destroyed.');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const message = JSON.stringify({
|
|
|
- pattern: 'get-user',
|
|
|
- data: { id: 1 },
|
|
|
- });
|
|
|
-
|
|
|
- client.write(message + '\n');
|
|
|
- });
|
|
|
+ public handleMessage(message: any): void {
|
|
|
+ let data = JSON.parse(message)
|
|
|
+ // console.log(data)
|
|
|
+ let extractionMessage: FpVerificationPayload = {
|
|
|
+ id: uuid(),
|
|
|
+ cmd: `Registration`,
|
|
|
+ date: new Date(),
|
|
|
+ data: data.messageData,
|
|
|
+ fpTemplateArray: [],
|
|
|
+ message: `LOLz`
|
|
|
+ }
|
|
|
+ this.javaClient.write(JSON.stringify(extractionMessage) + `\n`)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/* Reason based on ChatGPT: Why ClientProxy.send() or emit() won't work
|
|
@@ -86,4 +52,9 @@ A message envelope that wraps your pattern and data.
|
|
|
Message IDs and event headers.
|
|
|
A specific serialization/deserialization logic that is not plain JSON.
|
|
|
NO newline character at the end.
|
|
|
-So when your Java server, which expects a newline-terminated JSON message, receives this custom binary format, it gets confused or sees null.*/
|
|
|
+So when your Java server, which expects a newline-terminated JSON message, receives this custom binary format, it gets confused or sees null.*/
|
|
|
+
|
|
|
+/* data: {
|
|
|
+messageData: string
|
|
|
+messageTemplateData: string
|
|
|
+deviceInfo: object} */
|