sayali #81

Merged
Sayli.Parab merged 2 commits from sayali into main 2025-04-15 08:24:47 +00:00
3 changed files with 69 additions and 38 deletions

View File

@@ -86,13 +86,13 @@ class AssetadmintController extends Controller
? json_decode($request->additional_info, true)
: $request->additional_info;
$assetData = [
'entity_type' => $request->entity_type,
'createdTime' => $request->created_time ?? now()->timestamp,
'entity_type' => 'ASSET', // Backend defined
'createdTime' => now()->timestamp, // Backend defined
'tenantId' => $request->tenant_id ?? Str::uuid()->toString(),
'customerId' => $request->customer_xid,
'name' => $request->name,
'type' => $request->type,
'label' => $request->label,
'type' => $request->type ?? 'Default Type',
'label' => $request->label ?? '',
'assetProfileId' => $request->asset_profile_id ?? Str::uuid()->toString(),
'externalId' => $request->external_id ?? Str::uuid()->toString(),
'version' => $request->version ?? '1.0',
@@ -104,8 +104,8 @@ class AssetadmintController extends Controller
}
$asset = new Asset();
$asset->id = $response['id']['id'] ?? Str::uuid()->toString();
$asset->entity_type = $response['entityType'] ?? $assetData['entity_type'];
$asset->created_time = $response['createdTime'] ?? $assetData['createdTime'];
$asset->entity_type = $assetData['entity_type']; // From backend
$asset->created_time = $assetData['createdTime']; // From backend
$asset->tenant_id = $assetData['tenantId'];
$asset->customer_xid = $assetData['customerId'];
$asset->name = $response['name'] ?? $assetData['name'];
@@ -122,7 +122,7 @@ class AssetadmintController extends Controller
'message' => 'Asset created successfully!',
'data' => $asset,
'customer_name' => $customerName,
// 'api_response' => $response
'api_response' => $response
], 200);
} catch (\Exception $e) {
Log::error('Error in creating asset: ' . $e->getMessage());

View File

@@ -23,8 +23,8 @@ class CreateAssetRequest extends FormRequest
{
return [
// 'id' => 'required|uuid',
'entity_type' => 'required|string|in:ASSET',
'created_time' => 'required|integer',
// 'entity_type' => 'required|string|in:ASSET',
// 'created_time' => 'required|integer',
// 'tenant_id' => 'required|uuid',
'customer_xid' => 'required|uuid',
'name' => 'required|string|max:255',

View File

@@ -49,34 +49,67 @@ class AdminService
}
}
// public function createAsset(array $data)
// {
// $token = $this->getToken();
// $payload = [
// // 'entityType' => $data['entity_type'] ?? 'ASSET',
// // 'createdTime' => $data['createdTime'] ?? now()->timestamp,
// // 'tenantId' => [
// // 'id' => $data['tenantId'] ?? Str::uuid()->toString(),
// // // 'entityType' => 'TENANT'
// // ],
// 'customerId' => [
// 'id' => $data['customerId'] ?? null,
// 'entityType' => 'CUSTOMER'
// ],
// 'name' => $data['name'] ?? 'Default Asset',
// 'type' => $data['type'] ?? 'Default Type',
// 'label' => $data['label'] ?? '',
// // 'assetProfileId' => [
// // 'id' => $data['assetProfileId'] ?? Str::uuid()->toString(),
// // // 'entityType' => 'ASSET_PROFILE'
// // ],
// // 'externalId' => [
// // 'id' => $data['externalId'] ?? Str::uuid()->toString(),
// // 'entityType' => 'ASSET'
// // ],
// 'version' => $data['version'] ?? '1.0',
// 'additionalInfo' => $data['additionalInfo'] ?? ['description' => 'Default asset description']
// ];
// $response = Http::withHeaders([
// 'Authorization' => "Bearer $token",
// 'Accept' => 'application/json',
// 'Content-Type' => 'application/json',
// ])->withBody(json_encode($payload), 'application/json')
// ->post("{$this->baseUrl}/api/asset");
// Log::error('Error in creating asset: ' . $response);
// // dd($response->json());
// if ($response->successful()) {
// return $response->json();
// } else {
// throw new Exception('Failed to create asset: ' . $response->body());
// }
// }
public function createAsset(array $data)
{
$token = $this->getToken();
$payload = [
// 'entityType' => $data['entity_type'] ?? 'ASSET',
// 'createdTime' => $data['createdTime'] ?? now()->timestamp,
// 'tenantId' => [
// 'id' => $data['tenantId'] ?? Str::uuid()->toString(),
// // 'entityType' => 'TENANT'
// ],
'entityType' => $data['entity_type'], // Always 'ASSET' from backend
'createdTime' => $data['createdTime'], // Always from backend
'customerId' => [
'id' => $data['customerId'] ?? null,
'id' => $data['customerId'],
'entityType' => 'CUSTOMER'
],
'name' => $data['name'] ?? 'Default Asset',
'type' => $data['type'] ?? 'Default Type',
'label' => $data['label'] ?? '',
// 'assetProfileId' => [
// 'id' => $data['assetProfileId'] ?? Str::uuid()->toString(),
// // 'entityType' => 'ASSET_PROFILE'
// ],
// 'externalId' => [
// 'id' => $data['externalId'] ?? Str::uuid()->toString(),
// 'entityType' => 'ASSET'
// ],
'version' => $data['version'] ?? '1.0',
'additionalInfo' => $data['additionalInfo'] ?? ['description' => 'Default asset description']
'name' => $data['name'],
'type' => $data['type'],
'label' => $data['label'],
'version' => $data['version'],
'additionalInfo' => $data['additionalInfo'],
];
$response = Http::withHeaders([
@@ -85,11 +118,11 @@ class AdminService
'Content-Type' => 'application/json',
])->withBody(json_encode($payload), 'application/json')
->post("{$this->baseUrl}/api/asset");
Log::error('Error in creating asset: ' . $response);
// dd($response->json());
if ($response->successful()) {
return $response->json();
} else {
Log::error('Error in creating asset: ' . $response->body());
throw new Exception('Failed to create asset: ' . $response->body());
}
}
@@ -196,8 +229,8 @@ class AdminService
'accept' => 'application/json',
'Content-Type' => 'application/json',
])
->timeout(30)
->post("{$this->baseUrl}/api/user", $payload);
->timeout(30)
->post("{$this->baseUrl}/api/user", $payload);
if ($response->successful()) {
return $response->json();
@@ -216,7 +249,6 @@ class AdminService
'message' => 'Failed to create user in ThingsBoard',
'details' => $errorDetails
];
} catch (\Exception $e) {
Log::error('ThingsBoard Service Exception: ' . $e->getMessage());
return [
@@ -256,7 +288,7 @@ class AdminService
}
}
public function assignAssetToUser(array $data)
public function assignAssetToUser(array $data)
{
$token = $this->getToken();
@@ -383,7 +415,6 @@ class AdminService
'data' => $response->json(),
'status' => 200
];
} catch (Exception $e) {
Log::error('ThingsBoard deletion exception: ' . $e->getMessage());
return [
@@ -487,4 +518,4 @@ class AdminService
throw new Exception('Failed to fetch users: ' . $response->body());
}
}
}
}