sayliR #27
@@ -46,7 +46,7 @@ class CustomerController extends Controller
|
||||
|
||||
if (!empty($request->id) && is_array($request->id) && isset($request->id['id'])) {
|
||||
$userData['id'] = [
|
||||
'id' => $request->id['id'],
|
||||
'id' => $request->id['id'],
|
||||
'entityType' => 'CUSTOMER',
|
||||
];
|
||||
}
|
||||
@@ -60,7 +60,6 @@ class CustomerController extends Controller
|
||||
|
||||
// Call API to create/update customer
|
||||
$response = $this->adminService->createOrUpdateCustomer($userData);
|
||||
Log::info("API Response: ", $response);
|
||||
|
||||
if (is_array($response)) {
|
||||
$apiData = $response; // If it's an array, use it directly
|
||||
|
||||
@@ -3,12 +3,147 @@
|
||||
namespace App\Http\Controllers\APIS\CustomerApi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\CreateDeviceRequest;
|
||||
use App\Models\Device;
|
||||
use App\Services\DeviceService;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Customer;
|
||||
use Exception;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Exception;
|
||||
|
||||
|
||||
class DeviceController extends Controller
|
||||
{
|
||||
|
||||
|
||||
protected $deviceService;
|
||||
|
||||
public function __construct(DeviceService $deviceService)
|
||||
{
|
||||
$this->deviceService = $deviceService;
|
||||
}
|
||||
|
||||
|
||||
public function createOrUpdateDevice(CreateDeviceRequest $request)
|
||||
{
|
||||
try {
|
||||
|
||||
$deviceData = [
|
||||
'name' => $request->name ?? null,
|
||||
'type' => $request->type ?? null,
|
||||
'label' => $request->label ?? null,
|
||||
'version' => $request->version ?? 1,
|
||||
'customerId' => $request->customerId ?? 1,
|
||||
'additionalInfo' => $request->additionalInfo ?? [],
|
||||
'deviceData' => [
|
||||
'configuration' => ['type' => 'DEFAULT'],
|
||||
'transportConfiguration' => [
|
||||
'type' => 'DEFAULT',
|
||||
'powerMode' => $request->transportConfiguration['powerMode'] ?? 'PSM',
|
||||
'psmActivityTimer' => $request->transportConfiguration['psmActivityTimer'] ?? 9007199254740991,
|
||||
'edrxCycle' => $request->transportConfiguration['edrxCycle'] ?? 9007199254740991,
|
||||
'pagingTransmissionWindow' => $request->transportConfiguration['pagingTransmissionWindow'] ?? 9007199254740991,
|
||||
]
|
||||
],
|
||||
'deviceProfileId' => $request->deviceProfileId ?? 1,
|
||||
|
||||
];
|
||||
|
||||
// Optional Fields
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
// Call Service to create/update device
|
||||
$apiResponse = $this->deviceService->createOrUpdateDevice($deviceData);
|
||||
|
||||
// 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,
|
||||
'asset_id' => $apiResponse['asset_id'] ?? 'a5daeb60-f36c-11ef-a9dc-45dd276e4cd5',
|
||||
'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(
|
||||
!empty($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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,12 +24,7 @@ class CreateCustomerRequest extends FormRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
Log::info("CreateCustomerRequest: " . json_encode($this->all()));
|
||||
return [
|
||||
// 'id' => 'required|array',
|
||||
// 'id.id' => 'required|uuid',
|
||||
// 'id.entityType' => 'required|string|in:CUSTOMER',
|
||||
|
||||
'country' => 'required|string',
|
||||
'state' => 'required|string|max:255',
|
||||
'city' => 'required|string|max:255',
|
||||
@@ -38,16 +33,7 @@ class CreateCustomerRequest extends FormRequest
|
||||
'zip' => 'required|string|max:20',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'email' => 'required|email|max:255',
|
||||
|
||||
'title' => 'required|string|max:255',
|
||||
|
||||
// 'tenantId' => 'required',
|
||||
// 'tenantId.id' => 'required|uuid',
|
||||
// 'tenantId.entityType' => 'required|string|in:TENANT',
|
||||
|
||||
// 'version' => 'required|integer|min:1',
|
||||
|
||||
// 'additionalInfo' => 'nullable|array',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
36
app/Http/Requests/CreateDeviceRequest.php
Normal file
36
app/Http/Requests/CreateDeviceRequest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
|
||||
class CreateDeviceRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
// Log::info("CreateDeviceRequest: " . json_encode($this->all()));
|
||||
return [
|
||||
|
||||
'name' => 'required|string',
|
||||
'type' => 'required|string|max:255',
|
||||
'label' => 'required|string|max:255',
|
||||
'customerId' => 'required',
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ class Device extends Model
|
||||
'tenant_id',
|
||||
'customer_id',
|
||||
'name',
|
||||
'asset_id',
|
||||
'type',
|
||||
'label',
|
||||
'device_profile_id',
|
||||
|
||||
@@ -98,7 +98,7 @@ class AdminService
|
||||
public function createOrUpdateCustomer(array $data)
|
||||
{
|
||||
$token = $this->getToken();
|
||||
Log::info('Getting data before payload', ['data' => json_encode($data, JSON_PRETTY_PRINT)]);
|
||||
// Log::info('Getting data before payload', ['data' => json_encode($data, JSON_PRETTY_PRINT)]);
|
||||
|
||||
$payload = [
|
||||
'title' => $data['title'] ?? 'Default Title',
|
||||
@@ -135,7 +135,7 @@ class AdminService
|
||||
];
|
||||
}
|
||||
|
||||
Log::info('Final Payload:', ['payload' => json_encode($payload, JSON_PRETTY_PRINT)]);
|
||||
// Log::info('Final Payload:', ['payload' => json_encode($payload, JSON_PRETTY_PRINT)]);
|
||||
|
||||
|
||||
|
||||
|
||||
148
app/Services/DeviceService.php
Normal file
148
app/Services/DeviceService.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
|
||||
class DeviceService
|
||||
{
|
||||
private $baseUrl;
|
||||
private $username;
|
||||
private $password;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->baseUrl = env('THINGSBOARD_URL', 'http://65.0.131.117:8080');
|
||||
$this->username = env('THINGSBOARD_USERNAME', 'tenant1@thingsboard.org');
|
||||
$this->password = env('THINGSBOARD_PASSWORD', 'tenant1');
|
||||
}
|
||||
|
||||
public function getToken()
|
||||
{
|
||||
if (Cache::has('thingsboard_token')) {
|
||||
return Cache::get('thingsboard_token');
|
||||
}
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
])
|
||||
->post("{$this->baseUrl}/api/auth/login", [
|
||||
'username' => $this->username,
|
||||
'password' => $this->password,
|
||||
]);
|
||||
|
||||
|
||||
if ($response->successful()) {
|
||||
$token = $response->json('token');
|
||||
Cache::put('thingsboard_token', $token, now()->addMinutes(15));
|
||||
return $token;
|
||||
} else {
|
||||
Log::error("ThingsBoard Authentication Failed: " . $response->body());
|
||||
throw new Exception('Unable to authenticate with ThingsBoard: ' . $response->body());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function createOrUpdateDevice(array $data)
|
||||
{
|
||||
|
||||
$token = $this->getToken();
|
||||
|
||||
$payload = [
|
||||
'name' => $data['name'] ?? null,
|
||||
'type' => $data['type'] ?? null,
|
||||
'label' => $data['label'] ?? null,
|
||||
'version' => $data['version'] ?? 1,
|
||||
'deviceProfileId' => isset($data['deviceProfileId']['id']) ? $data['deviceProfileId'] : null,
|
||||
'customerId' => [
|
||||
'id' => $data['customerId'] ?? null,
|
||||
'entityType' => 'CUSTOMER'
|
||||
],
|
||||
'deviceProfileId' => [
|
||||
'id' => $data['deviceProfileId'] ?? null,
|
||||
'entityType' => 'DEVICE_PROFILE'
|
||||
],
|
||||
'deviceData' => $data['deviceData'] ?? [],
|
||||
'additionalInfo' => $data['additionalInfo'] ?? [],
|
||||
];
|
||||
|
||||
|
||||
// Optional fields
|
||||
if (!empty($data['id'])) {
|
||||
$payload['id'] = [
|
||||
'id' => $data['id'],
|
||||
'entityType' => 'DEVICE'
|
||||
];
|
||||
}
|
||||
if (!empty($data['firmwareId'])) {
|
||||
$payload['firmwareId'] = [
|
||||
'id' => $data['firmwareId'],
|
||||
'entityType' => 'OTA_PACKAGE'
|
||||
];
|
||||
}
|
||||
if (!empty($data['softwareId'])) {
|
||||
$payload['softwareId'] = [
|
||||
'id' => $data['softwareId'],
|
||||
'entityType' => 'OTA_PACKAGE'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$url = "{$this->baseUrl}/api/device";
|
||||
$method = 'post';
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => "Bearer $token",
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
])->post($url, $payload);
|
||||
|
||||
if (!$response->successful()) {
|
||||
Log::error("API Error Response:", ['body' => $response->body()]);
|
||||
throw new Exception('API Error: ' . $response->body());
|
||||
}
|
||||
|
||||
$apiResponse = $response->json();
|
||||
|
||||
return $apiResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function deleteDevice(array $data)
|
||||
{
|
||||
$token = $this->getToken();
|
||||
|
||||
|
||||
$url = "{$this->baseUrl}/api/device/{$data['deviceId']}";
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => "Bearer $token",
|
||||
'Accept' => 'application/json',
|
||||
])->delete($url);
|
||||
|
||||
|
||||
|
||||
// If response body is empty, assume success
|
||||
if ($response->status() === 200 && empty($response->body())) {
|
||||
return ['message' => 'Device deleted successfully'];
|
||||
}
|
||||
|
||||
if ($response->successful()) {
|
||||
return $response->json();
|
||||
} else {
|
||||
throw new Exception('Failed to create Device: ' . $response->body());
|
||||
Log::info('API Response:', ['response' => $response]);
|
||||
}
|
||||
|
||||
throw new Exception('Failed to create user: ' . $response->body());
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// use App\Http\Controllers\AssetController;
|
||||
use App\Http\Controllers\APIS\CustomerApi\CustomerController;
|
||||
use App\Http\Controllers\APIS\AdminApi\UsersController;
|
||||
use App\Http\Controllers\APIS\CustomerApi\DeviceController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\APIS\AdminApi\AssetadmintController;
|
||||
@@ -36,6 +37,14 @@ Route::delete('/users-delete/{userId}', [UsersController::class, 'delete']);
|
||||
Route::post('/activate/{id}', [UsersController::class, 'activate'])->name('activate.user');
|
||||
Route::post('/users-login', [UsersController::class, 'loginUser']);
|
||||
|
||||
|
||||
|
||||
//******************************************************* Device API********************************************************
|
||||
Route::post('/device/create-or-update', [DeviceController::class, 'createOrUpdateDevice'])->name('device.create-or-update');
|
||||
Route::get('/device/list', [DeviceController::class, 'listDevices'])->name('device.list');
|
||||
Route::delete('/device/delete/{deviceId}', [DeviceController::class, 'deleteDevice'])->name('device.delete');
|
||||
|
||||
|
||||
//******************************************************* Admin DeviceProfileMaster API ********************************************************
|
||||
Route::get('/device-profile-master/list', [DeviceProfileMasterController::class, 'deviceprofileMasterList'])->name('deviceMaster.list');
|
||||
Route::post('/update-device-profile-master/{deviceId}', [DeviceProfileMasterController::class, 'updateDevice'])->name('update.deviceMaster');
|
||||
|
||||
Reference in New Issue
Block a user