New fields in create device and update asset/device

This commit is contained in:
Nikhil Kadam
2025-05-21 18:16:45 +05:30
parent e4750c340d
commit 2735a92512
8 changed files with 121 additions and 43 deletions

View File

@@ -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);
}
}

View File

@@ -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,

View File

@@ -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,

View File

@@ -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!',
];
}
}

View File

@@ -23,6 +23,9 @@ class Device extends Model
'device_profile_id',
'tenant_id',
'customer_id',
'speed_limit',
'torque_limit',
'power_limit',
'label',
'version',
'active',

View File

@@ -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) {

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('devices', function (Blueprint $table) {
$table->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']);
});
}
};

View File

@@ -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');