提交 4dd64295 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean

上级 f932dea8
......@@ -100,14 +100,24 @@ function mp3ToWav(mp3Stream: NodeJS.ReadableStream): NodeJS.ReadableStream {
}
/**
* Baidu:
* export BAIDU_SPEECH_API_KEY=FK58sUlteAuAIXZl5dWzAHCT
* export BAIDU_SPEECH_SECRET_KEY=feaf24adcc5b8f02b147e7f7b1953030
* curl "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=${BAIDU_SPEECH_API_KEY}&client_secret=${BAIDU_SPEECH_SECRET_KEY}"
*
* OAuth: http://developer.baidu.com/wiki/index.php?title=docs/oauth/overview
* ASR: http://yuyin.baidu.com/docs/asr/57
*/
/**
* OAuth: http://developer.baidu.com/wiki/index.php?title=docs/oauth/overview
* ASR: http://yuyin.baidu.com/docs/asr/57
* YunZhiSheng:
* http://dev.hivoice.cn/download_file/USC_DevelGuide_WebAPI_audioTranscription.pdf
*/
/**
* Google:
* http://blog.csdn.net/dlangu0393/article/details/7214728
* http://elric2011.github.io/a/using_speech_recognize_service.html
*/
async function wavToText(readableStream: NodeJS.ReadableStream): Promise<string> {
const params = {
......
......@@ -24,7 +24,7 @@
"lint": "npm run eslint && npm run tslint",
"nycava": "WECHATY_HEAD=chrome nyc ava --serial --fail-fast --verbose --timeout=3m \"dist/{src,test}/**/*.spec.js\"",
"eslint": "eslint \"{bin,example,src,test}/**/*.js\" --ignore-pattern=\"test/fixture/**\"",
"tslint": "tslint \"{bin,example,src,test}/**/*.ts\" --exclude=\"test/fixture/**\" && tsc --noEmit",
"tslint": "tslint \"{bin,example,src,test}/**/*.ts\" --exclude=\"test/fixture/**\" --exclude="dist/" && tsc --noEmit",
"shlint": "bash -n bin/*.sh",
"pretest": "npm run clean && npm run lint && npm run dist",
"sloc": "sloc bin example src test index.ts --details --format cli-table --keys total,source,comment && sloc bin example src test index.ts",
......
......@@ -30,8 +30,8 @@ export class IoClient {
private state = new StateMonitor<'online', 'offline'>('IoClient', 'offline')
constructor(
private token: string = Config.token || Config.DEFAULT_TOKEN
, private log: any = new Brolog()
private token: string = Config.token || Config.DEFAULT_TOKEN,
private log: any = new Brolog(),
) {
if (!log) {
const e = new Error('constructor() log(npmlog/brolog) must be set')
......@@ -59,27 +59,22 @@ export class IoClient {
public async init(): Promise<void> {
this.log.verbose('IoClient', 'init()')
// if (/connecting|disconnecting/.test(this.currentState())) {
if (this.state.inprocess()) {
const e = new Error('state.inprocess(), skip init')
this.log.warn('IoClient', 'init() with %s', e.message)
throw e
}
// this.targetState('connected')
// this.currentState('connecting')
this.state.target('online')
this.state.current('online', false)
try {
await this.initIo()
await this.initWechaty()
// this.currentState('connected')
this.state.current('online')
return
} catch (e) {
this.log.error('IoClient', 'init() exception: %s', e.message)
// this.currentState('disconnected')
this.state.current('offline')
throw e
}
......@@ -88,7 +83,6 @@ export class IoClient {
private async initWechaty(): Promise<void> {
this.log.verbose('IoClient', 'initWechaty()')
// if (this.targetState() !== 'connected') {
if (this.state.target() !== 'online') {
const e = new Error('state.target() is not `online`, skipped')
this.log.warn('IoClient', 'initWechaty() %s', e.message)
......@@ -111,41 +105,34 @@ export class IoClient {
.catch(e => this.log.error('IoClient', 'message.ready() %s' , e))
})
await wechaty.init()
.then(_ => {
this.log.verbose('IoClient', 'wechaty.init() done')
return wechaty
})
.catch(e => {
this.log.error('IoClient', 'init() init fail: %s', e)
wechaty.quit()
throw e
})
try {
await wechaty.init()
this.log.verbose('IoClient', 'wechaty.init() done')
} catch (e) {
this.log.error('IoClient', 'init() init fail: %s', e)
wechaty.quit()
throw e
}
return
}
private async initIo(): Promise<void> {
this.log.verbose('IoClient', 'initIo() with token %s', this.token)
// if (this.targetState() !== 'connected') {
if (this.state.target() !== 'online') {
const e = new Error('initIo() targetState is not `connected`, skipped')
this.log.warn('IoClient', e.message)
throw e
}
// const io = this.io = new Io({
// wechaty: this.wechaty
// , token: this.token
// })
const io = this.io
try {
await this.io.init()
} catch (e) {
this.log.verbose('IoClient', 'initIo() init fail: %s', e.message)
throw e
}
await io.init()
.catch(e => {
this.log.verbose('IoClient', 'initIo() init fail: %s', e.message)
throw e
})
return
}
......@@ -193,27 +180,20 @@ export class IoClient {
return this.init()
}
// if (/connecting|disconnecting/.test(this.currentState())) {
if (this.state.inprocess()) {
this.log.warn('IoClient', 'start() with a pending state, not the time')
return Promise.reject('pending')
}
// this.targetState('connected')
// this.currentState('connecting')
this.state.target('online')
this.state.current('online', false)
try {
await this.initIo()
// .then(() => {
// // this.io = io
// // this.currentState('connected')
this.state.current('online')
return
} catch (e) {
this.log.error('IoClient', 'start() exception: %s', e.message)
// this.currentState('disconnected')
this.state.current('offline')
throw e
}
......@@ -221,8 +201,6 @@ export class IoClient {
public async stop(): Promise<void> {
this.log.verbose('IoClient', 'stop()')
// this.targetState('disconnected')
// this.currentState('disconnecting')
this.state.target('offline')
this.state.current('offline', false)
......@@ -260,14 +238,11 @@ export class IoClient {
public async quit(): Promise<void> {
this.log.verbose('IoClient', 'quit()')
// if (this.currentState() === 'disconnecting') {
if (this.state.current() === 'offline' && this.state.inprocess()) {
this.log.warn('IoClient', 'quit() with currentState() = `disconnecting`, skipped')
return Promise.reject('quit() with currentState = `disconnecting`')
}
// this.targetState('disconnected')
// this.currentState('disconnecting')
this.state.target('offline')
this.state.current('offline', false)
......@@ -282,7 +257,6 @@ export class IoClient {
// this.io = null
} else { this.log.warn('IoClient', 'quit() no this.io') }
// this.currentState('disconnected')
this.state.current('offline')
return
......@@ -291,7 +265,6 @@ export class IoClient {
this.log.error('IoClient', 'exception: %s', e.message)
// XXX fail safe?
// this.currentState('disconnected')
this.state.current('offline')
throw e
......
......@@ -50,8 +50,6 @@ export class Io {
private eventBuffer: IoEvent[] = []
private ws: WebSocket
// private _currentState: string
// private _targetState: string
private state = new StateMonitor<'online', 'offline'>('Io', 'offline')
private reconnectTimer: NodeJS.Timer | null
......@@ -76,32 +74,8 @@ export class Io {
, setting.protocol
, this.uuid
)
// this.purpose('offline')
// this.targetState('disconnected')
// this.currentState('disconnected')
// this.state.target('offline')
// this.state.current('offline')
}
// // targetState : 'connected' | 'disconnected'
// private targetState(newState?) {
// if (newState) {
// log.verbose('Io', 'targetState(%s)', newState)
// this._targetState = newState
// }
// return this._targetState
// }
// // currentState : 'connecting' | 'connected' | 'disconnecting' | 'disconnected'
// private currentState(newState?) {
// if (newState) {
// log.verbose('Io', 'currentState(%s)', newState)
// this._currentState = newState
// }
// return this._currentState
// }
public toString() { return 'Class Io(' + this.setting.token + ')'}
private connected() { return this.ws && this.ws.readyState === WebSocket.OPEN }
......@@ -109,8 +83,6 @@ export class Io {
public async init(): Promise<void> {
log.verbose('Io', 'init()')
// this.targetState('connected')
// this.currentState('connecting')
this.state.target('online')
this.state.current('online', false)
......@@ -118,13 +90,11 @@ export class Io {
await this.initEventHook()
await this.initWebSocket()
// this.currentState('connected')
this.state.current('online')
return
} catch (e) {
log.warn('Io', 'init() exception: %s', e.message)
// this.currentState('disconnected')
this.state.current('offline')
throw e
}
......@@ -132,7 +102,6 @@ export class Io {
private initWebSocket() {
log.verbose('Io', 'initWebSocket()')
// this.currentState('connecting')
this.state.current('online', false)
// const auth = 'Basic ' + new Buffer(this.setting.token + ':X').toString('base64')
......@@ -404,13 +373,10 @@ export class Io {
private close() {
log.verbose('Io', 'close()')
// this.targetState('disconnected')
// this.currentState('disconnecting')
this.state.target('offline')
this.state.current('offline', false)
this.ws.close()
// this.currentState('disconnected')
this.state.current('offline')
// TODO: remove listener for this.setting.wechaty.on(message )
......@@ -418,8 +384,6 @@ export class Io {
}
public quit() {
// this.targetState('disconnected')
// this.currentState('disconnecting')
this.state.target('offline')
this.state.current('offline', false)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册