Commit c865db244c028a824c33cf00748162147bae7ea4

Authored by Artem Babak
1 parent a477cad1

Edge Downlinks: fixed types, added error handler, refactored

... ... @@ -19,7 +19,7 @@ import { EMPTY, forkJoin, Observable, of, throwError } from 'rxjs';
19 19 import { HttpClient } from '@angular/common/http';
20 20 import { PageLink } from '@shared/models/page/page-link';
21 21 import { AliasEntityType, EntityType } from '@shared/models/entity-type.models';
22   -import { BaseData } from '@shared/models/base-data';
  22 +import { BaseData, HasId } from '@shared/models/base-data';
23 23 import { EntityId } from '@shared/models/id/entity-id';
24 24 import { DeviceService } from '@core/http/device.service';
25 25 import { TenantService } from '@core/http/tenant.service';
... ... @@ -76,8 +76,8 @@ import {
76 76 } from '@shared/models/query/query.models';
77 77 import { alarmFields } from '@shared/models/alarm.models';
78 78 import { EdgeService } from "@core/http/edge.service";
79   -import { Edge, EdgeEventType } from '@shared/models/edge.models';
80   -import { RuleChainType } from "@shared/models/rule-chain.models";
  79 +import { Edge, EdgeEvent, EdgeEventType } from '@shared/models/edge.models';
  80 +import { RuleChainMetaData, RuleChainType } from "@shared/models/rule-chain.models";
