sayali #24
@@ -8,6 +8,7 @@ use App\Http\Requests\CreateAssetRequest;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Services\AdminService;
|
||||
use App\Models\Customer;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -24,11 +25,7 @@ class AssetadmintController extends Controller
|
||||
$this->adminService = $adminService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function storeAssest(Request $request)
|
||||
public function storeAssest(CreateAssetRequest $request)
|
||||
{
|
||||
try {
|
||||
$additionalInfo = $request->has('additional_info') && is_string($request->additional_info)
|
||||
@@ -82,30 +79,6 @@ class AssetadmintController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function listAssest()
|
||||
{
|
||||
try {
|
||||
@@ -119,11 +92,6 @@ class AssetadmintController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function deleteAsset($assetId)
|
||||
{
|
||||
|
||||
@@ -158,10 +126,6 @@ class AssetadmintController extends Controller
|
||||
return jsonResponseWithSuccessMessage('Asset deleted successfully', ['api_response' => $response]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function assignAssetToUser(Request $request)
|
||||
{
|
||||
|
||||
@@ -220,4 +184,32 @@ class AssetadmintController extends Controller
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function customerList()
|
||||
{
|
||||
try {
|
||||
|
||||
$customers = Customer::all();
|
||||
return jsonResponseWithSuccessMessage('Customers fetched successfully', [
|
||||
'customers' => $customers
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error("An error occurred: " . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function assestlistCustomer($customerId)
|
||||
{
|
||||
$assets = Asset::where('customer_xid', $customerId)->get();
|
||||
|
||||
if ($assets->isEmpty()) {
|
||||
return response()->json(['message' => 'No assets found for this customer ID'], 404);
|
||||
}
|
||||
|
||||
return jsonResponseWithSuccessMessage('Assests fetched successfully', [
|
||||
'Assests' => $assets
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\APIS\AdminApi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\DeviceProfileMaster;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Services\AdminDeviceProfileMaster; // Include the service
|
||||
|
||||
use Exception;
|
||||
|
||||
|
||||
|
||||
class DeviceProfileMasterController extends Controller
|
||||
|
||||
{
|
||||
protected $AdminDeviceProfileMaster;
|
||||
|
||||
public function __construct(AdminDeviceProfileMaster $AdminDeviceProfileMaster)
|
||||
{
|
||||
$this->AdminDeviceProfileMaster = $AdminDeviceProfileMaster;
|
||||
}
|
||||
public function deviceprofileMasterList()
|
||||
{
|
||||
try {
|
||||
|
||||
$deviceMaster = DeviceProfileMaster::all();
|
||||
return jsonResponseWithSuccessMessage('device profile master fetched successfully', [
|
||||
'deviceprofilemaster' => $deviceMaster
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error("An error occurred: " . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateDevice(Request $request, $deviceId)
|
||||
{
|
||||
try {
|
||||
$deviceProfileMaster = DeviceProfileMaster::find($deviceId);
|
||||
|
||||
if (!$deviceProfileMaster) {
|
||||
return response()->json(['message' => 'No device found'], 404);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255' // Ensure the name field is provided
|
||||
]);
|
||||
|
||||
$deviceProfileMaster->name = $request->name; // Update the name
|
||||
$deviceProfileMaster->save(); // Save the updated name
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Device name updated successfully',
|
||||
'data' => $deviceProfileMaster
|
||||
], 200);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Failed to update device: ' . $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,11 @@ namespace App\Http\Controllers\APIS\CustomerApi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Customer;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class DeviceController extends Controller
|
||||
{
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
21
app/Models/DeviceProfileMaster.php
Normal file
21
app/Models/DeviceProfileMaster.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class DeviceProfileMaster extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'device_profile_master';
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'name',
|
||||
|
||||
];
|
||||
public $incrementing = false; // Use UUID as primary key
|
||||
protected $keyType = 'string';
|
||||
}
|
||||
55
app/Services/AdminDeviceProfileMaster.php
Normal file
55
app/Services/AdminDeviceProfileMaster.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Asset;
|
||||
use App\Models\DeviceProfileMaster;
|
||||
|
||||
class AdminDeviceProfileMaster
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -94,47 +94,7 @@ class AdminService
|
||||
}
|
||||
}
|
||||
|
||||
// public function createAsset(array $data)
|
||||
// {
|
||||
// $token = $this->getToken();
|
||||
|
||||
// $payload = [
|
||||
// // 'customerId' => [
|
||||
// // 'id' => $data['customerId'] ?? null,
|
||||
// // 'entityType' => 'CUSTOMER'
|
||||
// // ],
|
||||
// 'name' => $data['name'] ?? 'Default Asset',
|
||||
// 'type' => $data['type'] ?? 'Default Type',
|
||||
// 'label' => $data['label'] ?? '',
|
||||
// 'version' => $data['version'] ?? '1.0',
|
||||
// 'additionalInfo' => $data['additionalInfo'] ?? ['description' => 'Default asset description']
|
||||
// ];
|
||||
|
||||
// // Include the IDs only if they are present
|
||||
// if (isset($data['id'])) {
|
||||
// $payload['id'] = $data['id'];
|
||||
// }
|
||||
// if (isset($data['tenantId'])) {
|
||||
// $payload['tenantId'] = [
|
||||
// 'id' => $data['tenantId'],
|
||||
// 'entityType' => 'TENANT'
|
||||
// ];
|
||||
// }
|
||||
// // if (isset($data['assetProfileId'])) {
|
||||
// // $payload['assetProfileId'] = [
|
||||
// // 'id' => $data['assetProfileId'],
|
||||
// // 'entityType' => 'ASSET_PROFILE'
|
||||
// // ];
|
||||
// // }
|
||||
// if (isset($data['externalId'])) {
|
||||
// $payload['externalId'] = [
|
||||
// 'id' => $data['externalId'],
|
||||
// 'entityType' => 'ASSET'
|
||||
// ];
|
||||
// }
|
||||
|
||||
// Log::info('Payload being sent: ', $payload); // ✅ Log the payload for debugging
|
||||
// }
|
||||
public function createOrUpdateCustomer(array $data)
|
||||
{
|
||||
$token = $this->getToken();
|
||||
@@ -263,65 +223,7 @@ class AdminService
|
||||
}
|
||||
}
|
||||
|
||||
// public function assignAssetToCustomer(array $data)
|
||||
// {
|
||||
// $token = $this->getToken();
|
||||
|
||||
// // Validate required parameters
|
||||
// if (!isset($data['assetId']) || empty($data['assetId'])) {
|
||||
// throw new Exception('Asset ID is required.');
|
||||
// }
|
||||
|
||||
// if (!isset($data['customerId']) || empty($data['customerId'])) {
|
||||
// throw new Exception('Customer ID is required.');
|
||||
// }
|
||||
|
||||
// $assetId = $data['assetId'];
|
||||
// $customerId = $data['customerId'];
|
||||
|
||||
// // API request to assign asset to customer
|
||||
// $response = Http::withHeaders([
|
||||
// 'Authorization' => "Bearer $token",
|
||||
// 'Accept' => 'application/json',
|
||||
// 'Content-Type' => 'application/json',
|
||||
// ])->post("{$this->baseUrl}/api/customer/{$customerId}/asset/{$assetId}");
|
||||
|
||||
// // Log the response for debugging purposes
|
||||
// Log::info('Asset Assignment Response: ' . $response->body());
|
||||
|
||||
// // Handle API responses
|
||||
// if ($response->successful()) {
|
||||
// return [
|
||||
// 'success' => true,
|
||||
// 'message' => 'Asset assigned to customer successfully.',
|
||||
// 'data' => $response->json()
|
||||
// ];
|
||||
// }
|
||||
// else {
|
||||
// throw new Exception('Failed to delete asset: ' . $response->body());
|
||||
// }
|
||||
|
||||
// // Handle specific API error responses
|
||||
// // $statusCode = $response->status();
|
||||
// // $errorMessage = $response->json()['message'] ?? 'Unknown error occurred';
|
||||
|
||||
// // switch ($statusCode) {
|
||||
// // case 400:
|
||||
// // throw new Exception("Bad Request: $errorMessage", 400);
|
||||
// // case 401:
|
||||
// // throw new Exception("Unauthorized: $errorMessage", 401);
|
||||
// // case 403:
|
||||
// // throw new Exception("Forbidden: $errorMessage", 403);
|
||||
// // case 404:
|
||||
// // throw new Exception("Not Found: $errorMessage", 404);
|
||||
// // case 429:
|
||||
// // throw new Exception("Too Many Requests: $errorMessage", 429);
|
||||
// // default:
|
||||
// // throw new Exception("Failed to assign asset: $errorMessage", $statusCode);
|
||||
// // }
|
||||
// }
|
||||
|
||||
public function assignAssetToUser(array $data)
|
||||
public function assignAssetToUser(array $data)
|
||||
{
|
||||
$token = $this->getToken();
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ return new class extends Migration
|
||||
$table->id();
|
||||
$table->uuid('user_id');
|
||||
$table->uuid('asset_id');
|
||||
$table->tinyInteger('active')->default(1)->comment('1: Active, 0: Inactive');
|
||||
$table->timestamps();
|
||||
|
||||
// $table->foreign('user_id')->references('id')->on('users');
|
||||
@@ -29,4 +30,4 @@ return new class extends Migration
|
||||
{
|
||||
Schema::dropIfExists('user_asset_link');
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -6,16 +6,19 @@ use App\Http\Controllers\APIS\AdminApi\UsersController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\APIS\AdminApi\AssetadmintController;
|
||||
use App\Http\Controllers\APIS\AdminApi\DeviceProfileMasterController;
|
||||
Route::get('/adminapi', function () {
|
||||
return ('Welcome to admin api routes.');
|
||||
});
|
||||
|
||||
//******************************************************* Assest API********************************************************
|
||||
//******************************************************* Admin Assest API ********************************************************
|
||||
|
||||
Route::post('/asset', [AssetadmintController::class, 'storeAssest'])->name('assest_create');
|
||||
Route::get('/assets-list', [AssetadmintController::class, 'listAssest'])->name('assest_list');
|
||||
Route::delete('/assets-delete/{assetId}', [AssetadmintController::class, 'deleteAsset'])->name('assest_delete');
|
||||
Route::post('/asset-assign', [AssetadmintController::class, 'assignAssetToUser'])->name('assest_assign');
|
||||
Route::post('/asset', [AssetadmintController::class, 'storeAssest'])->name('assest.create');
|
||||
Route::get('/assets-list', [AssetadmintController::class, 'listAssest'])->name('assest.list');
|
||||
Route::delete('/assets-delete/{assetId}', [AssetadmintController::class, 'deleteAsset'])->name('assest.delete');
|
||||
Route::post('/asset-assign', [AssetadmintController::class, 'assignAssetToUser'])->name('assest.assign');
|
||||
Route::get('/customer-list', [AssetadmintController::class, 'customerList'])->name('list.custgomer');
|
||||
Route::get('/assets/{customer_id}', [AssetadmintController::class, 'assestlistCustomer'])->name('assestList.custgomer');
|
||||
|
||||
|
||||
//******************************************************* Customer API********************************************************
|
||||
@@ -32,3 +35,9 @@ Route::get('/users-list', [UsersController::class, 'list'])->name('user_list');
|
||||
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']);
|
||||
|
||||
//******************************************************* 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