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