ota-hub-reactjs 0.0.10 → 0.0.13

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.
@@ -26,7 +26,7 @@ export type DeviceConnectionState = {
26
26
  };
27
27
  export declare function createDefaultInitialDeviceState<T extends DeviceConnectionState>(uuid: string, props?: any): T;
28
28
  export type DeviceWhispererProps<T extends DeviceConnectionState> = {
29
- createInitialConnectionState?: (uuid: string) => T;
29
+ createInitialConnectionState?: (uuid: string) => Partial<T>;
30
30
  connectOn?: boolean;
31
31
  };
32
32
  export declare function MultiDeviceWhisperer<T extends DeviceConnectionState>({ createInitialConnectionState, }?: DeviceWhispererProps<T>): {
@@ -43,5 +43,6 @@ export declare function MultiDeviceWhisperer<T extends DeviceConnectionState>({
43
43
  appendLog: (uuid: string, log: LogLine) => void;
44
44
  isReady: boolean;
45
45
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
46
+ createInitialConnectionState: (uuid: string) => Partial<T>;
46
47
  };
47
48
  export {};
@@ -46,6 +46,7 @@ export function MultiDeviceWhisperer({ createInitialConnectionState = createDefa
46
46
  uuid = uuid ?? uniqueNamesGenerator({ dictionaries: [animals] });
47
47
  const props = propCreator?.(uuid);
48
48
  const newConnection = {
49
+ ...createDefaultInitialDeviceState(uuid),
49
50
  ...createInitialConnectionState(uuid),
50
51
  ...props
51
52
  };
@@ -76,6 +77,7 @@ export function MultiDeviceWhisperer({ createInitialConnectionState = createDefa
76
77
  getConnection,
77
78
  appendLog,
78
79
  isReady,
79
- setIsReady
80
+ setIsReady,
81
+ createInitialConnectionState
80
82
  };
81
83
  }
@@ -31,4 +31,5 @@ export declare function ProtobufMultiDeviceWhisperer<AppLayer extends DeviceConn
31
31
  appendLog: (uuid: string, log: import("../base/device-whisperer.js").LogLine) => void;
32
32
  isReady: boolean;
33
33
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
34
+ createInitialConnectionState: (uuid: string) => Partial<AppLayer>;
34
35
  };
@@ -33,4 +33,5 @@ export declare function ESP32MultiDeviceWhisperer<AppOrMessageLayer extends ESP3
33
33
  appendLog: (uuid: string, log: import("../base/device-whisperer.js").LogLine) => void;
34
34
  isReady: boolean;
35
35
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
36
+ createInitialConnectionState: (uuid: string) => Partial<AppOrMessageLayer>;
36
37
  };
@@ -137,20 +137,13 @@ export function ESP32MultiDeviceWhisperer({ ...props } = {}) {
137
137
  level: 0,
138
138
  message: `[!] Read loop error: ${e}`,
139
139
  });
140
- await disconnect(uuid);
141
140
  }
142
141
  finally {
143
- base.updateConnection(uuid, (c) => ({
144
- ...c,
145
- transport: null,
146
- isConnected: false,
147
- isConnecting: false,
148
- autoConnect: false,
149
- }));
150
142
  base.appendLog(uuid, {
151
143
  level: 0,
152
144
  message: "[!] Serial disconnected",
153
145
  });
146
+ await disconnect(uuid);
154
147
  }
155
148
  };
