Skip to content

Commit

Permalink
feat: disk formatting commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Balte de Wit committed Jul 26, 2019
1 parent a73084b commit 6fd124c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ describe('Parser', () => {
expect(parser.receivedString('\r\n200 ok\r\n200ok\r\n200 ok\r\n')).toHaveLength(2)
})

test('Parser: Format Command', async () => {
const rawLines = '216 format ready\r\ntgf66k'

const parser = new MultilineParser(false, () => null)
const res = parser.receivedString(rawLines)
expect(res[0]).toBeTruthy()

if (res[0]) {
expect(res[0].code).toEqual(216)
expect(res[0].name).toEqual('format ready')
expect(res[0].params).toEqual({
code: 'tgf66k'
})
}
})

})
3 changes: 2 additions & 1 deletion src/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export enum SynchronousCode {
SlotInfo = 202,
DeviceInfo = 204,
TransportInfo = 208,
Notify = 209
Notify = 209,
FormatReady = 216
}

export enum AsynchronousCode {
Expand Down
50 changes: 50 additions & 0 deletions src/commands/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { AbstractCommandNoResponse, AbstractCommand } from './abstractCommand'
import { NamedMessage, ResponseMessage } from '../message'
import { SynchronousCode } from '../codes'
import { FilesystemFormat } from '../enums'

export interface FormatCommandResponse {
code: string
}

export class FormatCommand extends AbstractCommand {
expectedResponseCode = SynchronousCode.FormatReady

filesystem?: FilesystemFormat

deserialize (msg: ResponseMessage): FormatCommandResponse {
return {
code: msg.params.code
}
}

serialize () {
const res: NamedMessage = {
name: 'format',
params: {}
}

if (this.filesystem) {
res.params['prepare'] = this.filesystem
}

return res
}
}

export class FormatConfirmCommand extends AbstractCommandNoResponse {
code?: string

serialize () {
const res: NamedMessage = {
name: 'format',
params: {}
}

if (this.code) {
res.params['confirm'] = this.code
}

return res
}
}
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { AbstractCommand, ErrorResponse } from './abstractCommand'

export * from './connect'
export * from './deviceInfo'
export * from './format'
export * from './notify'
export * from './record'
export * from './slotInfo'
Expand Down
5 changes: 5 additions & 0 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ export enum VideoFormat {
_4Kp5994 = '4Kp5994',
_4Kp60 = '4Kp60'
}

export enum FilesystemFormat {
exFAT = 'exFAT',
HFS = 'HFS+'
}
8 changes: 7 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as _ from 'underscore'
import { ResponseMessage, NamedMessage } from './message'
import { SynchronousCode } from './codes'

export function buildMessageStr (msg: NamedMessage) {
if (_.isEmpty(msg.params)) {
Expand Down Expand Up @@ -45,7 +46,12 @@ export class MultilineParser {
// if the first line has no colon, then it is a single line command
if (this._linesQueue[0].indexOf(':') === -1) {
const r = this.parseResponse(this._linesQueue.splice(0, 1))
if (r) res.push(r)
if (r) {
res.push(r)
if (r.code === SynchronousCode.FormatReady) { // edge case, where response has no header:
r.params['code'] = this._linesQueue.shift()!
}
}
continue
}

Expand Down

0 comments on commit 6fd124c

Please sign in to comment.