-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (113 loc) · 4.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var net = require('net');
var hl7 = require('hl7');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
//The header is a vertical tab character <VT> its hex value is 0x0b.
//The trailer is a field separator character <FS> (hex 0x1c) immediately followed by a carriage return <CR> (hex 0x0d)
var VT = String.fromCharCode(0x0b);
var FS = String.fromCharCode(0x1c);
var CR = String.fromCharCode(0x0d);
/**
* @constructor MLLPServer
* @param {string} host a resolvable hostname or IP Address
* @param {integer} port a valid free port for the server to listen on.
* @param {object} logger
*
* @fires MLLPServer#hl7
*
* @example
* var server = new MLLPServer('hl7server.mydomain', 3333, console.log);
*
* server.on('hl7', function(message) {
* console.log("Message: " + message);
* // INSERT Unmarshalling or Processing here
* });
*
* @example
* <caption>An ACK is sent back to the server</caption>
* MSH|^~\&|SOMELAB|SOMELAB|SOMELAB|SOMELAB|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
* MSA|AA|Q335939501T337311002
*
*/
function MLLPServer(host, port, logger) {
var self = this;
this.message = '';
var HOST = host || '127.0.0.1';
var PORT = port || 6969;
logger = logger || console.log;
var Server = net.createServer(function (sock) {
logger('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
function ackn(data, ack_type) {
//get message ID
var msg_id = data[0][10];
var header = [data[0]];
//switch around sender/receiver names
header[0][3] = data[0][5];
header[0][4] = data[0][6];
header[0][5] = data[0][3];
header[0][6] = data[0][4];
var result = hl7.serializeJSON(header);
result = result + "\r" + "MSA|" + ack_type + "|" + msg_id;
return result;
}
sock.on('data', function (data) {
data = data.toString();
//strip separators
logger("DATA:\nfrom " + sock.remoteAddress + ':\n' + data.split("\r").join("\n"));
if (data.indexOf(VT) > -1) {
self.message = '';
}
self.message += data.replace(VT, '');
if (data.indexOf(FS + CR) > -1) {
self.message = self.message.replace(FS + CR, '');
var data2 = hl7.parseString(self.message);
logger("Message:\r\n" + self.message + "\r\n\r\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|||
*/
self.emit('hl7', self.message);
var ack = ackn(data2, "AA");
sock.write(VT + ack + FS + CR);
}
});
sock.on('close', function (data) {
logger('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
});
});
self.send = function (receivingHost, receivingPort, hl7Data, callback) {
var sendingClient = new net.connect({
host: receivingHost,
port: receivingPort
}, function () {
logger('Sending data to ' + receivingHost + ':' + receivingPort);
sendingClient.write(VT + hl7Data + FS + CR);
});
var _terminate = function () {
logger('closing connection with ' + receivingHost + ':' + receivingPort);
sendingClient.end();
};
sendingClient.on('data', function (rawAckData) {
logger(receivingHost + ':' + receivingPort + ' ACKED data');
var ackData = rawAckData
.toString() // Buffer -> String
.replace(VT, '')
//.split('\r')[1] // Ack data
.replace(FS, '')
.replace(CR, '');
callback(null, ackData);
_terminate();
});
sendingClient.on('error', function (error) {
logger(receivingHost + ':' + receivingPort + ' couldn\'t process data');
callback(error, null);
_terminate();
});
};
Server.listen(PORT, HOST);
}
util.inherits(MLLPServer, EventEmitter);
exports.MLLPServer = MLLPServer;