<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SyncLabResultsSerial extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:sync-lab-results-serial';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $host = '192.168.1.100'; // IP alat
        $port = 4000;            // Port alat

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($socket, $host, $port);

        $response = socket_read($socket, 1024);

        // Proses response dari alat
        $this->processSocketData($response);

        socket_close($socket);
    }
    protected function processSocketData($data)
    {
        // Proses data yang diterima (misalnya parsing atau format hasil tes)
        $testResult = json_decode($data, true);

        // Simpan hasil tes ke database
        TestResult::create([
            'patient_id'    => $testResult['patient_id'],
            'test_id'       => $testResult['test_id'],
            'result_value'  => $testResult['result_value'],
            'timestamp'     => $testResult['timestamp']
        ]);
    }
}
