Create system connections service, mocks, and work on updating devices chart

This commit is contained in:
Jesse Lucas
2020-03-29 16:51:12 -04:00
parent 78a31449aa
commit bc7d71ee3d
9 changed files with 167 additions and 17 deletions
+51 -1
View File
@@ -1,10 +1,60 @@
import { Injectable } from '@angular/core';
import Device from '../device';
import { Observable } from 'rxjs';
import { SystemConfigService } from './system-config.service';
import { SystemConnectionsService } from './system-connections.service';
@Injectable({
providedIn: 'root'
})
export class DeviceService {
private devices: Device[];
constructor() { }
constructor(
private systemConfigService: SystemConfigService,
private systemConnectionsService: SystemConnectionsService
) { }
getAll(): Observable<Device> {
const deviceObservable: Observable<Device> = new Observable((observer) => {
this.systemConfigService.getDevices().subscribe(
devices => {
this.devices = devices;
// Check folder devices to see if the device is used
this.systemConfigService.getFolders().subscribe(
folders => {
// TODO: streamline
// Loop through all folder devices to see if the device is used
this.devices.forEach(device => {
folders.forEach(folder => {
folder.devices.forEach(fdevice => {
if (device.deviceID === fdevice.deviceID) {
device.used = true;
}
});
});
});
// See if the connection is connected or undefined
this.systemConnectionsService.getSystemConnections().subscribe(
connections => {
// TODO: check connection and request total
this.devices.forEach(device => {
observer.next(device);
});
observer.complete();
}
);
// Synchronously get the status of each device
// this.getDeviceStatusInOrder(observer, 0);
}
)
},
err => { console.log("getAll error!", err) },
() => { console.log("get all complete!") }
);
});
return deviceObservable
}
}
@@ -1,13 +1,14 @@
import { Injectable } from '@angular/core';
import { config } from '../mocks/mock-config'
import { dbStatus } from '../mocks/mock-db-status'
import { connections } from '../mocks/mock-connections'
@Injectable({
providedIn: 'root'
})
export class InMemoryConfigDataService {
createDb() {
return { config, dbStatus };
return { config, dbStatus, connections };
}
constructor() { }
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { SystemConnectionsService } from './system-connections.service';
describe('SystemConnectionsService', () => {
let service: SystemConnectionsService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SystemConnectionsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { apiURL, apiRetry } from '../api-utils';
import { HttpClient } from '@angular/common/http';
import { retry, map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { SystemConnections } from '../connections';
@Injectable({
providedIn: 'root'
})
export class SystemConnectionsService {
private systemConfigUrl = environment.production ? apiURL + 'rest/system/connections' : 'api/connections';
constructor(private http: HttpClient) { }
getSystemConnections(): Observable<SystemConnections> {
return this.http
.get<SystemConnections>(this.systemConfigUrl)
.pipe(
retry(apiRetry),
map(res => {
return res;
})
);
}
}