13 changed files with 1056 additions and 441 deletions
@ -0,0 +1,121 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
.tb-home-table { |
|||
width: 100%; |
|||
border-collapse: collapse; |
|||
|
|||
th { |
|||
height: 32px; |
|||
padding: 0 12px 0 0; |
|||
border-bottom: 1px solid var(--tb-line, rgba(0, 0, 0, 0.12)); |
|||
font: 400 12px/16px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.25px; |
|||
text-align: left; |
|||
white-space: nowrap; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
} |
|||
|
|||
.th-label { |
|||
display: inline-flex; |
|||
align-items: center; |
|||
gap: 4px; |
|||
margin: 0; |
|||
padding: 0; |
|||
border: 0; |
|||
background: none; |
|||
font: inherit; |
|||
letter-spacing: inherit; |
|||
color: inherit; |
|||
cursor: pointer; |
|||
user-select: none; |
|||
|
|||
&:hover { |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
|
|||
.tb-sort-icon { |
|||
opacity: .6; |
|||
} |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
border-radius: 2px; |
|||
} |
|||
} |
|||
|
|||
.tb-sort-icon { |
|||
width: 16px; |
|||
height: 16px; |
|||
font-size: 16px; |
|||
opacity: 0; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
transition: opacity .15s ease; |
|||
} |
|||
|
|||
th.sorted .tb-sort-icon { |
|||
opacity: 1; |
|||
} |
|||
|
|||
td { |
|||
height: 36px; |
|||
padding: 0 12px 0 0; |
|||
border-bottom: 1px solid rgba(0, 0, 0, 0.06); |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
vertical-align: middle; |
|||
white-space: nowrap; |
|||
font-variant-numeric: tabular-nums; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
|
|||
tbody tr { |
|||
cursor: pointer; |
|||
|
|||
&:last-child td { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
&:hover td { |
|||
background: rgba(0, 0, 0, 0.02); |
|||
} |
|||
} |
|||
|
|||
.col-time { |
|||
text-align: right; |
|||
} |
|||
|
|||
td.col-time { |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
th.col-time .th-label { |
|||
flex-direction: row-reverse; |
|||
} |
|||
} |
|||
|
|||
.tb-home-cell-link { |
|||
min-width: 0; |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
white-space: nowrap; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
text-decoration: none; |
|||
|
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
&:hover { |
|||
text-decoration: underline; |
|||
} |
|||
} |
|||
|
|||
.tb-home-sr-only { |
|||
position: absolute; |
|||
width: 1px; |
|||
height: 1px; |
|||
overflow: hidden; |
|||
clip: rect(0 0 0 0); |
|||
white-space: nowrap; |
|||
} |
|||
@ -0,0 +1,281 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
.tb-home-tiles { |
|||
display: flex; |
|||
flex-wrap: wrap; |
|||
gap: 8px; |
|||
} |
|||
|
|||
.tb-home-tile { |
|||
--tb-sel: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); |
|||
position: relative; |
|||
display: flex; |
|||
flex: 0 0 auto; |
|||
flex-direction: column; |
|||
justify-content: space-evenly; |
|||
box-sizing: border-box; |
|||
width: 104px; |
|||
min-width: 0; |
|||
margin: 0; |
|||
padding: 5px 10px; |
|||
border: 1px solid rgba(0, 0, 0, 0.10); |
|||
border-radius: var(--tb-tile-radius, 10px); |
|||
background: #fff; |
|||
text-align: left; |
|||
color: inherit; |
|||
cursor: pointer; |
|||
transition: border-color .15s ease, background-color .15s ease; |
|||
|
|||
&:hover { |
|||
border-color: rgba(0, 0, 0, 0.22); |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
} |
|||
|
|||
&[aria-pressed="true"], &[aria-pressed="true"]:hover { |
|||
padding-left: 8px; |
|||
border-color: transparent; |
|||
border-left: 3px solid var(--tb-sel); |
|||
border-radius: 0 var(--tb-tile-radius, 10px) var(--tb-tile-radius, 10px) 0; |
|||
background: color-mix(in srgb, var(--tb-sel) 6%, #fff); |
|||
|
|||
.tb-home-tile-label { |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
} |
|||
|
|||
&.can-reset { |
|||
.tb-home-tile-label { |
|||
padding-right: 16px; |
|||
} |
|||
|
|||
&::after { |
|||
content: 'close'; |
|||
position: absolute; |
|||
top: 4px; |
|||
right: 4px; |
|||
width: 20px; |
|||
height: 20px; |
|||
border-radius: 50%; |
|||
font-family: 'Material Icons'; |
|||
font-size: 16px; |
|||
line-height: 20px; |
|||
text-align: center; |
|||
font-feature-settings: 'liga'; |
|||
color: var(--tb-ink-54, rgba(0, 0, 0, 0.54)); |
|||
} |
|||
|
|||
&:hover::after { |
|||
background: #fff; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
} |
|||
|
|||
&.zero { |
|||
.tb-home-tile-count { |
|||
color: var(--tb-ink-38, rgba(0, 0, 0, 0.38)); |
|||
} |
|||
|
|||
&:disabled { |
|||
cursor: default; |
|||
|
|||
&:hover { |
|||
border-color: rgba(0, 0, 0, 0.10); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-home-tile-label { |
|||
overflow: hidden; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.1px; |
|||
white-space: nowrap; |
|||
text-overflow: ellipsis; |
|||
color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); |
|||
} |
|||
|
|||
.tb-home-tile-count { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 6px; |
|||
font: 500 20px/28px var(--tb-font, Roboto, sans-serif); |
|||
white-space: nowrap; |
|||
font-variant-numeric: tabular-nums; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
|
|||
.tb-home-all-clear { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 12px; |
|||
padding: 2px 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-home-all-clear-glyph { |
|||
display: inline-flex; |
|||
flex: none; |
|||
align-items: center; |
|||
justify-content: center; |
|||
width: 36px; |
|||
height: 36px; |
|||
border-radius: 50%; |
|||
background: var(--tb-ok-tint, rgba(25, 128, 56, 0.08)); |
|||
color: var(--tb-ok, #198038); |
|||
|
|||
.mat-icon { |
|||
width: 20px; |
|||
height: 20px; |
|||
font-size: 20px; |
|||
color: inherit; |
|||
} |
|||
} |
|||
|
|||
.tb-home-empty-note { |
|||
padding: 4px 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
@container (max-width: 459px) { |
|||
.tb-home-tile { |
|||
flex: 1 1 88px; |
|||
width: auto; |
|||
max-width: 140px; |
|||
padding-left: 8px; |
|||
padding-right: 8px; |
|||
} |
|||
|
|||
.tb-home-tile-label { |
|||
letter-spacing: 0; |
|||
} |
|||
} |
|||
|
|||
.tb-home-setup-empty { |
|||
display: flex; |
|||
flex-direction: column; |
|||
align-items: center; |
|||
justify-content: center; |
|||
gap: 8px; |
|||
padding: 8px 16px 12px; |
|||
text-align: center; |
|||
} |
|||
|
|||
.tb-home-setup-empty-glyph { |
|||
display: inline-flex; |
|||
flex: none; |
|||
align-items: center; |
|||
justify-content: center; |
|||
width: 44px; |
|||
height: 44px; |
|||
border-radius: 50%; |
|||
background: color-mix(in srgb, var(--tb-primary-500) 10%, transparent); |
|||
color: var(--tb-primary-500); |
|||
|
|||
.mat-icon { |
|||
width: 20px; |
|||
height: 20px; |
|||
font-size: 20px; |
|||
color: inherit; |
|||
} |
|||
} |
|||
|
|||
.tb-home-setup-empty-text { |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 4px; |
|||
min-width: 0; |
|||
|
|||
p { |
|||
max-width: 36em; |
|||
margin: 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
} |
|||
|
|||
.tb-home-setup-empty-title { |
|||
font: 500 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.25px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-home-setup-empty-actions { |
|||
display: flex; |
|||
flex-wrap: wrap; |
|||
align-items: center; |
|||
justify-content: center; |
|||
gap: 4px 16px; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
|
|||
a { |
|||
border-radius: 4px; |
|||
text-decoration: underline; |
|||
text-decoration-thickness: 1px; |
|||
text-underline-offset: 3px; |
|||
text-decoration-color: transparent; |
|||
color: var(--tb-primary-500); |
|||
transition: text-decoration-color .15s ease; |
|||
|
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
&:hover { |
|||
text-decoration-color: currentColor; |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-home-ext { |
|||
width: 14px; |
|||
height: 14px; |
|||
margin-left: 2px; |
|||
font-size: 14px; |
|||
line-height: 14px; |
|||
vertical-align: -2px; |
|||
opacity: .7; |
|||
} |
|||
|
|||
.tb-home-add.mat-mdc-button { |
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
.mat-icon { |
|||
margin-right: 6px; |
|||
} |
|||
} |
|||
|
|||
@container (max-width: 459px) { |
|||
.tb-home-add.mat-mdc-button { |
|||
min-width: 40px; |
|||
padding: 0 8px; |
|||
|
|||
.mat-icon { |
|||
margin: 0; |
|||
} |
|||
|
|||
.tb-home-add-label { |
|||
position: absolute; |
|||
width: 1px; |
|||
height: 1px; |
|||
overflow: hidden; |
|||
clip: rect(0 0 0 0); |
|||
white-space: nowrap; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,96 @@ |
|||
<!-- |
|||
|
|||
SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
SPDX-License-Identifier: BUSL-1.1 |
|||
|
|||
--> |
|||
<tb-home-card [card]="card" [count]="total()"> |
|||
<ng-container homeCardActions> |
|||
@if (canAdd) { |
|||
<a mat-button color="primary" class="tb-home-add" routerLink="/entities/devices/all" [queryParams]="{action: 'add'}"> |
|||
<mat-icon>add</mat-icon> |
|||
<span class="tb-home-add-label">{{ 'home.devices.add' | translate }}</span> |
|||
</a> |
|||
} |
|||
</ng-container> |
|||
@if (total() === 0) { |
|||
<div class="tb-home-setup-empty"> |
|||
<span class="tb-home-setup-empty-glyph" aria-hidden="true"><mat-icon>memory</mat-icon></span> |
|||
@if (tenantAdmin) { |
|||
<div class="tb-home-setup-empty-text"> |
|||
<p>{{ 'home.devices.none' | translate }}</p> |
|||
<div class="tb-home-setup-empty-actions"> |
|||
<a [href]="howToLink" target="_blank" rel="noopener">{{ 'home.devices.how-to' | translate }}<mat-icon class="tb-home-ext" aria-hidden="true">north_east</mat-icon><span class="tb-home-sr-only">{{ 'home.dashboards.opens-new-tab' | translate }}</span></a> |
|||
</div> |
|||
</div> |
|||
} @else { |
|||
<div class="tb-home-setup-empty-title">{{ 'home.devices.none-assigned' | translate }}</div> |
|||
} |
|||
</div> |
|||
} @else if (total()) { |
|||
<div class="tb-home-tiles"> |
|||
@for (tile of tiles(); track tile.state) { |
|||
<button type="button" class="tb-home-tile" [class]="tile.state" [class.zero]="!tile.count" [class.can-reset]="tile.pressed" |
|||
[disabled]="!tile.count && !tile.pressed" [attr.aria-pressed]="tile.count || tile.pressed ? tile.pressed : null" |
|||
[matTooltip]="(tile.pressed ? 'home.devices.show-every' : 'home.devices.show-only-' + tile.state) | translate" |
|||
[matTooltipDisabled]="!tile.count && !tile.pressed" matTooltipPosition="above" (click)="pick(tile)"> |
|||
<span class="tb-home-tile-label">{{ 'home.devices.' + tile.state | translate }}</span> |
|||
<span class="tb-home-tile-count">{{ tile.count | shortNumber }}</span> |
|||
</button> |
|||
} |
|||
</div> |
|||
@if (allActive()) { |
|||
<div class="tb-home-all-clear"> |
|||
<span class="tb-home-all-clear-glyph" aria-hidden="true"><mat-icon>check</mat-icon></span> |
|||
<span>{{ (total() === 1 ? 'home.devices.one-active' : 'home.devices.all-active') | translate: {count: total()} }}</span> |
|||
</div> |
|||
} |
|||
@if (rows().length) { |
|||
<table class="tb-home-table tb-devices-table"> |
|||
<thead> |
|||
<tr> |
|||
<th class="col-name sortable" [class.sorted]="sortKey() === 'name'" [attr.aria-sort]="ariaSort('name')"> |
|||
<button type="button" class="th-label" [matTooltip]="'home.devices.sort-name' | translate" (click)="sortBy('name')"> |
|||
<span>{{ 'home.devices.name' | translate }}</span> |
|||
<mat-icon class="tb-sort-icon">{{ sortIcon('name') }}</mat-icon> |
|||
</button> |
|||
</th> |
|||
<th class="col-profile">{{ 'home.devices.profile' | translate }}</th> |
|||
<th class="col-state"><span class="th-text">{{ 'home.devices.state' | translate }}</span></th> |
|||
<th class="col-time sortable" [class.sorted]="sortKey() === 'last'" [attr.aria-sort]="ariaSort('last')"> |
|||
<button type="button" class="th-label" [matTooltip]="'home.devices.sort-last' | translate" (click)="sortBy('last')"> |
|||
<span>{{ 'home.devices.last-activity' | translate }}</span> |
|||
<mat-icon class="tb-sort-icon">{{ sortIcon('last') }}</mat-icon> |
|||
</button> |
|||
</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
@for (row of rows(); track row.id) { |
|||
<tr (click)="open(row)"> |
|||
<td class="col-name"> |
|||
<span class="tb-devices-cell-main"><a class="tb-home-cell-link" [routerLink]="row.url" (click)="$event.stopPropagation()">{{ row.name }}</a></span> |
|||
<span class="tb-devices-cell-sub">{{ row.profile }}</span> |
|||
</td> |
|||
<td class="col-profile">{{ row.profile }}</td> |
|||
<td class="col-state"> |
|||
<span class="tb-devices-state" [class.active]="row.active" [class.inactive]="!row.active"> |
|||
<span class="tb-devices-state-word">{{ (row.active ? 'home.devices.active' : 'home.devices.inactive') | translate }}</span> |
|||
</span> |
|||
</td> |
|||
@if (row.lastActivity) { |
|||
<td class="col-time" [matTooltip]="'home.devices.last-activity-at' | translate: {time: (row.lastActivity | date: 'd MMM, HH:mm')}">{{ row.ago }}</td> |
|||
} @else { |
|||
<td class="col-time"><span class="tb-devices-never" [matTooltip]="'home.devices.no-data' | translate">{{ 'home.devices.never' | translate }}</span></td> |
|||
} |
|||
</tr> |
|||
} |
|||
</tbody> |
|||
</table> |
|||
} @else if (loaded()) { |
|||
<div class="tb-home-empty-note">{{ ('home.devices.none-' + (filter() ?? 'active')) | translate }}</div> |
|||
} |
|||
<tb-home-list-footer [total]="listed()" [expanded]="expanded()" link="/entities/devices/all" [params]="viewAllParams()" |
|||
(expandedChange)="expanded.set($event)"></tb-home-list-footer> |
|||
} |
|||
</tb-home-card> |
|||
@ -0,0 +1,118 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
@use './home-tiles'; |
|||
@use './home-table'; |
|||
|
|||
:host { |
|||
display: block; |
|||
min-width: 0; |
|||
container: tb-devices-home / inline-size; |
|||
} |
|||
|
|||
.tb-home-tile { |
|||
&.inactive:not(.zero) { |
|||
--tb-sel: var(--tb-crit, #D12730); |
|||
} |
|||
|
|||
&.active { |
|||
--tb-sel: var(--tb-ok, #198038); |
|||
} |
|||
} |
|||
|
|||
.tb-devices-table { |
|||
.tb-devices-cell-main { |
|||
display: flex; |
|||
min-width: 0; |
|||
max-width: 220px; |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
} |
|||
|
|||
td.col-profile { |
|||
max-width: 160px; |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.col-state { |
|||
white-space: nowrap; |
|||
} |
|||
} |
|||
|
|||
.tb-devices-cell-sub { |
|||
display: none; |
|||
overflow: hidden; |
|||
font-size: 12px; |
|||
line-height: 16px; |
|||
white-space: nowrap; |
|||
text-overflow: ellipsis; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-devices-state { |
|||
display: inline-flex; |
|||
align-items: center; |
|||
gap: 6px; |
|||
font: 400 13px/20px var(--tb-font, Roboto, sans-serif); |
|||
color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); |
|||
|
|||
&::before { |
|||
content: ''; |
|||
box-sizing: border-box; |
|||
width: 8px; |
|||
height: 8px; |
|||
border-radius: 50%; |
|||
background: var(--tb-ink-38, rgba(0, 0, 0, 0.38)); |
|||
} |
|||
|
|||
&.active::before { |
|||
background: var(--tb-ok, #198038); |
|||
} |
|||
|
|||
&.inactive::before { |
|||
border: 2px solid var(--tb-crit, #D12730); |
|||
background: transparent; |
|||
} |
|||
} |
|||
|
|||
.tb-devices-never { |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
} |
|||
|
|||
@container tb-devices-home (max-width: 599px) { |
|||
.tb-devices-state { |
|||
gap: 0; |
|||
} |
|||
|
|||
.tb-devices-state-word, .tb-devices-table th.col-state .th-text { |
|||
position: absolute; |
|||
width: 1px; |
|||
height: 1px; |
|||
overflow: hidden; |
|||
clip: rect(0 0 0 0); |
|||
white-space: nowrap; |
|||
} |
|||
} |
|||
|
|||
@container tb-devices-home (max-width: 459px) { |
|||
.tb-devices-table { |
|||
.col-profile { |
|||
display: none; |
|||
} |
|||
|
|||
td.col-name { |
|||
width: 99%; |
|||
padding-top: 6px; |
|||
padding-bottom: 6px; |
|||
} |
|||
|
|||
.tb-devices-cell-main { |
|||
max-width: none; |
|||
} |
|||
} |
|||
|
|||
.tb-devices-cell-sub { |
|||
display: block; |
|||
} |
|||
} |
|||
@ -1,16 +1,356 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component } from '@angular/core'; |
|||
import { |
|||
ChangeDetectionStrategy, |
|||
Component, |
|||
computed, |
|||
DestroyRef, |
|||
effect, |
|||
inject, |
|||
NgZone, |
|||
signal, |
|||
untracked, |
|||
WritableSignal |
|||
} from '@angular/core'; |
|||
import { Router } from '@angular/router'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
import { take } from 'rxjs/operators'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { getCurrentAuthUser, selectPersistDeviceStateToTelemetry } from '@core/auth/auth.selectors'; |
|||
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service'; |
|||
import { UserPermissionsService } from '@core/http/user-permissions.service'; |
|||
import { EntityCountCmd, EntityDataCmd, TelemetrySubscriber } from '@shared/models/telemetry/telemetry.models'; |
|||
import { AliasFilterType } from '@shared/models/alias.models'; |
|||
import { EntityType } from '@shared/models/entity-type.models'; |
|||
import { |
|||
BooleanOperation, |
|||
EntityData, |
|||
EntityDataPageLink, |
|||
EntityKey, |
|||
EntityKeyType, |
|||
EntityKeyValueType, |
|||
FilterPredicateType, |
|||
KeyFilter, |
|||
NumericOperation |
|||
} from '@shared/models/query/query.models'; |
|||
import { Direction } from '@shared/models/page/sort-order'; |
|||
import { Authority } from '@shared/models/authority.enum'; |
|||
import { Operation, Resource } from '@shared/models/security.models'; |
|||
import { HelpLinks } from '@shared/models/constants'; |
|||
import { HomeCardId } from '@shared/models/user-settings.models'; |
|||
import { getEntityDetailsPageURL } from '@core/utils'; |
|||
import { HOME_CARDS } from '@home/pages/home-links/home/home-cards.models'; |
|||
import { homeAgo, homeListPageSize, homeListShown } from '@home/pages/home-links/home/home-list.models'; |
|||
|
|||
type DeviceState = 'inactive' | 'active'; |
|||
type DeviceSortKey = 'last' | 'name'; |
|||
|
|||
const MINUTE = 60000; |
|||
|
|||
interface DeviceQuery { |
|||
state: DeviceState | null; |
|||
sortKey: EntityKey; |
|||
direction: Direction; |
|||
withActivity?: boolean; |
|||
neverConnected?: boolean; |
|||
counted: boolean; |
|||
} |
|||
|
|||
interface DeviceTile { |
|||
state: DeviceState; |
|||
count: number; |
|||
pressed: boolean; |
|||
} |
|||
|
|||
export interface DeviceHomeRow { |
|||
id: string; |
|||
name: string; |
|||
profile: string; |
|||
active: boolean; |
|||
lastActivity: number | null; |
|||
ago: string; |
|||
url: string; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-devices-home', |
|||
template: `<tb-home-card [card]="card"></tb-home-card>`, |
|||
templateUrl: './devices-home.component.html', |
|||
styleUrls: ['./devices-home.component.scss'], |
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class DevicesHomeComponent { |
|||
|
|||
readonly card = HOME_CARDS[HomeCardId.DEVICES]; |
|||
|
|||
readonly howToLink = HelpLinks.linksMap.devices; |
|||
|
|||
private telemetryWsService = inject(TelemetryWebsocketService); |
|||
private zone = inject(NgZone); |
|||
private store = inject(Store<AppState>); |
|||
private router = inject(Router); |
|||
private translate = inject(TranslateService); |
|||
private userPermissions = inject(UserPermissionsService); |
|||
|
|||
private readonly authUser = getCurrentAuthUser(this.store); |
|||
readonly tenantAdmin = this.authUser?.authority === Authority.TENANT_ADMIN; |
|||
readonly canAdd = this.tenantAdmin && this.userPermissions.hasGenericPermission(Resource.DEVICE, Operation.CREATE); |
|||
|
|||
readonly filter = signal<DeviceState | null>(null); |
|||
readonly expanded = signal(false); |
|||
readonly sortKey = signal<DeviceSortKey>('last'); |
|||
readonly sortDesc = signal(true); |
|||
|
|||
readonly total = signal<number | null>(null); |
|||
readonly inactive = signal<number | null>(null); |
|||
readonly active = signal<number | null>(null); |
|||
|
|||
private readonly stateKeyType = signal<EntityKeyType | null>(null); |
|||
private readonly lists = signal<(EntityData[] | null)[]>([]); |
|||
private readonly listTotals = signal<number[]>([]); |
|||
private readonly now = signal(Date.now()); |
|||
|
|||
private countSubscribers: TelemetrySubscriber[] = []; |
|||
private dataSubscribers: TelemetrySubscriber[] = []; |
|||
|
|||
readonly tiles = computed<DeviceTile[]>(() => { |
|||
const filter = this.filter(); |
|||
return [ |
|||
{state: 'inactive', count: this.inactive() ?? 0, pressed: filter === 'inactive'}, |
|||
{state: 'active', count: this.active() ?? 0, pressed: filter === 'active'} |
|||
]; |
|||
}); |
|||
|
|||
readonly allActive = computed(() => !this.filter() && this.inactive() === 0 && !!this.total()); |
|||
|
|||
readonly loaded = computed(() => this.lists().length > 0 && this.lists().every(list => list !== null)); |
|||
|
|||
readonly listed = computed(() => this.listTotals().reduce((sum, count, index) => |
|||
this.listQueries[index]?.counted ? sum + count : sum, 0)); |
|||
|
|||
private listQueries: DeviceQuery[] = []; |
|||
|
|||
readonly rows = computed<DeviceHomeRow[]>(() => { |
|||
const keyType = this.stateKeyType(); |
|||
const now = this.now(); |
|||
const merged = this.lists().flatMap((list, index) => { |
|||
const rows = list ?? []; |
|||
return this.listQueries[index]?.neverConnected && keyType |
|||
? rows.filter(data => !Number(data.latest?.[keyType]?.lastActivityTime?.value)) |
|||
: rows; |
|||
}); |
|||
return merged.slice(0, homeListShown(this.listed(), this.expanded())).map(data => this.toRow(data, keyType, now)); |
|||
}); |
|||
|
|||
readonly viewAllParams = computed(() => { |
|||
const filter = this.filter(); |
|||
return filter ? {active: filter === 'active'} : null; |
|||
}); |
|||
|
|||
private readonly queries = computed<DeviceQuery[]>(() => { |
|||
const keyType = this.stateKeyType(); |
|||
if (!keyType) { |
|||
return []; |
|||
} |
|||
const filter = this.filter(); |
|||
const desc = this.sortDesc(); |
|||
if (this.sortKey() === 'name') { |
|||
return [{state: filter, sortKey: {type: EntityKeyType.ENTITY_FIELD, key: 'name'}, direction: desc ? Direction.DESC : Direction.ASC, counted: true}]; |
|||
} |
|||
const lastActivity: EntityKey = {type: keyType, key: 'lastActivityTime'}; |
|||
const silent: DeviceQuery = {state: 'inactive', sortKey: lastActivity, direction: desc ? Direction.DESC : Direction.ASC, |
|||
withActivity: true, counted: false}; |
|||
const never: DeviceQuery = {state: 'inactive', sortKey: lastActivity, direction: Direction.DESC, neverConnected: true, counted: true}; |
|||
const active: DeviceQuery = {state: 'active', sortKey: lastActivity, direction: desc ? Direction.ASC : Direction.DESC, counted: true}; |
|||
const inactive = desc ? [silent, never] : [never, silent]; |
|||
if (filter === 'inactive') { |
|||
return inactive; |
|||
} |
|||
if (filter === 'active') { |
|||
return [active]; |
|||
} |
|||
return desc ? [...inactive, active] : [active, ...inactive]; |
|||
}); |
|||
|
|||
constructor() { |
|||
this.store.select(selectPersistDeviceStateToTelemetry).pipe(take(1)).subscribe(persisted => { |
|||
const keyType = persisted ? EntityKeyType.TIME_SERIES : EntityKeyType.SERVER_ATTRIBUTE; |
|||
this.countSubscribers = [ |
|||
this.subscribeCount(this.total), |
|||
this.subscribeCount(this.inactive, this.activeFilter(keyType, false)), |
|||
this.subscribeCount(this.active, this.activeFilter(keyType, true)) |
|||
]; |
|||
this.stateKeyType.set(keyType); |
|||
}); |
|||
effect(() => { |
|||
const queries = this.queries(); |
|||
const pageSize = homeListPageSize(this.expanded()); |
|||
untracked(() => this.subscribeLists(queries, pageSize)); |
|||
}); |
|||
const tick = setInterval(() => this.now.set(Date.now()), MINUTE); |
|||
inject(DestroyRef).onDestroy(() => { |
|||
clearInterval(tick); |
|||
this.countSubscribers.forEach(subscriber => subscriber.unsubscribe()); |
|||
this.dataSubscribers.forEach(subscriber => subscriber.unsubscribe()); |
|||
}); |
|||
} |
|||
|
|||
pick(tile: DeviceTile): void { |
|||
if (tile.count || tile.pressed) { |
|||
this.filter.set(tile.pressed ? null : tile.state); |
|||
} |
|||
} |
|||
|
|||
sortBy(key: DeviceSortKey): void { |
|||
if (this.sortKey() === key) { |
|||
this.sortDesc.set(!this.sortDesc()); |
|||
} else { |
|||
this.sortKey.set(key); |
|||
this.sortDesc.set(key === 'last'); |
|||
} |
|||
} |
|||
|
|||
ariaSort(key: DeviceSortKey): string { |
|||
if (this.sortKey() !== key) { |
|||
return 'none'; |
|||
} |
|||
return this.sortDesc() ? 'descending' : 'ascending'; |
|||
} |
|||
|
|||
sortIcon(key: DeviceSortKey): string { |
|||
if (key === 'name') { |
|||
return this.sortKey() === 'name' && this.sortDesc() ? 'arrow_downward' : 'arrow_upward'; |
|||
} |
|||
return this.sortKey() === 'last' && !this.sortDesc() ? 'arrow_upward' : 'arrow_downward'; |
|||
} |
|||
|
|||
open(row: DeviceHomeRow): void { |
|||
this.router.navigateByUrl(row.url); |
|||
} |
|||
|
|||
private subscribeLists(queries: DeviceQuery[], pageSize: number): void { |
|||
this.dataSubscribers.forEach(subscriber => subscriber.unsubscribe()); |
|||
const keyType = this.stateKeyType(); |
|||
if (!keyType || !queries.length) { |
|||
this.dataSubscribers = []; |
|||
return; |
|||
} |
|||
this.listQueries = queries; |
|||
const previous = this.lists(); |
|||
const previousTotals = this.listTotals(); |
|||
this.lists.set(queries.map((_, index) => previous[index] ?? null)); |
|||
this.listTotals.set(queries.map((_, index) => previousTotals[index] ?? 0)); |
|||
this.dataSubscribers = queries.map((query, index) => { |
|||
const cmd = new EntityDataCmd(); |
|||
const pageLink: EntityDataPageLink = { |
|||
page: 0, |
|||
pageSize: query.neverConnected ? pageSize * 2 : pageSize, |
|||
sortOrder: {key: query.sortKey, direction: query.direction}, |
|||
dynamic: true |
|||
}; |
|||
cmd.query = { |
|||
entityFilter: {type: AliasFilterType.entityType, entityType: EntityType.DEVICE}, |
|||
pageLink, |
|||
keyFilters: [ |
|||
...(query.state ? this.activeFilter(keyType, query.state === 'active') : []), |
|||
...(query.withActivity ? [this.hasActivityFilter(keyType)] : []) |
|||
], |
|||
entityFields: [ |
|||
{type: EntityKeyType.ENTITY_FIELD, key: 'name'}, |
|||
{type: EntityKeyType.ENTITY_FIELD, key: 'type'} |
|||
], |
|||
latestValues: [ |
|||
{type: keyType, key: 'active'}, |
|||
{type: keyType, key: 'lastActivityTime'} |
|||
] |
|||
}; |
|||
cmd.latestCmd = {keys: [{type: keyType, key: 'active'}, {type: keyType, key: 'lastActivityTime'}]}; |
|||
const subscriber = new TelemetrySubscriber(this.telemetryWsService, this.zone); |
|||
subscriber.subscriptionCommands.push(cmd); |
|||
subscriber.entityData$.subscribe(update => { |
|||
if (update.data) { |
|||
this.setList(index, update.data.data, update.data.totalElements); |
|||
} else if (update.update) { |
|||
const list = this.lists()[index] ?? []; |
|||
const byId = new Map(update.update.map(data => [data.entityId.id, data])); |
|||
this.setList(index, list.map(data => this.merge(data, byId.get(data.entityId.id))), null); |
|||
} |
|||
}); |
|||
subscriber.subscribe(); |
|||
return subscriber; |
|||
}); |
|||
} |
|||
|
|||
private setList(index: number, data: EntityData[], total: number | null): void { |
|||
const lists = [...this.lists()]; |
|||
lists[index] = data; |
|||
this.lists.set(lists); |
|||
if (total !== null) { |
|||
const totals = [...this.listTotals()]; |
|||
totals[index] = total; |
|||
this.listTotals.set(totals); |
|||
} |
|||
} |
|||
|
|||
private merge(current: EntityData, update?: EntityData): EntityData { |
|||
if (!update) { |
|||
return current; |
|||
} |
|||
const latest = {...current.latest}; |
|||
Object.keys(update.latest ?? {}).forEach(type => latest[type] = {...(latest[type] ?? {}), ...update.latest[type]}); |
|||
return {...current, latest}; |
|||
} |
|||
|
|||
private subscribeCount(target: WritableSignal<number | null>, keyFilters?: KeyFilter[]): TelemetrySubscriber { |
|||
const cmd = new EntityCountCmd(); |
|||
cmd.query = { |
|||
entityFilter: {type: AliasFilterType.entityType, entityType: EntityType.DEVICE}, |
|||
keyFilters |
|||
}; |
|||
const subscriber = new TelemetrySubscriber(this.telemetryWsService, this.zone); |
|||
subscriber.subscriptionCommands.push(cmd); |
|||
subscriber.entityCount$.subscribe(update => target.set(update.count ?? 0)); |
|||
subscriber.subscribe(); |
|||
return subscriber; |
|||
} |
|||
|
|||
private activeFilter(keyType: EntityKeyType, active: boolean): KeyFilter[] { |
|||
return [{ |
|||
key: {type: keyType, key: 'active'}, |
|||
valueType: EntityKeyValueType.BOOLEAN, |
|||
predicate: { |
|||
type: FilterPredicateType.BOOLEAN, |
|||
operation: BooleanOperation.EQUAL, |
|||
value: {defaultValue: active} |
|||
} |
|||
}]; |
|||
} |
|||
|
|||
private hasActivityFilter(keyType: EntityKeyType): KeyFilter { |
|||
return { |
|||
key: {type: keyType, key: 'lastActivityTime'}, |
|||
valueType: EntityKeyValueType.NUMERIC, |
|||
predicate: { |
|||
type: FilterPredicateType.NUMERIC, |
|||
operation: NumericOperation.GREATER, |
|||
value: {defaultValue: 0} |
|||
} |
|||
}; |
|||
} |
|||
|
|||
private toRow(data: EntityData, keyType: EntityKeyType | null, now: number): DeviceHomeRow { |
|||
const fields = data.latest?.[EntityKeyType.ENTITY_FIELD] ?? {}; |
|||
const state = keyType ? data.latest?.[keyType] ?? {} : {}; |
|||
const lastActivity = Number(state.lastActivityTime?.value) || null; |
|||
return { |
|||
id: data.entityId.id, |
|||
name: fields.name?.value ?? '', |
|||
profile: fields.type?.value ?? '', |
|||
active: state.active?.value === 'true', |
|||
lastActivity, |
|||
ago: lastActivity ? homeAgo(this.translate, now - lastActivity) : '', |
|||
url: getEntityDetailsPageURL(data.entityId.id, EntityType.DEVICE) |
|||
}; |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue