@zeppos/zeus-cli 1.3.5 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/main.js +8 -8
- package/modules/build.js +5 -3
- package/modules/create.js +6 -5
- package/package.json +1 -1
- package/private-modules/zeppos-app-utils/dist/config/device.js +105 -45
- package/private-modules/zeppos-app-utils/dist/modules/create/local-app.js +3 -2
- package/private-modules/zeppos-app-utils/dist/modules/fetchDevices.js +87 -0
- package/private-modules/zeppos-app-utils/dist/modules/index.js +1 -0
- package/private-modules/zeppos-app-utils/dist/public/template/os1.0/app/fetch-api/app-side/index.js +6 -18
- package/private-modules/zeppos-app-utils/dist/public/template/os1.0/app/fetch-api/pages/index.js +6 -6
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/app-side/index.js +6 -18
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/pages/index.js +6 -6
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.b/icon.png +0 -0
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.r/icon.png +0 -0
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.s/icon.png +0 -0
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/app-side/index.js +7 -35
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.js +1 -1
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.r.layout.js +8 -8
- package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.s.layout.js +1 -1
- package/private-modules/zeppos-app-utils/dist/tools/index.js +2 -1
- package/private-modules/zeppos-app-utils/dist/config/device-server.js +0 -116
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/.prettierrc.js +0 -6
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app-side/index.js +0 -45
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app.js +0 -25
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app.json +0 -64
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/assets/gts/icon.png +0 -0
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.page.js +0 -155
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.page.json +0 -1
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.style.js +0 -92
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/i18n/en-US.po +0 -9
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/secondary-widget/index.js +0 -160
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/setting/i18n/en-US.po +0 -5
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/setting/index.js +0 -143
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/data.js +0 -67
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/defer.js +0 -35
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/device-polyfill.js +0 -6
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/es6-promise.js +0 -1178
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/event.js +0 -42
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/message-side.js +0 -1190
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/message.js +0 -1196
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/utils/constants.js +0 -1
- package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/utils/index.js +0 -38
|
@@ -1,1190 +0,0 @@
|
|
|
1
|
-
import { EventBus } from './event'
|
|
2
|
-
import { Deferred, timeout } from './defer'
|
|
3
|
-
import { json2buf, buf2json, bin2hex, str2buf, buf2str } from './data'
|
|
4
|
-
|
|
5
|
-
let logger
|
|
6
|
-
|
|
7
|
-
function initLogger() {
|
|
8
|
-
logger = Logger.getLogger('side-message')
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const DEBUG = true
|
|
12
|
-
|
|
13
|
-
export const MESSAGE_SIZE = 3600
|
|
14
|
-
export const MESSAGE_HEADER = 16
|
|
15
|
-
export const MESSAGE_PAYLOAD = MESSAGE_SIZE - MESSAGE_HEADER
|
|
16
|
-
export const HM_MESSAGE_PROTO_HEADER = 66
|
|
17
|
-
export const HM_MESSAGE_PROTO_PAYLOAD = MESSAGE_PAYLOAD - HM_MESSAGE_PROTO_HEADER
|
|
18
|
-
|
|
19
|
-
export const MessageFlag = {
|
|
20
|
-
Runtime: 0x0,
|
|
21
|
-
App: 0x1
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const MessageType = {
|
|
25
|
-
Shake: 0x1,
|
|
26
|
-
Close: 0x2,
|
|
27
|
-
Heart: 0x3,
|
|
28
|
-
Data: 0x4,
|
|
29
|
-
DataWithSystemTool: 0x5,
|
|
30
|
-
Log: 0x6
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const MessageRuntimeType = {
|
|
34
|
-
Invoke: 0x1
|
|
35
|
-
}
|
|
36
|
-
export const MessageVersion = {
|
|
37
|
-
Version1: 0x1
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const MessagePayloadType = {
|
|
41
|
-
Request: 0x1,
|
|
42
|
-
Response: 0x2,
|
|
43
|
-
Notify: 0x3
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const DataType = {
|
|
47
|
-
empty: 'empty',
|
|
48
|
-
json: 'json',
|
|
49
|
-
text: 'text',
|
|
50
|
-
bin: 'bin'
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const MessagePayloadDataTypeOp = {
|
|
54
|
-
EMPTY: 0x0,
|
|
55
|
-
TEXT: 0x1,
|
|
56
|
-
JSON: 0x2,
|
|
57
|
-
BIN: 0x3
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function getDataType(type) {
|
|
61
|
-
switch (type.toLowerCase()) {
|
|
62
|
-
case DataType.json:
|
|
63
|
-
return MessagePayloadDataTypeOp.JSON
|
|
64
|
-
case DataType.text:
|
|
65
|
-
return MessagePayloadDataTypeOp.TEXT
|
|
66
|
-
case DataType.bin:
|
|
67
|
-
return MessagePayloadDataTypeOp.BIN
|
|
68
|
-
case DataType.empty:
|
|
69
|
-
return MessagePayloadDataTypeOp.EMPTY
|
|
70
|
-
default:
|
|
71
|
-
return MessagePayloadDataTypeOp.TEXT
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 中续,结束
|
|
76
|
-
export const MessagePayloadOpCode = {
|
|
77
|
-
Continued: 0x0,
|
|
78
|
-
Finished: 0x1
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
let traceId = 10000
|
|
82
|
-
export function genTraceId() {
|
|
83
|
-
return traceId++
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
let spanId = 1000
|
|
87
|
-
export function genSpanId() {
|
|
88
|
-
return spanId++
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function getTimestamp(t = Date.now()) {
|
|
92
|
-
return t % 10000000
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
class Session extends EventBus {
|
|
96
|
-
constructor(id, type, ctx) {
|
|
97
|
-
super()
|
|
98
|
-
this.id = id
|
|
99
|
-
this.type = type // payloadType
|
|
100
|
-
this.ctx = ctx
|
|
101
|
-
this.tempBuf = null
|
|
102
|
-
this.chunks = []
|
|
103
|
-
this.count = 0
|
|
104
|
-
this.finishChunk = null
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
addChunk(payload) {
|
|
108
|
-
if (payload.opCode === MessagePayloadOpCode.Finished) {
|
|
109
|
-
this.count = payload.seqId
|
|
110
|
-
this.finishChunk = payload
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (payload.payloadLength !== payload.payload.byteLength) {
|
|
114
|
-
logger.error(
|
|
115
|
-
'receive chunk data length error, expect %d but %d',
|
|
116
|
-
payload.payloadLength,
|
|
117
|
-
payload.payload.byteLength
|
|
118
|
-
)
|
|
119
|
-
this.emit(
|
|
120
|
-
'error',
|
|
121
|
-
Error(
|
|
122
|
-
`receive chunk data length error, expect ${payload.payloadLength} but ${payload.payload.byteLength}`
|
|
123
|
-
)
|
|
124
|
-
)
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this.chunks.push(payload)
|
|
129
|
-
this.checkIfReceiveAllChunks()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
checkIfReceiveAllChunks() {
|
|
133
|
-
if (this.count !== this.chunks.length) return
|
|
134
|
-
|
|
135
|
-
for (let i = 1; i <= this.count; i++) {
|
|
136
|
-
const chunk = this.chunks.find((c) => c.seqId === i)
|
|
137
|
-
|
|
138
|
-
if (!chunk) {
|
|
139
|
-
this.releaseBuf()
|
|
140
|
-
this.emit('error', Error('receive data error'))
|
|
141
|
-
return
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const buf = chunk.payload
|
|
145
|
-
this.tempBuf = this.tempBuf ? Buffer.concat([this.tempBuf, buf]) : buf
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (!this.finishChunk) return
|
|
149
|
-
|
|
150
|
-
this.finishChunk.payload = this.tempBuf
|
|
151
|
-
this.finishChunk.payloadLength = this.finishChunk.payload.byteLength
|
|
152
|
-
|
|
153
|
-
if (this.finishChunk.totalLength !== this.finishChunk.payloadLength) {
|
|
154
|
-
logger.error(
|
|
155
|
-
'receive full data length error, expect %d but %d',
|
|
156
|
-
this.finishChunk.payloadLength,
|
|
157
|
-
this.finishChunk.payload.byteLength
|
|
158
|
-
)
|
|
159
|
-
this.emit(
|
|
160
|
-
'error',
|
|
161
|
-
Error(
|
|
162
|
-
`receive full data length error, expect ${this.finishChunk.payloadLength} but ${this.finishChunk.payload.byteLength}`
|
|
163
|
-
)
|
|
164
|
-
)
|
|
165
|
-
return
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
this.emit('data', this.finishChunk)
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
getLength() {
|
|
172
|
-
return this.tempBufLength
|
|
173
|
-
}
|
|
174
|
-
releaseBuf() {
|
|
175
|
-
this.tempBuf = null
|
|
176
|
-
this.chunks = []
|
|
177
|
-
this.finishChunk = null
|
|
178
|
-
this.count = 0
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
class SessionMgr {
|
|
183
|
-
constructor() {
|
|
184
|
-
this.sessions = new Map()
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
key(session) {
|
|
188
|
-
return `${session.id}:${session.type}`
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
newSession(id, type, ctx) {
|
|
192
|
-
const newSession = new Session(id, type, ctx)
|
|
193
|
-
this.sessions.set(this.key(newSession), newSession)
|
|
194
|
-
return newSession
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
destroy(session) {
|
|
198
|
-
session.releaseBuf()
|
|
199
|
-
this.sessions.delete(this.key(session))
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
has(id, type) {
|
|
203
|
-
return this.sessions.has(
|
|
204
|
-
this.key({
|
|
205
|
-
id,
|
|
206
|
-
type
|
|
207
|
-
})
|
|
208
|
-
)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
getById(id, type) {
|
|
212
|
-
return this.sessions.get(
|
|
213
|
-
this.key({
|
|
214
|
-
id,
|
|
215
|
-
type
|
|
216
|
-
})
|
|
217
|
-
)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
clear() {
|
|
221
|
-
this.sessions.clear()
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export class MessageBuilder extends EventBus {
|
|
226
|
-
constructor(
|
|
227
|
-
{ appId = 0, appDevicePort = 20, appSidePort = 0, ble = undefined } = {
|
|
228
|
-
appId: 0,
|
|
229
|
-
appDevicePort: 20,
|
|
230
|
-
appSidePort: 0,
|
|
231
|
-
ble: undefined
|
|
232
|
-
}
|
|
233
|
-
) {
|
|
234
|
-
super()
|
|
235
|
-
initLogger()
|
|
236
|
-
this.isDevice = typeof __ZEPPOS__ !== 'undefined'
|
|
237
|
-
this.isSide = !this.isDevice
|
|
238
|
-
|
|
239
|
-
this.appId = appId
|
|
240
|
-
this.appDevicePort = appDevicePort
|
|
241
|
-
this.appSidePort = appSidePort
|
|
242
|
-
this.ble = ble
|
|
243
|
-
this.sendMsg = this.getSafeSend()
|
|
244
|
-
this.chunkSize = MESSAGE_PAYLOAD
|
|
245
|
-
this.tempBuf = null
|
|
246
|
-
this.shakeTask = Deferred()
|
|
247
|
-
this.waitingShakePromise = this.shakeTask.promise
|
|
248
|
-
this.sessionMgr = new SessionMgr()
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
getMessageSize() {
|
|
252
|
-
return MESSAGE_SIZE
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
getMessagePayloadSize() {
|
|
256
|
-
return MESSAGE_PAYLOAD
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
getMessageHeaderSize() {
|
|
260
|
-
return MESSAGE_HEADER
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
buf2Json(buf) {
|
|
264
|
-
return buf2json(buf)
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
json2Buf(json) {
|
|
268
|
-
return json2buf(json)
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
now(t = Date.now()) {
|
|
272
|
-
return getTimestamp(t)
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
connect(cb) {
|
|
276
|
-
this.on('message', (message) => {
|
|
277
|
-
this.onMessage(message)
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
this.ble &&
|
|
281
|
-
this.ble.createConnect((index, data, size) => {
|
|
282
|
-
DEBUG &&
|
|
283
|
-
logger.warn('[RAW] [R] receive index=>%d size=>%d bin=>%s', index, size, bin2hex(data))
|
|
284
|
-
this.onFragmentData(data)
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
this.sendShake()
|
|
288
|
-
cb && cb(this)
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
disConnect(cb) {
|
|
292
|
-
logger.debug('app ble disconnect')
|
|
293
|
-
this.sendClose()
|
|
294
|
-
this.off('message')
|
|
295
|
-
this.ble && this.ble.disConnect()
|
|
296
|
-
|
|
297
|
-
cb && cb(this)
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
listen(cb) {
|
|
301
|
-
if (typeof messaging === 'undefined') {
|
|
302
|
-
return
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
messaging &&
|
|
306
|
-
messaging.peerSocket.addListener('message', (message) => {
|
|
307
|
-
DEBUG &&
|
|
308
|
-
logger.warn('[RAW] [R] receive size=>%d bin=>%s', message.byteLength, bin2hex(message))
|
|
309
|
-
this.onMessage(message)
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
this.waitingShakePromise = Promise.resolve()
|
|
313
|
-
cb && cb(this)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
buildBin(data) {
|
|
317
|
-
if (data.payload.byteLength > this.chunkSize) {
|
|
318
|
-
throw new Error(`${data.payload.byteLength} greater than max size of ${this.chunkSize}`)
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const size = this.getMessageHeaderSize() + data.payload.byteLength
|
|
322
|
-
let buf = Buffer.alloc(size)
|
|
323
|
-
let offset = 0
|
|
324
|
-
|
|
325
|
-
buf.writeUInt8(data.flag, offset)
|
|
326
|
-
offset += 1
|
|
327
|
-
|
|
328
|
-
buf.writeUInt8(data.version, offset)
|
|
329
|
-
offset += 1
|
|
330
|
-
|
|
331
|
-
buf.writeUInt16LE(data.type, offset)
|
|
332
|
-
offset += 2
|
|
333
|
-
|
|
334
|
-
buf.writeUInt16LE(data.port1, offset)
|
|
335
|
-
offset += 2
|
|
336
|
-
|
|
337
|
-
buf.writeUInt16LE(data.port2, offset)
|
|
338
|
-
offset += 2
|
|
339
|
-
|
|
340
|
-
buf.writeUInt32LE(data.appId, offset)
|
|
341
|
-
offset += 4
|
|
342
|
-
|
|
343
|
-
buf.writeUInt32LE(data.extra, offset)
|
|
344
|
-
offset += 4
|
|
345
|
-
|
|
346
|
-
buf.fill(data.payload, offset, data.payload.byteLength + offset)
|
|
347
|
-
|
|
348
|
-
return buf
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
buildShake() {
|
|
352
|
-
return this.buildBin({
|
|
353
|
-
flag: MessageFlag.App,
|
|
354
|
-
version: MessageVersion.Version1,
|
|
355
|
-
type: MessageType.Shake,
|
|
356
|
-
port1: this.appDevicePort,
|
|
357
|
-
port2: this.appSidePort,
|
|
358
|
-
appId: this.appId,
|
|
359
|
-
extra: 0,
|
|
360
|
-
payload: Buffer.from([this.appId])
|
|
361
|
-
})
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
sendShake() {
|
|
365
|
-
if (this.appSidePort === 0) {
|
|
366
|
-
const shake = this.buildShake()
|
|
367
|
-
this.sendMsg(shake)
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
buildClose() {
|
|
372
|
-
return this.buildBin({
|
|
373
|
-
flag: MessageFlag.App,
|
|
374
|
-
version: MessageVersion.Version1,
|
|
375
|
-
type: MessageType.Close,
|
|
376
|
-
port1: this.appDevicePort,
|
|
377
|
-
port2: this.appSidePort,
|
|
378
|
-
appId: this.appId,
|
|
379
|
-
extra: 0,
|
|
380
|
-
payload: Buffer.from([this.appId])
|
|
381
|
-
})
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
sendClose() {
|
|
385
|
-
if (this.appSidePort !== 0) {
|
|
386
|
-
const close = this.buildClose()
|
|
387
|
-
|
|
388
|
-
this.sendMsg(close)
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
readBin(arrayBuf) {
|
|
393
|
-
const buf = Buffer.from(arrayBuf)
|
|
394
|
-
let offset = 0
|
|
395
|
-
|
|
396
|
-
const flag = buf.readUInt8(offset)
|
|
397
|
-
offset += 1
|
|
398
|
-
|
|
399
|
-
const version = buf.readUInt8(offset)
|
|
400
|
-
offset += 1
|
|
401
|
-
|
|
402
|
-
const type = buf.readUInt16LE(offset)
|
|
403
|
-
offset += 2
|
|
404
|
-
|
|
405
|
-
const port1 = buf.readUInt16LE(offset)
|
|
406
|
-
offset += 2
|
|
407
|
-
|
|
408
|
-
const port2 = buf.readUInt16LE(offset)
|
|
409
|
-
offset += 2
|
|
410
|
-
|
|
411
|
-
const appId = buf.readUInt32LE(offset)
|
|
412
|
-
offset += 4
|
|
413
|
-
|
|
414
|
-
const extra = buf.readUInt32LE(offset)
|
|
415
|
-
offset += 4
|
|
416
|
-
|
|
417
|
-
const payload = buf.subarray(offset)
|
|
418
|
-
|
|
419
|
-
return {
|
|
420
|
-
flag,
|
|
421
|
-
version,
|
|
422
|
-
type,
|
|
423
|
-
port1,
|
|
424
|
-
port2,
|
|
425
|
-
appId,
|
|
426
|
-
extra,
|
|
427
|
-
payload
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
// opts 覆盖头部选项
|
|
432
|
-
buildData(payload, opts = {}) {
|
|
433
|
-
return this.buildBin({
|
|
434
|
-
flag: MessageFlag.App,
|
|
435
|
-
version: MessageVersion.Version1,
|
|
436
|
-
type: MessageType.Data,
|
|
437
|
-
port1: this.appDevicePort,
|
|
438
|
-
port2: this.appSidePort,
|
|
439
|
-
appId: this.appId,
|
|
440
|
-
extra: 0,
|
|
441
|
-
...opts,
|
|
442
|
-
payload
|
|
443
|
-
})
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
sendBin(buf, debug = DEBUG) {
|
|
447
|
-
// ble 发送消息
|
|
448
|
-
debug && logger.warn('[RAW] [S] send size=%d bin=%s', buf.byteLength, bin2hex(buf.buffer))
|
|
449
|
-
const result = this.ble.send(buf.buffer, buf.byteLength)
|
|
450
|
-
|
|
451
|
-
if (!result) {
|
|
452
|
-
throw Error('send message error')
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
sendBinBySide(buf, debug = DEBUG) {
|
|
457
|
-
// side 发送消息
|
|
458
|
-
debug && logger.warn('[RAW] [S] send size=%d bin=%s', buf.byteLength, bin2hex(buf.buffer))
|
|
459
|
-
messaging && messaging.peerSocket.send(buf.buffer)
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// 通用获取逻辑
|
|
463
|
-
getSafeSend() {
|
|
464
|
-
if (this.isDevice) {
|
|
465
|
-
return this.sendBin.bind(this)
|
|
466
|
-
} else {
|
|
467
|
-
return this.sendBinBySide.bind(this)
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
_logSend(buf) {
|
|
472
|
-
this.sendMsg(buf, false)
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// 大数据的复杂头部分包协议
|
|
476
|
-
sendHmProtocol(
|
|
477
|
-
{ requestId, dataBin, type, contentType, dataType },
|
|
478
|
-
{ messageType = MessageType.Data } = {}
|
|
479
|
-
) {
|
|
480
|
-
const headerSize = 0
|
|
481
|
-
const hmDataSize = HM_MESSAGE_PROTO_PAYLOAD
|
|
482
|
-
const userDataLength = dataBin.byteLength
|
|
483
|
-
|
|
484
|
-
let offset = 0
|
|
485
|
-
const _buf = Buffer.alloc(hmDataSize)
|
|
486
|
-
const traceId = requestId ? requestId : genTraceId()
|
|
487
|
-
const spanId = genSpanId()
|
|
488
|
-
let seqId = 1
|
|
489
|
-
|
|
490
|
-
const count = Math.ceil(userDataLength / hmDataSize)
|
|
491
|
-
|
|
492
|
-
function genSeqId() {
|
|
493
|
-
return seqId++
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
for (let i = 1; i <= count; i++) {
|
|
497
|
-
this.errorIfBleDisconnect()
|
|
498
|
-
if (i === count) {
|
|
499
|
-
// last
|
|
500
|
-
const tailSize = userDataLength - offset
|
|
501
|
-
const tailBuf = Buffer.alloc(headerSize + tailSize)
|
|
502
|
-
|
|
503
|
-
dataBin.copy(tailBuf, headerSize, offset, offset + tailSize)
|
|
504
|
-
offset += tailSize
|
|
505
|
-
this.sendDataWithSession(
|
|
506
|
-
{
|
|
507
|
-
traceId,
|
|
508
|
-
spanId: spanId,
|
|
509
|
-
seqId: genSeqId(),
|
|
510
|
-
payload: tailBuf,
|
|
511
|
-
type,
|
|
512
|
-
opCode: MessagePayloadOpCode.Finished,
|
|
513
|
-
totalLength: userDataLength,
|
|
514
|
-
contentType,
|
|
515
|
-
dataType
|
|
516
|
-
},
|
|
517
|
-
{
|
|
518
|
-
messageType
|
|
519
|
-
}
|
|
520
|
-
)
|
|
521
|
-
|
|
522
|
-
break
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
dataBin.copy(_buf, headerSize, offset, offset + hmDataSize)
|
|
526
|
-
offset += hmDataSize
|
|
527
|
-
|
|
528
|
-
this.sendDataWithSession(
|
|
529
|
-
{
|
|
530
|
-
traceId,
|
|
531
|
-
spanId: spanId,
|
|
532
|
-
seqId: genSeqId(),
|
|
533
|
-
payload: _buf,
|
|
534
|
-
type,
|
|
535
|
-
opCode: MessagePayloadOpCode.Continued,
|
|
536
|
-
totalLength: userDataLength,
|
|
537
|
-
contentType,
|
|
538
|
-
dataType
|
|
539
|
-
},
|
|
540
|
-
{
|
|
541
|
-
messageType
|
|
542
|
-
}
|
|
543
|
-
)
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
if (offset === userDataLength) {
|
|
547
|
-
DEBUG && logger.debug('HmProtocol send ok msgSize=> %d dataSize=> %d', offset, userDataLength)
|
|
548
|
-
} else {
|
|
549
|
-
DEBUG &&
|
|
550
|
-
logger.error('HmProtocol send error msgSize=> %d dataSize=> %d', offset, userDataLength)
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// 大数据的简单分包协议
|
|
555
|
-
sendSimpleProtocol({ dataBin }, { messageType = MessageType.Data } = {}) {
|
|
556
|
-
const dataSize = this.chunkSize
|
|
557
|
-
const headerSize = 0
|
|
558
|
-
const userDataLength = dataBin.byteLength
|
|
559
|
-
|
|
560
|
-
let offset = 0
|
|
561
|
-
const _buf = Buffer.alloc(dataSize)
|
|
562
|
-
|
|
563
|
-
const count = Math.ceil(userDataLength / dataSize)
|
|
564
|
-
|
|
565
|
-
for (let i = 1; i <= count; i++) {
|
|
566
|
-
if (i === count) {
|
|
567
|
-
// last
|
|
568
|
-
const tailSize = userDataLength - offset
|
|
569
|
-
const tailBuf = Buffer.alloc(headerSize + tailSize)
|
|
570
|
-
|
|
571
|
-
dataBin.copy(tailBuf, headerSize, offset, offset + tailSize)
|
|
572
|
-
offset += tailSize
|
|
573
|
-
this.sendSimpleData(
|
|
574
|
-
{
|
|
575
|
-
payload: tailBuf
|
|
576
|
-
},
|
|
577
|
-
{
|
|
578
|
-
messageType
|
|
579
|
-
}
|
|
580
|
-
)
|
|
581
|
-
|
|
582
|
-
break
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
dataBin.copy(_buf, headerSize, offset, offset + dataSize)
|
|
586
|
-
offset += dataSize
|
|
587
|
-
|
|
588
|
-
this.sendSimpleData(
|
|
589
|
-
{
|
|
590
|
-
payload: _buf
|
|
591
|
-
},
|
|
592
|
-
{
|
|
593
|
-
messageType
|
|
594
|
-
}
|
|
595
|
-
)
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
if (offset === userDataLength) {
|
|
599
|
-
// logger.debug('SimpleProtocol send ok msgSize=> %d dataSize=> %d', offset, userDataLength)
|
|
600
|
-
} else {
|
|
601
|
-
// logger.error('SimpleProtocol send error msgSize=> %d dataSize=> %d', offset, userDataLength)
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
sendJson({ requestId = 0, json, type = MessagePayloadType.Request, contentType, dataType }) {
|
|
606
|
-
const packageBin = json2buf(json)
|
|
607
|
-
const traceId = requestId ? requestId : genTraceId()
|
|
608
|
-
|
|
609
|
-
this.sendHmProtocol({
|
|
610
|
-
requestId: traceId,
|
|
611
|
-
dataBin: packageBin,
|
|
612
|
-
type,
|
|
613
|
-
contentType,
|
|
614
|
-
dataType
|
|
615
|
-
})
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
sendBuf({ requestId = 0, buf, type = MessagePayloadType.Request, contentType, dataType }) {
|
|
619
|
-
const traceId = requestId ? requestId : genTraceId()
|
|
620
|
-
|
|
621
|
-
return this.sendHmProtocol({
|
|
622
|
-
requestId: traceId,
|
|
623
|
-
dataBin: buf,
|
|
624
|
-
type,
|
|
625
|
-
contentType,
|
|
626
|
-
dataType
|
|
627
|
-
})
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
sendLog(str) {
|
|
631
|
-
const packageBuf = str2buf(str)
|
|
632
|
-
this.sendSimpleProtocol(
|
|
633
|
-
{
|
|
634
|
-
dataBin: packageBuf
|
|
635
|
-
},
|
|
636
|
-
{
|
|
637
|
-
messageType: MessageType.Log
|
|
638
|
-
}
|
|
639
|
-
)
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
sendDataWithSession(
|
|
643
|
-
{ traceId, spanId, seqId, payload, type, opCode, totalLength, contentType, dataType },
|
|
644
|
-
{ messageType }
|
|
645
|
-
) {
|
|
646
|
-
const payloadBin = this.buildPayload({
|
|
647
|
-
traceId,
|
|
648
|
-
spanId,
|
|
649
|
-
seqId,
|
|
650
|
-
totalLength,
|
|
651
|
-
type,
|
|
652
|
-
opCode,
|
|
653
|
-
payload,
|
|
654
|
-
contentType,
|
|
655
|
-
dataType
|
|
656
|
-
})
|
|
657
|
-
|
|
658
|
-
let data = this.isDevice
|
|
659
|
-
? this.buildData(payloadBin, {
|
|
660
|
-
type: messageType
|
|
661
|
-
})
|
|
662
|
-
: payloadBin
|
|
663
|
-
|
|
664
|
-
this.sendMsg(data)
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
sendSimpleData({ payload }, { messageType }) {
|
|
668
|
-
let data = this.isDevice
|
|
669
|
-
? this.buildData(payload, {
|
|
670
|
-
type: messageType
|
|
671
|
-
})
|
|
672
|
-
: payload
|
|
673
|
-
|
|
674
|
-
this._logSend(data)
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
buildPayload(data) {
|
|
678
|
-
const size = HM_MESSAGE_PROTO_HEADER + data.payload.byteLength
|
|
679
|
-
let buf = Buffer.alloc(size)
|
|
680
|
-
let offset = 0
|
|
681
|
-
|
|
682
|
-
// header
|
|
683
|
-
// traceId
|
|
684
|
-
buf.writeUInt32LE(data.traceId, offset)
|
|
685
|
-
offset += 4
|
|
686
|
-
|
|
687
|
-
// parentId
|
|
688
|
-
buf.writeUInt32LE(0, offset)
|
|
689
|
-
offset += 4
|
|
690
|
-
|
|
691
|
-
// spanId
|
|
692
|
-
buf.writeUInt32LE(data.spanId, offset)
|
|
693
|
-
offset += 4
|
|
694
|
-
|
|
695
|
-
// seqId // 顺序 id,消息部分顺序序列号
|
|
696
|
-
buf.writeUInt32LE(data.seqId, offset)
|
|
697
|
-
offset += 4
|
|
698
|
-
|
|
699
|
-
// message total length
|
|
700
|
-
buf.writeUInt32LE(data.totalLength, offset)
|
|
701
|
-
offset += 4
|
|
702
|
-
|
|
703
|
-
// payload length 当前
|
|
704
|
-
buf.writeUInt32LE(data.payload.byteLength, offset)
|
|
705
|
-
offset += 4
|
|
706
|
-
|
|
707
|
-
// payload type
|
|
708
|
-
buf.writeUInt8(data.type, offset)
|
|
709
|
-
offset += 1
|
|
710
|
-
|
|
711
|
-
// opCode
|
|
712
|
-
buf.writeUInt8(data.opCode, offset)
|
|
713
|
-
offset += 1
|
|
714
|
-
|
|
715
|
-
// timestamp1
|
|
716
|
-
buf.writeUInt32LE(this.now(), offset)
|
|
717
|
-
offset += 4
|
|
718
|
-
|
|
719
|
-
// timestamp2
|
|
720
|
-
buf.writeUInt32LE(0, offset)
|
|
721
|
-
offset += 4
|
|
722
|
-
|
|
723
|
-
// timestamp3
|
|
724
|
-
buf.writeUInt32LE(0, offset)
|
|
725
|
-
offset += 4
|
|
726
|
-
|
|
727
|
-
// timestamp4
|
|
728
|
-
buf.writeUInt32LE(0, offset)
|
|
729
|
-
offset += 4
|
|
730
|
-
|
|
731
|
-
// timestamp5
|
|
732
|
-
buf.writeUInt32LE(0, offset)
|
|
733
|
-
offset += 4
|
|
734
|
-
|
|
735
|
-
// timestamp6
|
|
736
|
-
buf.writeUInt32LE(0, offset)
|
|
737
|
-
offset += 4
|
|
738
|
-
|
|
739
|
-
// timestamp7
|
|
740
|
-
buf.writeUInt32LE(0, offset)
|
|
741
|
-
offset += 4
|
|
742
|
-
|
|
743
|
-
// request content data type
|
|
744
|
-
buf.writeUInt8(data.contentType, offset)
|
|
745
|
-
offset += 1
|
|
746
|
-
|
|
747
|
-
// response data type
|
|
748
|
-
buf.writeUInt8(data.dataType, offset)
|
|
749
|
-
offset += 1
|
|
750
|
-
|
|
751
|
-
buf.writeUInt16LE(0, offset)
|
|
752
|
-
offset += 2
|
|
753
|
-
|
|
754
|
-
// extra1
|
|
755
|
-
buf.writeUInt32LE(0, offset)
|
|
756
|
-
offset += 4
|
|
757
|
-
|
|
758
|
-
// extra2
|
|
759
|
-
buf.writeUInt32LE(0, offset)
|
|
760
|
-
offset += 4
|
|
761
|
-
|
|
762
|
-
// payload
|
|
763
|
-
buf.fill(data.payload, offset, data.payload.byteLength + offset)
|
|
764
|
-
|
|
765
|
-
return buf
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
readPayload(arrayBuf) {
|
|
769
|
-
const buf = Buffer.from(arrayBuf)
|
|
770
|
-
let offset = 0
|
|
771
|
-
|
|
772
|
-
const traceId = buf.readUInt32LE(offset)
|
|
773
|
-
offset += 4
|
|
774
|
-
|
|
775
|
-
const parentId = buf.readUInt32LE(offset)
|
|
776
|
-
offset += 4
|
|
777
|
-
|
|
778
|
-
const spanId = buf.readUInt32LE(offset)
|
|
779
|
-
offset += 4
|
|
780
|
-
|
|
781
|
-
const seqId = buf.readUInt32LE(offset)
|
|
782
|
-
offset += 4
|
|
783
|
-
|
|
784
|
-
const totalLength = buf.readUInt32LE(offset)
|
|
785
|
-
offset += 4
|
|
786
|
-
|
|
787
|
-
const payloadLength = buf.readUInt32LE(offset)
|
|
788
|
-
offset += 4
|
|
789
|
-
|
|
790
|
-
const payloadType = buf.readUInt8(offset)
|
|
791
|
-
offset += 1
|
|
792
|
-
|
|
793
|
-
const opCode = buf.readUInt8(offset)
|
|
794
|
-
offset += 1
|
|
795
|
-
|
|
796
|
-
const timestamp1 = buf.readUInt32LE(offset)
|
|
797
|
-
offset += 4
|
|
798
|
-
|
|
799
|
-
const timestamp2 = buf.readUInt32LE(offset)
|
|
800
|
-
offset += 4
|
|
801
|
-
|
|
802
|
-
const timestamp3 = buf.readUInt32LE(offset)
|
|
803
|
-
offset += 4
|
|
804
|
-
|
|
805
|
-
const timestamp4 = buf.readUInt32LE(offset)
|
|
806
|
-
offset += 4
|
|
807
|
-
|
|
808
|
-
const timestamp5 = buf.readUInt32LE(offset)
|
|
809
|
-
offset += 4
|
|
810
|
-
|
|
811
|
-
const timestamp6 = buf.readUInt32LE(offset)
|
|
812
|
-
offset += 4
|
|
813
|
-
|
|
814
|
-
const timestamp7 = buf.readUInt32LE(offset)
|
|
815
|
-
offset += 4
|
|
816
|
-
|
|
817
|
-
// request data type
|
|
818
|
-
const contentType = buf.readUInt8(offset)
|
|
819
|
-
offset += 1
|
|
820
|
-
|
|
821
|
-
// response data type
|
|
822
|
-
const dataType = buf.readUInt8(offset)
|
|
823
|
-
offset += 1
|
|
824
|
-
|
|
825
|
-
const extra1 = buf.readUInt16LE(offset)
|
|
826
|
-
offset += 2
|
|
827
|
-
|
|
828
|
-
const extra2 = buf.readUInt32LE(offset)
|
|
829
|
-
offset += 4
|
|
830
|
-
|
|
831
|
-
const extra3 = buf.readUInt32LE(offset)
|
|
832
|
-
offset += 4
|
|
833
|
-
|
|
834
|
-
const payload = buf.subarray(offset)
|
|
835
|
-
|
|
836
|
-
return {
|
|
837
|
-
traceId,
|
|
838
|
-
parentId,
|
|
839
|
-
spanId,
|
|
840
|
-
seqId,
|
|
841
|
-
totalLength,
|
|
842
|
-
payloadLength,
|
|
843
|
-
payloadType,
|
|
844
|
-
opCode,
|
|
845
|
-
contentType,
|
|
846
|
-
dataType,
|
|
847
|
-
timestamp1,
|
|
848
|
-
timestamp2,
|
|
849
|
-
timestamp3,
|
|
850
|
-
timestamp4,
|
|
851
|
-
timestamp5,
|
|
852
|
-
timestamp6,
|
|
853
|
-
timestamp7,
|
|
854
|
-
extra1,
|
|
855
|
-
extra2,
|
|
856
|
-
extra3,
|
|
857
|
-
payload
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
onFragmentData(bin) {
|
|
862
|
-
const data = this.readBin(bin)
|
|
863
|
-
this.emit('raw', bin)
|
|
864
|
-
|
|
865
|
-
DEBUG && logger.debug('receive data=>', JSON.stringify(data))
|
|
866
|
-
if (data.flag === MessageFlag.App && data.type === MessageType.Shake) {
|
|
867
|
-
this.appSidePort = data.port2
|
|
868
|
-
logger.debug('appSidePort=>', data.port2)
|
|
869
|
-
this.shakeTask.resolve()
|
|
870
|
-
} else if (
|
|
871
|
-
data.flag === MessageFlag.App &&
|
|
872
|
-
data.type === MessageType.Data &&
|
|
873
|
-
data.port2 === this.appSidePort
|
|
874
|
-
) {
|
|
875
|
-
this.emit('message', data.payload)
|
|
876
|
-
this.emit('read', data)
|
|
877
|
-
} else if (
|
|
878
|
-
data.flag === MessageFlag.App &&
|
|
879
|
-
data.type === MessageType.DataWithSystemTool &&
|
|
880
|
-
data.port2 === this.appSidePort
|
|
881
|
-
) {
|
|
882
|
-
this.emit('message', data.payload)
|
|
883
|
-
this.emit('read', data)
|
|
884
|
-
} else if (
|
|
885
|
-
data.flag === MessageFlag.App &&
|
|
886
|
-
data.type === MessageType.Log &&
|
|
887
|
-
data.port2 === this.appSidePort
|
|
888
|
-
) {
|
|
889
|
-
this.emit('log', data.payload)
|
|
890
|
-
} else {
|
|
891
|
-
logger.error('error appSidePort=>%d data=>%j', this.appSidePort, data)
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
errorIfBleDisconnect() {}
|
|
896
|
-
|
|
897
|
-
onMessage(messagePayload) {
|
|
898
|
-
const payload = this.readPayload(messagePayload)
|
|
899
|
-
let session = this.sessionMgr.getById(payload.traceId, payload.payloadType)
|
|
900
|
-
|
|
901
|
-
if (!session) {
|
|
902
|
-
session = this.sessionMgr.newSession(payload.traceId, payload.payloadType, this)
|
|
903
|
-
|
|
904
|
-
// TODO: 需要考虑缓冲,监听回调要放到启动之前,或者没有增加监听就缓存请求
|
|
905
|
-
session.on('data', (fullPayload) => {
|
|
906
|
-
if (fullPayload.opCode === MessagePayloadOpCode.Finished) {
|
|
907
|
-
if (fullPayload.payloadType === MessagePayloadType.Request) {
|
|
908
|
-
this.emit('request', {
|
|
909
|
-
request: fullPayload,
|
|
910
|
-
response: ({ data }) => {
|
|
911
|
-
this.response({
|
|
912
|
-
requestId: fullPayload.traceId,
|
|
913
|
-
contentType: fullPayload.contentType,
|
|
914
|
-
dataType: fullPayload.dataType,
|
|
915
|
-
data
|
|
916
|
-
})
|
|
917
|
-
}
|
|
918
|
-
})
|
|
919
|
-
} else if (fullPayload.payloadType === MessagePayloadType.Response) {
|
|
920
|
-
this.emit('response', fullPayload)
|
|
921
|
-
} else if (fullPayload.payloadType === MessagePayloadType.Notify) {
|
|
922
|
-
this.emit('call', fullPayload)
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
this.emit('data', fullPayload)
|
|
926
|
-
this.sessionMgr.destroy(session)
|
|
927
|
-
}
|
|
928
|
-
})
|
|
929
|
-
|
|
930
|
-
session.on('error', (error) => {
|
|
931
|
-
this.sessionMgr.destroy(session)
|
|
932
|
-
this.emit('error', error)
|
|
933
|
-
})
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
session.addChunk(payload)
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
/**
|
|
940
|
-
* 发送请求
|
|
941
|
-
* @param {object buffer arraybuffer arraybuffer like} data 传输的数据
|
|
942
|
-
* @param {*} opts
|
|
943
|
-
* @returns
|
|
944
|
-
*/
|
|
945
|
-
request(data, opts) {
|
|
946
|
-
const _request = () => {
|
|
947
|
-
const defaultOpts = {
|
|
948
|
-
timeout: 60000,
|
|
949
|
-
contentType: 'json',
|
|
950
|
-
dataType: 'json'
|
|
951
|
-
}
|
|
952
|
-
const requestId = genTraceId()
|
|
953
|
-
const defer = Deferred()
|
|
954
|
-
opts = Object.assign(defaultOpts, opts)
|
|
955
|
-
|
|
956
|
-
const error = (error) => {
|
|
957
|
-
this.off('error', error)
|
|
958
|
-
defer.reject(error)
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
const transact = ({ traceId, payload, dataType }) => {
|
|
962
|
-
this.errorIfBleDisconnect()
|
|
963
|
-
DEBUG && logger.debug('traceId=>%d payload=>%s', traceId, payload.toString('hex'))
|
|
964
|
-
if (traceId === requestId) {
|
|
965
|
-
let result
|
|
966
|
-
switch (dataType) {
|
|
967
|
-
case MessagePayloadDataTypeOp.TEXT:
|
|
968
|
-
result = buf2str(payload)
|
|
969
|
-
break
|
|
970
|
-
case MessagePayloadDataTypeOp.BIN:
|
|
971
|
-
result = payload
|
|
972
|
-
break
|
|
973
|
-
case MessagePayloadDataTypeOp.JSON:
|
|
974
|
-
result = buf2json(payload)
|
|
975
|
-
break
|
|
976
|
-
default: // text
|
|
977
|
-
result = buf2str(payload)
|
|
978
|
-
break
|
|
979
|
-
}
|
|
980
|
-
DEBUG && logger.debug('request id=>%d payload=>%j', requestId, data)
|
|
981
|
-
DEBUG && logger.debug('response id=>%d payload=>%j', requestId, result)
|
|
982
|
-
|
|
983
|
-
this.off('response', transact)
|
|
984
|
-
this.off('error', error)
|
|
985
|
-
defer.resolve(result)
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
this.on('response', transact)
|
|
990
|
-
this.on('error', error)
|
|
991
|
-
if (Buffer.isBuffer(data)) {
|
|
992
|
-
this.sendBuf({
|
|
993
|
-
requestId,
|
|
994
|
-
buf: data,
|
|
995
|
-
type: MessagePayloadType.Request,
|
|
996
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
997
|
-
dataType: getDataType(opts.dataType)
|
|
998
|
-
})
|
|
999
|
-
} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
1000
|
-
this.sendBuf({
|
|
1001
|
-
requestId,
|
|
1002
|
-
buf: Buffer.from(data),
|
|
1003
|
-
type: MessagePayloadType.Request,
|
|
1004
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
1005
|
-
dataType: getDataType(opts.dataType)
|
|
1006
|
-
})
|
|
1007
|
-
} else {
|
|
1008
|
-
this.sendJson({
|
|
1009
|
-
requestId,
|
|
1010
|
-
json: data,
|
|
1011
|
-
type: MessagePayloadType.Request,
|
|
1012
|
-
contentType: MessagePayloadDataTypeOp.JSON,
|
|
1013
|
-
dataType: getDataType(opts.dataType)
|
|
1014
|
-
})
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
let hasReturned = false
|
|
1018
|
-
|
|
1019
|
-
return Promise.race([
|
|
1020
|
-
timeout(opts.timeout, (resolve, reject) => {
|
|
1021
|
-
if (hasReturned) {
|
|
1022
|
-
return resolve()
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
DEBUG &&
|
|
1026
|
-
logger.error(
|
|
1027
|
-
`request timeout in ${opts.timeout}ms error=> %d data=> %j`,
|
|
1028
|
-
requestId,
|
|
1029
|
-
data
|
|
1030
|
-
)
|
|
1031
|
-
this.off('response', transact)
|
|
1032
|
-
|
|
1033
|
-
reject(Error(`Timed out in ${opts.timeout}ms.`))
|
|
1034
|
-
}),
|
|
1035
|
-
defer.promise.finally(() => {
|
|
1036
|
-
hasReturned = true
|
|
1037
|
-
})
|
|
1038
|
-
])
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
return this.waitingShakePromise.then(_request)
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1044
|
-
requestCb(data, opts, cb) {
|
|
1045
|
-
const _requestCb = () => {
|
|
1046
|
-
const defaultOpts = {
|
|
1047
|
-
timeout: 60000,
|
|
1048
|
-
contentType: 'json',
|
|
1049
|
-
dataType: 'json'
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
if (typeof opts === 'function') {
|
|
1053
|
-
cb = opts
|
|
1054
|
-
opts = defaultOpts
|
|
1055
|
-
} else {
|
|
1056
|
-
opts = Object.assign(defaultOpts, opts)
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
const requestId = genTraceId()
|
|
1060
|
-
let timer1 = null
|
|
1061
|
-
let hasReturned = false
|
|
1062
|
-
|
|
1063
|
-
const transact = ({ traceId, payload, dataType }) => {
|
|
1064
|
-
DEBUG && logger.debug('traceId=>%d payload=>%s', traceId, payload.toString('hex'))
|
|
1065
|
-
if (traceId === requestId) {
|
|
1066
|
-
let result
|
|
1067
|
-
switch (dataType) {
|
|
1068
|
-
case MessagePayloadDataTypeOp.TEXT:
|
|
1069
|
-
result = buf2str(payload)
|
|
1070
|
-
break
|
|
1071
|
-
case MessagePayloadDataTypeOp.BIN:
|
|
1072
|
-
result = payload
|
|
1073
|
-
break
|
|
1074
|
-
case MessagePayloadDataTypeOp.JSON:
|
|
1075
|
-
result = buf2json(payload)
|
|
1076
|
-
break
|
|
1077
|
-
default: // text
|
|
1078
|
-
result = buf2str(payload)
|
|
1079
|
-
break
|
|
1080
|
-
}
|
|
1081
|
-
DEBUG && logger.debug('request id=>%d payload=>%j', requestId, data)
|
|
1082
|
-
DEBUG && logger.debug('response id=>%d payload=>%j', requestId, result)
|
|
1083
|
-
|
|
1084
|
-
timer1 && clearTimeout(timer1)
|
|
1085
|
-
timer1 = null
|
|
1086
|
-
this.off('response', transact)
|
|
1087
|
-
hasReturned = true
|
|
1088
|
-
cb(null, result)
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
this.on('response', transact)
|
|
1093
|
-
if (Buffer.isBuffer(data)) {
|
|
1094
|
-
this.sendBuf({
|
|
1095
|
-
requestId,
|
|
1096
|
-
buf: data,
|
|
1097
|
-
type: MessagePayloadType.Request,
|
|
1098
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
1099
|
-
dataType: getDataType(opts.dataType)
|
|
1100
|
-
})
|
|
1101
|
-
} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
1102
|
-
this.sendBuf({
|
|
1103
|
-
requestId,
|
|
1104
|
-
buf: Buffer.from(data),
|
|
1105
|
-
type: MessagePayloadType.Request,
|
|
1106
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
1107
|
-
dataType: getDataType(opts.dataType)
|
|
1108
|
-
})
|
|
1109
|
-
} else {
|
|
1110
|
-
this.sendJson({
|
|
1111
|
-
requestId,
|
|
1112
|
-
json: data,
|
|
1113
|
-
type: MessagePayloadType.Request,
|
|
1114
|
-
contentType: MessagePayloadDataTypeOp.JSON,
|
|
1115
|
-
dataType: getDataType(opts.dataType)
|
|
1116
|
-
})
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
timer1 = setTimeout(() => {
|
|
1120
|
-
timer1 = null
|
|
1121
|
-
if (hasReturned) {
|
|
1122
|
-
return
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
DEBUG &&
|
|
1126
|
-
logger.error(`request time out in ${opts.timeout}ms error=>%d data=>%j`, requestId, data)
|
|
1127
|
-
this.off('response', transact)
|
|
1128
|
-
cb(Error(`Timed out in ${opts.timeout}ms.`))
|
|
1129
|
-
}, opts.timeout)
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
return this.waitingShakePromise.then(_requestCb)
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
/**
|
|
1136
|
-
* 相应接口给当前请求
|
|
1137
|
-
* @param {obj} param0
|
|
1138
|
-
*/
|
|
1139
|
-
response({ requestId, contentType, dataType, data }) {
|
|
1140
|
-
if (MessagePayloadDataTypeOp.BIN === dataType) {
|
|
1141
|
-
this.sendBuf({
|
|
1142
|
-
requestId,
|
|
1143
|
-
buf: data,
|
|
1144
|
-
type: MessagePayloadType.Response,
|
|
1145
|
-
contentType,
|
|
1146
|
-
dataType
|
|
1147
|
-
})
|
|
1148
|
-
} else {
|
|
1149
|
-
this.sendJson({
|
|
1150
|
-
requestId,
|
|
1151
|
-
json: data,
|
|
1152
|
-
type: MessagePayloadType.Response,
|
|
1153
|
-
contentType,
|
|
1154
|
-
dataType
|
|
1155
|
-
})
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
/**
|
|
1160
|
-
* call 模式调用接口到伴生服务
|
|
1161
|
-
* @param {json | buffer} data
|
|
1162
|
-
* @returns
|
|
1163
|
-
*/
|
|
1164
|
-
call(data) {
|
|
1165
|
-
return this.waitingShakePromise.then(() => {
|
|
1166
|
-
if (Buffer.isBuffer(data)) {
|
|
1167
|
-
return this.sendBuf({
|
|
1168
|
-
buf: data,
|
|
1169
|
-
type: MessagePayloadType.Notify,
|
|
1170
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
1171
|
-
dataType: MessagePayloadDataTypeOp.EMPTY
|
|
1172
|
-
})
|
|
1173
|
-
} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
1174
|
-
return this.sendBuf({
|
|
1175
|
-
buf: Buffer.from(data),
|
|
1176
|
-
type: MessagePayloadType.Notify,
|
|
1177
|
-
contentType: MessagePayloadDataTypeOp.BIN,
|
|
1178
|
-
dataType: MessagePayloadDataTypeOp.EMPTY
|
|
1179
|
-
})
|
|
1180
|
-
} else {
|
|
1181
|
-
return this.sendJson({
|
|
1182
|
-
json: data,
|
|
1183
|
-
type: MessagePayloadType.Notify,
|
|
1184
|
-
contentType: MessagePayloadDataTypeOp.JSON,
|
|
1185
|
-
dataType: MessagePayloadDataTypeOp.EMPTY
|
|
1186
|
-
})
|
|
1187
|
-
}
|
|
1188
|
-
})
|
|
1189
|
-
}
|
|
1190
|
-
}
|