@medplum/agent 2.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/babel.config.json +3 -0
- package/jest.config.json +8 -0
- package/package.json +32 -0
- package/src/__mocks__/node-windows.ts +9 -0
- package/src/main.test.ts +53 -0
- package/src/main.ts +63 -0
- package/tsconfig.json +8 -0
package/README.md
ADDED
package/jest.config.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@medplum/agent",
|
|
3
|
+
"version": "2.0.27",
|
|
4
|
+
"description": "Medplum Agent",
|
|
5
|
+
"author": "Medplum <hello@medplum.com>",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"homepage": "https://www.medplum.com/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/medplum/medplum.git",
|
|
11
|
+
"directory": "packages/agent"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18.0.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"clean": "rimraf dist",
|
|
18
|
+
"build": "npm run clean && tsc",
|
|
19
|
+
"test": "jest"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@medplum/core": "*",
|
|
23
|
+
"@medplum/hl7": "*",
|
|
24
|
+
"node-windows": "1.0.0-beta.8"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@medplum/fhirtypes": "*",
|
|
28
|
+
"@medplum/mock": "*",
|
|
29
|
+
"@types/node-windows": "0.1.2",
|
|
30
|
+
"pkg": "5.8.1"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/main.test.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { allOk, Hl7Message } from '@medplum/core';
|
|
2
|
+
import { Bot, Resource } from '@medplum/fhirtypes';
|
|
3
|
+
import { Hl7Client } from '@medplum/hl7';
|
|
4
|
+
import { MockClient } from '@medplum/mock';
|
|
5
|
+
import { App } from './main';
|
|
6
|
+
|
|
7
|
+
jest.mock('node-windows');
|
|
8
|
+
|
|
9
|
+
const medplum = new MockClient();
|
|
10
|
+
let bot: Bot;
|
|
11
|
+
|
|
12
|
+
describe('Agent', () => {
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
console.log = jest.fn();
|
|
15
|
+
|
|
16
|
+
medplum.router.router.add('POST', ':resourceType/:id/$execute', async () => {
|
|
17
|
+
return [allOk, {} as Resource];
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
bot = await medplum.createResource<Bot>({ resourceType: 'Bot' });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('Runs successfully', async () => {
|
|
24
|
+
const app = new App(medplum, bot);
|
|
25
|
+
app.start();
|
|
26
|
+
app.stop();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('Send and receive', async () => {
|
|
30
|
+
const app = new App(medplum, bot);
|
|
31
|
+
app.start();
|
|
32
|
+
|
|
33
|
+
const client = new Hl7Client({
|
|
34
|
+
host: 'localhost',
|
|
35
|
+
port: 56000,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
await client.connect();
|
|
39
|
+
|
|
40
|
+
const response = await client.sendAndWait(
|
|
41
|
+
Hl7Message.parse(
|
|
42
|
+
'MSH|^~\\&|ADT1|MCM|LABADT|MCM|198808181126|SECURITY|ADT^A01|MSG00001|P|2.2\r' +
|
|
43
|
+
'PID|||PATID1234^5^M11||JONES^WILLIAM^A^III||19610615|M-\r' +
|
|
44
|
+
'NK1|1|JONES^BARBARA^K|SPO|||||20011105\r' +
|
|
45
|
+
'PV1|1|I|2000^2012^01||||004777^LEBAUER^SIDNEY^J.|||SUR||-||1|A0-'
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
expect(response).toBeDefined();
|
|
49
|
+
|
|
50
|
+
client.close();
|
|
51
|
+
app.stop();
|
|
52
|
+
});
|
|
53
|
+
});
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { MedplumClient, normalizeErrorString } from '@medplum/core';
|
|
2
|
+
import { Bot } from '@medplum/fhirtypes';
|
|
3
|
+
import { Hl7MessageEvent, Hl7Server } from '@medplum/hl7';
|
|
4
|
+
import { EventLogger } from 'node-windows';
|
|
5
|
+
|
|
6
|
+
const log = new EventLogger({
|
|
7
|
+
source: 'MedplumService',
|
|
8
|
+
eventLog: 'SYSTEM',
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const HL7_CONTENT_TYPE = 'x-application/hl7-v2+er7';
|
|
12
|
+
|
|
13
|
+
export class App {
|
|
14
|
+
readonly log: EventLogger;
|
|
15
|
+
readonly server: Hl7Server;
|
|
16
|
+
|
|
17
|
+
constructor(readonly medplum: MedplumClient, readonly bot: Bot) {
|
|
18
|
+
this.log = new EventLogger({
|
|
19
|
+
source: 'MedplumService',
|
|
20
|
+
eventLog: 'SYSTEM',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
this.server = new Hl7Server();
|
|
24
|
+
this.server.addEventListener('message', (event) => this.handler(event));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
start(): void {
|
|
28
|
+
this.log.info('Medplum service starting...');
|
|
29
|
+
this.server.start(56000);
|
|
30
|
+
this.log.info('Medplum service started successfully');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
stop(): void {
|
|
34
|
+
this.log.info('Medplum service stopping...');
|
|
35
|
+
this.server.stop();
|
|
36
|
+
this.log.info('Medplum service stopped successfully');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private async handler(event: Hl7MessageEvent): Promise<void> {
|
|
40
|
+
try {
|
|
41
|
+
console.log('Received:');
|
|
42
|
+
console.log(event.message.toString());
|
|
43
|
+
|
|
44
|
+
await this.medplum.post(
|
|
45
|
+
this.medplum.fhirUrl('Bot', this.bot.id as string, '$execute'),
|
|
46
|
+
event.message.toString(),
|
|
47
|
+
HL7_CONTENT_TYPE
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const ack = event.message.buildAck();
|
|
51
|
+
console.log('Response:');
|
|
52
|
+
console.log(ack.toString());
|
|
53
|
+
event.send(ack);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.log('HL7 error', err);
|
|
56
|
+
log.error(normalizeErrorString(err));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (typeof require !== 'undefined' && require.main === module) {
|
|
62
|
+
new App(new MedplumClient(), { resourceType: 'Bot', id: '00000000-00000000-00000000-00000000' }).start();
|
|
63
|
+
}
|