Synchronously get the status of each device

This commit is contained in:
Jesse Lucas 2020-03-26 18:23:53 -04:00
parent 09673ba0c6
commit 9d126760fa
3 changed files with 44 additions and 22 deletions

View File

@ -42,12 +42,10 @@ namespace Device {
} }
export function getStateType(d: Device): StateType { export function getStateType(d: Device): StateType {
// TODO // StateType Unknown is set in DeviceService
/* if (d.state === StateType.Unknown) {
if (typeof $scope.connections[deviceCfg.deviceID] === 'undefined') { return StateType.Unknown;
return 'unknown';
} }
*/
if (d.paused) { if (d.paused) {
return d.used ? StateType.Paused : StateType.UnusedPaused; return d.used ? StateType.Paused : StateType.UnusedPaused;

View File

@ -1,23 +1,57 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import Device from '../device'; import Device from '../device';
import { Observable } from 'rxjs'; import { Observable, Subscriber } from 'rxjs';
import { SystemConfigService } from './system-config.service'; import { SystemConfigService } from './system-config.service';
import { SystemConnectionsService } from './system-connections.service'; import { SystemConnectionsService } from './system-connections.service';
import { DbCompletionService } from './db-completion.service'; import { DbCompletionService } from './db-completion.service';
import { SystemConnections } from '../connections';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class DeviceService { export class DeviceService {
private devices: Device[]; private devices: Device[];
private sysConns: SystemConnections;
constructor( constructor(
private systemConfigService: SystemConfigService, private systemConfigService: SystemConfigService,
private systemConnectionsService: SystemConnectionsService, private systemConnectionsService: SystemConnectionsService,
private dbCompletionService: DbCompletionService private dbCompletionService: DbCompletionService
) { } ) { }
getDeviceStatusInOrder(observer: Subscriber<Device>, startIndex: number) {
// Return if there aren't any device at the index
if (startIndex >= (this.devices.length)) {
observer.complete();
return;
}
const device: Device = this.devices[startIndex];
startIndex = startIndex + 1;
// Check if device in the connections
if (this.sysConns.connections[device.deviceID] === undefined) {
console.log(this.sysConns.connections)
console.log("connections undefined", device.deviceID);
device.state = Device.StateType.Unknown;
} else {
// Set connected
device.connected = this.sysConns.connections[device.deviceID].connected;
}
this.dbCompletionService.getDeviceCompletion(device.deviceID).subscribe(
c => {
device.completion = c.completion;
observer.next(device);
// recursively get the status of the next folder
this.getDeviceStatusInOrder(observer, startIndex);
});
}
getAll(): Observable<Device> { getAll(): Observable<Device> {
const deviceObservable: Observable<Device> = new Observable((observer) => { const deviceObservable: Observable<Device> = new Observable((observer) => {
// TODO return devices if cached
this.systemConfigService.getDevices().subscribe( this.systemConfigService.getDevices().subscribe(
devices => { devices => {
this.devices = devices; this.devices = devices;
@ -25,7 +59,6 @@ export class DeviceService {
// Check folder devices to see if the device is used // Check folder devices to see if the device is used
this.systemConfigService.getFolders().subscribe( this.systemConfigService.getFolders().subscribe(
folders => { folders => {
// TODO: streamline
// Loop through all folder devices to see if the device is used // Loop through all folder devices to see if the device is used
this.devices.forEach(device => { this.devices.forEach(device => {
folders.forEach(folder => { folders.forEach(folder => {
@ -39,24 +72,13 @@ export class DeviceService {
// See if the connection is connected or undefined // See if the connection is connected or undefined
this.systemConnectionsService.getSystemConnections().subscribe( this.systemConnectionsService.getSystemConnections().subscribe(
connections => {
// TODO: check connection and request total
this.devices.forEach(device => {
// TODO make this synchronous
this.dbCompletionService.getDeviceCompletion(device.deviceID).subscribe(
c => { c => {
device.completion = c.completion; this.sysConns = c;
observer.next(device);
});
//TODO complete observer when finished
// observer.complete();
});
}
);
// Synchronously get the status of each device // Synchronously get the status of each device
// this.getDeviceStatusInOrder(observer, 0); this.getDeviceStatusInOrder(observer, 0);
}
);
} }
) )
}, },

View File

@ -39,6 +39,8 @@ export class FolderService {
* set all their statuses * set all their statuses
*/ */
getAll(): Observable<Folder> { getAll(): Observable<Folder> {
// TODO return this.folders if cached
const folderObservable: Observable<Folder> = new Observable((observer) => { const folderObservable: Observable<Folder> = new Observable((observer) => {
this.systemConfigService.getFolders().subscribe( this.systemConfigService.getFolders().subscribe(
folders => { folders => {