88 CommandNames ,
99 ErrorCode ,
1010 NotifyType ,
11- ResponseInterface
11+ ResponseInterface ,
1212} from './types.js'
1313import { createServer , Server } from 'net'
1414
@@ -25,25 +25,17 @@ export class HyperdeckServer {
2525 onRecord ?: ( command : DeserializedCommands . RecordCommand ) => Promise < void >
2626 onStop ?: ( command : DeserializedCommand ) => Promise < void >
2727 onClipsCount ?: ( command : DeserializedCommand ) => Promise < ResponseInterface . ClipsCount >
28- onClipsGet ?: (
29- command : DeserializedCommands . ClipsGetCommand
30- ) => Promise < ResponseInterface . ClipsGet >
28+ onClipsGet ?: ( command : DeserializedCommands . ClipsGetCommand ) => Promise < ResponseInterface . ClipsGet >
3129 onClipsAdd ?: ( command : DeserializedCommands . ClipsAddCommand ) => Promise < void >
3230 onClipsClear ?: ( command : DeserializedCommand ) => Promise < void >
3331 onTransportInfo ?: ( command : DeserializedCommand ) => Promise < ResponseInterface . TransportInfo >
34- onSlotInfo ?: (
35- command : DeserializedCommands . SlotInfoCommand
36- ) => Promise < ResponseInterface . SlotInfo >
32+ onSlotInfo ?: ( command : DeserializedCommands . SlotInfoCommand ) => Promise < ResponseInterface . SlotInfo >
3733 onSlotSelect ?: ( command : DeserializedCommands . SlotSelectCommand ) => Promise < void >
3834 onGoTo ?: ( command : DeserializedCommands . GoToCommand ) => Promise < void >
3935 onJog ?: ( command : DeserializedCommands . JogCommand ) => Promise < void >
4036 onShuttle ?: ( command : DeserializedCommands . ShuttleCommand ) => Promise < void >
41- onRemote ?: (
42- command : DeserializedCommands . RemoteCommand
43- ) => Promise < ResponseInterface . RemoteOptions >
44- onConfiguration ?: (
45- command : DeserializedCommands . ConfigurationCommand
46- ) => Promise < ResponseInterface . Configuration >
37+ onRemote ?: ( command : DeserializedCommands . RemoteCommand ) => Promise < ResponseInterface . RemoteOptions >
38+ onConfiguration ?: ( command : DeserializedCommands . ConfigurationCommand ) => Promise < ResponseInterface . Configuration >
4739 onUptime ?: ( command : DeserializedCommand ) => Promise < ResponseInterface . Uptime >
4840 onFormat ?: ( command : DeserializedCommands . FormatCommand ) => Promise < ResponseInterface . Format >
4941 onIdentify ?: ( command : DeserializedCommands . IdentifyCommand ) => Promise < void >
@@ -52,9 +44,7 @@ export class HyperdeckServer {
5244 constructor ( ip ?: string , port = 9993 , maxConnections = 1 ) {
5345 this . _server = createServer ( ( socket ) => {
5446 const socketId = Math . random ( ) . toString ( 35 ) . substr ( - 6 )
55- this . _sockets [ socketId ] = new HyperdeckSocket ( socket , async ( cmd ) =>
56- this . _receivedCommand ( cmd )
57- )
47+ this . _sockets [ socketId ] = new HyperdeckSocket ( socket , async ( cmd ) => this . _receivedCommand ( cmd ) )
5848 this . _sockets [ socketId ] . on ( 'disconnected' , ( ) => {
5949 delete this . _sockets [ socketId ]
6050 } )
@@ -87,97 +77,96 @@ export class HyperdeckServer {
8777 if ( err ) return new TResponse ( err . code , err . msg )
8878 else return new TResponse ( ErrorCode . InternalError , 'internal error' )
8979 }
90- let executor :
91- | ( ( command : DeserializedCommand ) => Promise < typeof ResponseInterface | void > )
92- | undefined
80+ let executor : ( ( command : DeserializedCommand ) => Promise < typeof ResponseInterface | void > ) | undefined
9381 let resHandler : ( ( res : Hash < string > | void ) => TResponse ) | undefined
9482
95- if ( cmd . name === CommandNames . DeviceInfoCommand ) {
83+ const commandName : CommandNames = cmd . name as CommandNames
84+
85+ if ( commandName === CommandNames . DeviceInfoCommand ) {
9686 executor = this . onDeviceInfo
9787 resHandler = ( res ) => new TResponse ( SynchronousCode . DeviceInfo , 'device info' , res )
98- } else if ( cmd . name === CommandNames . DiskListCommand ) {
88+ } else if ( commandName === CommandNames . DiskListCommand ) {
9989 executor = this . onDiskList
10090 resHandler = ( res ) => new TResponse ( SynchronousCode . DiskList , 'disk list' , res )
101- } else if ( cmd . name === CommandNames . PreviewCommand ) {
91+ } else if ( commandName === CommandNames . PreviewCommand ) {
10292 executor = this . onPreview
10393 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
104- } else if ( cmd . name === CommandNames . PlayCommand ) {
94+ } else if ( commandName === CommandNames . PlayCommand ) {
10595 executor = this . onPlay
10696 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
107- } else if ( cmd . name === CommandNames . PlayrangeSetCommand ) {
97+ } else if ( commandName === CommandNames . PlayrangeSetCommand ) {
10898 executor = this . onPlayrangeSet
10999 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
110- } else if ( cmd . name === CommandNames . PlayrangeClearCommand ) {
100+ } else if ( commandName === CommandNames . PlayrangeClearCommand ) {
111101 executor = this . onPlayrangeClear
112102 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
113- } else if ( cmd . name === CommandNames . RecordCommand ) {
103+ } else if ( commandName === CommandNames . RecordCommand ) {
114104 executor = this . onRecord
115105 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
116- } else if ( cmd . name === CommandNames . StopCommand ) {
106+ } else if ( commandName === CommandNames . StopCommand ) {
117107 executor = this . onStop
118108 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
119- } else if ( cmd . name === CommandNames . ClipsCountCommand ) {
109+ } else if ( commandName === CommandNames . ClipsCountCommand ) {
120110 executor = this . onClipsCount
121111 resHandler = ( res ) => new TResponse ( SynchronousCode . ClipsCount , 'clips count' , res )
122- } else if ( cmd . name === CommandNames . ClipsGetCommand ) {
112+ } else if ( commandName === CommandNames . ClipsGetCommand ) {
123113 executor = this . onClipsGet
124114 resHandler = ( res ) => new TResponse ( SynchronousCode . ClipsInfo , 'clips info' , res )
125- } else if ( cmd . name === CommandNames . ClipsAddCommand ) {
115+ } else if ( commandName === CommandNames . ClipsAddCommand ) {
126116 executor = this . onClipsAdd
127117 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
128- } else if ( cmd . name === CommandNames . ClipsClearCommand ) {
118+ } else if ( commandName === CommandNames . ClipsClearCommand ) {
129119 executor = this . onClipsClear
130120 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
131- } else if ( cmd . name === CommandNames . TransportInfoCommand ) {
121+ } else if ( commandName === CommandNames . TransportInfoCommand ) {
132122 executor = this . onTransportInfo
133- resHandler = ( res ) =>
134- new TResponse ( SynchronousCode . TransportInfo , 'transport info' , res )
135- } else if ( cmd . name === CommandNames . SlotInfoCommand ) {
123+ resHandler = ( res ) => new TResponse ( SynchronousCode . TransportInfo , 'transport info' , res )
124+ } else if ( commandName === CommandNames . SlotInfoCommand ) {
136125 executor = this . onSlotInfo
137126 resHandler = ( res ) => new TResponse ( SynchronousCode . SlotInfo , 'slot info' , res )
138- } else if ( cmd . name === CommandNames . SlotSelectCommand ) {
127+ } else if ( commandName === CommandNames . SlotSelectCommand ) {
139128 executor = this . onSlotSelect
140129 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
141- } else if ( cmd . name === CommandNames . NotifyCommand ) {
130+ } else if ( commandName === CommandNames . NotifyCommand ) {
142131 // implemented in socket.ts
143132 return new TResponse ( SynchronousCode . OK , 'ok' )
144- } else if ( cmd . name === CommandNames . GoToCommand ) {
133+ } else if ( commandName === CommandNames . GoToCommand ) {
145134 executor = this . onGoTo
146135 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
147- } else if ( cmd . name === CommandNames . JogCommand ) {
136+ } else if ( commandName === CommandNames . JogCommand ) {
148137 executor = this . onJog
149138 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
150- } else if ( cmd . name === CommandNames . ShuttleCommand ) {
139+ } else if ( commandName === CommandNames . ShuttleCommand ) {
151140 executor = this . onShuttle
152141 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
153- } else if ( cmd . name === CommandNames . RemoteCommand ) {
142+ } else if ( commandName === CommandNames . RemoteCommand ) {
154143 executor = this . onRemote
155144 resHandler = ( res ?) => {
156145 if ( ! res ) return new TResponse ( SynchronousCode . OK , 'ok' )
157146 else return new TResponse ( SynchronousCode . Remote , 'remote' , res )
158147 }
159- } else if ( cmd . name === CommandNames . ConfigurationCommand ) {
148+ } else if ( commandName === CommandNames . ConfigurationCommand ) {
160149 executor = this . onConfiguration
161150 resHandler = ( res ) => {
162151 if ( res ) return new TResponse ( SynchronousCode . Configuration , 'configuration' , res )
163152 else return new TResponse ( SynchronousCode . OK , 'ok' )
164153 }
165- } else if ( cmd . name === CommandNames . UptimeCommand ) {
154+ } else if ( commandName === CommandNames . UptimeCommand ) {
166155 executor = this . onUptime
167156 resHandler = ( res ) => new TResponse ( SynchronousCode . Uptime , 'uptime' , res )
168- } else if ( cmd . name === CommandNames . FormatCommand ) {
157+ } else if ( commandName === CommandNames . FormatCommand ) {
169158 executor = this . onFormat
170159 resHandler = ( res ?) => {
171160 if ( res ) return new TResponse ( SynchronousCode . FormatReady , 'format ready' , res )
172161 else return new TResponse ( SynchronousCode . OK , 'ok' )
173162 }
174- } else if ( cmd . name === CommandNames . IdentifyCommand ) {
163+ } else if ( commandName === CommandNames . IdentifyCommand ) {
175164 executor = this . onIdentify
176165 resHandler = ( ) => new TResponse ( SynchronousCode . OK , 'ok' )
177- } else if ( cmd . name === CommandNames . WatchdogCommand ) {
166+ } else if ( commandName === CommandNames . WatchdogCommand ) {
178167 // implemented in socket.ts
179168 return new TResponse ( SynchronousCode . OK , 'ok' )
180- } else if ( cmd . name === CommandNames . PingCommand ) {
169+ } else if ( commandName === CommandNames . PingCommand ) {
181170 // implemented in socket.ts
182171 return new TResponse ( SynchronousCode . OK , 'ok' )
183172 }
@@ -186,6 +175,6 @@ export class HyperdeckServer {
186175 return executor ( cmd ) . then ( resHandler , intErrorCatch )
187176 }
188177
189- return Promise . reject ( )
178+ return Promise . reject ( new Error ( 'Unknown command' ) )
190179 }
191180}
0 commit comments