127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class AdminService
|
|
{
|
|
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 {
|
|
throw new Exception('Unable to authenticate with ThingsBoard: ' . $response->body());
|
|
}
|
|
}
|
|
|
|
public function createUser(array $data)
|
|
{
|
|
$token = $this->getToken();
|
|
|
|
|
|
$payload = [
|
|
'email' => $data['email'] ?? 'default@example.com',
|
|
'tenantId' => [
|
|
'id' => $data['tenant_id'] ?? '6e9b7fde-0ca0-4d19-9d2a-fba98e3e12a0',
|
|
'entityType' => 'TENANT'
|
|
],
|
|
'customerId' => [
|
|
'id' => $data['customerId'],
|
|
'entityType' => 'CUSTOMER'
|
|
],
|
|
'authority' => $data['authority'] ?? 'TENANT_ADMIN',
|
|
'name' => $data['name'] ?? 'John Doe',
|
|
'phone' => $data['phone'] ?? '1234567890',
|
|
'additionalInfo' => [
|
|
'description' => $data['description'] ?? 'User description'
|
|
]
|
|
];
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => "Bearer $token",
|
|
'accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
])->withBody(json_encode($payload), 'application/json')
|
|
->post("{$this->baseUrl}/api/user");
|
|
|
|
if ($response->successful()) {
|
|
return $response->json();
|
|
} else {
|
|
throw new Exception('Failed to create user: ' . $response->body());
|
|
}
|
|
}
|
|
|
|
public function listUsers()
|
|
{
|
|
$token = $this->getToken();
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => "Bearer $token",
|
|
'accept' => 'application/json',
|
|
])->get("{$this->baseUrl}/api/users?pageSize=100&page=0");
|
|
|
|
if ($response->successful()) {
|
|
return $response->json();
|
|
} else {
|
|
throw new Exception('Failed to fetch users: ' . $response->body());
|
|
}
|
|
}
|
|
|
|
|
|
public function deleteUser($userId)
|
|
{
|
|
$token = $this->getToken();
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => "Bearer $token",
|
|
'accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
])->delete("{$this->baseUrl}/api/user/{$userId}");
|
|
|
|
// Handle ThingsBoard API errors
|
|
if ($response->failed()) {
|
|
Log::error('Failed to delete user: ' . $response->body());
|
|
|
|
// Return the ThingsBoard error message
|
|
return $response->json();
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|