import { inject , injectable } from 'inversify' ; import * as apid from '../../../api' ; import IConfigFile from '../IConfigFile' ; import IConfiguration from '../IConfiguration' ; import ILogger from '../ILogger' ; import ILoggerModel from '../ILoggerModel' ; import IIPCServer from '../ipc/IIPCServer' ; import IExternalCommandManageModel from '../operator/externalCommand/IExternalCommandManageModel' ; import IRecordedManageModel from '../operator/recorded/IRecordedManageModel' ; import IRecordedTagManadeModel from '../operator/recordedTag/IRecordedTagManadeModel' ; import IRecordingManageModel from '../operator/recording/IRecordingManageModel' ; import IReservationManageModel from '../operator/reservation/IReservationManageModel' ; import IThumbnailManageModel from '../operator/thumbnail/IThumbnailManageModel' ; import IOperatorEncodeEvent from './IOperatorEncodeEvent' ; import IEPGUpdateEvent from './IEPGUpdateEvent' ; import IEventSetter from './IEventSetter' ; import IRecordedEvent from './IRecordedEvent' ; import IRecordedTagEvent from './IRecordedTagEvent' ; import IRecordingEvent from './IRecordingEvent' ; import IReserveEvent from './IReserveEvent' ; import IRuleEvent from './IRuleEvent' ; import IThumbnailEvent from './IThumbnailEvent' ; @ injectable ( ) export default class EventSetter implements IEventSetter { private log : ILogger ; private epgUpdateEvent : IEPGUpdateEvent ; private encodeEvent : IOperatorEncodeEvent ; private ruleEvent : IRuleEvent ; private reserveEvent : IReserveEvent ; private recordedEvent : IRecordedEvent ; private recordingEvent : IRecordingEvent ; private recordedTagEvent : IRecordedTagEvent ; private thumbnailEvent : IThumbnailEvent ; private reservationManage : IReservationManageModel ; private recordingManage : IRecordingManageModel ; private recordedManage : IRecordedManageModel ; private recordedTagManage : IRecordedTagManadeModel ; private thumbnailManage : IThumbnailManageModel ; private externalCommandManage : IExternalCommandManageModel ; private ipc : IIPCServer ; private config : IConfigFile ; private isFirstreserveationUpdate : boolean = true ; constructor ( @ inject ( 'ILoggerModel' ) logger , @ inject ( 'IEPGUpdateEvent' ) epgUpdateEvent , @ inject ( 'IOperatorEncodeEvent' ) encodeEvent , @ inject ( 'IRuleEvent' ) ruleEvent , @ inject ( 'IReserveEvent' ) reserveEvent , @ inject ( 'IRecordingEvent' ) recordingEvent , @ inject ( 'IRecordedTagEvent' ) recordedTagEvent , @ inject ( 'IRecordedEvent' ) recordedEvent , @ inject ( 'IThumbnailEvent' ) thumbnailEvent , @ inject ( 'IReservationManageModel' ) reservationManage , @ inject ( 'IRecordingManageModel' ) recordingManage , @ inject ( 'IRecordedManageModel' ) recordedManage , @ inject ( 'IRecordedTagManadeModel' ) recordedTagManage , @ inject ( 'IThumbnailManageModel' ) thumbnailManage , @ inject ( 'IExternalCommandManageModel' ) externalCommandManage , @ inject ( 'IIPCServer' ) ipc , @ inject ( 'IConfiguration' ) configure ) { this . log = logger . getLogger ( ) ; this . epgUpdateEvent = epgUpdateEvent ; this . encodeEvent = encodeEvent ; this . ruleEvent = ruleEvent ; this . reserveEvent = reserveEvent ; this . recordedEvent = recordedEvent ; this . recordingEvent = recordingEvent ; this . recordedTagEvent = recordedTagEvent ; this . thumbnailEvent = thumbnailEvent ; this . reservationManage = reservationManage ; this . recordingManage = recordingManage ; this . recordedManage = recordedManage ; this . recordedTagManage = recordedTagManage ; this . thumbnailManage = thumbnailManage ; this . externalCommandManage = externalCommandManage ; this . ipc = ipc ; this . config = configure . getConfig ( ) ; } /**
     * event をセットする
     */ public set ( ) { this . epgUpdateEvent . setUpdated ( async ( ) => { await this . recordedManage . historyCleanup ( ) . catch ( ( ) => { } ) ; await this . reservationManage . updateAll ( this . isFirstreserveationUpdate ) ; this . isFirstreserveationUpdate = false ; } ) ; this . ruleEvent . setAdded ( ruleId => { this . ipc . notifyClient ( ) ; this . reservationManage . updateRule ( ruleId ) ; } ) ; this . ruleEvent . setUpdated ( ruleId => { this . ipc . notifyClient ( ) ; this . reservationManage . updateRule ( ruleId ) ; } ) ; this . ruleEvent . setEnabled ( ruleId => { this . ipc . notifyClient ( ) ; this . reservationManage . updateRule ( ruleId ) ; } ) ; this . ruleEvent . setDisabled ( ruleId => { this . ipc . notifyClient ( ) ; this . reservationManage . updateRule ( ruleId ) ; } ) ; this . ruleEvent . setDeleted ( ruleId => { this . ipc . notifyClient ( ) ; this . recordedManage . removeRuleId ( ruleId ) . catch ( err => { this . log . system . error ( `failed to remove ruleId from recorded. ruleId: ${ ruleId }` ) ; this . log . system . error ( err ) ; } ) ; this . reservationManage . updateRule ( ruleId ) . catch ( err => { this . log . system . error ( `falied to update rule. ruleId: ${ ruleId }` ) ; this . log . system . error ( err ) ; } ) ; } ) ; this . reserveEvent . setUpdated ( diff => { this . ipc . notifyClient ( ) ; this . recordingManage . update ( diff ) ; this . externalCommandManage . addUpdateReseves ( diff ) ; } ) ; this . recordingEvent . setStartPrepRecording ( reserve => { this . ipc . notifyClient ( ) ; this . externalCommandManage . addRecordingPrepStartCmd ( reserve ) ; } ) ; this . recordingEvent . setCancelPrepRecording ( reserve => { this . ipc . notifyClient ( ) ; this . externalCommandManage . addRecordingPrepRecFailedCmd ( reserve ) ; } ) ; this . recordingEvent . setPrepRecordingFailed ( reserve => { this . ipc . notifyClient ( ) ; this . reservationManage . cancel ( reserve . id ) ; this . externalCommandManage . addRecordingPrepRecFailedCmd ( reserve ) ; } ) ; this . recordingEvent . setStartRecording ( async ( reserve , recorded ) => { if ( reserve . tags !== null ) { await this . setTag ( recorded . id , reserve . tags ) . catch ( err => { this . log . system . fatal ( 'setTag error' ) ; this . log . system . fatal ( err ) ; } ) ; } this . ipc . notifyClient ( ) ; this . externalCommandManage . addRecordingStartCmd ( recorded ) ; } ) ; this . recordingEvent . setRecordingFailed ( ( _reserve , recorded ) => { this . ipc . notifyClient ( ) ; if ( recorded !== null ) { this . externalCommandManage . addRecordingFailedCmd ( recorded ) ; } } ) ; this . recordingEvent . setRecordingRetryOver ( reserve => { this . reservationManage . cancel ( reserve . id ) . catch ( ( ) => { } ) ; } ) ; this . recordingEvent . setFinishRecording ( async ( reserve , recorded , isNeedDeleteReservation ) => { if ( isNeedDeleteReservation === true ) { if ( reserve . ruleId === null ) { this . reservationManage . cancel ( reserve . id ) . catch ( ( ) => { } ) ; } else { this . reservationManage . updateRule ( reserve . ruleId ) . catch ( ( ) => { } ) ; } } if ( typeof recorded . videoFiles !== 'undefined' && recorded . videoFiles . length > 0 ) { this . thumbnailManage . add ( recorded . videoFiles [ 0 ] . id ) ; if ( reserve . encodeMode1 !== null ) { this . ipc . setEncode ( { recordedId : recorded . id , sourceVideoFileId : recorded . videoFiles [ 0 ] . id , parentDir : reserve . encodeParentDirectoryName1 === null ? this . config . recorded [ 0 ] . name : reserve . encodeParentDirectoryName1 , directory : reserve . encodeDirectory1 === null ? undefined : reserve . encodeDirectory1 , mode : reserve . encodeMode1 , removeOriginal : reserve . isDeleteOriginalAfterEncode , } ) ; } if ( reserve . encodeMode2 !== null ) { this . ipc . setEncode ( { recordedId : recorded . id , sourceVideoFileId : recorded . videoFiles [ 0 ] . id , parentDir : reserve . encodeParentDirectoryName2 === null ? this . config . recorded [ 0 ] . name : reserve . encodeParentDirectoryName2 , directory : reserve . encodeDirectory2 === null ? undefined : reserve . encodeDirectory2 , mode : reserve . encodeMode2 , removeOriginal : reserve . isDeleteOriginalAfterEncode , } ) ; } if ( reserve . encodeMode3 !== null ) { this . ipc . setEncode ( { recordedId : recorded . id , sourceVideoFileId : recorded . videoFiles [ 0 ] . id , parentDir : reserve . encodeParentDirectoryName3 === null ? this . config . recorded [ 0 ] . name : reserve . encodeParentDirectoryName3 , directory : reserve . encodeDirectory3 === null ? undefined : reserve . encodeDirectory3 , mode : reserve . encodeMode3 , removeOriginal : reserve . isDeleteOriginalAfterEncode , } ) ; } } if ( reserve . tags !== null ) { await this . setTag ( recorded . id , reserve . tags ) . catch ( err => { this . log . system . fatal ( 'setTag error' ) ; this . log . system . fatal ( err ) ; } ) ; } this . externalCommandManage . addRecordingFinishCmd ( recorded ) ; this . ipc . notifyClient ( ) ; } ) ; this . thumbnailEvent . setAdded ( ( _videoFileId , _recordedId ) => { this . ipc . notifyClient ( ) ; } ) ; this . thumbnailEvent . setDeleted ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedEvent . setDeleteRecorded ( recorded => { this . ipc . notifyClient ( ) ; if ( recorded . isRecording === true && recorded . reserveId !== null ) { this . reservationManage . cancel ( recorded . reserveId ) ; } } ) ; this . recordedEvent . setUpdateVideoFileSize ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedEvent . setAddVideoFile ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedEvent . setCreateNewRecorded ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedEvent . setAddUploadedVideoFile ( ( videoFileId , needsCreateThumbnail ) => { this . ipc . notifyClient ( ) ; if ( needsCreateThumbnail === true ) { this . thumbnailManage . add ( videoFileId ) ; } } ) ; this . recordedEvent . setDeleteVideoFile ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedTagEvent . setCreated ( _tag => { this . ipc . notifyClient ( ) ; } ) ; this . recordedTagEvent . setUpdated ( _tagId => { this . ipc . notifyClient ( ) ; } ) ; this . recordedTagEvent . setRelated ( ( _tagId , _recordedId ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedTagEvent . setDeleted ( _tagId => { this . ipc . notifyClient ( ) ; } ) ; this . recordedTagEvent . setDeletedRelation ( ( _tagId , _recordedId ) => { this . ipc . notifyClient ( ) ; } ) ; this . recordedEvent . setChangeProtect ( ( ) => { this . ipc . notifyClient ( ) ; } ) ; this . encodeEvent . setFinishEncode ( info => { this . externalCommandManage . addEncodingFinishCmd ( info ) ; } ) ; } param recordedId param tagsStr return private async setTag ( recordedId , tagsStr ) { let tags = [ ] ; try { tags = JSON . parse ( tagsStr ) ; } catch ( err ) { this . log . system . error ( `reserve tags parese error: ${ tagsStr }` ) ; this . log . system . error ( err ) ; return ; } if ( tags . length > 0 ) { for ( const tagId of tags ) { await this . recordedTagManage . setRelation ( tagId , recordedId ) . catch ( err => { this . log . system . error ( err ) ; } ) ; } } } }