Skip to content

Commit

Permalink
feat: Add some more simple commands and refactor deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 11, 2018
1 parent e5e264c commit ec6e4b1
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 45 deletions.
46 changes: 30 additions & 16 deletions src/commands/abstractCommand.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import { ResponseCode } from '../codes'
import { ResponseCode, SynchronousCode } from '../codes'
import { ResponseMessage, NamedMessage } from '../message'

export interface ErrorResponse extends ResponseMessage {
}

// export interface IResponse {
// }

export interface AbstractCommand {
expectedResponseCode: ResponseCode | null
expectedResponseCode: ResponseCode

deserialize(msg: ResponseMessage)
handle(msg: ResponseMessage)

//deserialize(msg: ResponseMessage): IResponse
serialize(): NamedMessage | null

markSent()
}

export abstract class AbstractCommandBase<T> implements Promise<T>, AbstractCommand {
abstract expectedResponseCode: ResponseCode | null
abstract expectedResponseCode: ResponseCode

handle(msg: ResponseMessage) {
if (msg.Code === this.expectedResponseCode) {
this.resolve(this.deserialize(msg))
} else {
this.reject(msg)
}
}

abstract deserialize(msg: ResponseMessage)
abstract deserialize(msg: ResponseMessage): T
abstract serialize(): NamedMessage | null

private _promise: Promise<T>
Expand Down Expand Up @@ -46,16 +59,17 @@ export abstract class AbstractCommandBase<T> implements Promise<T>, AbstractComm
}
}

// export abstract class AbstractCommandBaseNoResponse extends AbstractCommandBase<boolean>{ // TODO - is this type actually needed??
// expectedResponseCode = null
export abstract class AbstractCommandBaseNoResponse extends AbstractCommandBase<boolean>{
expectedResponseCode = SynchronousCode.OK

// markSent() {
// super.markSent()
// // No response will be received, so resolve the promise now
// this.resolve(true)
// }
// markSent() {
// super.markSent()
// // No response will be received, so resolve the promise now
// this.resolve(true)
// }

// deserialize(msg: ResponseMessage){
// msg.Name
// }
// }
deserialize(msg: ResponseMessage) {
msg.Code
return true
}
}
11 changes: 4 additions & 7 deletions src/commands/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ export class DummyConnectCommand extends AbstractCommandBase<ConnectionInfoRespo
expectedResponseCode = AsynchronousCode.ConnectionInfo

deserialize (msg: ResponseMessage) {
if (msg.Code === this.expectedResponseCode) {
this.resolve({
ProtocolVersion: parseFloat(msg.Params['protocol version']),
Model: msg.Params['model'],
})
} else {
this.reject(msg)
const res: ConnectionInfoResponse = {
ProtocolVersion: parseFloat(msg.Params['protocol version']),
Model: msg.Params['model'],
}
return res
}
serialize () {
// Nothing to send
Expand Down
30 changes: 30 additions & 0 deletions src/commands/deviceInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SynchronousCode } from '../codes'
import { ResponseMessage, NamedMessage } from '../message'
import { AbstractCommandBase } from './abstractCommand'

export interface DeviceInfoCommandResponse {
ProtocolVersion: number
Model: string
UniqueId: string
}

export class DeviceInfoCommand extends AbstractCommandBase<DeviceInfoCommandResponse> {
expectedResponseCode = SynchronousCode.Notify

deserialize (msg: ResponseMessage) {
const res: DeviceInfoCommandResponse = {
ProtocolVersion: parseFloat(msg.Params['protocol version']),
Model: msg.Params['model'],
UniqueId: msg.Params['unique id'],
}
return res
}
serialize () {
const res: NamedMessage = {
Name: 'device info',
Params: {}
}

return res
}
}
5 changes: 4 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { AbstractCommand, ErrorResponse } from './abstractCommand'

export * from './notify'
export { ConnectionInfoResponse } from './connect'
export * from './deviceInfo'
export * from './notify'
export * from './record'
export * from './stop'
32 changes: 12 additions & 20 deletions src/commands/notify.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { SynchronousCode } from '../codes'
import { ResponseMessage, NamedMessage } from '../message'
import { AbstractCommandBase } from './abstractCommand'
import { AbstractCommandBase, AbstractCommandBaseNoResponse } from './abstractCommand'
import { SetBoolIfDefined } from './util'

export interface NotifyCommandResponse {
Remote: boolean
Transport: boolean
Slot: boolean
Configuration: boolean
DroppedFrames: boolean
}

export class NotifyGetCommand extends AbstractCommandBase<NotifyCommandResponse> {
expectedResponseCode = SynchronousCode.Notify

deserialize (msg: ResponseMessage) {
if (msg.Code === this.expectedResponseCode) {
this.resolve({
Remote: msg.Params['remote'] === 'true',
Transport: msg.Params['transport'] === 'true',
Slot: msg.Params['slot'] === 'true',
Configuration: msg.Params['configuration'] === 'true',
})
} else {
this.reject(msg)
const res: NotifyCommandResponse = {
Remote: msg.Params['remote'] === 'true',
Transport: msg.Params['transport'] === 'true',
Slot: msg.Params['slot'] === 'true',
Configuration: msg.Params['configuration'] === 'true',
DroppedFrames: msg.Params['dropped frames'] === 'true',
}
return res
}
serialize () {
const res: NamedMessage = {
Expand All @@ -35,21 +34,13 @@ export class NotifyGetCommand extends AbstractCommandBase<NotifyCommandResponse>
}
}

export class NotifySetCommand extends AbstractCommandBase<boolean> {
expectedResponseCode = SynchronousCode.OK

export class NotifySetCommand extends AbstractCommandBaseNoResponse {
Remote: boolean | undefined
Transport: boolean | undefined
Slot: boolean | undefined
Configuration: boolean | undefined
DroppedFrames: boolean | undefined

deserialize (msg: ResponseMessage) {
if (msg.Code === this.expectedResponseCode) {
this.resolve(true)
} else {
this.reject(msg)
}
}
serialize () {
const res: NamedMessage = {
Name: 'notify',
Expand All @@ -60,6 +51,7 @@ export class NotifySetCommand extends AbstractCommandBase<boolean> {
SetBoolIfDefined(res, 'transport', this.Transport)
SetBoolIfDefined(res, 'slot', this.Slot)
SetBoolIfDefined(res, 'configuration', this.Configuration)
SetBoolIfDefined(res, 'dropped frames', this.DroppedFrames)

return res
}
Expand Down
17 changes: 17 additions & 0 deletions src/commands/record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NamedMessage } from '../message'
import { AbstractCommandBaseNoResponse } from './abstractCommand'

export class RecordCommand extends AbstractCommandBaseNoResponse {
Filename: string | undefined

serialize () {
const res: NamedMessage = {
Name: 'record',
Params: {}
}

if (this.Filename) res.Params.Name = this.Filename

return res
}
}
13 changes: 13 additions & 0 deletions src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NamedMessage } from '../message'
import { AbstractCommandBaseNoResponse } from './abstractCommand'

export class StopCommand extends AbstractCommandBaseNoResponse {
serialize () {
const res: NamedMessage = {
Name: 'stop',
Params: {}
}

return res
}
}
2 changes: 1 addition & 1 deletion src/hyperdeck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Hyperdeck extends EventEmitter {
const cmd = this._commandQueue[0]
this._commandQueue.pop()

cmd.deserialize(resMsg) // TODO - deserialize probably shouldnt have side effects (resolving the promise...)
cmd.handle(resMsg)
this._sendQueuedCommand()
return
}
Expand Down

0 comments on commit ec6e4b1

Please sign in to comment.