Added obd message

This commit is contained in:
dhanabalan
2025-05-23 16:47:07 +05:30
parent af793faf0d
commit 023d2e13fb
2 changed files with 415 additions and 37 deletions

View File

@@ -22,13 +22,158 @@ class ObdController extends Controller
* Store a newly created resource in storage.
*/
// public function store(Request $request)
// {
// $expectedUser = env('API_AUTH_USER');
// $expectedPw = env('API_AUTH_PW');
// $header_auth = $request->header('Authorization');
// $expectedToken = $expectedUser . ':' . $expectedPw;
// if("Bearer " . $expectedToken !== $header_auth)
// {
// return response("ERROR: Unauthorized", 403)
// ->header('Content-Type', 'text/plain');
// }
// try
// {
// $data = $request->all();
// // Validate required fields
// $missing = [];
// if (empty($data['plant_name'])) $missing[] = 'plant_name';
// if (empty($data['obd_number'])) $missing[] = 'obd_number';
// if (empty($data['line_numbers'])) $missing[] = 'line_numbers';
// if (!empty($missing))
// {
// $message = "ERROR: Missing required field(s): " . implode(', ', $missing);
// return response($message, 400)->header('Content-Type', 'text/plain');
// }
// // OBD Number validation
// $obdNumber = $data['obd_number'];
// if (Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
// {
// return response("ERROR: OBD Number should contain minimum 8 digits", 400)
// ->header('Content-Type', 'text/plain');
// }
// // Lookup plant_id by plant_name
// $plantId = Plant::where('name', $data['plant_name'])->value('id');
// if (!$plantId) {
// return response("ERROR: Plant '" . $data['plant_name'] . "' not found", 404)
// ->header('Content-Type', 'text/plain');
// }
// $missingLines = [];
// $updated = 0;
// foreach ($data['line_numbers'] as $line)
// {
// if ($line['line_number'] == '' || $line['line_number'] == null) {
// return response("ERROR: Line Number can't be empty", 400)
// ->header('Content-Type', 'text/plain');
// }
// $exists = WeightValidation::where('plant_id', $plantId)
// ->where('obd_number', $data['obd_number'])
// ->where('line_number', $line['line_number'])
// ->exists();
// if (!$exists)
// {
// $missingLines[] = $line['line_number'];
// continue;
// }
// $record = WeightValidation::where('plant_id', $plantId)
// ->where('obd_number', $data['obd_number'])
// ->where('line_number', $line['line_number'])
// ->first();
// if (!$record)
// {
// $missingLines[] = $line['line_number'];
// continue;
// }
// $mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
// $missingFields = [];
// foreach ($mandatoryFields as $field) {
// if ($line[$field] == '' || $line[$field] == null) {
// // Convert field name to a more readable format, e.g. vehicle_number -> Vehicle Number
// $missingFields[] = ucwords(str_replace('_', ' ', $field));
// }
// }
// if (!empty($missingFields)) {
// if (count($missingFields) == 1)
// {
// $fieldsString = $missingFields[0];
// }
// else
// {
// $lastField = array_pop($missingFields);
// $fieldsString = implode(', ', $missingFields) . ' and ' . $lastField;
// }
// $message = "ERROR: " . $fieldsString . " can't be empty for line_number " . $line['line_number'];
// return response($message, 400)->header('Content-Type', 'text/plain');
// }
// // Compare incoming data with existing record
// $isSame =
// ($record->vehicle_number == $line['vehicle_number']) &&
// ($record->bundle_number == $line['bundle_number']) &&
// ($record->picked_weight == $line['picked_weight']) &&
// ($record->scanned_by == $line['scanned_by']);
// if ($isSame) {
// return response(
// "ERROR: Already line number " . $line['line_number'] . " is updated",
// 400
// )->header('Content-Type', 'text/plain');
// }
// $count = WeightValidation::where([
// 'plant_id' => $plantId,
// 'obd_number' => $data['obd_number'],
// 'line_number'=> $line['line_number'],
// ])
// ->update([
// 'vehicle_number' => $line['vehicle_number'] ?? null,
// 'bundle_number' => $line['bundle_number'] ?? null,
// 'picked_weight' => $line['picked_weight'] ?? null,
// 'scanned_by' => $line['scanned_by'] ?? null,
// 'updated_at' => now(),
// ]);
// $updated += $count;
// }
// if (!empty($missingLines)) {
// $message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant ID '" . $plantId . "' and OBD Number: '" . $data['obd_number'] . "'";
// return response($message, 404)->header('Content-Type', 'text/plain');
// } else {
// $message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully";
// return response($message, 200)->header('Content-Type', 'text/plain');
// }
// }
// catch (\Exception $e)
// {
// return response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
// }
// }
public function store(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if("Bearer " . $expectedToken !== $header_auth)
@@ -52,7 +197,7 @@ class ObdController extends Controller
return response($message, 400)->header('Content-Type', 'text/plain');
}
// OBD Number validation
// OBD Number validation
$obdNumber = $data['obd_number'];
if (Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
{
@@ -67,54 +212,53 @@ class ObdController extends Controller
->header('Content-Type', 'text/plain');
}
//Check if OBD number exists for that plant
$obdRecords = WeightValidation::where('plant_id', $plantId)
->where('obd_number', $obdNumber)
->select('id', 'line_number') // Only fetch required columns
->get();
if ($obdRecords->isEmpty()) {
return response(
"ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",
404
)->header('Content-Type', 'text/plain');
}
$missingLines = [];
$updated = 0;
$alreadyUpdatedLines = [];
foreach ($data['line_numbers'] as $line)
{
//if (empty($line['line_number'])) continue;
//!isset($line['line_number']) ||
if ($line['line_number'] == '' || $line['line_number'] == null) {
return response("ERROR: Line Number can't be empty", 400)
->header('Content-Type', 'text/plain');
}
$exists = WeightValidation::where('plant_id', $plantId)
$record = WeightValidation::where('plant_id', $plantId)
->where('obd_number', $data['obd_number'])
->where('line_number', $line['line_number'])
->exists();
->first();
if (!$exists)
if (!$record)
{
$missingLines[] = $line['line_number'];
continue;
}
// $mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
// $missingFields = [];
// foreach ($mandatoryFields as $field) {
// if ($line[$field] == '' || $line[$field] == null) { //!isset($line[$field]) ||
// $missingFields[] = $field;
// }
// }
// if (!empty($missingFields)) {
// $message = "ERROR: Missing or empty field(s): " . implode(', ', $missingFields) .
// " for line_number " . $line['line_number'];
// return response($message, 400)->header('Content-Type', 'text/plain');
// }
$mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
$missingFields = [];
foreach ($mandatoryFields as $field) {
foreach ($mandatoryFields as $field)
{
if ($line[$field] == '' || $line[$field] == null) {
// Convert field name to a more readable format, e.g. vehicle_number -> Vehicle Number
$missingFields[] = ucwords(str_replace('_', ' ', $field));
}
}
if (!empty($missingFields)) {
if (!empty($missingFields))
{
if (count($missingFields) == 1)
{
$fieldsString = $missingFields[0];
@@ -128,7 +272,58 @@ class ObdController extends Controller
$message = "ERROR: " . $fieldsString . " can't be empty for line_number " . $line['line_number'];
return response($message, 400)->header('Content-Type', 'text/plain');
}
// Check if ANY of the fields are NOT empty in DB
$fields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
$alreadyUpdatedLines = [];
foreach ($data['line_numbers'] as $line) {
// Fetch the record for this line
$record = WeightValidation::where('plant_id', $plantId)
->where('obd_number', $data['obd_number'])
->where('line_number', $line['line_number'])
->first();
//If No Record Found
if (!$record)
{
continue; // Skip to next line
}
$hasAnyValue = false;
foreach ($fields as $field)
{
if (!empty($record->$field))
{
$hasAnyValue = true;
break;
}
}
if ($hasAnyValue)
{
$alreadyUpdatedLines[] = $line['line_number'];
}
}
// Only after checking all lines, return if any already updated
if (!empty($alreadyUpdatedLines)) {
return response(
"Already line number(s) updated for obd number '{$data['obd_number']}' is :" . implode(', ', $alreadyUpdatedLines),
400
)->header('Content-Type', 'text/plain');
}
}
// If any lines are missing, report
if (!empty($missingLines)) {
$message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant '" . $data['plant_name'] . "' and OBD Number: '" . $data['obd_number'] . "'";
return response($message, 404)->header('Content-Type', 'text/plain');
}
// If we reach here, all lines are valid and need updating
$updated = 0;
foreach ($data['line_numbers'] as $line)
{
$count = WeightValidation::where([
'plant_id' => $plantId,
'obd_number' => $data['obd_number'],
@@ -145,13 +340,192 @@ class ObdController extends Controller
$updated += $count;
}
if (!empty($missingLines)) {
$message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant ID '" . $plantId . "' and OBD Number: '" . $data['obd_number'] . "'";
return response($message, 404)->header('Content-Type', 'text/plain');
} else {
$message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully";
return response($message, 200)->header('Content-Type', 'text/plain');
}
$message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully";
return response($message, 200)->header('Content-Type', 'text/plain');
}
catch (\Exception $e)
{
return response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
}
}
//Route::post('obd/store-test-data', [ObdController::class, 'test'])
public function test(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
// if("Bearer " . $expectedToken !== $header_auth)
// {
// return response("ERROR: Unauthorized", 403)
// ->header('Content-Type', 'text/plain');
// }
try
{
return response("Unable to Received", 400)->header('Content-Type', 'text/plain');
// $data = $request->all();
// // Validate required fields
// $missing = [];
// if (empty($data['plant_name'])) $missing[] = 'plant_name';
// if (empty($data['obd_number'])) $missing[] = 'obd_number';
// if (empty($data['line_numbers'])) $missing[] = 'line_numbers';
// if (!empty($missing))
// {
// $message = "ERROR: Missing required field(s): " . implode(', ', $missing);
// return response($message, 400)->header('Content-Type', 'text/plain');
// }
// // OBD Number validation
// $obdNumber = $data['obd_number'];
// if (Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
// {
// return response("ERROR: OBD Number should contain minimum 8 digits", 400)
// ->header('Content-Type', 'text/plain');
// }
// // Lookup plant_id by plant_name
// $plantId = Plant::where('name', $data['plant_name'])->value('id');
// if (!$plantId) {
// return response("ERROR: Plant '" . $data['plant_name'] . "' not found", 404)
// ->header('Content-Type', 'text/plain');
// }
// //Check if OBD number exists for that plant
// $obdRecords = WeightValidation::where('plant_id', $plantId)
// ->where('obd_number', $obdNumber)
// ->select('id', 'line_number') // Only fetch required columns
// ->get();
// if ($obdRecords->isEmpty()) {
// return response(
// "ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",
// 404
// )->header('Content-Type', 'text/plain');
// }
// $missingLines = [];
// $alreadyUpdatedLines = [];
// foreach ($data['line_numbers'] as $line)
// {
// if ($line['line_number'] == '' || $line['line_number'] == null) {
// return response("ERROR: Line Number can't be empty", 400)
// ->header('Content-Type', 'text/plain');
// }
// $record = WeightValidation::where('plant_id', $plantId)
// ->where('obd_number', $data['obd_number'])
// ->where('line_number', $line['line_number'])
// ->first();
// if (!$record)
// {
// $missingLines[] = $line['line_number'];
// continue;
// }
// $mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
// $missingFields = [];
// foreach ($mandatoryFields as $field)
// {
// if ($line[$field] == '' || $line[$field] == null) {
// $missingFields[] = ucwords(str_replace('_', ' ', $field));
// }
// }
// if (!empty($missingFields))
// {
// if (count($missingFields) == 1)
// {
// $fieldsString = $missingFields[0];
// }
// else
// {
// $lastField = array_pop($missingFields);
// $fieldsString = implode(', ', $missingFields) . ' and ' . $lastField;
// }
// $message = "ERROR: " . $fieldsString . " can't be empty for line_number " . $line['line_number'];
// return response($message, 400)->header('Content-Type', 'text/plain');
// }
// // Check if ANY of the fields are NOT empty in DB
// $fields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
// $alreadyUpdatedLines = [];
// foreach ($data['line_numbers'] as $line) {
// // Fetch the record for this line
// $record = WeightValidation::where('plant_id', $plantId)
// ->where('obd_number', $data['obd_number'])
// ->where('line_number', $line['line_number'])
// ->first();
// //If No Record Found
// if (!$record)
// {
// continue; // Skip to next line
// }
// $hasAnyValue = false;
// foreach ($fields as $field)
// {
// if (!empty($record->$field))
// {
// $hasAnyValue = true;
// break;
// }
// }
// if ($hasAnyValue)
// {
// $alreadyUpdatedLines[] = $line['line_number'];
// }
// }
// // Only after checking all lines, return if any already updated
// if (!empty($alreadyUpdatedLines)) {
// return response(
// "Already line number(s) updated for obd number '{$data['obd_number']}' is :" . implode(', ', $alreadyUpdatedLines),
// 400
// )->header('Content-Type', 'text/plain');
// }
// }
// // If any lines are missing, report
// if (!empty($missingLines)) {
// $message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant '" . $data['plant_name'] . "' and OBD Number: '" . $data['obd_number'] . "'";
// return response($message, 404)->header('Content-Type', 'text/plain');
// }
// // If we reach here, all lines are valid and need updating
// $updated = 0;
// foreach ($data['line_numbers'] as $line)
// {
// $count = WeightValidation::where([
// 'plant_id' => $plantId,
// 'obd_number' => $data['obd_number'],
// 'line_number'=> $line['line_number'],
// ])
// ->update([
// 'vehicle_number' => $line['vehicle_number'] ?? null,
// 'bundle_number' => $line['bundle_number'] ?? null,
// 'picked_weight' => $line['picked_weight'] ?? null,
// 'scanned_by' => $line['scanned_by'] ?? null,
// 'updated_at' => now(),
// ]);
// $updated += $count;
// }
// $message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully";
// return response($message, 200)->header('Content-Type', 'text/plain');
}
catch (\Exception $e)
{
@@ -231,14 +605,15 @@ class ObdController extends Controller
// return response()->json($ObdResponseStructure);
// }
public function get(Request $request)
public function get_obd(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if ("Bearer " . $expectedToken !== $header_auth) {
if ("Bearer " . $expectedToken !== $header_auth)
{
return response("ERROR: Unauthorized", 403)
->header('Content-Type', 'text/plain');
}
@@ -258,7 +633,7 @@ class ObdController extends Controller
}
else if(Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
{
return response("ERROR: OBD Number should contain minimum 8 digits", 400)
return response("ERROR: OBD Number should contain minimum 8 digits: '$obdNumber'", 400)
->header('Content-Type', 'text/plain');
}