Newer
Older
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { decode, makeEncode, makeRecipes, readMsg } from "../commands";
import {
CallFn,
OnFn,
Options,
USP,
Command,
Proto,
BuildConnectionFn,
} from "../types";
import { makeCallbackRouter, makeRouter } from "../util";
const defaultPublishEndpoint = "/usp/endpoint";
const defaultSubscribeEndpoint = "/usp/controller";
const defaultIdEndpoint = "obuspa/EndpointID";
const defaultFromId = "proto::interop-usp-controller";
const idResolveTimeout = 5000;
const fixId = (s: string) => s.split("+").join("%2B");
const wait = (ms: number) => {
let waitId;
let waitPromise = new Promise((_, reject) => {
waitId = setTimeout(() => {
reject(`timeout of ${ms}ms reached`);
}, ms);
});
return [waitId, waitPromise];
};
const timeoutWrapper = (promise: () => Promise<any>, ms?: number) => {
if (typeof ms === "number" && ms >= 0) {
const [waitId, waitPromise] = wait(ms);
return Promise.race([
promise().then((res) => {
clearTimeout(waitId);
return res;
}),
waitPromise,
]);
} else return promise();
};
const wrap = <T extends (...args: any[]) => any>(
opts: Options,
cmdName: keyof USP,
cmd: T
): T => {
return <T>((...args: any[]) => {
// Promise.resolve added to handle non-promise commands
const cmdCall = () => Promise.resolve(cmd(...args));
const finalCall = () =>
timeoutWrapper(cmdCall, opts.timeout)
.then((res) => {
opts.postCall && opts.postCall(cmdName, args, res);
return res;
})
.catch((err) => {
opts.postCall && opts.postCall(cmdName, args, err);
throw err;
});
if (opts.preCall) opts.preCall(cmdName, args);
return finalCall();
});
};
const addOptions = (usp: Partial<USP>, opts: Options): USP => {
const mainUSP = Object.entries(usp).reduce(
(acc: Partial<USP>, [k, fn]): Partial<USP> => ({
...acc,
[k]: wrap(opts, k as keyof USP, fn as Command),
}),
{} as any
) as USP;
mainUSP.options = (opts) => addOptions(mainUSP, opts);
return mainUSP;
};
const buildConnect: BuildConnectionFn =
({ connectClient, decodeID, loadProtobuf }) =>
/**
* Connect to device
* @param opts - Connection options
* @param events - Optional event handlers
* @returns A set of functions for interacting with the device
*/
async (options, events) => {
const subscribeEndpoint =
options.subscribeEndpoint || defaultSubscribeEndpoint;
const publishEndpoint = options.publishEndpoint || defaultPublishEndpoint;
const idEndpoint = options.idEndpoint || defaultIdEndpoint;
const proto: Proto = await loadProtobuf();
const router = makeRouter();
const callbackRouter = makeCallbackRouter();
const handleError = (err: any) =>
events && events.onError && events.onError(err);
callbackRouter.add("error", handleError);
const client = await connectClient(options);
const handleInit = () =>
new Promise<string>((resolve, reject) => {
const id = setTimeout(
() =>
reject({
errMsg: `toId was not received within timeout(${idResolveTimeout})`,
}),
idResolveTimeout
);
client.on("message", (_topic, data: any) => {
clearTimeout(id);
client.unsubscribe(idEndpoint);
resolve(decodeID(data));
});
client.subscribe(idEndpoint);
});
const toId = fixId(options.toId || (await handleInit()));
const fromId = options.fromId || defaultFromId;
client.on("message", (_topic, data: any) => {
const parsedMsg = readMsg(proto, data);
const [id, message, err, cmdType] = decode(parsedMsg);
const call = router.get(id, cmdType || "NOTIFY");
if (call && call.resolve && call.reject) {
if (err) call.reject(err);
else call.resolve(message);
}
const cbs = callbackRouter.get(id);
cbs.forEach((cb) => {
if (message) cb(message, parsedMsg);
else if (err) cb(err, parsedMsg);
});
});
client.on("error", (err) => {
callbackRouter.get("error").forEach((cb) => cb(err));
handleError(JSON.stringify(err, null, 2));
});
const on: OnFn = (ident, callback) => {
callbackRouter.add(ident, callback);
return () => {
callbackRouter.del(ident);
};
};
const encode = makeEncode(proto, { fromId, toId });
const call: CallFn = (command, args, callOpts) =>
new Promise((resolve, reject) => {
const [id, msg, err] = encode(command, args);
if (err) reject(err);
else {
router.add(id, callOpts?.responseMsgType || command, {
resolve,
reject,
});
client.publish(publishEndpoint, msg);
}
});
await client.subscribe(subscribeEndpoint);
const baseUSP: Partial<USP> = {
get: (paths) => call("GET", { paths }),
set: (path, value) => call("SET", { path, value }),
add: (path, value) => call("ADD", { path, value }),
del: (paths, allowPartial) => call("DELETE", { paths, allowPartial }),
instances: (paths, opts) => call("GET_INSTANCES", { paths, opts }),
supportedDM: (paths, opts) => call("GET_SUPPORTED_DM", { paths, opts }),
supportedProto: (versions) => call("GET_SUPPORTED_PROTO", { versions }),
_operate: (path, id, resp, input) =>
call("OPERATE", { path, input, id, resp }),
on,
...makeRecipes(call, on),
disconnect: () => client.end(),
};
return {
...baseUSP,
options: (opts) => addOptions(baseUSP, opts),
} as USP;
};
export default buildConnect;