Browse Source

lwm2m: bootstrap new: add toast

pull/14084/head
nickAS21 11 months ago
parent
commit
44dcbb4359
  1. 94
      ui-ngx/src/app/core/http/device.service.ts
  2. 27
      ui-ngx/src/app/modules/home/components/device/device-credentials-lwm2m.component.ts

94
ui-ngx/src/app/core/http/device.service.ts

@ -35,7 +35,7 @@ import { AuthService } from '@core/auth/auth.service';
import { BulkImportRequest, BulkImportResult } from '@shared/import-export/import-export.models'; import { BulkImportRequest, BulkImportResult } from '@shared/import-export/import-export.models';
import { PersistentRpc, RpcStatus } from '@shared/models/rpc.models'; import { PersistentRpc, RpcStatus } from '@shared/models/rpc.models';
import { ResourcesService } from '@core/services/resources.service'; import { ResourcesService } from '@core/services/resources.service';
import {map} from "rxjs/operators"; import {map, switchMap} from "rxjs/operators";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -221,53 +221,51 @@ export class DeviceService {
return this.resourcesService.downloadResource(`/api/device-connectivity/gateway-launch/${deviceId}/docker-compose/download`); return this.resourcesService.downloadResource(`/api/device-connectivity/gateway-launch/${deviceId}/docker-compose/download`);
} }
public rebootDevice(deviceId: string = '', isBootstrapServer: boolean): void { public rebootDevice(deviceId: string = '', isBootstrapServer: boolean): Observable<{ result: string, msg: string }> {
const urlApi = `/api/plugins/rpc/twoway/${deviceId}`; const urlApi = `/api/plugins/rpc/twoway/${deviceId}`;
// DiscoveryAll} const rebootName = isBootstrapServer ? 'Bootstrap-Request Trigger' : 'Registration Update Trigger';
this.http.post(urlApi, { method: "DiscoverAll" }) return this.http.post(urlApi, { method: 'DiscoverAll' }).pipe(
.pipe( timeout(10000),
timeout(10000), // 10 sec switchMap((response: any) => {
catchError(err => { if (response.result && response.result.toUpperCase() === 'CONTENT') {
console.error('DiscoverAll timeout or error', err); const resourceId = isBootstrapServer ? 9 : 8;
return throwError(() => err); const resourcePath = `/1/0/${resourceId}`;
}) return this.rebootTrigger(resourcePath, urlApi).pipe(
) map((responseReboot: any) => {
.subscribe({
next: (response: any) => {
console.log('success: Discovery');
console.log(response);
// result = 'CONTENT'
if (response.result && response.result.toUpperCase() === 'CONTENT') {
const resourceId = isBootstrapServer ? 9 : 8;
const rebutName = isBootstrapServer ? "Bootstrap-Request Trigger" :
"Registration Update Trigger";
const resourcePath = `/1/0/${resourceId}`;
// first rebootTrigger
this.rebootTrigger(resourcePath, urlApi).subscribe(responseReboot => {
if (responseReboot.result === 'CHANGED') { if (responseReboot.result === 'CHANGED') {
console.info(`info: ${rebutName} success.`); return {
result: 'SUCCESS',
msg: `<b>\"${rebootName}\"</b> - Started Successfully.` };
} else { } else {
console.error(`error: ${rebutName} failed: ${responseReboot.toString()}`); return {
result: 'ERROR',
msg: `<b>\"${rebootName}\"</b> failed:<pre>${JSON.stringify(responseReboot, null, 2)}</pre>`
}
} }
}); }),
} catchError(err =>
else { of({
console.error(`error3: Bad registration device with id = ${deviceId} ❗ RPC result is not CONTENT`); result: 'ERROR',
} msg: `<b>\"${rebootName}\"</b> failed.<br>Error: ${err.message || err}` })
}, )
error: (e) => { );
console.error(`error4: Bad registration device with id = ${deviceId} ${e.toString()}`); } else {
return throwError(() => new Error('Could not get JWT token from store.')); return of({
// return throwError(() => e); result: 'ERROR',
}, msg: `<b>\"${rebootName}\"</b> failed.<br>Bad registration device with id = ${deviceId}.<br><b>\"DiscoverAll\"</b> - RPC result is not \"CONTENT\"`
complete: () => { });
console.log('Discovery stream complete'); } }
}); }),
} catchError(err =>
of({
private rebootTrigger(resourcePath: string, urlApi: string): Observable<{ result: string;}> { result: 'ERROR',
console.log(`Sending reboot command to ${resourcePath}`); msg: `<b>\"${rebootName}\"</b> failed.<br>Bad registration device with id = ${deviceId}.<br>Error: ${err.message || err}`
})
)
);
}
private rebootTrigger(resourcePath: string, urlApi: string): Observable<{ result: string, msg?: string }> {
return this.http.post<any>(urlApi, { return this.http.post<any>(urlApi, {
method: 'Execute', method: 'Execute',
params: { id: resourcePath } params: { id: resourcePath }
@ -278,12 +276,14 @@ export class DeviceService {
if (res?.result?.toUpperCase() === 'CHANGED') { if (res?.result?.toUpperCase() === 'CHANGED') {
return { result: 'CHANGED' }; return { result: 'CHANGED' };
} else { } else {
return {result: 'ERROR'} return {
result:`${res?.result}`,
msg: `${res?.error} `
}
}; };
}), }),
catchError(err => { catchError(err => {
console.error(`Execute error5 for ${resourcePath}:`, err); return throwError(() => err);
return of({ result: 'ERROR' });
}) })
); );
} }

