2025-03-11 17:15:41 +05:30
|
|
|
<?php
|
|
|
|
|
|
2025-03-26 11:37:58 +05:30
|
|
|
namespace App\Http\Controllers\APIS\AdminApi;
|
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;
|
2025-05-27 19:06:33 +05:30
|
|
|
use App\Models\DeviceProfileMaster;
|
2025-03-20 12:01:01 +05:30
|
|
|
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-26 16:53:02 +05:30
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
2025-03-20 12:01:01 +05:30
|
|
|
|
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-05-21 18:16:45 +05:30
|
|
|
public function createOrUpdateDevice(Request $request)
|
2025-03-20 12:01:01 +05:30
|
|
|
{
|
|
|
|
|
try {
|
2025-03-21 19:24:02 +05:30
|
|
|
|
2025-05-21 18:16:45 +05:30
|
|
|
$deviceNameExists = Device::where('name', $request->name)->first();
|
|
|
|
|
if (empty($request->id) && $deviceNameExists) {
|
2025-06-19 17:17:12 +05:30
|
|
|
return response()->json(['status' => 'error','message' => 'Device name already exists'],400);
|
2025-05-21 18:16:45 +05:30
|
|
|
}
|
|
|
|
|
|
2025-03-20 12:01:01 +05:30
|
|
|
$deviceData = [
|
2025-05-27 19:06:33 +05:30
|
|
|
'id' => $request->id ?? null,
|
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-04-22 15:53:42 +05:30
|
|
|
'asset_id' => $request->asset_id ?? 'null',
|
2025-03-21 19:24:02 +05:30
|
|
|
|
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-05-27 19:06:33 +05:30
|
|
|
// dd($request->id);
|
2025-03-21 19:24:02 +05:30
|
|
|
// Handle updating existing devices
|
2025-06-12 15:57:33 +05:30
|
|
|
if (!empty($request->id)) {
|
2025-03-21 19:24:02 +05:30
|
|
|
$deviceData['id'] = $request->id;
|
2025-06-12 15:57:33 +05:30
|
|
|
|
|
|
|
|
$deviceNameExists = Device::where('name', $request->name)->where('id','!=',$request->id)->first();
|
|
|
|
|
if ($deviceNameExists) {
|
2025-06-19 17:17:12 +05:30
|
|
|
return response()->json(['status' => 'error','message' => 'Device name already exists'],400);
|
2025-06-12 15:57:33 +05:30
|
|
|
}
|
2025-03-21 19:24:02 +05:30
|
|
|
}
|
|
|
|
|
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
|
2025-05-27 19:06:33 +05:30
|
|
|
if (empty($request->id)) {
|
|
|
|
|
$apiResponse = $this->deviceService->createOrUpdateDevice($deviceData);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
// Log::info("API Response: " . json_encode($apiResponse));
|
2025-03-20 12:01:01 +05:30
|
|
|
|
2025-05-27 19:06:33 +05:30
|
|
|
$deviceProfileName = DeviceProfileMaster::where('id', $request->deviceProfileId)->first();
|
|
|
|
|
|
2025-03-20 12:01:01 +05:30
|
|
|
// Store device in the database
|
|
|
|
|
$device = Device::updateOrCreate(
|
2025-05-27 19:06:33 +05:30
|
|
|
['id' => $apiResponse['id']['id'] ?? $request->id],
|
2025-03-20 12:01:01 +05:30
|
|
|
[
|
2025-05-27 19:06:33 +05:30
|
|
|
'entity_type' => 'DEVICE',
|
|
|
|
|
'created_time' => now()->timestamp,
|
|
|
|
|
'customer_id' => $request->customerId ?? null,
|
|
|
|
|
'name' => $request->name ?? null,
|
|
|
|
|
'type' => $deviceProfileName['name'] ?? null,
|
|
|
|
|
'label' => $request->label ?? null,
|
2025-05-21 18:16:45 +05:30
|
|
|
'asset_id' => $request->asset_id ?? null,
|
|
|
|
|
'speed_limit' => $request->speed_limit ?? null,
|
|
|
|
|
'torque_limit' => $request->torque_limit ?? null,
|
|
|
|
|
'power_limit' => $request->power_limit ?? null,
|
2025-05-27 19:06:33 +05:30
|
|
|
'device_profile_id' => $request->deviceProfileId ?? null,
|
|
|
|
|
'firmware_id' => $request->firmware_id ?? null,
|
|
|
|
|
'software_id' => $request->software_id ?? null,
|
|
|
|
|
'version' => $request->version ?? 1,
|
|
|
|
|
'tenant_id' => $request->tenant_id ?? null,
|
|
|
|
|
'device_data' => json_encode($request->device_data ?? []),
|
|
|
|
|
'additional_info' => $request->additionalInfo
|
2025-03-20 12:01:01 +05:30
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return jsonResponseWithSuccessMessage(
|
2025-03-21 19:24:02 +05:30
|
|
|
!empty($request->id) ? 'Device updated successfully' : 'Device created successfully',
|
2025-05-27 19:06:33 +05:30
|
|
|
['device' => $device]
|
2025-03-20 12:01:01 +05:30
|
|
|
);
|
|
|
|
|
} 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-08 12:52:01 +05:30
|
|
|
// public function listDevices(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
// try {
|
|
|
|
|
// $devices = Device::with('deviceProfile','customer');
|
|
|
|
|
|
|
|
|
|
// return jsonResponseWithSuccessMessage('Devices fetched successfully', [
|
|
|
|
|
// 'devices' => $devices
|
|
|
|
|
// ]);
|
|
|
|
|
// } catch (Exception $e) {
|
|
|
|
|
// Log::error("An error occurred: " . $e->getMessage());
|
|
|
|
|
// return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2025-03-21 19:24:02 +05:30
|
|
|
|
2025-04-08 12:52:01 +05:30
|
|
|
public function listDevices(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-05-15 19:17:47 +05:30
|
|
|
$devices = Device::with('deviceProfile', 'customer')->orderBy('created_at', 'desc')->get()->map(function ($device) {
|
2025-04-08 12:52:01 +05:30
|
|
|
$deviceData = $device->toArray();
|
|
|
|
|
unset($deviceData['device_profile'], $deviceData['customer']); // remove full relations
|
|
|
|
|
|
|
|
|
|
$deviceData['device_profile_name'] = $device->deviceProfile?->name;
|
|
|
|
|
$deviceData['customer_name'] = $device->customer?->name;
|
|
|
|
|
|
|
|
|
|
return $deviceData;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return jsonResponseWithSuccessMessage('Devices fetched successfully', [
|
|
|
|
|
'devices' => $devices
|
|
|
|
|
]);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Log::error("An error occurred: " . $e->getMessage());
|
|
|
|
|
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
2025-03-21 19:24:02 +05:30
|
|
|
}
|
2025-04-08 12:52:01 +05:30
|
|
|
}
|
2025-03-21 19:24:02 +05:30
|
|
|
|
|
|
|
|
|
2025-03-26 16:53:02 +05:30
|
|
|
public function deleteDevice(Request $request)
|
2025-03-21 19:24:02 +05:30
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try {
|
2025-03-26 16:53:02 +05:30
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
|
|
|
|
|
'device_id' => 'required|string',
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
2025-03-21 19:24:02 +05:30
|
|
|
}
|
2025-03-26 16:53:02 +05:30
|
|
|
$deviceId = $request->input('device_id');
|
|
|
|
|
|
2025-03-21 19:24:02 +05:30
|
|
|
$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-26 11:37:58 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
public function devicelistCustomer($customerId)
|
2025-04-24 13:32:49 +05:30
|
|
|
{
|
2025-03-26 11:37:58 +05:30
|
|
|
try {
|
2025-04-08 13:40:11 +05:30
|
|
|
$devices = Device::with('deviceProfile:id,name', 'customer:id,name')
|
|
|
|
|
->where('customer_id', $customerId)
|
2025-06-12 15:57:33 +05:30
|
|
|
->orderBy('created_at','desc')
|
2025-04-08 13:40:11 +05:30
|
|
|
->get()
|
|
|
|
|
->map(function ($device) {
|
|
|
|
|
$deviceArray = $device->toArray();
|
2025-03-26 11:37:58 +05:30
|
|
|
|
2025-04-08 13:40:11 +05:30
|
|
|
unset($deviceArray['device_profile'], $deviceArray['customer']);
|
|
|
|
|
|
|
|
|
|
$deviceArray['customer_name'] = optional($device->customer)->name;
|
|
|
|
|
$deviceArray['device_profile_name'] = optional($device->deviceProfile)->name;
|
|
|
|
|
|
|
|
|
|
return $deviceArray;
|
|
|
|
|
});
|
2025-03-26 11:37:58 +05:30
|
|
|
|
|
|
|
|
if ($devices->isEmpty()) {
|
2025-04-24 13:32:49 +05:30
|
|
|
return response()->json(['message' => 'No devices found for this customer ID'], 200);
|
2025-03-26 11:37:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return jsonResponseWithSuccessMessage('Devices fetched successfully', [
|
2025-04-08 13:40:11 +05:30
|
|
|
'devices' => $devices
|
2025-03-26 11:37:58 +05:30
|
|
|
]);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Log::error("An error occurred in customer device listing: " . $e->getMessage());
|
|
|
|
|
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-08 13:40:11 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-12 19:19:13 +05:30
|
|
|
}
|