diff --git a/src/Entities/Asset.php b/src/Entities/Asset.php index 06858b9..bb8c2a5 100644 --- a/src/Entities/Asset.php +++ b/src/Entities/Asset.php @@ -370,4 +370,85 @@ public function getTenantAsset(string $name = null): static return $this->fill($asset); } + + /** + * Creates assignment of an existing asset to an instance of The Edge. + * Assignment works in async way - first, notification event pushed to edge service queue on platform. + * Second, remote edge service will receive a copy of assignment asset + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once asset will be delivered to edge service, it's going to be available for usage on remote edge instance. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function assignAssetToEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'assetId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $asset = $this->api()->post("edge/{$edgeId}/asset/{$id}")->json(); + + return $this->fill($asset); + } + + /** + * Clears assignment of the asset to the edge. + * Unassignment works in async way - first, 'unassign' notification event pushed to edge queue on platform. + * Second, remote edge service will receive an 'unassign' command to remove asset + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once 'unassign' command will be delivered to edge service, it's going to remove asset locally. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function unassignAssetFromEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'assetId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $asset = $this->api()->delete("edge/{$edgeId}/asset/{$id}")->json(); + + return $this->fill($asset); + } + + /** + * Returns a page of assets assigned to edge. + * You can specify parameters to filter the results. + * The result is wrapped with PageData object that allows you to iterate over result set using pagination. + * See the 'Model' tab of the Response Class for more details. + * + * @param PaginationArguments $paginationArguments + * @param string|null $edgeId + * @param string|null $type + * @return LengthAwarePaginator + * + * @author JalalLinuX + * + * @group TENANT_ADMIN | CUSTOMER_USER + */ + public function getEdgeAssets(PaginationArguments $paginationArguments, string $edgeId = null, string $type = null): LengthAwarePaginator + { + $edgeId = $edgeId ?? $this->forceAttribute('id')->id; + $paginationArguments->validateSortProperty(EnumAssetSortProperty::class); + + $response = $this->api()->get("edge/{$edgeId}/assets", $paginationArguments->queryParams([ + 'type' => $type ?? @$this->type, + ])); + + return $this->paginatedResponse($response, $paginationArguments); + } } diff --git a/src/Entities/Dashboard.php b/src/Entities/Dashboard.php index 2fbd7ae..a0cf614 100644 --- a/src/Entities/Dashboard.php +++ b/src/Entities/Dashboard.php @@ -393,4 +393,83 @@ public function removeDashboardCustomers(array $customerIds, string $id = null): return $this->fill($dashboard); } + + /** + * Creates assignment of an existing dashboard to an instance of The Edge. + * Assignment works in async way - first, notification event pushed to edge service queue on platform. + * Second, remote edge service will receive a copy of assignment dashboard + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once dashboard will be delivered to edge service, it's going to be available for usage on remote edge instance. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function assignDashboardToEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'dashboardId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $dashboard = $this->api()->post("edge/{$edgeId}/dashboard/{$id}")->json(); + + return $this->fill($dashboard); + } + + /** + * Clears assignment of the dashboard to the edge. + * Unassignment works in async way - first, 'unassign' notification event pushed to edge queue on platform. + * Second, remote edge service will receive an 'unassign' command to remove dashboard + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once 'unassign' command will be delivered to edge service, it's going to remove dashboard locally. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function unassignDashboardFromEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'dashboardId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $dashboard = $this->api()->delete("edge/{$edgeId}/dashboard/{$id}")->json(); + + return $this->fill($dashboard); + } + + /** + * Returns a page of dashboard info objects assigned to the specified edge. + * The Dashboard Info object contains lightweight information about the dashboard (e.g. title, image, assigned customers) but does not contain the heavyweight configuration JSON. + * You can specify parameters to filter the results. + * The result is wrapped with PageData object that allows you to iterate over result set using pagination. + * See the 'Model' tab of the Response Class for more details. + * + * @param PaginationArguments $paginationArguments + * @param string|null $edgeId + * @return LengthAwarePaginator + * + * @author JalalLinuX + * + * @group TENANT_ADMIN | CUSTOMER_USER + */ + public function getEdgeDashboards(PaginationArguments $paginationArguments, string $edgeId = null): LengthAwarePaginator + { + $edgeId = $edgeId ?? $this->forceAttribute('id')->id; + $paginationArguments->validateSortProperty(EnumDashboardSortProperty::class); + + $response = $this->api()->get("edge/{$edgeId}/dashboards", $paginationArguments->queryParams()); + + return $this->paginatedResponse($response, $paginationArguments); + } } diff --git a/src/Entities/Device.php b/src/Entities/Device.php index 5825579..71e2f54 100644 --- a/src/Entities/Device.php +++ b/src/Entities/Device.php @@ -390,4 +390,88 @@ public function assignDeviceToPublicCustomer(string $id = null): static return $this->fill($device); } + + /** + * Creates assignment of an existing device to an instance of The Edge. + * Assignment works in async way - first, notification event pushed to edge service queue on platform. + * Second, remote edge service will receive a copy of assignment device. + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once device will be delivered to edge service, it's going to be available for usage on remote edge instance. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function assignDeviceToEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'deviceId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $device = $this->api()->post("edge/{$edgeId}/device/{$id}")->json(); + + return $this->fill($device); + } + + /** + * Clears assignment of the device to the edge. + * Unassignment works in async way - first, 'unassign' notification event pushed to edge queue on platform. + * Second, remote edge service will receive an 'unassign' command to remove device + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once 'unassign' command will be delivered to edge service, it's going to remove device locally. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function unassignDeviceFromEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'deviceId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $device = $this->api()->delete("edge/{$edgeId}/device/{$id}")->json(); + + return $this->fill($device); + } + + /** + * Returns a page of devices assigned to edge. + * You can specify parameters to filter the results. + * The result is wrapped with PageData object that allows you to iterate over result set using pagination. + * See the 'Model' tab of the Response Class for more details. + * + * @param PaginationArguments $paginationArguments + * @param string|null $edgeId + * @param string|null $deviceProfileId + * @param bool|null $active + * @param string|null $type + * @return LengthAwarePaginator + * + * @author JalalLinuX + * + * @group TENANT_ADMIN | CUSTOMER_USER + */ + public function getEdgeDevices(PaginationArguments $paginationArguments, string $edgeId = null, string $deviceProfileId = null, bool $active = null, string $type = null): LengthAwarePaginator + { + $edgeId = $edgeId ?? $this->forceAttribute('id')->id; + $paginationArguments->validateSortProperty(EnumDeviceSortProperty::class); + + $response = $this->api()->get("edge/{$edgeId}/devices", $paginationArguments->queryParams([ + 'active' => $active ?? @$this->active, 'type' => $type ?? @$this->type, + 'deviceProfileId' => $deviceProfileId ?? @$this->deviceProfileId->id, + ])); + + return $this->paginatedResponse($response, $paginationArguments); + } } diff --git a/src/Entities/Edge.php b/src/Entities/Edge.php index 3d1f2a6..96d7f7e 100644 --- a/src/Entities/Edge.php +++ b/src/Entities/Edge.php @@ -300,7 +300,7 @@ public function getCustomerEdges(string $customerId, PaginationArguments $pagina */ public function setEdgeRootRuleChain(string $ruleChainId, string $id = null): static { - $id = $id ?? $this->forceAttribute('id'); + $id = $id ?? $this->forceAttribute('id')->id; Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'edgeId']); Thingsboard::validation(! Str::isUuid($ruleChainId), 'uuid', ['attribute' => 'ruleChainId']); diff --git a/src/Entities/EdgeEvent.php b/src/Entities/EdgeEvent.php new file mode 100644 index 0000000..ffdf02f --- /dev/null +++ b/src/Entities/EdgeEvent.php @@ -0,0 +1,78 @@ + 'array', + 'tenantId' => CastId::class, + 'createdTime' => 'timestamp', + 'edgeId' => CastId::class, + 'action' => EnumEdgeEventAction::class, + 'type' => EnumEdgeEventType::class, + 'body' => 'array', + ]; + + public function entityType(): ?EnumEntityType + { + return EnumEntityType::EDGE_EVENT(); + } + + /** + * Returns a page of edge events for the requested edge. + * You can specify parameters to filter the results. + * The result is wrapped with PageData object that allows you to iterate over result set using pagination. + * See the 'Model' tab of the Response Class for more details. + * + * @param PaginationArguments $paginationArguments + * @param string $edgeId + * @return LengthAwarePaginator + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function getEdgeEvents(PaginationArguments $paginationArguments, string $edgeId): LengthAwarePaginator + { + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $response = $this->api()->get("edge/{$edgeId}/events", $paginationArguments->queryParams()); + + return $this->paginatedResponse($response, $paginationArguments); + } +} diff --git a/src/Entities/RuleChain.php b/src/Entities/RuleChain.php index e9abf88..6e481b8 100644 --- a/src/Entities/RuleChain.php +++ b/src/Entities/RuleChain.php @@ -333,4 +333,85 @@ public function getLatestRuleNodeDebugInput(string $id = null): ?array return $this->api()->get("ruleNode/{$id}/debugIn")->json(); } + + /** + * Creates assignment of an existing rule chain to an instance of The Edge. + * Assignment works in async way - first, notification event pushed to edge service queue on platform. + * Second, remote edge service will receive a copy of assignment rule chain + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once rule chain will be delivered to edge service, it's going to start processing messages locally. + * Only rule chain with type 'EDGE' can be assigned to edge. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function assignRuleChainToEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'ruleChainId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $ruleChain = $this->api()->post("edge/{$edgeId}/ruleChain/{$id}")->json(); + + return $this->fill($ruleChain); + } + + /** + * Clears assignment of the rule chain to the edge. + * Unassignment works in async way - first, 'unassign' notification event pushed to edge queue on platform. + * Second, remote edge service will receive an 'unassign' command to remove rule chain + * (Edge will receive this instantly, if it's currently connected, or once it's going to be connected to platform). + * Third, once 'unassign' command will be delivered to edge service, it's going to remove rule chain locally. + * + * @param string $edgeId + * @param string|null $id + * @return $this + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function unassignRuleChainFromEdge(string $edgeId, string $id = null): static + { + $id = $id ?? $this->forceAttribute('id')->id; + + Thingsboard::validation(! Str::isUuid($id), 'uuid', ['attribute' => 'ruleChainId']); + Thingsboard::validation(! Str::isUuid($edgeId), 'uuid', ['attribute' => 'edgeId']); + + $ruleChain = $this->api()->delete("edge/{$edgeId}/ruleChain/{$id}")->json(); + + return $this->fill($ruleChain); + } + + /** + * Returns a page of Rule Chains assigned to the specified edge. + * The rule chain object is lightweight and contains general information about the rule chain. + * List of rule nodes and their connection is stored in a separate 'metadata' object. + * You can specify parameters to filter the results. + * The result is wrapped with PageData object that allows you to iterate over result set using pagination. + * See the 'Model' tab of the Response Class for more details. + * + * @param PaginationArguments $paginationArguments + * @param string|null $edgeId + * @return LengthAwarePaginator + * + * @author JalalLinuX + * + * @group TENANT_ADMIN + */ + public function getEdgeRuleChains(PaginationArguments $paginationArguments, string $edgeId = null): LengthAwarePaginator + { + $edgeId = $edgeId ?? $this->forceAttribute('id')->id; + $paginationArguments->validateSortProperty(EnumRuleChainSortProperty::class); + + $response = $this->api()->get("edge/{$edgeId}/ruleChains", $paginationArguments->queryParams()); + + return $this->paginatedResponse($response, $paginationArguments); + } } diff --git a/src/Enums/EnumEdgeEventAction.php b/src/Enums/EnumEdgeEventAction.php new file mode 100644 index 0000000..3a0b1df --- /dev/null +++ b/src/Enums/EnumEdgeEventAction.php @@ -0,0 +1,83 @@ + 'ADDED', + 'ALARM_ACK' => 'ALARM_ACK', + 'ALARM_ASSIGNED' => 'ALARM_ASSIGNED', + 'ALARM_CLEAR' => 'ALARM_CLEAR', + 'ALARM_UNASSIGNED' => 'ALARM_UNASSIGNED', + 'ASSIGNED_TO_CUSTOMER' => 'ASSIGNED_TO_CUSTOMER', + 'ASSIGNED_TO_EDGE' => 'ASSIGNED_TO_EDGE', + 'ATTRIBUTES_DELETED' => 'ATTRIBUTES_DELETED', + 'ATTRIBUTES_UPDATED' => 'ATTRIBUTES_UPDATED', + 'CREDENTIALS_REQUEST' => 'CREDENTIALS_REQUEST', + 'CREDENTIALS_UPDATED' => 'CREDENTIALS_UPDATED', + 'DELETED' => 'DELETED', + 'ENTITY_MERGE_REQUEST' => 'ENTITY_MERGE_REQUEST', + 'POST_ATTRIBUTES' => 'POST_ATTRIBUTES', + 'RELATION_ADD_OR_UPDATE' => 'RELATION_ADD_OR_UPDATE', + 'RELATION_DELETED' => 'RELATION_DELETED', + 'RPC_CALL' => 'RPC_CALL', + 'TIMESERIES_UPDATED' => 'TIMESERIES_UPDATED', + 'UNASSIGNED_FROM_CUSTOMER' => 'UNASSIGNED_FROM_CUSTOMER', + 'UNASSIGNED_FROM_EDGE' => 'UNASSIGNED_FROM_EDGE', + 'UPDATED' => 'UPDATED', + ]; + } + + protected static function labels(): array + { + return [ + 'ADDED' => 'Added', + 'ALARM_ACK' => 'Alarm Ack', + 'ALARM_ASSIGNED' => 'Alarm Assigned', + 'ALARM_CLEAR' => 'Alarm Clear', + 'ALARM_UNASSIGNED' => 'Alarm UnAssign', + 'ASSIGNED_TO_CUSTOMER' => 'Assigned To Customer', + 'ASSIGNED_TO_EDGE' => 'Assigned To Edge', + 'ATTRIBUTES_DELETED' => 'Attribute Deleted', + 'ATTRIBUTES_UPDATED' => 'Attribute Updated', + 'CREDENTIALS_REQUEST' => 'Credentials Request', + 'CREDENTIALS_UPDATED' => 'Credentials Updated', + 'DELETED' => 'Deleted', + 'ENTITY_MERGE_REQUEST' => 'Entity Merge request', + 'POST_ATTRIBUTES' => 'Post Attributes', + 'RELATION_ADD_OR_UPDATE' => 'Relation Add Or Update', + 'RELATION_DELETED' => 'Relation Deleted', + 'RPC_CALL' => 'RPC Call', + 'TIMESERIES_UPDATED' => 'Timeseries Updated', + 'UNASSIGNED_FROM_CUSTOMER' => 'UnAssigned From Customer', + 'UNASSIGNED_FROM_EDGE' => 'UnAssigned From Edge', + 'UPDATED' => 'Updated', + ]; + } +} diff --git a/src/Enums/EnumEdgeEventType.php b/src/Enums/EnumEdgeEventType.php new file mode 100644 index 0000000..cc3d110 --- /dev/null +++ b/src/Enums/EnumEdgeEventType.php @@ -0,0 +1,77 @@ + 'ADMIN_SETTINGS', + 'ALARM' => 'ALARM', + 'ASSET' => 'ASSET', + 'ASSET_PROFILE' => 'ASSET_PROFILE', + 'CUSTOMER' => 'CUSTOMER', + 'DASHBOARD' => 'DASHBOARD', + 'DEVICE' => 'DEVICE', + 'DEVICE_PROFILE' => 'DEVICE_PROFILE', + 'EDGE' => 'EDGE', + 'ENTITY_VIEW' => 'ENTITY_VIEW', + 'OTA_PACKAGE' => 'OTA_PACKAGE', + 'QUEUE' => 'QUEUE', + 'RELATION' => 'RELATION', + 'RULE_CHAIN' => 'RULE_CHAIN', + 'RULE_CHAIN_METADATA' => 'RULE_CHAIN_METADATA', + 'TENANT' => 'TENANT', + 'USER' => 'USER', + 'WIDGETS_BUNDLE' => 'WIDGETS_BUNDLE', + 'WIDGET_TYPE' => 'WIDGET_TYPE', + ]; + } + + protected static function labels(): array + { + return [ + 'ADMIN_SETTINGS' => 'Admin Settings', + 'ALARM' => 'Alarm', + 'ASSET' => 'Asset', + 'ASSET_PROFILE' => 'Asset Profile', + 'CUSTOMER' => 'Customer', + 'DASHBOARD' => 'Dashboard', + 'DEVICE' => 'Device', + 'DEVICE_PROFILE' => 'Device Profile', + 'EDGE' => 'Edge', + 'ENTITY_VIEW' => 'Entity View', + 'OTA_PACKAGE' => 'OTA Package', + 'QUEUE' => 'Queue', + 'RELATION' => 'Relation', + 'RULE_CHAIN' => 'RuleChain', + 'RULE_CHAIN_METADATA' => 'RuleChain Metadata', + 'TENANT' => 'tenant', + 'USER' => 'User', + 'WIDGETS_BUNDLE' => 'Widget Bundle', + 'WIDGET_TYPE' => 'Widget Type', + ]; + } +} diff --git a/src/Enums/EnumEntityType.php b/src/Enums/EnumEntityType.php index a01454a..3fa40be 100644 --- a/src/Enums/EnumEntityType.php +++ b/src/Enums/EnumEntityType.php @@ -11,6 +11,7 @@ * @method static self DEVICE() * @method static self DEVICE_PROFILE() * @method static self EDGE() + * @method static self EDGE_EVENT() * @method static self ENTITY_VIEW() * @method static self OTA_PACKAGE() * @method static self RPC() @@ -37,6 +38,7 @@ protected static function values(): array 'DEVICE' => 'DEVICE', 'DEVICE_PROFILE' => 'DEVICE_PROFILE', 'EDGE' => 'EDGE', + 'EDGE_EVENT' => 'EDGE_EVENT', 'ENTITY_VIEW' => 'ENTITY_VIEW', 'OTA_PACKAGE' => 'OTA_PACKAGE', 'RPC' => 'RPC', @@ -63,6 +65,7 @@ protected static function labels(): array 'DEVICE' => 'Device', 'DEVICE_PROFILE' => 'Device Profile', 'EDGE' => 'Edge', + 'EDGE_EVENT' => 'Edge Event', 'ENTITY_VIEW' => 'Entity View', 'OTA_PACKAGE' => 'OTA Pacakge', 'RPC' => 'RPC', diff --git a/src/Thingsboard.php b/src/Thingsboard.php index 3658ff6..42cbb0c 100644 --- a/src/Thingsboard.php +++ b/src/Thingsboard.php @@ -38,6 +38,7 @@ * @method Entities\ComponentDescriptor componentDescriptor(array $attributes = []) * @method Entities\Queue queue(array $attributes = []) * @method Entities\Edge edge(array $attributes = []) + * @method Entities\EdgeEvent edgeEvent(array $attributes = []) */ class Thingsboard { diff --git a/tests/Unit/Asset/AssignAssetToEdgeTest.php b/tests/Unit/Asset/AssignAssetToEdgeTest.php new file mode 100644 index 0000000..37c53ff --- /dev/null +++ b/tests/Unit/Asset/AssignAssetToEdgeTest.php @@ -0,0 +1,40 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $assetProfileId = thingsboard($tenantUser)->assetProfile()->getDefaultAssetProfileInfo()->id->id; + $newAsset = thingsboard($tenantUser)->asset()->saveAsset("- {$this->faker->sentence(3)}", $assetProfileId); + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $asset = thingsboard($tenantUser)->asset()->assignAssetToEdge($edge->id->id, $newAsset->id->id); + $this->assertInstanceOf(Asset::class, $asset); + + $edge->deleteEdge(); + $newAsset->deleteAsset(); + } + + public function testInvalidEdgeUuid() + { + $uuid = $this->faker->uuid; + $tenantUser = $this->thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $assetProfileId = thingsboard($tenantUser)->assetProfile()->getDefaultAssetProfileInfo()->id->id; + $newAsset = thingsboard($tenantUser)->asset()->saveAsset("- {$this->faker->sentence(3)}", $assetProfileId); + + try { + $this->expectExceptionCode(404); + $this->expectExceptionMessageMatches("/{$uuid}/"); + thingsboard($tenantUser)->asset()->assignAssetToEdge($uuid, $newAsset->id->id); + } finally { + $newAsset->deleteAsset(); + } + } +} diff --git a/tests/Unit/Asset/GetEdgeAssetsTest.php b/tests/Unit/Asset/GetEdgeAssetsTest.php new file mode 100644 index 0000000..a9e58ca --- /dev/null +++ b/tests/Unit/Asset/GetEdgeAssetsTest.php @@ -0,0 +1,30 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $assetProfileId = thingsboard($tenantUser)->assetProfile()->getDefaultAssetProfileInfo()->id->id; + $newAsset = thingsboard($tenantUser)->asset()->saveAsset("- {$this->faker->sentence(3)}", $assetProfileId); + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + thingsboard($tenantUser)->asset()->assignAssetToEdge($edge->id->id, $newAsset->id->id); + + $assets = thingsboard($tenantUser)->asset()->getEdgeAssets(PaginationArguments::make(), $edge->id->id); + $edge->deleteEdge(); + $newAsset->deleteAsset(); + + $assets->each(function ($asset) use ($newAsset) { + $this->assertInstanceOf(Asset::class, $asset); + $this->assertTrue($asset->id->id == $newAsset->id->id); + }); + } +} diff --git a/tests/Unit/Asset/UnAssignAssetFromEdgeTest.php b/tests/Unit/Asset/UnAssignAssetFromEdgeTest.php new file mode 100644 index 0000000..11c8d05 --- /dev/null +++ b/tests/Unit/Asset/UnAssignAssetFromEdgeTest.php @@ -0,0 +1,27 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $assetProfileId = thingsboard($tenantUser)->assetProfile()->getDefaultAssetProfileInfo()->id->id; + $newAsset = thingsboard($tenantUser)->asset()->saveAsset("- {$this->faker->sentence(3)}", $assetProfileId); + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $asset = thingsboard($tenantUser)->asset()->assignAssetToEdge($edge->id->id, $newAsset->id->id); + $this->assertInstanceOf(Asset::class, $asset); + + $asset = thingsboard($tenantUser)->asset()->unassignAssetFromEdge($edge->id->id, $newAsset->id->id); + $this->assertInstanceOf(Asset::class, $asset); + + $edge->deleteEdge(); + $newAsset->deleteAsset(); + } +} diff --git a/tests/Unit/Dashboard/AssignDashboardToEdgeTest.php b/tests/Unit/Dashboard/AssignDashboardToEdgeTest.php new file mode 100644 index 0000000..947ba06 --- /dev/null +++ b/tests/Unit/Dashboard/AssignDashboardToEdgeTest.php @@ -0,0 +1,34 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $dashboardId = thingsboard($tenantUser)->dashboard()->getDashboards(PaginationArguments::make())->collect()->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $dashboard = thingsboard($tenantUser)->dashboard()->assignDashboardToEdge($edge->id->id, $dashboardId); + $this->assertInstanceOf(Dashboard::class, $dashboard); + + $edge->deleteEdge(); + } + + public function testInvalidEdgeUuid() + { + $uuid = $this->faker->uuid; + $tenantUser = $this->thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $dashboardId = thingsboard($tenantUser)->dashboard()->getDashboards(PaginationArguments::make())->collect()->first()->id->id; + + $this->expectExceptionCode(404); + $this->expectExceptionMessageMatches("/{$uuid}/"); + thingsboard($tenantUser)->dashboard()->assignDashboardToEdge($uuid, $dashboardId); + } +} diff --git a/tests/Unit/Dashboard/GetEdgeDashboardTest.php b/tests/Unit/Dashboard/GetEdgeDashboardTest.php new file mode 100644 index 0000000..fe9bba9 --- /dev/null +++ b/tests/Unit/Dashboard/GetEdgeDashboardTest.php @@ -0,0 +1,28 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $dashboardId = thingsboard($tenantUser)->dashboard()->getDashboards(PaginationArguments::make())->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + thingsboard($tenantUser)->dashboard()->assignDashboardToEdge($edge->id->id, $dashboardId); + + $dashboards = thingsboard($tenantUser)->dashboard()->getEdgeDashboards(PaginationArguments::make(), $edge->id->id); + $edge->deleteEdge(); + + $dashboards->each(function ($dashboard) use ($dashboardId) { + $this->assertInstanceOf(Dashboard::class, $dashboard); + $this->assertTrue($dashboard->id->id == $dashboardId); + }); + } +} diff --git a/tests/Unit/Dashboard/UnAssignDashboardFromEdgeTest.php b/tests/Unit/Dashboard/UnAssignDashboardFromEdgeTest.php new file mode 100644 index 0000000..6697a61 --- /dev/null +++ b/tests/Unit/Dashboard/UnAssignDashboardFromEdgeTest.php @@ -0,0 +1,26 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $dashboardId = thingsboard($tenantUser)->dashboard()->getDashboards(PaginationArguments::make())->collect()->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $dashboard = thingsboard($tenantUser)->dashboard()->assignDashboardToEdge($edge->id->id, $dashboardId); + $this->assertInstanceOf(Dashboard::class, $dashboard); + + $dashboard = thingsboard($tenantUser)->dashboard()->unassignDashboardFromEdge($edge->id->id, $dashboardId); + $this->assertInstanceOf(Dashboard::class, $dashboard); + + $edge->deleteEdge(); + } +} diff --git a/tests/Unit/Device/AssignDeviceToEdgeTest.php b/tests/Unit/Device/AssignDeviceToEdgeTest.php new file mode 100644 index 0000000..4725788 --- /dev/null +++ b/tests/Unit/Device/AssignDeviceToEdgeTest.php @@ -0,0 +1,34 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $deviceId = thingsboard($tenantUser)->device()->getTenantDeviceInfos(PaginationArguments::make())->collect()->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $device = thingsboard($tenantUser)->device()->assignDeviceToEdge($edge->id->id, $deviceId); + $this->assertInstanceOf(Device::class, $device); + + $edge->deleteEdge(); + } + + public function testInvalidEdgeUuid() + { + $uuid = $this->faker->uuid; + $tenantUser = $this->thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $deviceId = thingsboard($tenantUser)->device()->getTenantDeviceInfos(PaginationArguments::make())->collect()->first()->id->id; + + $this->expectExceptionCode(404); + $this->expectExceptionMessageMatches("/{$uuid}/"); + thingsboard($tenantUser)->device()->assignDeviceToEdge($uuid, $deviceId); + } +} diff --git a/tests/Unit/Device/GetEdgeDevicesTest.php b/tests/Unit/Device/GetEdgeDevicesTest.php new file mode 100644 index 0000000..7d1cfe8 --- /dev/null +++ b/tests/Unit/Device/GetEdgeDevicesTest.php @@ -0,0 +1,28 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $deviceId = thingsboard($tenantUser)->device()->getTenantDeviceInfos(PaginationArguments::make())->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + thingsboard($tenantUser)->device()->assignDeviceToEdge($edge->id->id, $deviceId); + + $devices = thingsboard($tenantUser)->device()->getEdgeDevices(PaginationArguments::make(), $edge->id->id); + $edge->deleteEdge(); + + $devices->each(function ($device) use ($deviceId) { + $this->assertInstanceOf(Device::class, $device); + $this->assertTrue($device->id->id == $deviceId); + }); + } +} diff --git a/tests/Unit/Device/UnAssignDeviceFromEdgeTest.php b/tests/Unit/Device/UnAssignDeviceFromEdgeTest.php new file mode 100644 index 0000000..1b737f4 --- /dev/null +++ b/tests/Unit/Device/UnAssignDeviceFromEdgeTest.php @@ -0,0 +1,26 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $deviceId = thingsboard($tenantUser)->device()->getTenantDeviceInfos(PaginationArguments::make())->collect()->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + $device = thingsboard($tenantUser)->device()->assignDeviceToEdge($edge->id->id, $deviceId); + $this->assertInstanceOf(Device::class, $device); + + $device = thingsboard($tenantUser)->device()->unassignDeviceFromEdge($edge->id->id, $deviceId); + $this->assertInstanceOf(Device::class, $device); + + $edge->deleteEdge(); + } +} diff --git a/tests/Unit/EdgeEvent/GetEdgeEventsTest.php b/tests/Unit/EdgeEvent/GetEdgeEventsTest.php new file mode 100644 index 0000000..8552e3e --- /dev/null +++ b/tests/Unit/EdgeEvent/GetEdgeEventsTest.php @@ -0,0 +1,23 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + $events = thingsboard($tenantUser)->edgeEvent()->getEdgeEvents(PaginationArguments::make(), $edge->id->id); + + $events->each(function ($event) use ($edge) { + $this->assertInstanceOf(EdgeEvent::class, $event); + $this->assertEquals($edge->id->id, $event->edgeId->id); + }); + } +} diff --git a/tests/Unit/RuleChain/AssignRuleChainToEdgeTest.php b/tests/Unit/RuleChain/AssignRuleChainToEdgeTest.php new file mode 100644 index 0000000..76c11d1 --- /dev/null +++ b/tests/Unit/RuleChain/AssignRuleChainToEdgeTest.php @@ -0,0 +1,36 @@ +thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $ruleChainId = thingsboard($tenantUser)->ruleChain()->getRuleChains(PaginationArguments::make())->collect()->first()->id->id; + $edge = thingsboard($tenantUser)->edge()->saveEdge("- {$this->faker->sentence(3)}"); + + try { + $this->expectExceptionCode(400); + $this->expectExceptionMessageMatches('/non EDGE/'); + thingsboard($tenantUser)->ruleChain()->assignRuleChainToEdge($edge->id->id, $ruleChainId); + } finally { + $edge->deleteEdge(); + } + } + + public function testInvalidEdgeUuid() + { + $uuid = $this->faker->uuid; + $tenantUser = $this->thingsboardUser(EnumAuthority::TENANT_ADMIN()); + $ruleChainId = thingsboard($tenantUser)->ruleChain()->getRuleChains(PaginationArguments::make())->collect()->first()->id->id; + + $this->expectExceptionCode(404); + $this->expectExceptionMessageMatches("/{$uuid}/"); + thingsboard($tenantUser)->ruleChain()->assignRuleChainToEdge($uuid, $ruleChainId); + } +}