> ## Documentation Index
> Fetch the complete documentation index at: https://hmis-docs.derrickmugabwa.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# ASTM/TCP results inbound channel

> Receive ASTM-style analyzer results and normalize them for HMIS.

## Purpose

`Mock ASTM Results Inbound` is the pilot ASTM result listener. It accepts ASTM-like records over TCP, extracts the specimen and results, and sends normalized JSON to the Results Relay channel.

```mermaid theme={null}
flowchart LR
  A[Analyzer TCP client] -->|ASTM records| B[Mock ASTM Results Inbound :5000]
  B -->|Normalized JSON| C[HMIS Laboratory Results Relay :6661]
  C --> D[HMIS]
```

## Source: TCP Listener

| Setting              | Value                                  |
| -------------------- | -------------------------------------- |
| Local interface      | `0.0.0.0`                              |
| Local port           | `5000`                                 |
| Transmission mode    | Basic TCP                              |
| Mode                 | Server                                 |
| Data type            | Raw / Text                             |
| Encoding             | Default / UTF-8 compatible             |
| Buffer size          | `65536` bytes                          |
| Maximum connections  | `10`                                   |
| Keep connection open | Yes                                    |
| Receive timeout      | `0`                                    |
| Source response      | Auto-generate after source transformer |

This pilot accepts simple CR-delimited ASTM records. A real analyzer may require its vendor-specific ASTM framing, handshakes, checksums, and acknowledgements.

## Source transformer

Add one enabled JavaScript transformer step:

```javascript theme={null}
var raw = String(connectorMessage.getRawData())
    .replace(/\r\n/g, '\r')
    .replace(/\n/g, '\r');

var records = raw.split('\r').filter(function (record) {
    return record.length > 0;
});

var header = records.filter(function (record) {
    return record.indexOf('H|') === 0;
})[0];

var patient = records.filter(function (record) {
    return record.indexOf('P|') === 0;
})[0];

if (!patient) {
    throw 'ASTM message does not contain a P specimen record.';
}

var specimenNumber = patient.split('|')[3];

var results = records
    .filter(function (record) {
        return record.indexOf('R|') === 0;
    })
    .map(function (record) {
        var field = record.split('|');
        var testParts = field[2].split('^');

        return {
            analyzer_test_code: testParts[testParts.length - 1],
            value: field[3],
            unit: field[4],
            flag: field[6] || null,
            observed_at: new Date().toISOString()
        };
    });

if (results.length === 0) {
    throw 'ASTM message does not contain result records.';
}

var headerFields = header ? header.split('|') : [];
var messageId = headerFields.length
    ? headerFields[headerFields.length - 1]
    : java.util.UUID.randomUUID().toString();

msg = JSON.stringify({
    message_id: messageId,
    instrument_code: 'PILOT-HEM-01',
    specimen_number: specimenNumber,
    received_at: new Date().toISOString(),
    results: results
});
```

For a real analyzer, replace `PILOT-HEM-01` and validate every ASTM field position from the vendor interface manual.

## Destination: Results Relay

| Setting                 | Value                                 |
| ----------------------- | ------------------------------------- |
| Connector               | HTTP Sender                           |
| URL                     | `http://oie:6661/laboratory/results/` |
| Method                  | POST                                  |
| Content type            | `application/json`                    |
| Request body            | `${message.encodedData}`              |
| Authentication          | No                                    |
| Destination transformer | None                                  |

No HMIS token belongs in this channel. The Results Relay channel is the only OIE channel that holds the results-write credential.

## Validation

1. Dispatch a new specimen to `PILOT-HEM-01`.
2. Query it in the mock analyzer.
3. Select **ASTM / TCP**, host `oie`, port `5000`.
4. Enter one mapped result and transmit it.
5. Confirm OIE source and destination success, then confirm the HMIS audit/result.

## Source artifact

`oie-channel-exports/Mock ASTM Results Inbound.xml`
