clqms-be/app/Controllers/EdgeController.php
mahdahar cd65e91db1 refactor: Rename controllers to follow CodeIgniter 4 naming convention
- Rename all controllers from X.php to XController.php format
- Add new RefTxtModel for text-based reference ranges
- Rename group_dialog.php to grp_dialog.php and remove title_dialog.php
- Add comprehensive test suite for v2/master/TestDef module
- Update Routes.php to reflect controller renames
- Remove obsolete data files (clqms_v2.sql, lab.dbml)
2026-01-05 16:55:34 +07:00

170 lines
4.9 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
class EdgeController extends Controller
{
use ResponseTrait;
protected $db;
protected $edgeResModel;
public function __construct()
{
$this->db = \Config\Database::connect();
$this->edgeResModel = new \App\Models\EdgeResModel();
}
/**
* POST /api/edge/results
* Receive results from tiny-edge
*/
public function results()
{
try {
$input = $this->request->getJSON(true);
if (empty($input)) {
return $this->failValidationErrors('Invalid JSON payload');
}
// Extract key fields from payload
$sampleId = $input['sample_id'] ?? null;
$instrumentId = $input['instrument_id'] ?? null;
$patientId = $input['patient_id'] ?? null;
// Store in edgeres table
$data = [
'SiteID' => 1, // Default site, can be configured
'InstrumentID' => $instrumentId,
'SampleID' => $sampleId,
'PatientID' => $patientId,
'Payload' => json_encode($input),
'Status' => 'pending',
'AutoProcess' => 0, // Default to manual processing
'CreateDate' => date('Y-m-d H:i:s')
];
$id = $this->edgeResModel->insert($data);
if (!$id) {
return $this->failServerError('Failed to save result');
}
return $this->respondCreated([
'status' => 'success',
'message' => 'Result received and queued',
'data' => [
'edge_res_id' => $id,
'sample_id' => $sampleId,
'instrument_id' => $instrumentId
]
]);
} catch (\Throwable $e) {
return $this->failServerError('Error processing result: ' . $e->getMessage());
}
}
/**
* GET /api/edge/orders
* Return pending orders for an instrument
*/
public function orders()
{
try {
$instrumentId = $this->request->getGet('instrument');
if (!$instrumentId) {
return $this->failValidationErrors('instrument parameter is required');
}
// TODO: Implement order fetching logic
// For now, return empty array
return $this->respond([
'status' => 'success',
'message' => 'Orders fetched',
'data' => []
]);
} catch (\Throwable $e) {
return $this->failServerError('Error fetching orders: ' . $e->getMessage());
}
}
/**
* POST /api/edge/orders/:id/ack
* Acknowledge order delivery
*/
public function ack($orderId = null)
{
try {
if (!$orderId) {
return $this->failValidationErrors('Order ID is required');
}
$input = $this->request->getJSON(true);
$instrumentId = $input['instrument_id'] ?? null;
// Log acknowledgment
$this->db->table('edgeack')->insert([
'OrderID' => $orderId,
'InstrumentID' => $instrumentId,
'AckDate' => date('Y-m-d H:i:s'),
'CreateDate' => date('Y-m-d H:i:s')
]);
return $this->respond([
'status' => 'success',
'message' => 'Order acknowledged',
'data' => [
'order_id' => $orderId
]
]);
} catch (\Throwable $e) {
return $this->failServerError('Error acknowledging order: ' . $e->getMessage());
}
}
/**
* POST /api/edge/status
* Log instrument status
*/
public function status()
{
try {
$input = $this->request->getJSON(true);
$instrumentId = $input['instrument_id'] ?? null;
$status = $input['status'] ?? null;
$lastActivity = $input['last_activity'] ?? null;
$timestamp = $input['timestamp'] ?? date('Y-m-d H:i:s');
if (!$instrumentId || !$status) {
return $this->failValidationErrors('instrument_id and status are required');
}
// Store status log
$this->db->table('edgestatus')->insert([
'InstrumentID' => $instrumentId,
'Status' => $status,
'LastActivity' => $lastActivity,
'Timestamp' => $timestamp,
'CreateDate' => date('Y-m-d H:i:s')
]);
return $this->respond([
'status' => 'success',
'message' => 'Status logged'
]);
} catch (\Throwable $e) {
return $this->failServerError('Error logging status: ' . $e->getMessage());
}
}
}