diff --git a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php index db20069..f93e510 100644 --- a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php +++ b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php @@ -79,15 +79,12 @@ class AssetadmintController extends Controller // } // } - public function storeAssest(CreateAssetRequest $request) + public function storeAssest(Request $request) { try { - // $additionalInfo = $request->has('additional_info') && is_string($request->additional_info) - // ? json_decode($request->additional_info, true) - // : $request->additional_info; $assetData = [ - 'entity_type' => 'ASSET', // Backend defined - 'createdTime' => now()->timestamp, // Backend defined + 'entity_type' => 'ASSET', + 'createdTime' => now()->timestamp, 'tenantId' => $request->tenant_id ?? Str::uuid()->toString(), 'customerId' => $request->customer_xid, 'name' => $request->name, @@ -96,38 +93,69 @@ class AssetadmintController extends Controller 'assetProfileId' => $request->asset_profile_id ?? Str::uuid()->toString(), 'externalId' => $request->external_id ?? Str::uuid()->toString(), 'version' => $request->version ?? '1.0', - // 'additionalInfo' => $additionalInfo ?? ['description' => 'Default asset description'], 'additionalInfo' => $request->additionalInfo ]; - $response = $this->adminService->createAsset($assetData); - if (!$response) { - throw new \Exception('Failed to create asset via admin service'); + + // Check if ID exists in request for update + if ($request->has('id')) { + $asset = Asset::find($request->id); + if (!$asset) { + throw new \Exception('Asset not found'); + } + + // Update asset via admin service + // $response = $this->adminService->updateAsset($request->id, $assetData); + // if (!$response) { + // throw new \Exception('Failed to update asset via admin service'); + // } + + // Update local database record + $asset->name = $assetData['name']; + $asset->type = $assetData['type']; + $asset->label = $assetData['label']; + $asset->version = $assetData['version']; + $asset->additional_info = json_encode( $assetData['additionalInfo']); + $asset->save(); + + $message = 'Asset updated successfully!'; + } else { + // Create new asset via admin service + $response = $this->adminService->createAsset($assetData); + if (!$response) { + throw new \Exception('Failed to create asset via admin service'); + } + + // Create new local database record + $asset = new Asset(); + $asset->id = $response['id']['id'] ?? Str::uuid()->toString(); + $asset->entity_type = $assetData['entity_type']; + $asset->created_time = $assetData['createdTime']; + $asset->tenant_id = $assetData['tenantId']; + $asset->customer_xid = $assetData['customerId']; + $asset->name = $response['name'] ?? $assetData['name']; + $asset->type = $response['type'] ?? $assetData['type']; + $asset->label = $response['label'] ?? $assetData['label']; + $asset->asset_profile_id = $assetData['assetProfileId']; + $asset->external_id = $assetData['externalId']; + $asset->version = $response['version'] ?? $assetData['version']; + $asset->additional_info = json_encode($response['additionalInfo'] ?? $assetData['additionalInfo']); + $asset->save(); + + $message = 'Asset created successfully!'; } - $asset = new Asset(); - $asset->id = $response['id']['id'] ?? Str::uuid()->toString(); - $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']; - $asset->type = $response['type'] ?? $assetData['type']; - $asset->label = $response['label'] ?? $assetData['label']; - $asset->asset_profile_id = $assetData['assetProfileId']; - $asset->external_id = $assetData['externalId']; - $asset->version = $response['version'] ?? $assetData['version']; - $asset->additional_info = json_encode($response['additionalInfo'] ?? $assetData['additionalInfo']); - // 'additional_info'  => json_encode($apiData['additionalInfo'] ?? [])  - $asset->save(); - // Fetch only the customer name + + // Fetch customer name $customerName = \App\Models\Customer::where('id', $assetData['customerId'])->value('name'); + return response()->json([ - 'message' => 'Asset created successfully!', + 'message' => $message, 'data' => $asset, 'customer_name' => $customerName, - 'api_response' => $response + // 'api_response' => $response ], 200); + } catch (\Exception $e) { - Log::error('Error in creating asset: ' . $e->getMessage()); + Log::error('Error in asset operation: ' . $e->getMessage()); return response()->json(['error' => $e->getMessage()], 500); } } diff --git a/app/Http/Controllers/APIS/AdminApi/DeviceController.php b/app/Http/Controllers/APIS/AdminApi/DeviceController.php index bacb07a..75b1025 100644 --- a/app/Http/Controllers/APIS/AdminApi/DeviceController.php +++ b/app/Http/Controllers/APIS/AdminApi/DeviceController.php @@ -25,10 +25,15 @@ class DeviceController extends Controller } - public function createOrUpdateDevice(CreateDeviceRequest $request) + public function createOrUpdateDevice(Request $request) { try { + $deviceNameExists = Device::where('name', $request->name)->first(); + if (empty($request->id) && $deviceNameExists) { + return jsonResponseWithErrorMessage('Device name already exists', 400); + } + $deviceData = [ 'name' => $request->name ?? null, 'type' => $request->type ?? null, @@ -80,7 +85,10 @@ class DeviceController extends Controller 'name' => $apiResponse['name'] ?? null, 'type' => $apiResponse['type'] ?? null, 'label' => $apiResponse['label'] ?? null, - 'asset_id' => $request->asset_id ?? 'null', + 'asset_id' => $request->asset_id ?? null, + 'speed_limit' => $request->speed_limit ?? null, + 'torque_limit' => $request->torque_limit ?? null, + 'power_limit' => $request->power_limit ?? null, 'device_profile_id' => $apiResponse['deviceProfileId']['id'] ?? null, 'firmware_id' => $apiResponse['firmwareId']['id'] ?? null, 'software_id' => $apiResponse['softwareId']['id'] ?? null, diff --git a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php index 6ee8dfa..c6ba7f4 100644 --- a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php +++ b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php @@ -1306,7 +1306,7 @@ class TelemetryController extends Controller $deviceLiveStatus = $deviceLiveStatus instanceof \Illuminate\Http\JsonResponse ? $deviceLiveStatus->getData(true) : $deviceLiveStatus; // Transform the data using our TelemetryService - $transformed = $this->telemetryService->transformTelemetryData($data, $displayNameMap, $deviceLiveStatus['active']); + $transformed = $this->telemetryService->transformTelemetryData($deviceId, $data, $displayNameMap, $deviceLiveStatus['active']); $response = [ 'success' => true, diff --git a/app/Http/Requests/CreateDeviceRequest.php b/app/Http/Requests/CreateDeviceRequest.php index 04ae6b5..e11df10 100644 --- a/app/Http/Requests/CreateDeviceRequest.php +++ b/app/Http/Requests/CreateDeviceRequest.php @@ -25,12 +25,20 @@ class CreateDeviceRequest extends FormRequest { // Log::info("CreateDeviceRequest: " . json_encode($this->all())); return [ - - 'name' => 'required|string', - // 'type' => 'required|string|max:255', + 'name' => 'required|string|max:255', 'label' => 'required|string|max:255', - 'customerId' => 'required', + 'customerId' => 'required|integer', + 'asset_id' => 'required|string', + ]; + } + public function messages() + { + return [ + 'name.required' => 'Name is required!', + 'label.required' => 'Label is required!', + 'customerId.required' => 'Customer is required!', + 'asset_id.required' => 'Asset is required!', ]; } } diff --git a/app/Models/Device.php b/app/Models/Device.php index e8c485d..a0092ea 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -23,6 +23,9 @@ class Device extends Model 'device_profile_id', 'tenant_id', 'customer_id', + 'speed_limit', + 'torque_limit', + 'power_limit', 'label', 'version', 'active', diff --git a/app/Services/TelemetryService.php b/app/Services/TelemetryService.php index 7f218fc..d658fa4 100644 --- a/app/Services/TelemetryService.php +++ b/app/Services/TelemetryService.php @@ -94,11 +94,12 @@ class TelemetryService ]; } - public function getThresholdLimit(): array + public function getThresholdLimit($deviceId): array { + $device = Device::find($deviceId); return [ 'ChannelSpeed' => [ - ['min' => 0, 'max' => 50000] + ['min' => 0, 'max' => $device->speed_limit] ], 'PowerLoss_value' => [ ['min' => 0, 'max' => 10] @@ -107,10 +108,10 @@ class TelemetryService ['min' => 0, 'max' => 10] ], 'StaticTorque_value' => [ - ['min' => 0, 'max' => 50000] + ['min' => 0, 'max' => $device->torque_limit] ], 'StaticPower_value' => [ - ['min' => 0, 'max' => 500000] + ['min' => 0, 'max' => $device->power_limit] ], 'StaticTorsion_value' => [ ['min' => 0, 'max' => 6] @@ -136,7 +137,7 @@ class TelemetryService return 'Stable'; } - public function transformTelemetryData(array $data, array $displayNameMap, bool $isActive): array + public function transformTelemetryData(string $deviceId, array $data, array $displayNameMap, bool $isActive): array { $transformedTelemetry = []; $dateTime = null; @@ -148,7 +149,7 @@ class TelemetryService $value = $item['value']; $thresholds = $this->getThresholdMap()[$key] ?? $this->getThresholdMap()['default']; - $thresholdLimits = $this->getThresholdLimit()[$key] ?? $this->getThresholdLimit()['default']; + $thresholdLimits = $this->getThresholdLimit($deviceId)[$key] ?? $this->getThresholdLimit()['default']; $status = $this->getHealthStatus($value, $thresholds); $statusColor = match($status) { diff --git a/database/migrations/2025_05_21_110044_add_limit_columns_to_devices_table.php b/database/migrations/2025_05_21_110044_add_limit_columns_to_devices_table.php new file mode 100644 index 0000000..3373286 --- /dev/null +++ b/database/migrations/2025_05_21_110044_add_limit_columns_to_devices_table.php @@ -0,0 +1,30 @@ +integer('speed_limit')->nullable()->after('device_profile_id'); + $table->integer('torque_limit')->nullable()->after('speed_limit'); + $table->integer('power_limit')->nullable()->after('torque_limit'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('devices', function (Blueprint $table) { + $table->dropColumn(['speed_limit', 'torque_limit', 'power_limit']); + }); + } +}; diff --git a/routes/admin_api.php b/routes/admin_api.php index 10be0a0..181d58d 100644 --- a/routes/admin_api.php +++ b/routes/admin_api.php @@ -51,7 +51,7 @@ Route::post('/users-customer-list', [UsersController::class, 'userlistCustomer'] Route::get('/users/{customer_id}', [UsersController::class, 'UserByCustomerId'])->name('usertList.customerId'); //******************************************************* Device API******************************************************** -Route::post('/device/create-or-update', [DeviceController::class, 'createOrUpdateDevice'])->name('device.create-or-update'); +Route::post('/device/create-or-update', [DeviceController::class, 'createOrUpdateDevice']); Route::get('/device/list', [DeviceController::class, 'listDevices'])->name('device.list'); Route::post('/device/delete', [DeviceController::class, 'deleteDevice'])->name('device.delete'); Route::get('/device/{customer_id}', [DeviceController::class, 'devicelistCustomer'])->name('devicetList.customer');