/
BaseDevice.ts
71 lines (58 loc) · 2.91 KB
/
BaseDevice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { BaseRpcHandler, ConstructorParameter } from "./BaseRpcHandler"
import { RpcRequest } from "./transports/Transport"
import * as utils from "../../utils"
import { dayjs, chalk, log, sleep, getDateStr, generateRandomColorcode, generateRandomColorcodeClosure } from "../../utils" // DEBUG:
// const dbg = (...v) => console.log(chalk.gray.bgYellowBright(getDateStr(), "[BaseDevice]", ...v)) // DEBUG:
export { ConstructorParameter }
export type RpcTuple = [method: string, ...params: any]
export const isRpcTuple = (arg: any): arg is RpcTuple => typeof arg !== "string" && typeof arg[0] === "string"
declare global {
interface Function {
rpc: (...params: any[]) => [string, ...any]
}
}
// MEMO: RPCメソッドからRPC文字列を取得する関数をFunctionから生やす
Function.prototype.rpc = function(...params: any) {
const method = this.toString().match(/\.exec\("(.*)"/)![1] // eslint-disable-line @typescript-eslint/no-non-null-assertion
return [method, ...params]
}
export abstract class BaseDevice extends BaseRpcHandler {
public async requestRpc(rpcRequest: RpcRequest |RpcRequest[]): Promise<string[]> {
// dbg("[requestRpc]")
const rpcRequestString = JSON.stringify(Array.isArray(rpcRequest) ? rpcRequest : [rpcRequest])
return JSON.parse(await this.request(rpcRequestString))
}
public async sendRpc(rpcRequest: RpcRequest |RpcRequest[]): Promise<void> {
// dbg("[sendRpc]")
const rpcRequestString = JSON.stringify(Array.isArray(rpcRequest) ? rpcRequest : [rpcRequest])
await this.send(rpcRequestString)
}
public async exec(rpcTuple: RpcTuple): Promise<string | undefined>
public async exec(...rpcTuple: RpcTuple): Promise<string | undefined>
public async exec(arg1: string | RpcTuple, ...arg2: any): Promise<string | undefined> {
// dbg("[exec]")
let rpcRequest: RpcRequest | undefined
if (isRpcTuple(arg1)) rpcRequest = this.createRpcRequest(arg1)
if (typeof arg1 === "string") rpcRequest = this.createRpcRequest(arg1, ...arg2)
if (!rpcRequest) return
const rpcResponse = (await this.requestRpc(rpcRequest))[0]
return rpcResponse !== "notmatch" ? rpcResponse : undefined
}
public async execs(rpcs: RpcTuple[]): Promise<(string | undefined)[]> {
// dbg("[execs]")
const rpcRequests: RpcRequest[] = rpcs.map((rpc: RpcTuple): RpcRequest => this.createRpcRequest(rpc))
const rpcResponses = (await this.requestRpc(rpcRequests)).map(rpcResponse => rpcResponse !== "notmatch" ? rpcResponse : undefined)
return rpcResponses
}
public createRpcRequest(rpcTuple: RpcTuple): RpcRequest
public createRpcRequest(...rpcTuple: RpcTuple): RpcRequest
public createRpcRequest(arg1: string | RpcTuple, ...arg2: any): RpcRequest | undefined {
// dbg("[createRpc]")
if (isRpcTuple(arg1)) return { method: arg1.shift(), params: arg1 }
if (typeof arg1 === "string") return { method: arg1, params: arg2 }
}
// utils
public utils = utils
public sleep = utils.sleep
public wait = utils.wait
}