@altronix/cli 0.2.0 → 0.3.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/src/index.ts CHANGED
@@ -1,19 +1,32 @@
1
1
  import { getLogger } from "@altronix/device";
2
2
  import { program } from "commander";
3
- import setCloud from "./setCloud";
4
- import setIp from "./setIp";
5
- import setDhcp from "./setDhcp";
3
+ import * as poe from "./poe";
4
+ import * as cloud from "./cloud";
5
+ import * as ip from "./ip";
6
+ import * as dhcp from "./dhcp";
7
+ import * as about from "./about";
8
+ import { runUpdate } from "./update";
6
9
 
7
10
  (async function main() {
8
11
  getLogger().debug("starting app");
9
12
 
10
13
  program.name("atx").description("access linq device via cli").version("TODO");
11
14
 
15
+ program
16
+ .command("get-about")
17
+ .description("get about data on the device")
18
+ .action(about.getter);
19
+
12
20
  program
13
21
  .command("set-cloud")
14
22
  .description("update network interface on the device")
15
23
  .argument("<endpoint>", "cloud service location")
16
- .action(setCloud);
24
+ .action(cloud.setter);
25
+
26
+ program
27
+ .command("get-cloud")
28
+ .description("get cloud endpoint on the device")
29
+ .action(cloud.getter);
17
30
 
18
31
  program
19
32
  .command("set-ip")
@@ -21,12 +34,44 @@ import setDhcp from "./setDhcp";
21
34
  .argument("<ip>", "set IP address")
22
35
  .argument("<sn>", "set SN address")
23
36
  .argument("<gw>", "set GW address")
24
- .action(setIp);
37
+ .action(ip.setter);
38
+
39
+ program
40
+ .command("get-ip")
41
+ .description("get ip interface on the device")
42
+ .action(ip.getter);
25
43
 
26
44
  program
27
45
  .command("set-dhcp")
28
46
  .description("update ip interface [DHCP]")
29
- .action(setDhcp);
47
+ .action(dhcp.setter);
48
+
49
+ program
50
+ .command("set-poe-port-on")
51
+ .description("turn on a poe port")
52
+ .argument("<port>", "index of port to turn on")
53
+ .action(poe.setPortOn);
54
+
55
+ program
56
+ .command("set-poe-port-off")
57
+ .description("turn off a poe port")
58
+ .argument("<port>", "index of port to turn on")
59
+ .action(poe.setPortOff);
60
+
61
+ program
62
+ .command("get-poe-ports")
63
+ .description("read the poe port data")
64
+ .action(poe.getPorts);
65
+
66
+ program
67
+ .command("update")
68
+ .description("run firmware update from file")
69
+ .argument("<file>", "file to update device with")
70
+ .action(runUpdate);
30
71
 
31
- await program.parseAsync();
72
+ try {
73
+ await program.parseAsync();
74
+ } catch (e) {
75
+ console.error(e.json ? e.json() : e);
76
+ }
32
77
  })();
package/src/ip.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { NetIp } from "@altronix/cbor";
2
+ import { cliRunGet, cliRunPut } from "./common";
3
+
4
+ export function setter(ip: string, sn: string, gw: string): Promise<void> {
5
+ return cliRunPut("/api/v1/net/ip", new NetIp({ ip, sn, gw }));
6
+ }
7
+
8
+ export function getter(): Promise<void> {
9
+ return cliRunGet("/api/v1/net/ip", NetIp);
10
+ }
package/src/poe.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { PoePort } from "@altronix/cbor";
2
+ import { cliRunGetArray, cliRunPut } from "./common";
3
+
4
+ export function getPorts(): Promise<void> {
5
+ return cliRunGetArray("/api/v1/netway/ports", PoePort);
6
+ }
7
+
8
+ export function setPortOn(port: number): Promise<void> {
9
+ let data = new PoePort({ enabled: true });
10
+ return cliRunPut(`/api/v1/netway/ports?port=${port}`, data);
11
+ }
12
+
13
+ export function setPortOff(port: number): Promise<void> {
14
+ let data = new PoePort({ enabled: false });
15
+ return cliRunPut(`/api/v1/netway/ports?port=${port}`, data);
16
+ }
package/src/update.ts ADDED
@@ -0,0 +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/cbor";
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
+ }