@altronix/cli 0.4.3 → 0.6.0
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/atx +2 -2
- package/build/about.d.ts +3 -1
- package/build/about.js +37 -14
- package/build/about.js.map +1 -1
- package/build/cloud.d.ts +2 -2
- package/build/cloud.js +41 -19
- package/build/cloud.js.map +1 -1
- package/build/common.d.ts +5 -5
- package/build/common.js +48 -51
- package/build/common.js.map +1 -1
- package/build/confirmDevice.d.ts +5 -5
- package/build/confirmDevice.js +35 -35
- package/build/dhcp.d.ts +1 -1
- package/build/dhcp.js +9 -9
- package/build/dhcp.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +69 -78
- package/build/index.js.map +1 -1
- package/build/ip.d.ts +2 -2
- package/build/ip.js +17 -17
- package/build/ip.js.map +1 -1
- package/build/linq.d.ts +3 -3
- package/build/linq.js +5 -5
- package/build/linq.js.map +1 -1
- package/build/net.d.ts +3 -0
- package/build/net.js +47 -0
- package/build/net.js.map +1 -0
- package/build/poe.d.ts +4 -3
- package/build/poe.js +28 -23
- package/build/poe.js.map +1 -1
- package/build/reboot.d.ts +1 -0
- package/build/reboot.js +16 -0
- package/build/reboot.js.map +1 -0
- package/build/site.d.ts +2 -1
- package/build/site.js +18 -13
- package/build/site.js.map +1 -1
- package/build/stress.d.ts +1 -0
- package/build/stress.js +23 -0
- package/build/stress.js.map +1 -0
- package/build/unix.d.ts +1 -1
- package/build/unix.js +13 -13
- package/build/unix.js.map +1 -1
- package/build/update.d.ts +1 -1
- package/build/update.js +82 -82
- package/build/update.js.map +1 -1
- package/jest.config.js +6 -6
- package/package.json +4 -3
- package/src/about.ts +50 -11
- package/src/cloud.ts +55 -16
- package/src/index.ts +80 -89
- package/src/net.ts +68 -0
- package/src/reboot.ts +15 -0
- package/src/stress.ts +30 -0
- package/src/{update.ts → update.ts.bak} +98 -98
- package/tsconfig.json +29 -29
- package/tsconfig.lib.json +8 -8
- package/tsconfig.lib.tsbuildinfo +1 -1
- package/src/common.ts +0 -66
- package/src/confirmDevice.ts +0 -51
- package/src/dhcp.ts +0 -6
- package/src/ip.ts +0 -14
- package/src/linq.ts +0 -4
- package/src/poe.ts +0 -22
- package/src/site.ts +0 -10
- package/src/unix.ts +0 -10
package/src/cloud.ts
CHANGED
|
@@ -1,16 +1,55 @@
|
|
|
1
|
-
import { NetCloud } from "@altronix/
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import { cborDecodeNum, NetCloud } from "@altronix/netway";
|
|
2
|
+
import Linq, { log } from "@altronix/device";
|
|
3
|
+
import { ResponseBody, ResponseMeta } from "@altronix/device";
|
|
4
|
+
import {
|
|
5
|
+
concat,
|
|
6
|
+
finalize,
|
|
7
|
+
lastValueFrom,
|
|
8
|
+
map,
|
|
9
|
+
reduce,
|
|
10
|
+
switchMap,
|
|
11
|
+
take,
|
|
12
|
+
} from "rxjs";
|
|
13
|
+
|
|
14
|
+
export async function getCloud(): Promise<void> {
|
|
15
|
+
const linq = new Linq();
|
|
16
|
+
const path = "/api/v1/net/cloud";
|
|
17
|
+
const status = "/api/v1/net/cloud/status";
|
|
18
|
+
const obs = linq.connections().pipe(
|
|
19
|
+
take(1),
|
|
20
|
+
switchMap(({ about }) => {
|
|
21
|
+
const o0 = linq
|
|
22
|
+
.get<ResponseBody>(about.sid, path)
|
|
23
|
+
.pipe(map((resp) => NetCloud.fromCbor(resp.body).toJson()));
|
|
24
|
+
const o1 = linq
|
|
25
|
+
.get<ResponseBody>(about.sid, status)
|
|
26
|
+
.pipe(map((resp) => ({ status: cborDecodeNum(resp.body) })));
|
|
27
|
+
return concat(o0, o1);
|
|
28
|
+
}),
|
|
29
|
+
reduce((acc, curr) => ({ ...acc, ...curr })),
|
|
30
|
+
finalize(() => linq.shutdown())
|
|
31
|
+
);
|
|
32
|
+
return lastValueFrom(obs).then((cloud) => {
|
|
33
|
+
log.info(`request complete`, { ...cloud });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function setCloud(endpoint: string) {
|
|
38
|
+
const [ip, port] = endpoint.split(":");
|
|
39
|
+
const body = new NetCloud({ ip, port: parseInt(port), portEn: true });
|
|
40
|
+
const path = "/api/v1/net/cloud";
|
|
41
|
+
const save = "/api/v1/exe/saveAndReboot";
|
|
42
|
+
const linq = new Linq();
|
|
43
|
+
const obs = linq.connections().pipe(
|
|
44
|
+
take(1),
|
|
45
|
+
switchMap(({ about }) => {
|
|
46
|
+
const o0 = linq.put<ResponseMeta>(about.sid, path, body);
|
|
47
|
+
const o1 = linq.get<ResponseMeta>(about.sid, save);
|
|
48
|
+
return concat(o0, o1);
|
|
49
|
+
}),
|
|
50
|
+
finalize(() => linq.shutdown())
|
|
51
|
+
);
|
|
52
|
+
return lastValueFrom(obs).then(({ meta }) => {
|
|
53
|
+
log.info(`request complete`, { code: meta.code, message: meta.mesg });
|
|
54
|
+
});
|
|
55
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,89 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { program } from "commander";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
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
|
-
.action(
|
|
42
|
-
|
|
43
|
-
program
|
|
44
|
-
.command("set-
|
|
45
|
-
.description("
|
|
46
|
-
.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
program
|
|
62
|
-
.command("
|
|
63
|
-
.description("
|
|
64
|
-
.argument("<
|
|
65
|
-
.action(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
.
|
|
71
|
-
.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
.argument("<file>", "file to update device with")
|
|
82
|
-
.action(runUpdate);
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
await program.parseAsync();
|
|
86
|
-
} catch (e) {
|
|
87
|
-
console.error(e.json ? e.json() : e);
|
|
88
|
-
}
|
|
89
|
-
})();
|
|
1
|
+
import { log } from "@altronix/device";
|
|
2
|
+
import { program } from "commander";
|
|
3
|
+
import { getAbout, getSite, setSite } from "./about";
|
|
4
|
+
import { getCloud, setCloud } from "./cloud";
|
|
5
|
+
import { getNet, setDhcp, setNet } from "./net";
|
|
6
|
+
import { reboot } from "./reboot";
|
|
7
|
+
import { stress } from "./stress";
|
|
8
|
+
|
|
9
|
+
(async function main() {
|
|
10
|
+
log.info("running cli");
|
|
11
|
+
|
|
12
|
+
program.name("atx").description("access linq device via cli").version("TODO");
|
|
13
|
+
|
|
14
|
+
program
|
|
15
|
+
.command("get-about")
|
|
16
|
+
.description("get about data on the device")
|
|
17
|
+
.action(getAbout);
|
|
18
|
+
|
|
19
|
+
program
|
|
20
|
+
.command("get-site")
|
|
21
|
+
.description("get site id from the device")
|
|
22
|
+
.action(getSite);
|
|
23
|
+
|
|
24
|
+
program
|
|
25
|
+
.command("set-site")
|
|
26
|
+
.description("set site id from the device")
|
|
27
|
+
.argument("<site>", "new site id")
|
|
28
|
+
.action(setSite);
|
|
29
|
+
|
|
30
|
+
program
|
|
31
|
+
.command("get-net")
|
|
32
|
+
.description("get site id from the device")
|
|
33
|
+
.action(getNet);
|
|
34
|
+
|
|
35
|
+
program
|
|
36
|
+
.command("set-net")
|
|
37
|
+
.description("set network itnerface into static ip mode")
|
|
38
|
+
.argument("<ip>", "the new IP address")
|
|
39
|
+
.argument("<sn>", "the new SUBNET mask")
|
|
40
|
+
.argument("<gw>", "the new GATEWAY address")
|
|
41
|
+
.action(setNet);
|
|
42
|
+
|
|
43
|
+
program
|
|
44
|
+
.command("set-dhcp")
|
|
45
|
+
.description("set network interface into DHCP mode")
|
|
46
|
+
.action(setDhcp);
|
|
47
|
+
|
|
48
|
+
program
|
|
49
|
+
.command("get-cloud")
|
|
50
|
+
.description("get cloud endpoint on the device")
|
|
51
|
+
.action(getCloud);
|
|
52
|
+
|
|
53
|
+
program
|
|
54
|
+
.command("set-cloud")
|
|
55
|
+
.description("update network interface on the device")
|
|
56
|
+
.argument("<endpoint>", "cloud service location")
|
|
57
|
+
.action(setCloud);
|
|
58
|
+
|
|
59
|
+
program.command("reboot").description("reboot the device").action(reboot);
|
|
60
|
+
|
|
61
|
+
program
|
|
62
|
+
.command("stress")
|
|
63
|
+
.description("run a stress test on a device")
|
|
64
|
+
.argument("<count>", "how many requests to make")
|
|
65
|
+
.action(stress);
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
program
|
|
69
|
+
.command("update")
|
|
70
|
+
.description("run firmware update from file")
|
|
71
|
+
.argument("<file>", "file to update device with")
|
|
72
|
+
.action(runUpdate);
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
await program.parseAsync();
|
|
77
|
+
} catch (e) {
|
|
78
|
+
log.error(e.message, { ...e });
|
|
79
|
+
}
|
|
80
|
+
})();
|
package/src/net.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import Linq, { log } from "@altronix/device";
|
|
2
|
+
import {
|
|
3
|
+
concat,
|
|
4
|
+
finalize,
|
|
5
|
+
firstValueFrom,
|
|
6
|
+
lastValueFrom,
|
|
7
|
+
map,
|
|
8
|
+
switchMap,
|
|
9
|
+
take,
|
|
10
|
+
} from "rxjs";
|
|
11
|
+
import { ResponseBody, ResponseMeta } from "@altronix/device";
|
|
12
|
+
import { NetIp } from "@altronix/netway";
|
|
13
|
+
|
|
14
|
+
export async function getNet(): Promise<void> {
|
|
15
|
+
const linq = new Linq();
|
|
16
|
+
const path = "/api/v1/net/ip";
|
|
17
|
+
const obs = linq.connections().pipe(
|
|
18
|
+
take(1),
|
|
19
|
+
switchMap(({ about }) => linq.get<ResponseBody>(about.sid, path)),
|
|
20
|
+
map((resp) => NetIp.fromCbor(resp.body)),
|
|
21
|
+
finalize(() => linq.shutdown())
|
|
22
|
+
);
|
|
23
|
+
return firstValueFrom(obs).then((net) => {
|
|
24
|
+
log.info(`request complete`, { ...net.toJson() });
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function setNet(
|
|
29
|
+
ip: string,
|
|
30
|
+
sn: string,
|
|
31
|
+
gw: string
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const linq = new Linq();
|
|
34
|
+
const path = "/api/v1/net/ip";
|
|
35
|
+
const save = "/api/v1/exe/saveAndReboot";
|
|
36
|
+
const body = new NetIp({ ip, sn, gw, dhcp: false });
|
|
37
|
+
const obs = linq.connections().pipe(
|
|
38
|
+
take(1),
|
|
39
|
+
switchMap(({ about }) => {
|
|
40
|
+
const o0 = linq.put<ResponseMeta>(about.sid, path, body);
|
|
41
|
+
const o1 = linq.get<ResponseMeta>(about.sid, save);
|
|
42
|
+
return concat(o0, o1);
|
|
43
|
+
}),
|
|
44
|
+
finalize(() => linq.shutdown())
|
|
45
|
+
);
|
|
46
|
+
return lastValueFrom(obs).then(({ meta }) => {
|
|
47
|
+
log.info(`request complete`, { code: meta.code, message: meta.mesg });
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function setDhcp(): Promise<void> {
|
|
52
|
+
const linq = new Linq();
|
|
53
|
+
const path = "/api/v1/net/ip";
|
|
54
|
+
const save = "/api/v1/exe/saveAndReboot";
|
|
55
|
+
const body = new NetIp({ dhcp: true });
|
|
56
|
+
const obs = linq.connections().pipe(
|
|
57
|
+
take(1),
|
|
58
|
+
switchMap(({ about }) => {
|
|
59
|
+
const o0 = linq.put<ResponseMeta>(about.sid, path, body);
|
|
60
|
+
const o1 = linq.get<ResponseMeta>(about.sid, save);
|
|
61
|
+
return concat(o0, o1);
|
|
62
|
+
}),
|
|
63
|
+
finalize(() => linq.shutdown())
|
|
64
|
+
);
|
|
65
|
+
return lastValueFrom(obs).then(({ meta }) => {
|
|
66
|
+
log.info(`request complete`, { code: meta.code, message: meta.mesg });
|
|
67
|
+
});
|
|
68
|
+
}
|
package/src/reboot.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Linq, { log, ResponseMeta } from "@altronix/device";
|
|
2
|
+
import { finalize, firstValueFrom, switchMap, take } from "rxjs";
|
|
3
|
+
|
|
4
|
+
export async function reboot(): Promise<void> {
|
|
5
|
+
const linq = new Linq();
|
|
6
|
+
const path = "/api/v1/exe/reboot";
|
|
7
|
+
const obs = linq.connections().pipe(
|
|
8
|
+
take(1),
|
|
9
|
+
switchMap(({ about }) => linq.get<ResponseMeta>(about.sid, path)),
|
|
10
|
+
finalize(() => linq.shutdown())
|
|
11
|
+
);
|
|
12
|
+
return firstValueFrom(obs).then(({ meta }) => {
|
|
13
|
+
log.info(`request complete`, { code: meta.code, message: meta.mesg });
|
|
14
|
+
});
|
|
15
|
+
}
|
package/src/stress.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { About } from "@altronix/wasm/zdk";
|
|
2
|
+
import Linq, { log } from "@altronix/device";
|
|
3
|
+
import { lastValueFrom, map, repeat, switchScan, take, tap } from "rxjs";
|
|
4
|
+
import { CborResponseBody } from "@altronix/device/build/cbor";
|
|
5
|
+
|
|
6
|
+
export async function stress(count: number): Promise<void> {
|
|
7
|
+
const linq = new Linq();
|
|
8
|
+
const path = "/api/v1/about";
|
|
9
|
+
const obs = linq.connections().pipe(
|
|
10
|
+
take(1),
|
|
11
|
+
switchScan(
|
|
12
|
+
(acc, { about }) =>
|
|
13
|
+
linq.get<CborResponseBody>(about.sid, path).pipe(
|
|
14
|
+
map((resp) => ({
|
|
15
|
+
...About.fromCbor(resp.body).toJson(),
|
|
16
|
+
count: acc.count++,
|
|
17
|
+
})),
|
|
18
|
+
repeat(count)
|
|
19
|
+
),
|
|
20
|
+
{ count: 0 }
|
|
21
|
+
),
|
|
22
|
+
tap({
|
|
23
|
+
next: (about) => log.info("response", { ...about }),
|
|
24
|
+
finalize: () => linq.shutdown(),
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
return lastValueFrom(obs).then(() => {
|
|
28
|
+
log.info(`stress test complete`);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This example will listen for the first connected device and make a get
|
|
3
|
-
* request for the about data and log the about data to the console.
|
|
4
|
-
*/
|
|
5
|
-
import {
|
|
6
|
-
ConnectedEvent,
|
|
7
|
-
installDebugLogger,
|
|
8
|
-
getLogger,
|
|
9
|
-
CONNECTED,
|
|
10
|
-
} from "@altronix/device";
|
|
11
|
-
import { About, Update } from "@altronix/
|
|
12
|
-
import { from, concat, defer } from "rxjs";
|
|
13
|
-
import { take, filter, switchMap, concatMap, map, tap } from "rxjs/operators";
|
|
14
|
-
import linq from "./linq";
|
|
15
|
-
import fs from "fs";
|
|
16
|
-
|
|
17
|
-
function chunks(data: Uint8Array, size: number): Uint8Array[] {
|
|
18
|
-
let arr = [];
|
|
19
|
-
let remainder = data.length % size;
|
|
20
|
-
function slicer(n: number) {
|
|
21
|
-
for (let i = 0; i < n; i++) {
|
|
22
|
-
let idx = i * size;
|
|
23
|
-
arr.push(data.slice(idx, idx + size));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (remainder == 0) {
|
|
27
|
-
slicer(data.length / size);
|
|
28
|
-
} else {
|
|
29
|
-
let n = Math.ceil(data.length / size);
|
|
30
|
-
let end = size * (n - 1);
|
|
31
|
-
slicer(n - 1);
|
|
32
|
-
let last = new Uint8Array(size);
|
|
33
|
-
last.set(data.slice(end));
|
|
34
|
-
arr.push(last);
|
|
35
|
-
}
|
|
36
|
-
return arr;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
installDebugLogger();
|
|
40
|
-
const logger = getLogger();
|
|
41
|
-
|
|
42
|
-
// Reduce a blob of binary data and convert into an Update[]
|
|
43
|
-
function updateReducer(arr: Update[], data: Uint8Array, idx: number) {
|
|
44
|
-
let update = new Update({ data, offset: idx * 512 });
|
|
45
|
-
// NOTE: Have to initialize the Uint8Array across binding this way
|
|
46
|
-
// https://gitlab.altronix.com/software-engineering/sdk/atx-zdk/-/issues/1
|
|
47
|
-
update.data = data;
|
|
48
|
-
return arr.concat(update);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function runUpdate(file: string): Promise<void> {
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
const sub = linq
|
|
54
|
-
.listen()
|
|
55
|
-
.pipe(
|
|
56
|
-
filter((ev): ev is ConnectedEvent => ev.type == CONNECTED),
|
|
57
|
-
take(1),
|
|
58
|
-
switchMap(({ device }) => linq.get(device.about.sid, "/api/v1/about")),
|
|
59
|
-
map(About.fromCbor),
|
|
60
|
-
switchMap(({ sid: serial, verPrj }) => {
|
|
61
|
-
logger.info("found device", { serial, version: verPrj });
|
|
62
|
-
logger.info("sending update :", { file });
|
|
63
|
-
return from(fs.promises.readFile(file)).pipe(
|
|
64
|
-
switchMap((update) => {
|
|
65
|
-
// Chunk up the update file into packets and send update to device
|
|
66
|
-
const packets = chunks(update, 512).reduce(updateReducer, []);
|
|
67
|
-
const length = packets.length;
|
|
68
|
-
const urlStart = "/api/v1/update/start";
|
|
69
|
-
const urlTransfer = "/api/v1/update/transfer";
|
|
70
|
-
const urlFinish = "/api/v1/update/finish";
|
|
71
|
-
let sent = 0;
|
|
72
|
-
const start$ = defer(() => linq.get(serial, urlStart));
|
|
73
|
-
const transfer$ = from(packets).pipe(
|
|
74
|
-
concatMap((u) => linq.put(serial, urlTransfer, u.cbor())),
|
|
75
|
-
map((meta) => ({ code: meta.json().code, sent, length })),
|
|
76
|
-
tap(() => sent++)
|
|
77
|
-
);
|
|
78
|
-
const finished$ = defer(() => linq.get(serial, urlFinish));
|
|
79
|
-
return concat(start$, transfer$, finished$);
|
|
80
|
-
})
|
|
81
|
-
);
|
|
82
|
-
})
|
|
83
|
-
)
|
|
84
|
-
.subscribe({
|
|
85
|
-
next: async (mesg) => {
|
|
86
|
-
logger.info("<-", { ...mesg });
|
|
87
|
-
resolve();
|
|
88
|
-
},
|
|
89
|
-
error: async (error) => (await linq.shutdown(), reject(error)),
|
|
90
|
-
complete: async () => (await linq.shutdown(), resolve()),
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
process.on("SIGINT", async () => {
|
|
94
|
-
await linq.shutdown();
|
|
95
|
-
sub.unsubscribe();
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* This example will listen for the first connected device and make a get
|
|
3
|
+
* request for the about data and log the about data to the console.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
ConnectedEvent,
|
|
7
|
+
installDebugLogger,
|
|
8
|
+
getLogger,
|
|
9
|
+
CONNECTED,
|
|
10
|
+
} from "@altronix/device";
|
|
11
|
+
import { About, Update } from "@altronix/wasm/zdk";
|
|
12
|
+
import { from, concat, defer } from "rxjs";
|
|
13
|
+
import { take, filter, switchMap, concatMap, map, tap } from "rxjs/operators";
|
|
14
|
+
import linq from "./linq";
|
|
15
|
+
import fs from "fs";
|
|
16
|
+
|
|
17
|
+
function chunks(data: Uint8Array, size: number): Uint8Array[] {
|
|
18
|
+
let arr = [];
|
|
19
|
+
let remainder = data.length % size;
|
|
20
|
+
function slicer(n: number) {
|
|
21
|
+
for (let i = 0; i < n; i++) {
|
|
22
|
+
let idx = i * size;
|
|
23
|
+
arr.push(data.slice(idx, idx + size));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (remainder == 0) {
|
|
27
|
+
slicer(data.length / size);
|
|
28
|
+
} else {
|
|
29
|
+
let n = Math.ceil(data.length / size);
|
|
30
|
+
let end = size * (n - 1);
|
|
31
|
+
slicer(n - 1);
|
|
32
|
+
let last = new Uint8Array(size);
|
|
33
|
+
last.set(data.slice(end));
|
|
34
|
+
arr.push(last);
|
|
35
|
+
}
|
|
36
|
+
return arr;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
installDebugLogger();
|
|
40
|
+
const logger = getLogger();
|
|
41
|
+
|
|
42
|
+
// Reduce a blob of binary data and convert into an Update[]
|
|
43
|
+
function updateReducer(arr: Update[], data: Uint8Array, idx: number) {
|
|
44
|
+
let update = new Update({ data, offset: idx * 512 });
|
|
45
|
+
// NOTE: Have to initialize the Uint8Array across binding this way
|
|
46
|
+
// https://gitlab.altronix.com/software-engineering/sdk/atx-zdk/-/issues/1
|
|
47
|
+
update.data = data;
|
|
48
|
+
return arr.concat(update);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function runUpdate(file: string): Promise<void> {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const sub = linq
|
|
54
|
+
.listen()
|
|
55
|
+
.pipe(
|
|
56
|
+
filter((ev): ev is ConnectedEvent => ev.type == CONNECTED),
|
|
57
|
+
take(1),
|
|
58
|
+
switchMap(({ device }) => linq.get(device.about.sid, "/api/v1/about")),
|
|
59
|
+
map(About.fromCbor),
|
|
60
|
+
switchMap(({ sid: serial, verPrj }) => {
|
|
61
|
+
logger.info("found device", { serial, version: verPrj });
|
|
62
|
+
logger.info("sending update :", { file });
|
|
63
|
+
return from(fs.promises.readFile(file)).pipe(
|
|
64
|
+
switchMap((update) => {
|
|
65
|
+
// Chunk up the update file into packets and send update to device
|
|
66
|
+
const packets = chunks(update, 512).reduce(updateReducer, []);
|
|
67
|
+
const length = packets.length;
|
|
68
|
+
const urlStart = "/api/v1/update/start";
|
|
69
|
+
const urlTransfer = "/api/v1/update/transfer";
|
|
70
|
+
const urlFinish = "/api/v1/update/finish";
|
|
71
|
+
let sent = 0;
|
|
72
|
+
const start$ = defer(() => linq.get(serial, urlStart));
|
|
73
|
+
const transfer$ = from(packets).pipe(
|
|
74
|
+
concatMap((u) => linq.put(serial, urlTransfer, u.cbor())),
|
|
75
|
+
map((meta) => ({ code: meta.json().code, sent, length })),
|
|
76
|
+
tap(() => sent++)
|
|
77
|
+
);
|
|
78
|
+
const finished$ = defer(() => linq.get(serial, urlFinish));
|
|
79
|
+
return concat(start$, transfer$, finished$);
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
})
|
|
83
|
+
)
|
|
84
|
+
.subscribe({
|
|
85
|
+
next: async (mesg) => {
|
|
86
|
+
logger.info("<-", { ...mesg });
|
|
87
|
+
resolve();
|
|
88
|
+
},
|
|
89
|
+
error: async (error) => (await linq.shutdown(), reject(error)),
|
|
90
|
+
complete: async () => (await linq.shutdown(), resolve()),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
process.on("SIGINT", async () => {
|
|
94
|
+
await linq.shutdown();
|
|
95
|
+
sub.unsubscribe();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "Node16",
|
|
5
|
-
"lib": ["ES2022"],
|
|
6
|
-
"moduleResolution": "Node16",
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"composite": true,
|
|
9
|
-
"isolatedModules": true,
|
|
10
|
-
"allowSyntheticDefaultImports": true,
|
|
11
|
-
"importHelpers": true,
|
|
12
|
-
"alwaysStrict": true,
|
|
13
|
-
"sourceMap": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"noFallthroughCasesInSwitch": true,
|
|
16
|
-
"noImplicitReturns": true,
|
|
17
|
-
"noUnusedLocals": true,
|
|
18
|
-
"noUnusedParameters": true,
|
|
19
|
-
"noImplicitAny": false,
|
|
20
|
-
"noImplicitThis": false,
|
|
21
|
-
"strictNullChecks": false,
|
|
22
|
-
"paths":{
|
|
23
|
-
"@altronix/
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"references":[
|
|
27
|
-
{"path": "tsconfig.lib.json"}
|
|
28
|
-
]
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "Node16",
|
|
5
|
+
"lib": ["ES2022"],
|
|
6
|
+
"moduleResolution": "Node16",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"composite": true,
|
|
9
|
+
"isolatedModules": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"importHelpers": true,
|
|
12
|
+
"alwaysStrict": true,
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": true,
|
|
16
|
+
"noImplicitReturns": true,
|
|
17
|
+
"noUnusedLocals": true,
|
|
18
|
+
"noUnusedParameters": true,
|
|
19
|
+
"noImplicitAny": false,
|
|
20
|
+
"noImplicitThis": false,
|
|
21
|
+
"strictNullChecks": false,
|
|
22
|
+
"paths":{
|
|
23
|
+
"@altronix/wasm/zdk": ["../wasm/zdk"]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"references":[
|
|
27
|
+
{"path": "tsconfig.lib.json"}
|
|
28
|
+
]
|
|
29
|
+
}
|
package/tsconfig.lib.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "build",
|
|
5
|
-
"rootDir": "src"
|
|
6
|
-
},
|
|
7
|
-
"include": ["src/**/*"]
|
|
8
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "build",
|
|
5
|
+
"rootDir": "src"
|
|
6
|
+
},
|
|
7
|
+
"include": ["src/**/*"]
|
|
8
|
+
}
|