import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../../api' ; import GenreUtil from '../../../..//util/GenreUtil' ; import { AudioComponentType , AudioSamplingRate , VideoComponentType } from '../../../../lib/event' ; import DateUtil from '../../../../util/DateUtil' ; import IReservesApiModel from '../../../api/reserves/IReservesApiModel' ; import IScheduleApiModel from '../../../api/schedule/IScheduleApiModel' ; import IChannelModel from '../../../channels/IChannelModel' ; import IServerConfigModel from '../../../serverConfig/IServerConfigModel' ; import { ISettingStorageModel } from '../../../storage/setting/ISettingStorageModel' ; import IManualReserveState , { EncodedOption , ManualReserveOption , ManualSaveOption , ProgramStateData , SelectorItem , TimeSpecifiedOption } from './IManualReserveState' ; @ injectable ( ) export default class ManualReserveState implements IManualReserveState { public isTimeSpecification : boolean = false ; public timeSpecifiedOption : TimeSpecifiedOption = { name : null , channelId : null , startAt : null , endAt : null , } ; public reserveOption : ManualReserveOption = { allowEndLack : true , } ; public saveOption : ManualSaveOption = { parentDirectoryName : null , directory : null , recordedFormat : null , } ; public encodeOption : EncodedOption = { mode1 : null , encodeParentDirectoryName1 : null , directory1 : null , mode2 : null , encodeParentDirectoryName2 : null , directory2 : null , mode3 : null , encodeParentDirectoryName3 : null , directory3 : null , isDeleteOriginalAfterEncode : false , } ; public optionPanel : number [ ] = [ ] ; private scheduleApiModel : IScheduleApiModel ; private reservesApiModel : IReservesApiModel ; private channelModel : IChannelModel ; private serverConfig : IServerConfigModel ; private settingModel : ISettingStorageModel ; private programInfo : ProgramStateData | null = null ; constructor ( @ inject ( 'IScheduleApiModel' ) scheduleApiModel , @ inject ( 'IReservesApiModel' ) reservesApiModel , @ inject ( 'IChannelModel' ) channelModel , @ inject ( 'IServerConfigModel' ) serverConfig , @ inject ( 'ISettingStorageModel' ) settingModel ) { this . scheduleApiModel = scheduleApiModel ; this . reservesApiModel = reservesApiModel ; this . channelModel = channelModel ; this . serverConfig = serverConfig ; this . settingModel = settingModel ; } /**
     * 各種オプションの初期化
     */ public init ( ) { this . isTimeSpecification = false ; this . timeSpecifiedOption = { name : null , channelId : null , startAt : null , endAt : null , } ; this . reserveOption = { allowEndLack : true , } ; this . saveOption = { parentDirectoryName : null , directory : null , recordedFormat : null , } ; this . encodeOption = { mode1 : null , encodeParentDirectoryName1 : null , directory1 : null , mode2 : null , encodeParentDirectoryName2 : null , directory2 : null , mode3 : null , encodeParentDirectoryName3 : null , directory3 : null , isDeleteOriginalAfterEncode : false , } ; this . optionPanel = [ 0 , 1 , 2 , 3 , 6 ] ; } /**
     * programInfo から手動時刻オプションを設定する
     * programInfo が null の場合はエラーとなる
     */ public setTimeSpecifiedOption ( ) { if ( this . programInfo === null ) { throw new Error ( 'ProgramInfoIsNull' ) ; } this . timeSpecifiedOption . name = this . programInfo . programItem . name ; this . timeSpecifiedOption . channelId = this . programInfo . programItem . channelId ; this . timeSpecifiedOption . startAt = new Date ( this . programInfo . programItem . startAt ) ; this . timeSpecifiedOption . endAt = new Date ( this . programInfo . programItem . endAt ) ; } param reserveItem public setOptions ( reserveItem ) { this . isTimeSpecification = reserveItem . isTimeSpecified ; if ( reserveItem . isTimeSpecified === true ) { this . timeSpecifiedOption = { name : reserveItem . name , channelId : reserveItem . channelId , startAt : new Date ( reserveItem . startAt ) , endAt : new Date ( reserveItem . endAt ) , } ; } this . reserveOption . allowEndLack = reserveItem . allowEndLack ; if ( typeof reserveItem . parentDirectoryName !== 'undefined' ) { this . saveOption . parentDirectoryName = reserveItem . parentDirectoryName ; } if ( typeof reserveItem . directory !== 'undefined' ) { this . saveOption . directory = reserveItem . directory ; } if ( typeof reserveItem . recordedFormat !== 'undefined' ) { this . saveOption . recordedFormat = reserveItem . recordedFormat ; } if ( typeof reserveItem . encodeMode1 !== 'undefined' ) { this . encodeOption . mode1 = reserveItem . encodeMode1 ; } if ( typeof reserveItem . encodeParentDirectoryName1 !== 'undefined' ) { this . encodeOption . encodeParentDirectoryName1 = reserveItem . encodeParentDirectoryName1 ; } if ( typeof reserveItem . encodeDirectory1 !== 'undefined' ) { this . encodeOption . directory1 = reserveItem . encodeDirectory1 ; } if ( typeof reserveItem . encodeMode2 !== 'undefined' ) { this . encodeOption . mode2 = reserveItem . encodeMode2 ; this . optionPanel . push ( 4 ) ; } if ( typeof reserveItem . encodeParentDirectoryName2 !== 'undefined' ) { this . encodeOption . encodeParentDirectoryName2 = reserveItem . encodeParentDirectoryName2 ; } if ( typeof reserveItem . encodeDirectory2 !== 'undefined' ) { this . encodeOption . directory2 = reserveItem . encodeDirectory2 ; } if ( typeof reserveItem . encodeMode3 !== 'undefined' ) { this . encodeOption . mode3 = reserveItem . encodeMode3 ; this . optionPanel . push ( 5 ) ; } if ( typeof reserveItem . encodeParentDirectoryName3 !== 'undefined' ) { this . encodeOption . encodeParentDirectoryName3 = reserveItem . encodeParentDirectoryName3 ; } if ( typeof reserveItem . encodeDirectory3 !== 'undefined' ) { this . encodeOption . directory3 = reserveItem . encodeDirectory3 ; } if ( typeof reserveItem . encodeMode1 !== 'undefined' ) { this . encodeOption . mode1 = reserveItem . encodeMode1 ; } if ( typeof reserveItem . encodeParentDirectoryName1 !== 'undefined' ) { this . encodeOption . encodeParentDirectoryName1 = reserveItem . encodeParentDirectoryName1 ; } this . encodeOption . isDeleteOriginalAfterEncode = reserveItem . isDeleteOriginalAfterEncode ; } param programId param isHalfWidth return public async fetchProgramInfo ( programId , isHalfWidth ) { const program = await this . scheduleApiModel . getSchedule ( programId , isHalfWidth ) ; this . programInfo = this . convertScheduleProgramItemToStateData ( program , isHalfWidth ) ; } param reserveId param isHalfWidth return public async getReserveItem ( reserveId , isHalfWidth ) { return await this . reservesApiModel . get ( reserveId , isHalfWidth ) ; } param program param isHalfWidth return private convertScheduleProgramItemToStateData ( program , isHalfWidth ) { const startAt = DateUtil . getJaDate ( new Date ( program . startAt ) ) ; const endAt = DateUtil . getJaDate ( new Date ( program . endAt ) ) ; const channel = this . channelModel . findChannel ( program . channelId , isHalfWidth ) ; const result = { display : { channelName : channel === null ? program . channelId . toString ( 10 ) : channel . name , name : program . name , day : DateUtil . format ( startAt , 'MM/dd' ) , dow : DateUtil . format ( startAt , 'w' ) , startTime : DateUtil . format ( startAt , 'hh:mm' ) , endTime : DateUtil . format ( endAt , 'hh:mm' ) , duration : Math . floor ( ( program . endAt - program . startAt ) / 1000 / 60 ) , genres : this . createGenres ( program ) , description : program . description , extended : program . extended , isFree : program . isFree , } , programItem : program , } ; if ( typeof program . videoComponentType !== 'undefined' ) { const videoType = this . getVideoType ( program . videoComponentType ) ; if ( videoType !== null ) { result . display . videoType = videoType ; } } if ( typeof program . audioComponentType !== 'undefined' ) { const audioType = this . getAudioMode ( program . audioComponentType ) ; if ( audioType !== null ) { result . display . audioType = audioType ; } } if ( typeof program . audioSamplingRate !== 'undefined' ) { const audioSamplingRate = this . getAudioSamplingRate ( program . audioSamplingRate ) ; if ( audioSamplingRate !== null ) { result . display . audioSamplingRate = audioSamplingRate ; } } return result ; } param program return private createGenres ( program ) { const genres = [ ] ; if ( typeof program . genre1 !== 'undefined' ) { const genre = GenreUtil . getGenres ( program . genre1 , program . subGenre1 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } if ( typeof program . genre2 !== 'undefined' ) { const genre = GenreUtil . getGenres ( program . genre2 , program . subGenre2 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } if ( typeof program . genre3 !== 'undefined' ) { const genre = GenreUtil . getGenres ( program . genre3 , program . subGenre3 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } return genres ; } param videoComponentType return private getVideoType ( videoComponentType ) { const str = ( VideoComponentType as any ) [ videoComponentType ] ; return typeof str === 'undefined' ? null : str ; } param audioComponentType return private getAudioMode ( audioComponentType ) { const str = ( AudioComponentType as any ) [ audioComponentType ] ; return typeof str === 'undefined' ? null : str ; } return private getAudioSamplingRate ( audioSamplingRate ) { const str = AudioSamplingRate [ audioSamplingRate ] ; return typeof str === 'undefined' ? null : str ; } return public getChannelItems ( ) { return this . channelModel . getChannels ( this . settingModel . getSavedValue ( ) . isHalfWidthDisplayed ) . map ( c => { return { text : c . name , value : c . id , } ; } ) ; } return public getProgramInfo ( ) { return this . programInfo ; } return public getPrentDirectoryItems ( ) { const config = this . serverConfig . getConfig ( ) ; return config === null ? [ ] : config . recorded ; } return public getEncodeModeItems ( ) { const config = this . serverConfig . getConfig ( ) ; return config === null ? [ ] : config . encode ; } /**
     * エンコードに対応しているか
     */ public isEnableEncodeMode ( ) { return this . getEncodeModeItems ( ) . length > 0 ; } /**
     * 予約追加
     */ public async addReserve ( ) { const option = this . createManualReserveOption ( ) ; await this . reservesApiModel . add ( option ) ; } param reserveId public async updateReserve ( reserveId ) { const option = this . createEditManualReserveOption ( ) ; await this . reservesApiModel . edit ( reserveId , option ) ; } return private createManualReserveOption ( ) { const result = { allowEndLack : this . reserveOption . allowEndLack , } ; if ( this . isTimeSpecification === true ) { if ( this . timeSpecifiedOption . name === null || this . timeSpecifiedOption . name . length === 0 ) { throw new Error ( 'TimeSpecifiedOptionNameError' ) ; } if ( this . timeSpecifiedOption . channelId === null ) { throw new Error ( 'TimeSpecifiedOptionChannelIdError' ) ; } if ( this . timeSpecifiedOption . startAt === null || this . timeSpecifiedOption . endAt === null ) { throw new Error ( 'TimeSpecifiedOptionTimeError' ) ; } result . timeSpecifiedOption = { name : this . timeSpecifiedOption . name , channelId : this . timeSpecifiedOption . channelId , startAt : this . timeSpecifiedOption . startAt . getTime ( ) , endAt : this . timeSpecifiedOption . endAt . getTime ( ) , } ; } else { if ( this . programInfo === null ) { throw new Error ( 'ProgramIdIsNull' ) ; } result . programId = this . programInfo . programItem . id ; } const saveOption = this . getSaveOption ( ) ; if ( saveOption !== null ) { result . saveOption = saveOption ; } const encodeOption = this . getEncodeOption ( ) ; if ( encodeOption !== null ) { result . encodeOption = encodeOption ; } return result ; } return private createEditManualReserveOption ( ) { const result = { allowEndLack : this . reserveOption . allowEndLack , } ; const saveOption = this . getSaveOption ( ) ; if ( saveOption !== null ) { result . saveOption = saveOption ; } const encodeOption = this . getEncodeOption ( ) ; if ( encodeOption !== null ) { result . encodeOption = encodeOption ; } return result ; } return private getSaveOption ( ) { const saveOption = { } ; if ( this . saveOption . parentDirectoryName !== null ) { saveOption . parentDirectoryName = this . saveOption . parentDirectoryName ; } if ( this . saveOption . directory !== null ) { saveOption . directory = this . saveOption . directory ; } if ( this . saveOption . recordedFormat !== null ) { saveOption . recordedFormat = this . saveOption . recordedFormat ; } return Object . keys ( saveOption ) . length > 0 ? saveOption : null ; } return private getEncodeOption ( ) { const encodeOption = { isDeleteOriginalAfterEncode : this . encodeOption . isDeleteOriginalAfterEncode , } ; if ( this . encodeOption . mode1 !== null ) { encodeOption . mode1 = this . encodeOption . mode1 ; if ( this . encodeOption . encodeParentDirectoryName1 !== null ) { encodeOption . encodeParentDirectoryName1 = this . encodeOption . encodeParentDirectoryName1 ; } if ( this . encodeOption . directory1 !== null ) { encodeOption . directory1 = this . encodeOption . directory1 ; } } if ( this . encodeOption . mode2 !== null ) { encodeOption . mode2 = this . encodeOption . mode2 ; if ( this . encodeOption . encodeParentDirectoryName2 !== null ) { encodeOption . encodeParentDirectoryName2 = this . encodeOption . encodeParentDirectoryName2 ; } if ( this . encodeOption . directory2 !== null ) { encodeOption . directory2 = this . encodeOption . directory2 ; } } if ( this . encodeOption . mode3 !== null ) { encodeOption . mode3 = this . encodeOption . mode3 ; if ( this . encodeOption . encodeParentDirectoryName3 !== null ) { encodeOption . encodeParentDirectoryName3 = this . encodeOption . encodeParentDirectoryName3 ; } if ( this . encodeOption . directory3 !== null ) { encodeOption . directory3 = this . encodeOption . directory3 ; } } return Object . keys ( encodeOption ) . length > 1 ? encodeOption : null ; } }