smithtek-mako-rf 3.0.2 → 3.1.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/package.json +1 -1
- package/smithtek-mako-rf.js +42 -19
- package/smithtek-mako-rf-3.0.1.tgz +0 -0
- package/smithtek-mako-rf-3.0.2.tgz +0 -0
package/package.json
CHANGED
package/smithtek-mako-rf.js
CHANGED
|
@@ -426,25 +426,41 @@ state._rssiCleanup = cleanup;
|
|
|
426
426
|
// tiny settle time helps on Pi serial
|
|
427
427
|
await sleep(50);
|
|
428
428
|
}
|
|
429
|
-
|
|
430
429
|
async function connectIfNeeded(cfg, state) {
|
|
431
430
|
if (!cfg) throw new Error("Bus config missing");
|
|
432
431
|
|
|
433
432
|
const portKey = makePortKey(cfg);
|
|
434
433
|
|
|
435
|
-
// If settings changed,
|
|
436
|
-
if (state.
|
|
434
|
+
// If serial settings have actually changed, reconnect.
|
|
435
|
+
if (state.lastPortKey && state.lastPortKey !== portKey) {
|
|
437
436
|
state.connected = false;
|
|
438
|
-
|
|
437
|
+
|
|
438
|
+
if (state.client.isOpen) {
|
|
439
|
+
await closeClientSafe(state);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Trust the actual modbus-serial port state.
|
|
444
|
+
// If the serial port is already open, reuse it.
|
|
445
|
+
if (state.client.isOpen) {
|
|
446
|
+
state.connected = true;
|
|
447
|
+
state.lastPortKey = portKey;
|
|
448
|
+
return;
|
|
439
449
|
}
|
|
440
450
|
|
|
441
|
-
|
|
442
|
-
|
|
451
|
+
if (state.connected) {
|
|
452
|
+
state.connected = false;
|
|
453
|
+
}
|
|
443
454
|
|
|
444
|
-
// If
|
|
455
|
+
// If another request is already opening the port, wait.
|
|
445
456
|
if (state.connecting) {
|
|
446
457
|
for (let i = 0; i < 200; i++) {
|
|
447
|
-
if (state.
|
|
458
|
+
if (state.client.isOpen) {
|
|
459
|
+
state.connected = true;
|
|
460
|
+
state.lastPortKey = portKey;
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
448
464
|
if (!state.connecting) break;
|
|
449
465
|
await sleep(50);
|
|
450
466
|
}
|
|
@@ -463,23 +479,35 @@ state._rssiCleanup = cleanup;
|
|
|
463
479
|
parity: cfg.parity || "none",
|
|
464
480
|
};
|
|
465
481
|
|
|
466
|
-
//
|
|
467
|
-
|
|
482
|
+
// Only close if the library says the port is actually open.
|
|
483
|
+
if (state.client.isOpen) {
|
|
484
|
+
await closeClientSafe(state);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Allow Linux/serialport time to release any previous lock.
|
|
488
|
+
await sleep(200);
|
|
468
489
|
|
|
469
|
-
// Open
|
|
470
490
|
await state.client.connectRTUBuffered(port, options);
|
|
471
491
|
|
|
472
|
-
// UI uses seconds; library uses
|
|
492
|
+
// UI uses seconds; library uses milliseconds.
|
|
473
493
|
const timeout_s = Math.max(1, toNum(cfg.timeout_s, 8));
|
|
474
|
-
state.client.setTimeout(
|
|
494
|
+
state.client.setTimeout(
|
|
495
|
+
Math.max(1000, Math.floor(timeout_s * 1000))
|
|
496
|
+
);
|
|
475
497
|
|
|
476
498
|
state.connected = true;
|
|
477
499
|
state.lastPortKey = portKey;
|
|
500
|
+
|
|
501
|
+
} catch (err) {
|
|
502
|
+
state.connected = false;
|
|
503
|
+
throw err;
|
|
504
|
+
|
|
478
505
|
} finally {
|
|
479
506
|
state.connecting = false;
|
|
480
507
|
}
|
|
481
508
|
}
|
|
482
509
|
|
|
510
|
+
|
|
483
511
|
async function doRequest(cfg, state, req) {
|
|
484
512
|
await connectIfNeeded(cfg, state);
|
|
485
513
|
|
|
@@ -576,12 +604,7 @@ state._rssiCleanup = cleanup;
|
|
|
576
604
|
} catch (err) {
|
|
577
605
|
lastErr = err;
|
|
578
606
|
|
|
579
|
-
|
|
580
|
-
state.connected = false;
|
|
581
|
-
try {
|
|
582
|
-
await new Promise((resolve) => state.client.close(() => resolve()));
|
|
583
|
-
} catch (_e) {}
|
|
584
|
-
|
|
607
|
+
node.warn(`${busName}: RF error fc${req.fc} uid${req.unitid} try ${attempt}/${totalTries}: ${err.message || err}`);
|
|
585
608
|
|
|
586
609
|
if (attempt < totalTries && gap_s > 0) {
|
|
587
610
|
await sleep(Math.floor(gap_s * 1000));
|
|
Binary file
|
|
Binary file
|