27
ui-ngx/src/app/modules/home/components/device/device-credentials-lwm2m.component.ts

@ -37,6 +37,9 @@ import {takeUntil} from 'rxjs/operators';
import { isDefinedAndNotNull } from '@core/utils'; import { isDefinedAndNotNull } from '@core/utils';
import {DeviceId} from "@shared/models/id/device-id"; import {DeviceId} from "@shared/models/id/device-id";
import {DeviceService} from "@core/http/device.service"; import {DeviceService} from "@core/http/device.service";
import {ActionNotificationShow} from "@core/notification/notification.actions";
import {Store} from "@ngrx/store";
import {AppState} from "@core/core.state";
@Component({ @Component({
selector: 'tb-device-credentials-lwm2m', selector: 'tb-device-credentials-lwm2m',
@ -70,7 +73,8 @@ export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Va
@Input() @Input()
deviceId: DeviceId; deviceId: DeviceId;
constructor(private fb: UntypedFormBuilder, constructor(protected store: Store<AppState>,
private fb: UntypedFormBuilder,
private deviceService: DeviceService) { private deviceService: DeviceService) {
this.lwm2mConfigFormGroup = this.initLwm2mConfigForm(); this.lwm2mConfigFormGroup = this.initLwm2mConfigForm();
} }
@ -120,7 +124,26 @@ export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Va
*/ */
public rebootDevice(isBootstrapServer: boolean): void { public rebootDevice(isBootstrapServer: boolean): void {
this.deviceService.rebootDevice(this.deviceId.id, isBootstrapServer); this.deviceService.rebootDevice(this.deviceId.id, isBootstrapServer).subscribe(responseReboot => {
if (responseReboot.result === 'SUCCESS') {
this.store.dispatch(new ActionNotificationShow(
{
message: responseReboot.msg,
type: 'success',
duration: 1500,
verticalPosition: 'top',
horizontalPosition: 'left'
}));
} else {
this.store.dispatch(new ActionNotificationShow(
{
message: responseReboot.msg,
type: 'error',
verticalPosition: 'top',
horizontalPosition: 'left'
}));
}
});
} }
private initClientSecurityConfig(config: Lwm2mSecurityConfigModels): void { private initClientSecurityConfig(config: Lwm2mSecurityConfigModels): void {

Loading…
Cancel
Save