Files
backend_vib360_laravel/app/Http/Controllers/APIS/CustomerApi/DeviceController.php

150 lines
5.6 KiB
PHP
Raw Normal View History

2025-03-11 17:15:41 +05:30
<?php
2025-03-11 17:52:55 +05:30
namespace App\Http\Controllers\APIS\CustomerApi;
2025-03-11 17:15:41 +05:30
use App\Http\Controllers\Controller;
2025-03-21 19:24:02 +05:30
use App\Http\Requests\CreateDeviceRequest;
2025-03-20 12:01:01 +05:30
use App\Models\Device;
use App\Services\DeviceService;
2025-03-11 17:15:41 +05:30
use Illuminate\Http\Request;
2025-03-20 12:01:01 +05:30
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Exception;
2025-03-11 17:15:41 +05:30
class DeviceController extends Controller
{
2025-03-20 12:01:01 +05:30
protected $deviceService;
public function __construct(DeviceService $deviceService)
{
$this->deviceService = $deviceService;
}
2025-03-21 19:24:02 +05:30
public function createOrUpdateDevice(CreateDeviceRequest $request)
2025-03-20 12:01:01 +05:30
{
try {
2025-03-21 19:24:02 +05:30
2025-03-20 12:01:01 +05:30
$deviceData = [
2025-03-21 19:24:02 +05:30
'name' => $request->name ?? null,
'type' => $request->type ?? null,
'label' => $request->label ?? null,
2025-03-20 12:01:01 +05:30
'version' => $request->version ?? 1,
2025-03-21 19:24:02 +05:30
'customerId' => $request->customerId ?? 1,
2025-03-20 12:01:01 +05:30
'additionalInfo' => $request->additionalInfo ?? [],
'deviceData' => [
'configuration' => ['type' => 'DEFAULT'],
'transportConfiguration' => [
'type' => 'DEFAULT',
2025-03-21 19:24:02 +05:30
'powerMode' => $request->transportConfiguration['powerMode'] ?? 'PSM',
2025-03-20 12:01:01 +05:30
'psmActivityTimer' => $request->transportConfiguration['psmActivityTimer'] ?? 9007199254740991,
'edrxCycle' => $request->transportConfiguration['edrxCycle'] ?? 9007199254740991,
'pagingTransmissionWindow' => $request->transportConfiguration['pagingTransmissionWindow'] ?? 9007199254740991,
]
],
2025-03-21 19:24:02 +05:30
'deviceProfileId' => $request->deviceProfileId ?? 1,
2025-03-20 12:01:01 +05:30
];
2025-03-21 19:24:02 +05:30
// Optional Fields
2025-03-20 12:01:01 +05:30
2025-03-21 19:24:02 +05:30
// Handle updating existing devices
if (!empty($request->id)) {
$deviceData['id'] = $request->id;
}
if (!empty($request->firmwareId)) {
$deviceData['firmwareId'] = $request->firmwareId;
}
if (!empty($request->softwareId)) {
$deviceData['softwareId'] = $request->softwareId;
}
2025-03-20 12:01:01 +05:30
2025-03-21 19:24:02 +05:30
// Call Service to create/update device
$apiResponse = $this->deviceService->createOrUpdateDevice($deviceData);
2025-03-20 12:01:01 +05:30
// Store device in the database
$device = Device::updateOrCreate(
['id' => $apiResponse['id']['id'] ?? Str::uuid()->toString()],
[
'entity_type' => $apiResponse['id']['entityType'] ?? 'DEVICE',
'created_time' => $apiResponse['createdTime'] ?? now()->timestamp,
'customer_id' => $apiResponse['customerId']['id'] ?? null,
'name' => $apiResponse['name'] ?? null,
'type' => $apiResponse['type'] ?? null,
'label' => $apiResponse['label'] ?? null,
2025-03-21 19:24:02 +05:30
'asset_id' => $apiResponse['asset_id'] ?? 'a5daeb60-f36c-11ef-a9dc-45dd276e4cd5',
2025-03-20 12:01:01 +05:30
'device_profile_id' => $apiResponse['deviceProfileId']['id'] ?? null,
'firmware_id' => $apiResponse['firmwareId']['id'] ?? null,
'software_id' => $apiResponse['softwareId']['id'] ?? null,
'version' => $apiResponse['version'] ?? 1,
'tenant_id' => $apiResponse['tenantId']['id'] ?? null,
'device_data' => json_encode($apiResponse['deviceData'] ?? []),
'additional_info' => json_encode($apiResponse['additionalInfo'] ?? [])
]
);
return jsonResponseWithSuccessMessage(
2025-03-21 19:24:02 +05:30
!empty($request->id) ? 'Device updated successfully' : 'Device created successfully',
2025-03-20 12:01:01 +05:30
['device' => $device, 'api_response' => $apiResponse]
);
} catch (Exception $e) {
Log::error("Error: " . $e->getMessage());
$errorResponse = json_decode($e->getMessage(), true);
if (json_last_error() === JSON_ERROR_NONE) {
return jsonResponseWithErrorMessage($errorResponse['message'] ?? 'Something went wrong', 400, $errorResponse);
}
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
2025-03-21 19:24:02 +05:30
public function listDevices(Request $request)
{
try {
$devices = Device::all();
return jsonResponseWithSuccessMessage('Devices fetched successfully', [
'devices' => $devices
]);
} catch (Exception $e) {
Log::error("An error occurred: " . $e->getMessage());
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
public function deleteDevice($deviceId)
{
try {
if (!$deviceId) {
return jsonResponseWithErrorMessage('Device ID is required', 400);
}
$response = $this->deviceService->deleteDevice(['deviceId' => $deviceId]);
$Device = Device::where('id', $deviceId)->first();
if ($Device) {
$Device->delete();
}
return jsonResponseWithSuccessMessage('Device deleted successfully', ['api_response' => $response]);
} catch (Exception $e) {
Log::error("An error occurred: " . $e->getMessage());
$errorResponse = json_decode($e->getMessage(), true);
if (json_last_error() === JSON_ERROR_NONE) {
return jsonResponseWithErrorMessage($errorResponse['message'] ?? 'Something went wrong', 400, $errorResponse);
}
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
2025-03-12 19:19:13 +05:30
}