import { ChildProcess } from 'child_process' ; import { inject , injectable } from 'inversify' ; import * as apid from '../../../api' ; import IOperatorEncodeEvent , { OperatorFinishEncodeInfo } from '../event/IOperatorEncodeEvent' ; import IRecordedManageModel , { AddVideoFileOption , UploadedVideoFileOption , } from '../operator/recorded/IRecordedManageModel' ; import IRecordedTagManadeModel from '../operator/recordedTag/IRecordedTagManadeModel' ; import IRecordingManageModel from '../operator/recording/IRecordingManageModel' ; import IReservationManageModel from '../operator/reservation/IReservationManageModel' ; import IRuleManageModel from '../operator/rule/IRuleManageModel' ; import IThumbnailManageModel from '../operator/thumbnail/IThumbnailManageModel' ; import IIPCServer from './IIPCServer' ; import { OperatorEncodeEventFunctions , ModelName , NotifyClientMessage , PushEncodeMessage , RecordedFunctions , RecordedTagFunctions , RecordingFunctions , ReplayMessage , ReserveationFunctions , RuleFuntions , SendMessage , ThumbnailFunctions , } from './IPCMessageDefine' ; interface IFunctionIndex { [ functionName ] : ( msg : SendMessage ) => Promise < any > ; } @ injectable ( ) export default class IPCServer implements IIPCServer { private reservationManage : IReservationManageModel ; private recordedManage : IRecordedManageModel ; private recordedTagManage : IRecordedTagManadeModel ; private recordingManage : IRecordingManageModel ; private ruleManage : IRuleManageModel ; private thumbnailManage : IThumbnailManageModel ; private encodeEvent : IOperatorEncodeEvent ; private child : ChildProcess | null = null ; private functions : { [ modelName ] : IFunctionIndex ; } = { } ; constructor ( @ inject ( 'IReservationManageModel' ) reservationManage , @ inject ( 'IRecordedManageModel' ) recordedManage , @ inject ( 'IRecordedTagManadeModel' ) recordedTagManage , @ inject ( 'IRecordingManageModel' ) recordingManage , @ inject ( 'IRuleManageModel' ) ruleManage , @ inject ( 'IThumbnailManageModel' ) thumbnailManage , @ inject ( 'IOperatorEncodeEvent' ) encodeEvent ) { this . reservationManage = reservationManage ; this . recordedManage = recordedManage ; this . recordedTagManage = recordedTagManage ; this . recordingManage = recordingManage ; this . ruleManage = ruleManage ; this . thumbnailManage = thumbnailManage ; this . encodeEvent = encodeEvent ; this . init ( ) ; } public register ( child ) { this . child = child ; this . child . on ( 'message' , async ( msg ) => { if ( typeof this . functions [ msg . model ] !== 'undefined' && typeof this . functions [ msg . model ] [ msg . func ] !== 'undefined' ) { try { const result = await this . functions [ msg . model ] [ msg . func ] ( msg ) ; this . replay ( { id : msg . id , result : result , } ) ; } catch ( err ) { this . replay ( { id : msg . id , error : err . message , } ) ; } } else { this . replay ( { id : msg . id , error : 'IPCFunctionError' , } ) ; } } ) ; } /**
     * 子プロセスに socket.io による状態更新通知を依頼する
     */ public notifyClient ( ) { if ( this . child === null ) { return ; } this . child . send ( < any > ( < NotifyClientMessage > { type : 'notifyClient' , } ) ) ; } param addOption public setEncode ( addOption ) { if ( this . child === null ) { throw new Error ( 'ChildIsNull' ) ; } this . child . send ( < any > ( < PushEncodeMessage > { type : 'pushEncode' , value : addOption , } ) ) ; } param msg private replay ( msg ) { if ( this . child === null ) { throw new Error ( 'IPCSendReplayError' ) ; } this . child . send ( msg ) ; } /**
     * 関数登録処理
     */ private init ( ) { this . functions [ ModelName . reserveation ] = this . getReserveationFunctions ( ) ; this . functions [ ModelName . recorded ] = this . getRecordedFunctions ( ) ; this . functions [ ModelName . recordedTag ] = this . getRecordedTagFunctions ( ) ; this . functions [ ModelName . recording ] = this . getRecordingFunctions ( ) ; this . functions [ ModelName . rule ] = this . getRuleFunctions ( ) ; this . functions [ ModelName . thumbnail ] = this . getThumbnailFunctions ( ) ; this . functions [ ModelName . encodeEvent ] = this . getOperatorEncodeEventFunctions ( ) ; } /**
     * set reserveation functions
     */ private getReserveationFunctions ( ) { const index = { } ; index [ ReserveationFunctions . getBroadcastStatus ] = async ( ) => { return this . reservationManage . getBroadcastStatus ( ) ; } ; index [ ReserveationFunctions . add ] = async ( msg ) => { const option = this . getArgsValue < apid . ManualReserveOption > ( msg , 'option' ) ; return await this . reservationManage . add ( option ) ; } ; index [ ReserveationFunctions . update ] = async ( msg ) => { const reserveId = this . getArgsValue < apid . ReserveId > ( msg , 'reserveId' ) ; await this . reservationManage . update ( reserveId ) ; } ; index [ ReserveationFunctions . updateRule ] = async ( msg ) => { const ruleId = this . getArgsValue < apid . RuleId > ( msg , 'ruleId' ) ; await this . reservationManage . updateRule ( ruleId ) ; } ; index [ ReserveationFunctions . updateAll ] = async ( msg ) => { const isUntilComplete = this . getArgsValue < boolean > ( msg , 'isUntilComplete' ) ; if ( isUntilComplete === true ) { await this . reservationManage . updateAll ( ) ; } else { this . reservationManage . updateAll ( ) ; } } ; index [ ReserveationFunctions . cancel ] = async ( msg ) => { const reserveId = this . getArgsValue < apid . ReserveId > ( msg , 'reserveId' ) ; await this . reservationManage . cancel ( reserveId ) ; } ; index [ ReserveationFunctions . removeSkip ] = async ( msg ) => { const reserveId = this . getArgsValue < apid . ReserveId > ( msg , 'reserveId' ) ; await this . reservationManage . removeSkip ( reserveId ) ; } ; index [ ReserveationFunctions . removeOverlap ] = async ( msg ) => { const reserveId = this . getArgsValue < apid . ReserveId > ( msg , 'reserveId' ) ; await this . reservationManage . removeOverlap ( reserveId ) ; } ; index [ ReserveationFunctions . edit ] = async ( msg ) => { const reserveId = this . getArgsValue < apid . ReserveId > ( msg , 'reserveId' ) ; const option = this . getArgsValue < apid . EditManualReserveOption > ( msg , 'option' ) ; await this . reservationManage . edit ( reserveId , option ) ; } ; return index ; } /**
     * set recorded functions
     */ private getRecordedFunctions ( ) { const index = { } ; index [ RecordedFunctions . delete ] = async ( msg ) => { const recordedId = this . getArgsValue < apid . RecordedId > ( msg , 'recordedId' ) ; await this . recordedManage . delete ( recordedId ) ; } ; index [ RecordedFunctions . updateVideoFileSize ] = async ( msg ) => { const videoFileId = this . getArgsValue < apid . VideoFileId > ( msg , 'videoFileId' ) ; await this . recordedManage . updateVideoFileSize ( videoFileId ) ; } ; index [ RecordedFunctions . addVideoFile ] = async ( msg ) => { const option = this . getArgsValue < AddVideoFileOption > ( msg , 'option' ) ; return await this . recordedManage . addVideoFile ( option ) ; } ; index [ RecordedFunctions . addUploadedVideoFile ] = async ( msg ) => { const option = this . getArgsValue < UploadedVideoFileOption > ( msg , 'option' ) ; await this . recordedManage . addUploadedVideoFile ( option ) ; } ; index [ RecordedFunctions . createNewRecorded ] = async ( msg ) => { const option = this . getArgsValue < apid . CreateNewRecordedOption > ( msg , 'option' ) ; return await this . recordedManage . createNewRecorded ( option ) ; } ; index [ RecordedFunctions . deleteVideoFile ] = async ( msg ) => { const videoFileId = this . getArgsValue < apid . VideoFileId > ( msg , 'videoFileId' ) ; await this . recordedManage . deleteVideoFile ( videoFileId ) ; } ; index [ RecordedFunctions . changeProtect ] = async ( msg ) => { const recordedId = this . getArgsValue < apid . RecordedId > ( msg , 'recordedId' ) ; const isProtect = this . getArgsValue < boolean > ( msg , 'isProtect' ) ; await this . recordedManage . changeProtect ( recordedId , isProtect ) ; } ; index [ RecordedFunctions . videoFileCleanup ] = async ( ) => { await this . recordedManage . videoFileCleanup ( ) ; } ; index [ RecordedFunctions . dropLogFileCleanup ] = async ( ) => { await this . recordedManage . dropLogFileCleanup ( ) ; } ; return index ; } /**
     * set recordedTag functions
     */ private getRecordedTagFunctions ( ) { const index = { } ; index [ RecordedTagFunctions . create ] = async ( msg ) => { const name = this . getArgsValue < string > ( msg , 'name' ) ; const color = this . getArgsValue < string > ( msg , 'color' ) ; return await this . recordedTagManage . create ( name , color ) ; } ; index [ RecordedTagFunctions . update ] = async ( msg ) => { const tagId = this . getArgsValue < apid . RecordedTagId > ( msg , 'tagId' ) ; const name = this . getArgsValue < string > ( msg , 'name' ) ; const color = this . getArgsValue < string > ( msg , 'color' ) ; await this . recordedTagManage . update ( tagId , name , color ) ; } ; index [ RecordedTagFunctions . setRelation ] = async ( msg ) => { const tagId = this . getArgsValue < apid . RecordedTagId > ( msg , 'tagId' ) ; const recordedId = this . getArgsValue < apid . RecordedId > ( msg , 'recordedId' ) ; await this . recordedTagManage . setRelation ( tagId , recordedId ) ; } ; index [ RecordedTagFunctions . delete ] = async ( msg ) => { const tagId = this . getArgsValue < apid . RecordedTagId > ( msg , 'tagId' ) ; await this . recordedTagManage . delete ( tagId ) ; } ; index [ RecordedTagFunctions . deleteRelation ] = async ( msg ) => { const tagId = this . getArgsValue < apid . RecordedTagId > ( msg , 'tagId' ) ; const recordedId = this . getArgsValue < apid . RecordedId > ( msg , 'recordedId' ) ; await this . recordedTagManage . deleteRelation ( tagId , recordedId ) ; } ; return index ; } /**
     * set recording functions
     */ private getRecordingFunctions ( ) { const index = { } ; index [ RecordingFunctions . resetTimer ] = async ( ) => { this . recordingManage . resetTimer ( ) ; } ; return index ; } /**
     * set rule functions
     */ private getRuleFunctions ( ) { const index = { } ; index [ RuleFuntions . add ] = async ( msg ) => { const rule = this . getArgsValue < apid . AddRuleOption > ( msg , 'rule' ) ; return await this . ruleManage . add ( rule ) ; } ; index [ RuleFuntions . update ] = async ( msg ) => { const rule = this . getArgsValue < apid . Rule > ( msg , 'rule' ) ; await this . ruleManage . update ( rule ) ; } ; index [ RuleFuntions . enable ] = async ( msg ) => { const ruleId = this . getArgsValue < apid . RuleId > ( msg , 'ruleId' ) ; await this . ruleManage . enable ( ruleId ) ; } ; index [ RuleFuntions . disable ] = async ( msg ) => { const ruleId = this . getArgsValue < apid . RuleId > ( msg , 'ruleId' ) ; await this . ruleManage . disable ( ruleId ) ; } ; index [ RuleFuntions . delete ] = async ( msg ) => { const ruleId = this . getArgsValue < apid . RuleId > ( msg , 'ruleId' ) ; await this . ruleManage . delete ( ruleId ) ; } ; index [ RuleFuntions . deletes ] = async ( msg ) => { const ruleIds = this . getArgsValue < apid . RuleId [ ] > ( msg , 'ruleIds' ) ; await this . ruleManage . deletes ( ruleIds ) ; } ; return index ; } /**
     * set thumbnail functions
     */ private getThumbnailFunctions ( ) { const index = { } ; index [ ThumbnailFunctions . regenerate ] = async ( ) => { await this . thumbnailManage . regenerate ( ) ; } ; index [ ThumbnailFunctions . fileCleanup ] = async ( ) => { await this . thumbnailManage . fileCleanup ( ) ; } ; index [ ThumbnailFunctions . add ] = async ( msg ) => { const videoFileId = this . getArgsValue < apid . VideoFileId > ( msg , 'videoFileId' ) ; this . thumbnailManage . add ( videoFileId ) ; } ; index [ ThumbnailFunctions . delete ] = async ( msg ) => { const thumbnailId = this . getArgsValue < apid . ThumbnailId > ( msg , 'thumbnailId' ) ; await this . thumbnailManage . delete ( thumbnailId ) ; } ; return index ; } /**
     * set operator encode event functions
     */ private getOperatorEncodeEventFunctions ( ) { const index = { } ; index [ OperatorEncodeEventFunctions . emitFinishEncode ] = async ( msg ) => { const info = this . getArgsValue < OperatorFinishEncodeInfo > ( msg , 'info' ) ; this . encodeEvent . emitFinishEncode ( info ) ; } ; return index ; } param msg param argsName return private getArgsValue < T > ( msg , argsName ) { if ( typeof msg . args === 'undefined' || typeof msg . args [ argsName ] === 'undefined' ) { throw new Error ( 'IPCArgsError' ) ; } return < T > msg . args [ argsName ] ; } }