Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
Fixed inconsistencies for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
pmanko authored Jan 6, 2023
1 parent dd69e89 commit 8769069
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/mllp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class MllpServer extends TypedEmitter<MllpEvents> {
host: string
logger: any
server: net.Server
message: string;

static readonly VT = String.fromCharCode(0x0b);
static readonly FS = String.fromCharCode(0x1c);
Expand All @@ -45,33 +46,23 @@ export class MllpServer extends TypedEmitter<MllpEvents> {

this.host = host || '127.0.0.1'
this.port = port || 6969
this.logger = logger || console;

this.logger = logger || console
this.message = ''
this.server = net.createServer(this.serverListener.bind(this))

}

private serverListener(sock: net.Socket) {
this.logger.info('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);

sock.on('data', (data: any) => {
let { msg, ack } = this.handleData(data.toString())
data = data.toString()
logger("DATA:\nfrom " + sock.remoteAddress + ':\n' + data.split("\r").join("\n"));

/**
* MLLP HL7 Event. Fired when a HL7 Message is received.
* @event MllpServer#hl7
* @type {string}
* @property {string} message string containing the HL7 Message (see example below)
* @example MSH|^~\&|XXXX|C|SOMELAB|SOMELAB|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
*/
this.emit('hl7', msg);

sock.write(MllpServer.VT + this.ackn(ack, "AA") + MllpServer.FS + MllpServer.CR);
let { msg, ack } = this.handleData(data.toString())
})
sock.on('close', (data) => {
this.logger.info('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
});

}

private ackn(data: any, ack_type: string): string {
Expand Down Expand Up @@ -100,23 +91,30 @@ export class MllpServer extends TypedEmitter<MllpEvents> {
}

private handleData(msg: string) {
let message: string = ''
let ackData: any

//strip separators
if (msg.indexOf(MllpServer.VT) > -1) {
message = '';
this.message = '';
}

message += msg.replace(MllpServer.VT, '');
this.message += msg.replace(MllpServer.VT, '');

if (msg.indexOf(MllpServer.FS + MllpServer.CR) > -1) {
message = message.replace(MllpServer.FS + MllpServer.CR, '');
ackData = hl7.parseString(message);
this.logger.info("Message:\r\n" + message + "\r\n\r\n");
}
this.message = this.message.replace(MllpServer.FS + MllpServer.CR, '');
let ackData: any = hl7.parseString(this.message);
this.logger.info("Message:\r\n" + this.message + "\r\n\r\n");

return { msg: message, ack: ackData };
/**
* MLLP HL7 Event. Fired when a full HL7 Message is received.
* @event MllpServer#hl7
* @type {string}
* @property {string} message string containing the HL7 Message (see example below)
* @example MSH|^~\&|XXXX|C|SOMELAB|SOMELAB|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
*/
this.emit('hl7', this.message);
let ack = ackn(ackData, "AA")

sock.write(MllpServer.VT + ack + MllpServer.FS + MllpServer.CR);
}
}

send(receivingHost: string, receivingPort: number, hl7Data: any, callback: Function) {
Expand Down

0 comments on commit 8769069

Please sign in to comment.