81 81 import { WidgetService } from "@core/http/widget.service";
82 82 import { DeviceProfileService } from "@core/http/device-profile.service";
83 83
... ... @@ -1333,8 +1333,8 @@ export class EntityService {
1333 1333 return entitiesObservable;
1334 1334 }
1335 1335
1336   - public getEdgeEventContentByEntityType(entity: any): Observable<any> {
1337   - let entityObservable: Observable<any>;
  1336 + public getEdgeEventContent(entity: EdgeEvent): Observable<BaseData<HasId> | RuleChainMetaData | string> {
  1337 + let entityObservable: Observable<BaseData<HasId> | RuleChainMetaData | string>;
1338 1338 const entityId: string = entity.entityId;
1339 1339 const entityType: any = entity.type;
1340 1340 switch (entityType) {
... ...
... ... @@ -22,6 +22,7 @@ import {
22 22 } from '@home/models/entity/entities-table-config.models';
23 23 import {
24 24 EdgeEvent,
  25 + EdgeEventActionType,
25 26 edgeEventActionTypeTranslations,
26 27 EdgeEventStatus,
27 28 edgeEventStatusColor,
... ... @@ -33,7 +34,6 @@ import { TranslateService } from '@ngx-translate/core';
33 34 import { DatePipe } from '@angular/common';
34 35 import { MatDialog } from '@angular/material/dialog';
35 36 import { EntityId } from '@shared/models/id/entity-id';
36   -import { EntityTypeResource } from '@shared/models/entity-type.models';
37 37 import { Observable } from 'rxjs';
38 38 import { PageData } from '@shared/models/page/page-data';
39 39 import { Direction } from '@shared/models/page/sort-order';
... ... @@ -49,18 +49,22 @@ import { EdgeDownlinkTableHeaderComponent } from '@home/components/edge/edge-dow
49 49 import { EdgeService } from '@core/http/edge.service';
50 50 import { map } from 'rxjs/operators';
51 51 import { EntityService } from "@core/http/entity.service";
  52 +import { Store } from '@ngrx/store';
  53 +import { AppState } from '@core/core.state';
  54 +import { ActionNotificationShow } from '@core/notification/notification.actions';
52 55
53 56 export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePageLink> {
54 57
55 58 queueStartTs: number;
56 59
57   - constructor(private edgeService: EdgeService,
58   - private entityService: EntityService,
59   - private dialogService: DialogService,
60   - private translate: TranslateService,
61   - private attributeService: AttributeService,
  60 + constructor(private attributeService: AttributeService,
62 61 private datePipe: DatePipe,
  62 + private dialogService: DialogService,
63 63 private dialog: MatDialog,
  64 + private edgeService: EdgeService,
  65 + private entityService: EntityService,
  66 + private translate: TranslateService,
  67 + private store: Store<AppState>,
64 68 public entityId: EntityId) {
65 69 super();
66 70
... ... @@ -74,12 +78,9 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
74 78 this.entitiesDeleteEnabled = false;
75 79
76 80 this.headerComponent = EdgeDownlinkTableHeaderComponent;
77   - this.entityTranslations = {
78   - noEntities: 'edge.no-downlinks-prompt'
79   - };
80   - this.entityResources = {} as EntityTypeResource<EdgeEvent>;
  81 + this.entityTranslations = { noEntities: 'edge.no-downlinks-prompt' };
81 82 this.entitiesFetchFunction = pageLink => this.fetchEvents(pageLink);
82   - this.defaultSortOrder = {property: 'createdTime', direction: Direction.DESC};
  83 + this.defaultSortOrder = { property: 'createdTime', direction: Direction.DESC };
83 84
84 85 this.updateColumns();
85 86 }
... ... @@ -96,7 +97,7 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
96 97 );
97 98 }
98 99
99   - onUpdate(attributes) {
  100 + onUpdate(attributes): void {
100 101 this.queueStartTs = 0;
101 102 let edge = attributes.reduce(function (map, attribute) {
102 103 map[attribute.key] = attribute;
... ... @@ -126,12 +127,13 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
126 127 {
127 128 name: this.translate.instant('action.view'),
128 129 icon: 'more_horiz',
129   - isEnabled: (entity) => this.isEdgeEventHasData(entity.type),
  130 + isEnabled: (entity) => this.isEdgeEventHasData(entity),
130 131 onAction: ($event, entity) =>
131 132 {
132   - this.prepareEdgeEventContent(entity).subscribe((content) => {
133   - this.showEdgeEventContent($event, content,'event.data');
134   - });
  133 + this.prepareEdgeEventContent(entity).subscribe(
  134 + (content) => this.showEdgeEventContent($event, content,'event.data'),
  135 + () => this.showEntityNotFoundError()
  136 + );
135 137 }
136 138 },
137 139 '40px'),
... ... @@ -141,7 +143,7 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
141 143 }
142 144 }
143 145
144   - updateEdgeEventStatus(createdTime): string {
  146 + updateEdgeEventStatus(createdTime: number): string {
145 147 if (this.queueStartTs && createdTime < this.queueStartTs) {
146 148 return this.translate.instant('edge.deployed');
147 149 } else {
... ... @@ -149,21 +151,17 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
149 151 }
150 152 }
151 153
152   - isPending(createdTime): boolean {
  154 + isPending(createdTime: number): boolean {
153 155 return createdTime > this.queueStartTs;
154 156 }
155 157
156   - isEdgeEventHasData(edgeEventType: EdgeEventType): boolean {
157   - switch (edgeEventType) {
158   - case EdgeEventType.ADMIN_SETTINGS:
159   - return false;
160   - default:
161   - return true;
162   - }
  158 + isEdgeEventHasData(entity: EdgeEvent): boolean {
  159 + return !(entity.type === EdgeEventType.ADMIN_SETTINGS ||
  160 + entity.action === EdgeEventActionType.DELETED);
163 161 }
164 162
165   - prepareEdgeEventContent(entity: any): Observable<string> {
166   - return this.entityService.getEdgeEventContentByEntityType(entity).pipe(
  163 + prepareEdgeEventContent(entity: EdgeEvent): Observable<string> {
  164 + return this.entityService.getEdgeEventContent(entity).pipe(
167 165 map((result) => JSON.stringify(result))
168 166 );
169 167 }
... ... @@ -182,4 +180,15 @@ export class EdgeDownlinkTableConfig extends EntityTableConfig<EdgeEvent, TimePa
182 180 }
183 181 });
184 182 }
  183 +
  184 + showEntityNotFoundError(): void {
  185 + this.store.dispatch(new ActionNotificationShow(
  186 + {
  187 + message: this.translate.instant('edge.load-entity-error'),
  188 + type: 'error',
  189 + verticalPosition: 'top',
  190 + horizontalPosition: 'left'
  191 + }
  192 + ));
  193 + }
185 194 }
... ...
... ... @@ -35,8 +35,4 @@ export class EdgeDownlinkTableHeaderComponent extends EntityTableHeaderComponent
35 35 constructor(protected store: Store<AppState>) {
36 36 super(store);
37 37 }
38   -
39   - eventTypeChanged() {
40   - this.eventTableConfig.table.resetSortAndFilter(true, true);
41   - }
42 38 }
... ...
... ... @@ -25,6 +25,8 @@ import { DialogService } from '@core/services/dialog.service';
25 25 import { AttributeService } from '@core/http/attribute.service';
26 26 import { EdgeService } from '@core/http/edge.service';
27 27 import { EntityService } from "@core/http/entity.service";
  28 +import { Store } from '@ngrx/store';
  29 +import { AppState } from '@core/core.state';
28 30
29 31 @Component({
30 32 selector: 'tb-edge-downlink-table',
... ... @@ -64,27 +66,28 @@ export class EdgeDownlinkTableComponent implements OnInit {
64 66
65 67 edgeDownlinkTableConfig: EdgeDownlinkTableConfig;
66 68
67   - constructor(private edgeService: EdgeService,
68   - private entityService: EntityService,
  69 + constructor(private attributeService: AttributeService,
  70 + private datePipe: DatePipe,
69 71 private dialogService: DialogService,
  72 + private dialog: MatDialog,
  73 + private edgeService: EdgeService,
  74 + private entityService: EntityService,
70 75 private translate: TranslateService,
71   - private attributeService: AttributeService,
72   - private datePipe: DatePipe,
73   - private dialog: MatDialog) {
  76 + protected store: Store<AppState>) {
74 77 }
75 78
76 79 ngOnInit() {
77 80 this.dirtyValue = !this.activeValue;
78 81 this.edgeDownlinkTableConfig = new EdgeDownlinkTableConfig(
79   - this.edgeService,
80   - this.entityService,
81   - this.dialogService,
82   - this.translate,
83 82 this.attributeService,
84 83 this.datePipe,
  84 + this.dialogService,
85 85 this.dialog,
  86 + this.edgeService,
  87 + this.entityService,
  88 + this.translate,
  89 + this.store,
86 90 this.entityIdValue
87 91 );
88 92 }
89   -
90 93 }
... ...
... ... @@ -1300,7 +1300,7 @@
1300 1300 "make-private-edge-text": "After the confirmation the edge and all its data will be made private and won't be accessible by others.",
1301 1301 "import": "Import edge",
1302 1302 "label": "Label",
1303   - "load-entity-error": "Failed to load data. Entity not found or has been deleted.",
  1303 + "load-entity-error": "Failed to load data. Entity has been deleted.",
1304 1304 "assign-new-edge": "Assign new edge",
1305 1305 "manage-edge-dashboards": "Edge dashboards",
1306 1306 "unassign-from-edge": "Unassign from edge",
... ...