member_webapp/@overflow/discovery/component/discovery.component.ts

124 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-05-31 11:53:11 +00:00
import {
2018-06-01 11:25:51 +00:00
Component, Input, Output, EventEmitter, OnDestroy, ViewChild,
2018-05-31 11:53:11 +00:00
} from '@angular/core';
2018-06-04 08:55:47 +00:00
2018-05-31 11:53:11 +00:00
import { Store, select } from '@ngrx/store';
2018-06-04 08:55:47 +00:00
2018-05-31 11:53:11 +00:00
import { Observable, of, Subscription } from 'rxjs';
2018-06-04 08:55:47 +00:00
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
import { Probe, ProbeHost } from '@overflow/commons-typescript/model/probe';
2018-05-31 11:53:11 +00:00
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
2018-06-04 08:55:47 +00:00
import { Anim } from './animation';
import { DiscoveryService } from '../service/discovery.service';
2018-05-31 13:11:23 +00:00
import { DiscoverySubscriber, DiscoveryNotify } from '../subscriber/discovery.subscriber';
2018-06-01 11:25:51 +00:00
import { SearchResultComponent } from './search-result.component';
2018-06-04 04:13:06 +00:00
import { SearchFilterComponent } from './search-filter.component';
2018-05-31 11:53:11 +00:00
@Component({
selector: 'of-discovery',
templateUrl: './discovery.component.html',
2018-06-01 07:32:16 +00:00
animations: Anim,
2018-05-31 11:53:11 +00:00
})
2018-06-01 09:56:00 +00:00
export class DiscoveryComponent implements OnDestroy {
2018-05-31 11:53:11 +00:00
@Input() probeHostID;
pending$: Observable<boolean>;
error$: Observable<any>;
discoverySubscription: Subscription;
selectedProbe: ProbeHost;
requested: boolean;
2018-06-01 02:47:37 +00:00
discoverZone: DiscoverZone;
2018-06-05 02:56:45 +00:00
finished: boolean;
2018-05-31 11:53:11 +00:00
2018-06-04 08:31:00 +00:00
filterWord: string;
filterServices: Service[];
2018-06-01 11:25:51 +00:00
@ViewChild('discoveryResult') discoveryResult: SearchResultComponent;
2018-06-04 04:13:06 +00:00
@ViewChild('discoveryFilter') discoveryFilter: SearchFilterComponent;
2018-06-01 11:25:51 +00:00
2018-05-31 11:53:11 +00:00
constructor(
private discoveryService: DiscoveryService,
2018-05-31 12:44:11 +00:00
private discoverySubscriber: DiscoverySubscriber,
2018-05-31 11:53:11 +00:00
private store: Store<any>
) {
2018-06-04 03:42:30 +00:00
this.discoverySubscription = null;
2018-06-05 02:56:45 +00:00
this.finished = false;
2018-05-31 11:53:11 +00:00
}
2018-06-01 09:56:00 +00:00
ngOnDestroy(): void {
if (null !== this.discoverySubscription) {
this.discoverySubscription.unsubscribe();
}
}
2018-05-31 11:53:11 +00:00
onRequestDiscovery(dz: DiscoverZone) {
this.requested = true;
2018-06-01 02:47:37 +00:00
this.discoverZone = dz;
2018-06-04 09:25:48 +00:00
2018-06-04 12:50:46 +00:00
// TODO: fix
const zone: Zone = {
network: '192.168.1.0/24',
2018-06-12 05:45:21 +00:00
address: '192.168.1.103',
2018-06-04 12:50:46 +00:00
iface: 'enp3s0',
2018-06-05 09:32:20 +00:00
mac: '44:8a:5b:44:8c:e8',
2018-06-04 12:50:46 +00:00
};
this.discoveryService.discoverHost(this.selectedProbe.probe.probeKey, zone, dz.discoverHost);
2018-06-04 09:25:48 +00:00
2018-06-01 09:56:00 +00:00
this.discoverySubscription = this.discoverySubscriber.observable().pipe(
2018-05-31 11:53:11 +00:00
map((discoveryNotify: DiscoveryNotify) => {
switch (discoveryNotify.method) {
case 'DiscoveryService.discoveryStart': {
const startDate = discoveryNotify.params as Date;
2018-06-01 03:22:42 +00:00
2018-05-31 11:53:11 +00:00
break;
}
case 'DiscoveryService.discoveryStop': {
const stopDate = discoveryNotify.params as Date;
2018-06-05 02:56:45 +00:00
this.finished = true;
2018-05-31 11:53:11 +00:00
this.discoverySubscription.unsubscribe();
2018-06-01 09:56:00 +00:00
this.discoverySubscription = null;
2018-06-01 11:25:51 +00:00
2018-05-31 11:53:11 +00:00
break;
}
case 'DiscoveryService.discoveredZone': {
2018-06-04 12:50:46 +00:00
const _zone = discoveryNotify.params as Zone;
2018-05-31 11:53:11 +00:00
break;
}
case 'DiscoveryService.discoveredHost': {
const host = discoveryNotify.params as Host;
2018-06-04 08:31:00 +00:00
this.discoveryResult.addHost(host);
2018-05-31 11:53:11 +00:00
break;
}
case 'DiscoveryService.discoveredPort': {
const port = discoveryNotify.params as Port;
2018-06-04 08:31:00 +00:00
this.discoveryResult.addPort(port);
2018-05-31 11:53:11 +00:00
break;
}
case 'DiscoveryService.discoveredService': {
const service = discoveryNotify.params as Service;
2018-06-04 08:31:00 +00:00
this.discoveryFilter.addService(service);
this.discoveryResult.addService(service);
2018-05-31 11:53:11 +00:00
break;
}
default: {
break;
}
}
}),
catchError(error => {
return of();
}),
).subscribe();
}
2018-06-04 12:50:46 +00:00
onRequestStop() {
2018-06-01 02:47:37 +00:00
this.discoverZone = null;
2018-05-31 11:53:11 +00:00
this.requested = false;
2018-06-04 12:50:46 +00:00
this.discoveryService.stopDiscovery(this.selectedProbe.probe.probeKey);
2018-05-31 11:53:11 +00:00
}
}