-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathble.ts
More file actions
149 lines (115 loc) · 4.79 KB
/
ble.ts
File metadata and controls
149 lines (115 loc) · 4.79 KB
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
import { Arg, Command, Env } from "./lib/command.js";
import { stdout, stderr } from "process";
import { getDevice } from "./util.js";
enum BleKvNs {
Main = "ble_cfg",
}
enum BleKeys {
Mode = "mode",
Name = "name",
}
enum BleMode {
DISABLED,
ENABLED_STREAM,
// TODO: Can we add this for the future? (use will handle the BLE yourself)
UNMANAGED,
}
export const bleGet = new Command("Display current BLE config", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const device = await getDevice(port, baudrate, socket, ble, env);
await new Promise((resolve) => setTimeout(resolve, 1000));
stdout.write("\n-----\n");
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
const mode = await device.controller.configGetInt(BleKvNs.Main, BleKeys.Mode);
const name = await device.controller.configGetString(BleKvNs.Main, BleKeys.Name);
stdout.write(`BLE Mode: ${BleMode[mode]}
Device Name: ${name}
`);
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
},
chainable: true
});
export const bleDisable = new Command("Disable WiFi", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
await device.controller.configSetInt(BleKvNs.Main, BleKeys.Mode, BleMode.DISABLED);
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
stdout.write("Wifi config changed.\n");
},
chainable: true
});
export const bleEnableStream = new Command("Enable BLE stream mode", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
await device.controller.configSetInt(BleKvNs.Main, BleKeys.Mode, BleMode.ENABLED_STREAM);
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
stdout.write("Wifi config changed.\n");
},
chainable: true
});
export const bleSetName = new Command("Set BLE device name", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const name = args["name"] as string | undefined;
if (!name) {
stderr.write("Name is required\n");
throw 1;
}
if (name && name.length >= 20) {
stderr.write("Name is too long\n");
throw 1;
}
const device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
// TODO: Can we enable stream mode here?
await device.controller.configSetInt(BleKvNs.Main, BleKeys.Mode, BleMode.ENABLED_STREAM);
await device.controller.configSetString(BleKvNs.Main, BleKeys.Name, name);
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
stdout.write("Wifi config changed.\n");
},
args: [
new Arg("name", "Name of the BLE device", { required: true }),
],
chainable: true
});