import {Inject, Injectable} from '@angular/core'; import {DeviceStore} from './device.store'; import {BaseApi} from '../../core/base-api.class'; import {HttpClient} from '@angular/common/http'; import {CFG} from '../../const'; import {tap} from 'rxjs/operators'; import {Device, Log} from './device.model'; @Injectable({providedIn: 'root'}) export class DeviceService extends BaseApi { constructor( @Inject(CFG) protected cfg: any, private deviceStore: DeviceStore, private readonly http: HttpClient ) { super(); } setActive(sn: string): void { this.deviceStore.setActive(sn); } fetchDevices() { return this.http.get(`${this.endpoint}/client/devices`).pipe( tap((devices) => this.deviceStore.set(devices)) ); } fetchLogs(sn: string) { return this.http.get(`${this.endpoint}/client/logs/${sn}`).pipe( tap(logs => this.deviceStore.update({logs: {[sn]: logs}})) ); } delete(sn: string) { return this.http.delete(`${this.endpoint}/client/delete/device/${sn}`).pipe( tap(() => this.deviceStore.remove(sn)) ); } }