deviceService = $deviceService; } public function createOrUpdateDevice(Request $request) { try { Log::info('Request Data: ', $request->all()); $deviceData = [ 'name' => $request->name, 'type' => $request->type, 'label' => $request->label, 'version' => $request->version ?? 1, 'additionalInfo' => $request->additionalInfo ?? [], 'deviceData' => [ 'configuration' => ['type' => 'DEFAULT'], 'customerId' => [ 'id' => $request->customerId['id'] ?? null, 'entityType' => 'CUSTOMER' ], 'tenantId' => [ 'id' => $request->tenantId['id'] ?? null, 'entityType' => 'TENANT' ], 'transportConfiguration' => [ 'type' => 'DEFAULT', 'powerMode' => 'PSM', 'psmActivityTimer' => $request->transportConfiguration['psmActivityTimer'] ?? 9007199254740991, 'edrxCycle' => $request->transportConfiguration['edrxCycle'] ?? 9007199254740991, 'pagingTransmissionWindow' => $request->transportConfiguration['pagingTransmissionWindow'] ?? 9007199254740991, ] ], // 'deviceProfileId' => [ // 'id' => $request->deviceProfileId['id'] ?? null, // 'entityType' => 'DEVICE_PROFILE' // ], // 'firmwareId' => [ // 'id' => $request->firmwareId['id'] ?? null, // 'entityType' => 'OTA_PACKAGE' // ], // 'softwareId' => [ // 'id' => $request->softwareId['id'] ?? null, // 'entityType' => 'OTA_PACKAGE' // ] ]; if (!empty($request->id) && isset($request->id['id'])) { $deviceData['id'] = [ 'id' => $request->id['id'], 'entityType' => 'DEVICE' ]; } Log::info('Device Data:', ['device_data' => $deviceData]); // Call Service to create/update device $apiResponse = $this->deviceService->createOrUpdateDevice($deviceData); Log::info("API Response Data:", ['response' => $apiResponse]); // if (!is_array($apiResponse)) { // return jsonResponseWithErrorMessage('Unexpected API response format', 500); // } // 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, '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( isset($request->id) ? 'Device updated successfully' : 'Device created successfully', ['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); } } }