import { HttpService, Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @Injectable() export class DeviceService { constructor( private readonly cfg: ConfigService, private readonly http: HttpService ) { } private get schema(): string { return this.cfg.get('DEVICE_HTTP_SCHEMA'); } private get port(): string { return this.cfg.get('DEVICE_HTTP_PORT'); } private endpoint(ip: string): string { return `${this.schema}://${ip}:${this.port}`; } async setCert(ip: string, payload: any) { try { const response = await this.http.put(`${this.endpoint(ip)}/credentials`, payload).toPromise(); return response.data; } catch ({ response }) { throw { data: response.data, status: response.status, path: response.config.url }; } } async connectivityTest(ip: string) { try { const response = await this.http.head(`${this.endpoint(ip)}/validate`).toPromise(); return response.data; } catch ({ response }) { throw { data: response.data, status: response.status, path: response.config.url }; } } async reset(ip: string) { try { const response = await this.http.head(`${this.endpoint(ip)}/reset`).toPromise(); return response.data; } catch ({ response }) { throw { data: response.data, status: response.status, path: response.config.url }; } } }