Fix BD ASTM frame reassembly and result parsing
This commit is contained in:
No files matched your search
@@ -664,7 +664,7 @@ class AstmMessageService
|
||||
protected function splitBDAstmMessages(string $raw): array
|
||||
{
|
||||
// bersihkan karakter kontrol
|
||||
$clean = preg_replace('/[\x02\x03\x04]/', '', $raw);
|
||||
$clean = preg_replace('/[\x02\x03\x04\x17]/', '', $raw);
|
||||
|
||||
$lines = preg_split("/\r\n|\n|\r/", $clean);
|
||||
|
||||
@@ -698,17 +698,48 @@ class AstmMessageService
|
||||
protected function reassembleAstmFrames(string $raw): string
|
||||
{
|
||||
$buffer = '';
|
||||
$frames = [];
|
||||
$frames = explode("\x02", $raw);
|
||||
|
||||
// ambil semua frame
|
||||
preg_match_all('/\x02(\d)(.*?)\x03(..)/s', $raw, $frames);
|
||||
foreach ($frames as $frame) {
|
||||
if ($frame === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content = $frame;
|
||||
$etxPos = strpos($content, "\x03");
|
||||
$etbPos = strpos($content, "\x17");
|
||||
|
||||
if ($etxPos !== false && ($etbPos === false || $etxPos < $etbPos)) {
|
||||
$content = substr($content, 0, $etxPos);
|
||||
} elseif ($etbPos !== false) {
|
||||
$content = substr($content, 0, $etbPos);
|
||||
}
|
||||
|
||||
if ($content !== '' && ctype_digit($content[0])) {
|
||||
$content = substr($content, 1);
|
||||
}
|
||||
|
||||
foreach ($frames[2] as $content) {
|
||||
// gabungkan isi frame (tanpa ETX & checksum)
|
||||
$buffer .= $content;
|
||||
}
|
||||
|
||||
return trim($buffer);
|
||||
return trim($buffer) !== '' ? trim($buffer) : trim($raw);
|
||||
}
|
||||
protected function normalizeBDResult(?string $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (stripos($value, 'NEGATIVE') !== false) {
|
||||
return 'NEGATIVE';
|
||||
}
|
||||
|
||||
if (stripos($value, 'POSITIVE') !== false) {
|
||||
return 'POSITIVE';
|
||||
}
|
||||
|
||||
return explode('^', $value)[0] ?: null;
|
||||
}
|
||||
protected function processBDAstmResponse(string $raw, $data): bool
|
||||
{
|
||||
@@ -718,7 +749,8 @@ class AstmMessageService
|
||||
// =========================
|
||||
$accnumber= '';
|
||||
$resultDateTime = null;
|
||||
$rawClean = preg_replace('/[\x02\x03\x04]/', '', $raw);
|
||||
$bdResult = null;
|
||||
$rawClean = preg_replace('/[\x02\x03\x04\x17]/', '', $this->reassembleAstmFrames($raw));
|
||||
$lines = preg_split("/\r\n|\n|\r/", $rawClean);
|
||||
|
||||
$parsed = [
|
||||
@@ -771,14 +803,18 @@ class AstmMessageService
|
||||
} elseif (str_starts_with($line, 'R|')) {
|
||||
$f = explode('|', $line);
|
||||
$test = explode('^', $f[2] ?? '');
|
||||
$bdResult = $this->normalizeBDResult($f[3] ?? null) ?? $bdResult;
|
||||
$resultDateTime = $this->astmToDateTime($f[12] ?? null)
|
||||
?? $this->astmToDateTime($f[11] ?? null)
|
||||
?? $resultDateTime;
|
||||
|
||||
// Instrument detail di akhir baris
|
||||
preg_match('/(MGIT960|BACTECFX)[^|]*/', $line, $inst);
|
||||
|
||||
$parsed['result'] = [
|
||||
'organism' => $test[2] ?? null,
|
||||
'test_status' => explode('^', $f[3] ?? '')[0],
|
||||
'result_status_datetime' => $this->astmToDateTime($f[13] ?? $resultDateTime),
|
||||
'test_status' => $bdResult ?? explode('^', $f[3] ?? '')[0],
|
||||
'result_status_datetime' => $resultDateTime,
|
||||
];
|
||||
|
||||
$parsed['instrument'] = [
|
||||
@@ -834,6 +870,19 @@ class AstmMessageService
|
||||
]
|
||||
);
|
||||
}
|
||||
if ($accnumber != '' && $bdResult){
|
||||
KomponenJawaban::updateOrCreate(
|
||||
[
|
||||
'accnumber' => $accnumber,
|
||||
'komponen' => 'bd_result',
|
||||
'isidata' => $bdResult,
|
||||
],
|
||||
[
|
||||
'template' => 'Kultur',
|
||||
'created_by' => 'BD'
|
||||
]
|
||||
);
|
||||
}
|
||||
//Log::info("Data BD ASTM Berhasil di Parse dan di simpan ", $resultSample->toArray());
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user