import IServerConfigModel from '@/model/serverConfig/IServerConfigModel' ; import DateUtil from '@/util/DateUtil' ; import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../api' ; import IScheduleApiModel from '../../api/schedule/IScheduleApiModel' ; import IGuideReserveUtil , { ReserveStateItemIndex } from '../guide/IGuideReserveUtil' ; import IOnAirState , { OnAirDisplayData } from './IOnAirState' ; @ injectable ( ) export default class OnAirState implements IOnAirState { public selectedTab : apid . ChannelType | undefined ; private scheduleApiModel : IScheduleApiModel ; private reserveUtil : IGuideReserveUtil ; private schedules : OnAirDisplayData [ ] = [ ] ; private reserveIndex : ReserveStateItemIndex = { } ; private tabs : apid . ChannelType [ ] = [ ] ; constructor ( @ inject ( 'IServerConfigModel' ) serverConfigModel , @ inject ( 'IScheduleApiModel' ) scheduleApiModel , @ inject ( 'IGuideReserveUtil' ) reserveUtil ) { this . scheduleApiModel = scheduleApiModel ; this . reserveUtil = reserveUtil ; const config = serverConfigModel . getConfig ( ) ; if ( config !== null ) { if ( config . broadcast . GR === true ) { this . tabs . push ( 'GR' ) ; } if ( config . broadcast . BS === true ) { this . tabs . push ( 'BS' ) ; } if ( config . broadcast . CS === true ) { this . tabs . push ( 'CS' ) ; } if ( config . broadcast . SKY === true ) { this . tabs . push ( 'SKY' ) ; } } } /**
     * 取得した番組情報をクリア
     */ public clearData ( ) { this . schedules = [ ] ; this . reserveIndex = { } ; } param option public async fetchData ( option ) { const now = new Date ( ) . getTime ( ) ; this . reserveIndex = await this . reserveUtil . getReserveIndex ( { startAt : now , endAt : now + 3600 * 1000 , } ) ; const datas = await this . scheduleApiModel . getScheduleOnAir ( option ) ; this . schedules = datas . map ( d => { return this . createDisplayData ( now , d ) ; } ) ; } param baseTime param schedule return private createDisplayData ( baseTime , schedule ) { const startAt = DateUtil . getJaDate ( new Date ( schedule . programs [ 0 ] . startAt ) ) ; const endAt = DateUtil . getJaDate ( new Date ( schedule . programs [ 0 ] . endAt ) ) ; const result = { display : { channelId : schedule . channel . id , channelName : schedule . channel . name , time : `${ DateUtil . format ( startAt , 'hh:mm' ) } ~ ${ DateUtil . format ( endAt , 'hh:mm' ) }` , name : schedule . programs [ 0 ] . name , description : schedule . programs [ 0 ] . description , extended : schedule . programs [ 0 ] . extended , digestibility : this . getDigestibility ( baseTime , schedule . programs [ 0 ] . startAt , schedule . programs [ 0 ] . endAt ) , } , schedule : schedule , } ; if ( schedule . channel . hasLogoData === true ) { result . display . logoSrc = `./api/channels/${ schedule . channel . id . toString ( 10 ) }/logo` ; } return result ; } param baseTime param startAt param endAt return private getDigestibility ( baseTime , startAt , endAt ) { if ( baseTime <= startAt ) { return 0 ; } return ( ( baseTime - startAt ) / ( endAt - startAt ) ) * 100 ; } /**
     * digestibility を更新する
     */ public updateDigestibility ( ) { const now = new Date ( ) . getTime ( ) ; for ( const s of this . schedules ) { s . display . digestibility = s . schedule . programs . length === 0 ? 0 : this . getDigestibility ( now , s . schedule . programs [ 0 ] . startAt , s . schedule . programs [ 0 ] . endAt ) ; } } return public getSchedules ( type ? ) { return typeof type === 'undefined' ? this . schedules : this . schedules . filter ( s => { return s . schedule . channel . channelType === type ; } ) ; } return public getReserveIndex ( ) { return this . reserveIndex ; } return public getTabs ( ) { return this . tabs ; } return public getUpdateTime ( ) { if ( this . schedules . length === 0 ) { return 1000 ; } let min = 6048000000 ; const now = new Date ( ) . getTime ( ) ; for ( const s of this . schedules ) { const endTime = s . schedule . programs [ 0 ] . endAt - now ; if ( min > endTime ) { min = endTime ; } } if ( min < 0 ) { min = 0 ; } return min ; } }