Added OBD post and get data
This commit is contained in:
258
app/Http/Controllers/ObdController.php
Normal file
258
app/Http/Controllers/ObdController.php
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Item;
|
||||||
|
use App\Models\Plant;
|
||||||
|
use App\Models\WeightValidation;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class ObdController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 (empty($line['line_number'])) continue;
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// public function get(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');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $plantName = $request->header('Plant_Name');
|
||||||
|
// $obdNumber = $request->header('obd_number');
|
||||||
|
|
||||||
|
// if (empty($plantName) || empty($obdNumber)) {
|
||||||
|
// return response("ERROR: Missing required headers (Plant_Name, OBD_Number)", 400)
|
||||||
|
// ->header('Content-Type', 'text/plain');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Fetch the plant record by name
|
||||||
|
// $plant = Plant::where('name', $plantName)->value('id');
|
||||||
|
|
||||||
|
// if (!$plant)
|
||||||
|
// {
|
||||||
|
// return response("ERROR: Plant not found", 400)
|
||||||
|
// ->header('Content-Type', 'text/plain');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $plantId = $plant->id;
|
||||||
|
|
||||||
|
// $records = WeightValidation::where('plant_id', $plantId)
|
||||||
|
// ->where('obd_number', $obdNumber)
|
||||||
|
// ->get();
|
||||||
|
|
||||||
|
// if ($records->isEmpty()) {
|
||||||
|
// return response("ERROR: No records found", 404)->header('Content-Type', 'text/plain');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $itemIds = $records->pluck('item_id')->unique();
|
||||||
|
// $itemCodes = Item::whereIn('id', $itemIds)
|
||||||
|
// ->select('id', 'code', 'description')
|
||||||
|
// ->get()
|
||||||
|
// ->keyBy('id');
|
||||||
|
|
||||||
|
// $ObdResponseStructure = [
|
||||||
|
// 'OBD_Number' => [
|
||||||
|
// [
|
||||||
|
// 'OBD_Number' => $obdNumber,
|
||||||
|
// 'Line_Numbers' => $records->map(function ($item) use ($itemCodes) {
|
||||||
|
// $itemInfo = $itemCodes[$item->item_id] ?? null;
|
||||||
|
// return [
|
||||||
|
// 'Line' => $item->line_number,
|
||||||
|
// 'Material_Code' => $itemInfo->code ?? "",
|
||||||
|
// 'Material_Description' => $itemInfo->description ?? "",
|
||||||
|
// // 'Quantity' => $item->picked_weight ?? "",
|
||||||
|
// 'Batch_Number' => $item->batch_number ?? "",
|
||||||
|
// 'Heat_Number' => $item->heat_number ?? "",
|
||||||
|
// ];
|
||||||
|
// })->toArray()
|
||||||
|
// ]
|
||||||
|
// ]
|
||||||
|
// ];
|
||||||
|
// return response()->json($ObdResponseStructure);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public function get(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');
|
||||||
|
}
|
||||||
|
|
||||||
|
$plantName = $request->header('plant_name'); // use lowercase
|
||||||
|
$obdNumber = $request->header('obd_number');
|
||||||
|
|
||||||
|
if (empty($plantName) || empty($obdNumber)) {
|
||||||
|
return response("ERROR: Missing required headers (plant_name, obd_number)", 400)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the plant id by name
|
||||||
|
$plantId = Plant::where('name', $plantName)->value('id');
|
||||||
|
|
||||||
|
if (!$plantId) {
|
||||||
|
return response("ERROR: Plant not found", 400)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
|
$records = WeightValidation::where('plant_id', $plantId)
|
||||||
|
->where('obd_number', $obdNumber)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($records->isEmpty()) {
|
||||||
|
return response("ERROR: No records found", 404)->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
|
$itemIds = $records->pluck('item_id')->unique();
|
||||||
|
$itemCodes = Item::whereIn('id', $itemIds)
|
||||||
|
->select('id', 'code', 'description')
|
||||||
|
->get()
|
||||||
|
->keyBy('id');
|
||||||
|
|
||||||
|
$ObdResponseStructure = [
|
||||||
|
'OBD_Number' => [
|
||||||
|
[
|
||||||
|
'OBD_Number' => $obdNumber,
|
||||||
|
'Line_Numbers' => $records->map(function ($item) use ($itemCodes) {
|
||||||
|
$itemInfo = $itemCodes[$item->item_id] ?? null;
|
||||||
|
return [
|
||||||
|
'Line' => $item->line_number,
|
||||||
|
'Material_Code' => $itemInfo->code ?? "",
|
||||||
|
'Material_Description' => $itemInfo->description ?? "",
|
||||||
|
'Batch_Number' => $item->batch_number ?? "",
|
||||||
|
'Heat_Number' => $item->heat_number ?? "",
|
||||||
|
];
|
||||||
|
})->toArray()
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
return response()->json($ObdResponseStructure);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
41
routes/api.php
Normal file
41
routes/api.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Http\Controllers\ObdController;
|
||||||
|
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
// Route::get('/user', function (Request $request) {
|
||||||
|
// // return "test";
|
||||||
|
|
||||||
|
// $item = \App\Models\Item::get();
|
||||||
|
|
||||||
|
// return $item;
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
|
// Route::post('/user/update', function (Request $request) {
|
||||||
|
// // Return the request data as JSON
|
||||||
|
|
||||||
|
// // dd($request);
|
||||||
|
// return response()->json([
|
||||||
|
// 'message' => 'User updated successfully',
|
||||||
|
// 'data' => $request->all()
|
||||||
|
// ]);
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
|
// Route::middleware('auth.basic')->post('/user/update', function (Request $request) {
|
||||||
|
// return response()->json([
|
||||||
|
// 'message' => 'Authenticated via Basic Auth',
|
||||||
|
// 'user' => $request->user(),
|
||||||
|
// 'data' => $request->all()
|
||||||
|
// ]);
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Route::post('obd/store-data', [ObdController::class, 'store'])
|
||||||
|
->withoutMiddleware(VerifyCsrfToken::class);
|
||||||
|
|
||||||
|
Route::get('obd/store-data/get', [ObdController::class, 'get']);
|
||||||
Reference in New Issue
Block a user