|
@@ -1,83 +1,54 @@
|
|
|
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 { Fingerprint, FingerprintPayload, FingerprintPayloadUI, FisMessage, JavaResponse, PersonFingerprintData } 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<JavaResponse>
|
|
|
+ private registeredFingerprintData: Fingerprint[] = []
|
|
|
|
|
|
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: JavaResponse = JSON.parse(data.toString())
|
|
|
+ console.log(`Java server response: ${message.message}`)
|
|
|
+ if (message.operation == `Registration` && message.status == `Success`) {
|
|
|
+ this.registeredFingerprintData.push((message.data as PersonFingerprintData).fingerprints[0] as Fingerprint)
|
|
|
+ console.log(message.data)
|
|
|
+ // console.log(this.registeredFingerprintData)
|
|
|
+ console.log(`${this.registeredFingerprintData.length} registered print.`)
|
|
|
+ }
|
|
|
+ // 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: FisMessage): void {
|
|
|
+ console.log(`Sending fingerprint data to Java verification....`)
|
|
|
+ let payload: FingerprintPayloadUI = message.data as FingerprintPayloadUI
|
|
|
+ let command: FingerprintPayload = {
|
|
|
+ ...payload,
|
|
|
+ fpTemplateArray: this.registeredFingerprintData
|
|
|
+ }
|
|
|
+ this.javaClient.write(JSON.stringify(command) + `\n`)
|
|
|
+ // let data = JSON.parse(message)
|
|
|
+ // console.log(data)
|
|
|
+ // this.javaClient.write(JSON.stringify(extractionMessage) + `\n`)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/* Reason based on ChatGPT: Why ClientProxy.send() or emit() won't work
|
|
@@ -86,4 +57,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} */
|