56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?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());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|