156
149
  const restartDevice = async (uuid, default_transport) => {
@@ -168,15 +161,19 @@ export function ESP32MultiDeviceWhisperer({ ...props } = {}) {
168
161
  };
169
162
  const connect = async (uuid, baudrate, restart_on_connect = true) => {
170
163
  const conn = base.getConnection(uuid);
171
- if (!conn?.port)
164
+ if (!conn)
172
165
  return;
173
- if (!conn?.transport) {
174
- await disconnect(uuid);
166
+ let port = conn?.port;
167
+ if (!port) {
168
+ port = await navigator.serial.requestPort({
169
+ filters: [{ usbVendorId: 0x303a }]
170
+ });
171
+ base.updateConnection(uuid, (c) => ({ ...c, port }));
175
172
  }
176
173
  ;
177
174
  base.updateConnection(uuid, (c) => ({ ...c, isConnecting: true }));
178
175
  const use_baudrate = baudrate ?? conn.baudrate ?? 115200;
179
- const transport = new Transport(conn.port, false, false);
176
+ const transport = new Transport(port, false, false);
180
177
  try {
181
178
  const esploader = new ESPLoader({
182
179
  transport,
@@ -247,6 +244,7 @@ export function ESP32MultiDeviceWhisperer({ ...props } = {}) {
247
244
  // Always clear the transport and reset connection state
248
245
  base.updateConnection(uuid, (c) => ({
249
246
  ...c,
247
+ port: null,
250
248
  transport: null,
251
249
  isConnected: false,
252
250
  isConnecting: false,
@@ -263,9 +261,12 @@ export function ESP32MultiDeviceWhisperer({ ...props } = {}) {
263
261
  propCreator: (id) => {
264
262
  const props = propCreator?.(id);
265
263
  return {
266
- send: props?.send || ((d) => defaultSend(id, d)),
267
- onReceive: props?.onReceive || ((d) => defaultOnReceive(id, d)),
264
+ send: (d) => defaultSend(id, d),
265
+ onReceive: (d) => defaultOnReceive(id, d),
268
266
  port,
267
+ // Initial connection state
268
+ ...base.createInitialConnectionState(id),
269
+ // From props
269
270
  ...props
270
271
  };
271
272
  }
@@ -292,7 +293,7 @@ export function ESP32MultiDeviceWhisperer({ ...props } = {}) {
292
293
  const conn = base.getConnection(uuid);
293
294
  if (!conn || !conn.port || assetsToFlash.length === 0)
294
295
  return;
295
- await base.disconnect(uuid);
296
+ await disconnect(uuid);
296
297
  base.updateConnection(uuid, (c) => ({ ...c, isFlashing: true, flashProgress: 0, flashError: undefined }));
297
298
  try {
298
299
  // --- Connect ONCE ---
@@ -28,4 +28,5 @@ export declare function MQTTMultiDeviceWhisperer<AppOrMessageLayer extends MQTTC
28
28
  appendLog: (uuid: string, log: import("../base/device-whisperer.js").LogLine) => void;
29
29
  isReady: boolean;
30
30
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
31
+ createInitialConnectionState: (uuid: string) => Partial<AppOrMessageLayer>;
31
32
  };
@@ -6,7 +6,7 @@ export function MQTTMultiDeviceWhisperer({ serverUrl, uuidFromMessage, subTopicF
6
6
  const clientRef = useRef(null);
7
7
  const isUnmountedRef = useRef(false);
8
8
  const watchdogTimers = useRef({});
9
- const addingConnections = new Set();
9
+ const addingConnections = useRef(new Set());
10
10
  const connectToMQTTServer = () => {
11
11
  isUnmountedRef.current = false;
12
12
  if (clientRef.current) {
@@ -189,7 +189,7 @@ export function MQTTMultiDeviceWhisperer({ serverUrl, uuidFromMessage, subTopicF
189
189
  Error("In MQTT you MUST define a UUID otherwise we don't know what device we're connecting to!");
190
190
  return;
191
191
  }
192
- if (base.connectionsRef.current.some(c => c.uuid === uuid) || addingConnections.has(uuid)) {
192
+ if (base.connectionsRef.current.some(c => c.uuid === uuid) || addingConnections.current.has(uuid)) {
193
193
  return;
194
194
  }
195
195
  await base.addConnection({
@@ -197,15 +197,19 @@ export function MQTTMultiDeviceWhisperer({ serverUrl, uuidFromMessage, subTopicF
197
197
  propCreator: (id) => {
198
198
  const props = propCreator?.(id);
199
199
  return {
200
- send: props?.send || ((d) => defaultSend(id, d)),
201
- onReceive: props?.onReceive || ((d) => defaultOnReceive(id, d)),
202
- touchHeartbeat: props?.touchHeartbeat || (() => touchHeartbeat(id)),
200
+ // Defaults, may be overridden by props
201
+ send: (d) => defaultSend(id, d),
202
+ onReceive: (d) => defaultOnReceive(id, d),
203
+ touchHeartbeat: () => touchHeartbeat(id),
204
+ // Initial connection state
205
+ ...base.createInitialConnectionState(id),
206
+ // From props
203
207
  ...props
204
208
  };
205
209
  }
206
210
  });
207
211
  // Delete this adding connections item
208
- addingConnections.delete(uuid);
212
+ addingConnections.current.delete(uuid);
209
213
  // Connect immediately
210
214
  connect(uuid);
211
215
  return uuid;
@@ -224,8 +228,10 @@ export function MQTTMultiDeviceWhisperer({ serverUrl, uuidFromMessage, subTopicF
224
228
  }
225
229
  };
226
230
  useEffect(() => {
227
- if (autoConnect || props.connectOn)
228
- connectToMQTTServer();
231
+ if (!(autoConnect || props.connectOn))
232
+ return;
233
+ const cleanup = connectToMQTTServer();
234
+ return cleanup;
229
235
  }, [serverUrl, props.connectOn]);
230
236
  return {
231
237
  ...base,
@@ -51,4 +51,5 @@ export declare function SerialMultiDeviceWhisperer<AppOrMessageLayer extends Ser
51
51
  appendLog: (uuid: string, log: import("../base/device-whisperer.js").LogLine) => void;
52
52
  isReady: boolean;
53
53
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
54
+ createInitialConnectionState: (uuid: string) => Partial<AppOrMessageLayer>;
54
55
  };
@@ -289,18 +289,21 @@ export function SerialMultiDeviceWhisperer({ ...props } = {}) {
289
289
  };
290
290
  const addConnection = async ({ uuid, propCreator }) => {
291
291
  try {
292
- // NATIVE WEBUSB REQUEST
293
292
  const device = await navigator.usb.requestDevice({
294
- filters: [] // Espressif Vendor ID
293
+ filters: []
295
294
  });
296
295
  return await base.addConnection({
297
296
  uuid,
298
297
  propCreator: (id) => {
299
298
  const props = propCreator?.(id);
300
299
  return {
301
- send: props?.send || ((d) => defaultSend(id, d)),
302
- onReceive: props?.onReceive || ((d) => defaultOnReceive(id, d)),
303
- device, // Storing USBDevice instead of SerialPort
300
+ // Defaults, may be overridden by props
301
+ send: (d) => defaultSend(id, d),
302
+ onReceive: (d) => defaultOnReceive(id, d),
303
+ device,
304
+ // Initial connection state
305
+ ...base.createInitialConnectionState(id),
306
+ // From props
304
307
  ...props
305
308
  };
306
309
  }
@@ -27,4 +27,5 @@ export declare function WebsocketMultiDeviceWhisperer<AppOrMessageLayer extends
27
27
  appendLog: (uuid: string, log: import("../base/device-whisperer.js").LogLine) => void;
28
28
  isReady: boolean;
29
29
  setIsReady: import("react").Dispatch<import("react").SetStateAction<boolean>>;
30
+ createInitialConnectionState: (uuid: string) => Partial<AppOrMessageLayer>;
30
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ota-hub-reactjs",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "description": "ReactJS tools for building web apps to flash MCU devices such as esp32, brought to you by OTA Hub.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",