135 lines
4.1 KiB
PHP
135 lines
4.1 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 {
|
|
Log::error("ThingsBoard Authentication Failed: " . $response->body());
|
|
throw new Exception('Unable to authenticate with ThingsBoard: ' . $response->body());
|
|
}
|
|
}
|
|
public function createOrUpdateCustomer(array $data)
|
|
{
|
|
$token = $this->getToken();
|
|
Log::info('Getting data before payload', ['data' => json_encode($data, JSON_PRETTY_PRINT)]);
|
|
|
|
$payload = [
|
|
'title' => $data['title'] ?? 'Default Title',
|
|
'email' => $data['email'] ?? 'default@example.com',
|
|
'country' => $data['country'] ?? 'India',
|
|
'state' => $data['state'] ?? 'Karnataka',
|
|
'city' => $data['city'] ?? 'Bangalore',
|
|
'zip' => $data['zip'] ?? '560001',
|
|
'name' => $data['name'] ?? 'John Doe',
|
|
'address' => $data['address'] ?? '123, 4th Cross, 5th Main',
|
|
'address2' => $data['address2'] ?? 'Near Park',
|
|
'phone' => $data['phone'] ?? '1234567890',
|
|
'version' => $data['version'] ?? '794665488',
|
|
'additionalInfo' => [
|
|
'description' => $data['description'] ?? 'User description'
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($data['id']) && is_array($data['id']) && isset($data['id']['id'])) {
|
|
$payload['id'] = [
|
|
'id' => $data['id']['id'],
|
|
'entityType' => 'CUSTOMER'
|
|
];
|
|
}
|
|
|
|
// Check if `tenantId` exists and assign correctly
|
|
if (!empty($data['tenantId']) && is_array($data['tenantId']) && isset($data['tenantId']['id'])) {
|
|
$payload['tenantId'] = [
|
|
'id' => $data['tenantId']['id'],
|
|
'entityType' => 'TENANT',
|
|
];
|
|
}
|
|
|
|
Log::info('Final Payload:', ['payload' => json_encode($payload, JSON_PRETTY_PRINT)]);
|
|
|
|
|
|
|
|
$url = "{$this->baseUrl}/api/customer";
|
|
$method = 'post';
|
|
// Send request
|
|
$response = Http::withHeaders([
|
|
'Authorization' => "Bearer $token",
|
|
'Accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
])->$method($url, $payload);
|
|
|
|
if ($response->successful()) {
|
|
return $response->json();
|
|
} else {
|
|
throw new Exception('Failed to process customer: ' . $response->body());
|
|
}
|
|
}
|
|
|
|
|
|
public function deleteCustomer(array $data)
|
|
{
|
|
$token = $this->getToken();
|
|
|
|
|
|
$url = "{$this->baseUrl}/api/customer/{$data['customerId']}";
|
|
|
|
$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' => 'Customer deleted successfully'];
|
|
}
|
|
|
|
if ($response->successful()) {
|
|
return $response->json();
|
|
} else {
|
|
throw new Exception('Failed to create customer: ' . $response->body());
|
|
}
|
|
}
|
|
}
|