rulechain

This commit is contained in:
sayaliparab
2025-03-28 20:01:51 +05:30
parent cddbb815d1
commit fbaced66ae
4 changed files with 220 additions and 27 deletions

View File

@@ -92,32 +92,7 @@ class AssetadmintController extends Controller
}
}
// public function deleteAsset($assetId)
// {
// if (!$assetId) {
// return jsonResponseWithErrorMessage('Asset ID is required', 400);
// }
// $response = $this->adminService->deleteAsset(['assetId' => $assetId]);
// Log::info("Response: " . json_encode($response));
// if (!is_array($response)) {
// Log::error("Unexpected API response format.", ['response' => $response]);
// return jsonResponseWithErrorMessage('Unexpected API response format', 500);
// }
// if (isset($response['status']) && $response['status'] === 400) {
// Log::error("Failed to delete asset: " . $response['message']);
// return jsonResponseWithErrorMessage($response['message'], 400, $response);
// }
// if (empty($response)) {
// Log::error("API Data is empty, cannot delete asset.");
// return jsonResponseWithErrorMessage('Failed to delete asset', 400);
// }
// $asset = Asset::where('id', $assetId)->first();
// if ($asset) {
// $asset->delete();
// }
// return jsonResponseWithSuccessMessage('Asset deleted successfully', ['api_response' => $response]);
// }
public function deleteAsset(Request $request)
{
@@ -127,7 +102,6 @@ class AssetadmintController extends Controller
return jsonResponseWithErrorMessage('Asset ID is required', 400);
}
// Call the service to delete the asset
$response = $this->adminService->deleteAsset(['assetId' => $assetId]);
Log::info("Response: " . json_encode($response));
@@ -242,4 +216,6 @@ class AssetadmintController extends Controller
'Assests' => $assets
]);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace App\Http\Controllers\APIS\AdminApi;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\RuleChainService;
use Exception;
class RuleChainController extends Controller
{
private $ruleChainService;
public function __construct(RuleChainService $ruleChainService)
{
$this->ruleChainService = $ruleChainService;
}
public function getRuleChainList(Request $request)
{
try {
$page = $request->get('page', 0); // Default: first page
$pageSize = $request->get('pageSize', 10); // Default: 10 records per page
$data = $this->ruleChainService->getRuleChains($page, $pageSize);
if (!empty($data['data'])) {
return jsonResponseWithSuccessMessage('Rule chains fetched successfully', $data);
} else {
return jsonResponseWithErrorMessage('No rule chains found', 404);
}
} catch (Exception $e) {
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
public function exportruleChain(Request $request)
{
try {
$limit = $request->get('limit', 10000); // Default limit: 100
$data = $this->ruleChainService->exportRuleChains($limit);
if (!empty($data['ruleChains'])) {
return jsonResponseWithSuccessMessage('Rule chains exported successfully', $data);
} else {
return jsonResponseWithErrorMessage('No rule chains found', 404);
}
} catch (Exception $e) {
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
public function showruleChain($ruleChainId)
{
try {
$data = $this->ruleChainService->getRuleChainById($ruleChainId);
if (!empty($data)) {
return jsonResponseWithSuccessMessage('Rule chain fetched successfully', $data);
} else {
return jsonResponseWithErrorMessage('No rule chain found', 404);
}
} catch (Exception $e) {
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
}

View File

@@ -0,0 +1,136 @@
<?php
namespace App\Services;
use App\Models\TimeseriesKeyMaster;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Exception;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
class RuleChainService
{
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 getRuleChains($page, $pageSize)
{
try {
$token = $this->getToken();
$response = Http::withHeaders([
'accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Authorization' => "Bearer {$token}",
])->get("{$this->baseUrl}/api/ruleChains", [
'page' => $page,
'pageSize' => $pageSize,
'sortProperty' => 'createdTime',
'sortOrder' => 'DESC'
]);
if ($response->successful()) {
return $response->json();
} else {
Log::error("Failed to fetch rule chains: " . $response->body());
throw new Exception('Failed to fetch rule chains: ' . $response->body());
}
} catch (Exception $e) {
Log::error("Error: " . $e->getMessage());
throw new Exception('Error while fetching rule chains: ' . $e->getMessage());
}
}
public function exportRuleChains($limit)
{
try {
$token = $this->getToken();
// Make the export API request
$response = Http::withHeaders([
'accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Authorization' => "Bearer {$token}",
])->get("{$this->baseUrl}/api/ruleChains/export", [
'limit' => $limit
]);
if ($response->successful()) {
return $response->json();
} else {
Log::error("Failed to export rule chains: " . $response->body());
throw new Exception('Failed to export rule chains: ' . $response->body());
}
} catch (Exception $e) {
Log::error("Error exporting rule chains: " . $e->getMessage());
throw new Exception('Error exporting rule chains: ' . $e->getMessage());
}
}
public function getRuleChainById($ruleChainId)
{
try {
$token = $this->getToken();
$response = Http::withHeaders([
'accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Authorization' => "Bearer {$token}",
])->get("{$this->baseUrl}/api/ruleChain/{$ruleChainId}");
if ($response->successful()) {
return $response->json();
} else {
Log::error("Failed to fetch rule chain: " . $response->body());
throw new Exception('Failed to fetch rule chain: ' . $response->body());
}
} catch (Exception $e) {
Log::error("Error fetching rule chain: " . $e->getMessage());
throw new Exception('Error fetching rule chain: ' . $e->getMessage());
}
}
}

View File

@@ -10,7 +10,7 @@ use App\Http\Controllers\APIS\AdminApi\DeviceController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\APIS\AdminApi\AssetadmintController;
use App\Http\Controllers\APIS\AdminApi\RuleChainController;
use App\Http\Controllers\APIS\AdminApi\DeviceProfileMasterController;
use App\Http\Controllers\APIS\AdminApi\LoginController;
@@ -67,3 +67,10 @@ Route::post('/alarm/create-or-update', [AlarmControllerCommon::class, 'createOrU
Route::get('/alarm/{id}', [AlarmControllerCommon::class, 'getAlarmById'])->name('get.alarm');
Route::post('/alarm/ack/{id}', [AlarmControllerCommon::class, 'acknowledgeAlarmById'])->name('ack.alarm');
Route::post('/alarm/filter', [AlarmControllerCommon::class, 'filterAlarm'])->name('alarm.filter');
//******************************************************* Rule Chain API ********************************************************
Route::get('/rule-chains', [RuleChainController::class, 'getRuleChainList'])->name('list.RuleChain');
Route::get('/rule-chains-export', [RuleChainController::class, 'exportruleChain'])->name('export.RuleChain');
Route::get('/rule-chains/{ruleChainId}', [RuleChainController::class, 'showruleChain'])->name('show.RuleChain');