This commit is contained in:
Dwi Swandhana
2026-07-19 20:47:08 +07:00
parent 616f0658d6
commit 30e39875c0
4 changed files with 137 additions and 41 deletions
+72 -17
View File
@@ -240,6 +240,51 @@ class AstmMessageService
return explode('^', $value)[0] ?: null;
}
protected function normalizeAstmRecordLines(string $raw): string
{
$normalized = str_replace(["\r\n", "\r"], "\n", $raw);
if (str_contains($normalized, "\n")) {
return $normalized;
}
$fields = explode('|', $normalized);
$records = [];
$current = [];
foreach ($fields as $index => $field) {
$isSegmentStart = $this->isAstmSegmentStart($fields, $index);
if ($isSegmentStart && !empty($current)) {
$records[] = implode('|', $current);
$current = [];
}
$current[] = $field;
}
if (!empty($current)) {
$records[] = implode('|', $current);
}
return !empty($records) ? implode("\n", $records) : $normalized;
}
protected function isAstmSegmentStart(array $fields, int $index): bool
{
$field = $fields[$index] ?? '';
if ($field === 'H') {
return $index === 0;
}
if (!in_array($field, ['P', 'O', 'R', 'L'], true)) {
return false;
}
$next = $fields[$index + 1] ?? null;
return $next !== null && preg_match('/^\d+$/', (string) $next) === 1;
}
protected function processBDAstmResponse(string $raw, $data): bool
{
try {
@@ -602,7 +647,7 @@ class AstmMessageService
}
public function processAstmMessagesgenExpert($response, $alat)
{
$normalizedData = str_replace(["\r\n", "\r"], "\n", $response);
$normalizedData = $this->normalizeAstmRecordLines($response);
$segments = explode("\n", $normalizedData);
$patient_id = '';
@@ -621,9 +666,12 @@ class AstmMessageService
// 1. Parsing Pasien
if ($recordType === 'P') {
$patient_id = !empty($cekdata[3]) ? $cekdata[3] : (!empty($cekdata[4]) ? $cekdata[4] : '');
$rawPatientId = $cekdata[2] ?? ($cekdata[3] ?? '');
$patient_id = trim(str_replace(['\R\\', '\\R\\', '\\'], '', $rawPatientId));
$raw_name = isset($cekdata[5]) ? trim(str_replace('^', ' ', $cekdata[5])) : '';
$raw_name = !empty($cekdata[4])
? trim(str_replace('^', ' ', $cekdata[4]))
: (isset($cekdata[5]) ? trim(str_replace('^', ' ', $cekdata[5])) : '');
$patient_name = !empty(trim($raw_name)) ? $raw_name : $patient_id;
}
@@ -650,24 +698,28 @@ class AstmMessageService
return trim($val) !== '';
}));
$target_name = '';
$metric_type = 'Result';
$target_parts = $target_match;
$last_part = end($target_parts);
// Jika string test_info mengandung 'Ct' atau 'EndPt', elemen terakhir adalah metrik,
// dan elemen sebelum terakhir adalah nama targetnya.
if (strpos($rsegmen, 'Ct|') !== false) {
// Jika string test_info mengandung metrik, elemen terakhir adalah metrik,
// dan elemen sebelumnya adalah nama target.
if ($last_part === 'Ct') {
$metric_type = 'Ct';
// Mengambil nama target (elemen sebelum 'Ct')
$target_name = count($target_match) > 1 ? $target_match[count($target_match) - 2] : '';
} elseif (strpos($rsegmen, 'EndPt|') !== false) {
array_pop($target_parts);
} elseif ($last_part === 'EndPt') {
$metric_type = 'EndPt';
// Mengambil nama target (elemen sebelum 'EndPt')
$target_name = count($target_match) > 1 ? $target_match[count($target_match) - 2] : '';
} else {
// Jika bukan Ct/EndPt, ini adalah Result murni.
// Target name biasanya adalah elemen terakhir dari array yang tidak kosong.
$target_name = count($target_match) > 0 ? end($target_match) : 'Unknown Target';
array_pop($target_parts);
} elseif (in_array($last_part, ['Delta Ct', 'LOG'], true)) {
$metric_type = $last_part;
array_pop($target_parts);
}
if (!empty($target_parts) && preg_match('/^\d+$/', (string) end($target_parts))) {
array_pop($target_parts);
}
$target_name = count($target_parts) > 0 ? end($target_parts) : 'Unknown Target';
$target_name = trim($target_name);
@@ -861,11 +913,14 @@ class AstmMessageService
}
public function processAstmResponse($response, $alat) {
// Bersihkan data
$astmData = $this->cleanString($response);
$astmData = $this->normalizeAstmRecordLines($this->cleanString($response));
// Validasi awal: response tidak kosong
if (empty($astmData)) {
Log::error("ASTM data kosong atau tidak valid.");
}
if (stripos($astmData, 'GeneXpert') !== false) {
return $this->processAstmMessagesgenExpert($astmData, $alat);
}
$jsonantibiotik = array(
'Oxacillin-OX',
'Cefoxitin-FOX',