From 6fd124cdba53e0ed9fc33201df19c5fcf05ce549 Mon Sep 17 00:00:00 2001 From: Balte de Wit Date: Fri, 26 Jul 2019 16:51:23 +0200 Subject: [PATCH] feat: disk formatting commands --- src/__tests__/parser.spec.ts | 16 ++++++++++++ src/codes.ts | 3 ++- src/commands/format.ts | 50 ++++++++++++++++++++++++++++++++++++ src/commands/index.ts | 1 + src/enums.ts | 5 ++++ src/parser.ts | 8 +++++- 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/commands/format.ts diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index 1272783..f204b5a 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -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' + }) + } + }) + }) diff --git a/src/codes.ts b/src/codes.ts index 2c2b894..e36befb 100644 --- a/src/codes.ts +++ b/src/codes.ts @@ -27,7 +27,8 @@ export enum SynchronousCode { SlotInfo = 202, DeviceInfo = 204, TransportInfo = 208, - Notify = 209 + Notify = 209, + FormatReady = 216 } export enum AsynchronousCode { diff --git a/src/commands/format.ts b/src/commands/format.ts new file mode 100644 index 0000000..8c7c1b9 --- /dev/null +++ b/src/commands/format.ts @@ -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 + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index e93c650..58e527a 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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' diff --git a/src/enums.ts b/src/enums.ts index 0d31447..c51afda 100644 --- a/src/enums.ts +++ b/src/enums.ts @@ -46,3 +46,8 @@ export enum VideoFormat { _4Kp5994 = '4Kp5994', _4Kp60 = '4Kp60' } + +export enum FilesystemFormat { + exFAT = 'exFAT', + HFS = 'HFS+' +} diff --git a/src/parser.ts b/src/parser.ts index d55fd26..6bb38e7 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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)) { @@ -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 }