taurus-tv/src/app/app.component.ts

130 lines
4.6 KiB
TypeScript
Raw Normal View History

import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from "rxjs/Subscription";
import {TimerObservable} from "rxjs/observable/TimerObservable";
import {KanbanService} from "./kanban/shared/kanban.service";
2017-09-08 09:43:50 +02:00
import {ActivatedRoute, Router} from "@angular/router";
import {SelfUpdaterService} from "./kanban/shared/self-updater.service";
import {TspInfoService} from "./tsp-info/shared/tsp-info.service";
2017-09-08 09:43:50 +02:00
const TIMER_DEPLOY_REFRESH = 30000;
const TIMER_JIRA_REFRESH = 60000;
const TIMER_TSPINFO_REFRESH = 60000;
2017-09-08 09:43:50 +02:00
const TIMER_PAGE_SWITCH_TICK = 1000;
/**
* Page switch timer in seconds
* @type {number}
*/
const TIMESPENT_KANBAN = 120000;
const TIMESPENT_TSPINFO = 30000;
2017-09-08 09:43:50 +02:00
const PAGE_KANBAN = '/kanban';
const PAGE_TSPINFO = '/tspinfopage';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
2017-09-08 09:43:50 +02:00
private currentPage: string = PAGE_KANBAN;
private lastNavOccured: number = Date.now();
private autoSwitchEnabled: boolean = true;
2017-09-08 09:43:50 +02:00
private selfUpdateCheckerTimer: Subscription;
private reloadKanbanTimer: Subscription;
private reloadTspInfoTimer: Subscription;
2017-09-08 09:43:50 +02:00
private pageSwitchTimer: Subscription;
2017-09-08 09:43:50 +02:00
constructor(private selfUpdaterService: SelfUpdaterService,
private kanbanService: KanbanService,
private tspInfoService: TspInfoService,
2017-09-08 09:43:50 +02:00
private router: Router,
private activatedRoute: ActivatedRoute) {
}
/**
* Initialize application timers:
* - selfUpdateCheckerTimer is used to see if there is a newer revision deployed on the server
* - reloadKanbanTimer is used to refresh the status of the jira board
* - pageSwitchTimer handles switching back and forth between page views
*/
public ngOnInit() {
let timer0 = TimerObservable.create(TIMER_DEPLOY_REFRESH, TIMER_JIRA_REFRESH);
this.selfUpdateCheckerTimer = timer0.subscribe(() => {
this.selfUpdaterService.checkAndReloadIfNecessary();
});
let timer1 = TimerObservable.create(TIMER_JIRA_REFRESH, TIMER_JIRA_REFRESH);
this.reloadKanbanTimer = timer1.subscribe(() => {
this.kanbanService.reload();
});
let timer2 = TimerObservable.create(TIMER_TSPINFO_REFRESH, TIMER_TSPINFO_REFRESH);
this.reloadTspInfoTimer = timer2.subscribe(() => {
this.tspInfoService.reload();
});
2017-09-08 09:43:50 +02:00
this.activatedRoute.queryParamMap.subscribe(params => {
if (!params.has("disableAutoSwitch")) {
let timer4 = TimerObservable.create(TIMER_PAGE_SWITCH_TICK, TIMER_PAGE_SWITCH_TICK);
this.pageSwitchTimer = timer4.subscribe(this.switchSubscriber.bind(this));
2017-09-08 09:43:50 +02:00
}
});
}
public ngOnDestroy() {
this.selfUpdateCheckerTimer.unsubscribe();
this.reloadKanbanTimer.unsubscribe();
this.reloadTspInfoTimer.unsubscribe();
2017-09-08 09:43:50 +02:00
if (this.pageSwitchTimer) {
this.pageSwitchTimer.unsubscribe();
2017-09-08 09:43:50 +02:00
}
}
private switchSubscriber() {
let now = new Date();
let weekDay = now.getDay();
// on weekdays
if (weekDay > 0 && weekDay < 6) {
2017-09-08 18:28:42 +02:00
if(now.getHours() == 10) {
if (this.autoSwitchEnabled && now.getMinutes() > 14) {
this.navigateToPage(PAGE_KANBAN);
this.autoSwitchEnabled = false;
}
if (!this.autoSwitchEnabled && now.getMinutes() > 30) {
this.navigateToPage(PAGE_TSPINFO);
this.autoSwitchEnabled = true;
}
2017-09-08 09:43:50 +02:00
}
}
if (this.autoSwitchEnabled) {
let compareTimer: number = 0;
let switchTo: string = '';
switch (this.currentPage) {
case PAGE_KANBAN:
compareTimer = TIMESPENT_KANBAN;
switchTo = PAGE_TSPINFO;
break;
case PAGE_TSPINFO:
compareTimer = TIMESPENT_TSPINFO;
switchTo = PAGE_KANBAN;
break;
default:
console.error("Unknown page in pageSwitcherTimer");
return false;
}
if ((Date.now() - this.lastNavOccured) > compareTimer) {
this.navigateToPage(switchTo);
}
}
}
private navigateToPage(page: string) {
if (page != this.currentPage) {
this.router.navigate([page]);
this.currentPage = page;
this.lastNavOccured = Date.now();
}
}
}