smart-home-engine 1.18.0 → 1.18.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smart-home-engine",
3
- "version": "1.18.0",
3
+ "version": "1.18.2",
4
4
  "description": "Node.js based script runner for use in MQTT based Smart Home environments",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -384,26 +384,6 @@ require('./web/server').setStatsProvider(() => {
384
384
  // Push current script:running state so the UI green dots survive a browser reload.
385
385
  setWelcomeProvider(() => Object.keys(scripts).map((f) => ({ type: 'script:running', path: makeLabel(f).slice(0, -1), running: true })));
386
386
 
387
- if (config.heartbeat?.enabled) {
388
- const _hbInterval = typeof config.heartbeat.interval === 'number' ? config.heartbeat.interval : 50;
389
- const _hbThreshold = typeof config.heartbeat.threshold === 'number' ? config.heartbeat.threshold : 300;
390
- let _lastBeat = Date.now();
391
- const _hbTimer = setInterval(() => {
392
- const now = Date.now();
393
- const lag = now - _lastBeat - _hbInterval;
394
- _lastBeat = now;
395
- if (lag > _hbThreshold) {
396
- const label = _blockingScript ?? 'unknown';
397
- if (lag > 2000) {
398
- log.warn(`event loop blocked ${lag}ms — script: ${label}`);
399
- } else {
400
- log.warn(`event loop lag ${lag}ms — last active script: ${label}`);
401
- }
402
- }
403
- }, _hbInterval);
404
- _hbTimer.unref();
405
- }
406
-
407
387
  if (!config.url) {
408
388
  log.warn('no MQTT broker URL configured — set "url" in ' + path.join(require('os').homedir(), '.she', 'config.json'));
409
389
  }
@@ -1025,12 +1005,14 @@ function runScript(script, name, _origin) {
1025
1005
 
1026
1006
  const Sandbox = {
1027
1007
  setTimeout: (fn, delay, ...args) => {
1028
- const id = setTimeout(fn, delay, ...args);
1008
+ const wrapped = args.length ? () => fn(...args) : fn;
1009
+ const id = setTimeout(() => _dispatch(name, wrapped), delay);
1029
1010
  _myTimers.add(id);
1030
1011
  return id;
1031
1012
  },
1032
1013
  setInterval: (fn, delay, ...args) => {
1033
- const id = setInterval(fn, delay, ...args);
1014
+ const wrapped = args.length ? () => fn(...args) : fn;
1015
+ const id = setInterval(() => _dispatch(name, wrapped), delay);
1034
1016
  _myTimers.add(id);
1035
1017
  return id;
1036
1018
  },
@@ -1104,7 +1086,8 @@ function runScript(script, name, _origin) {
1104
1086
  // she.setTimeout / she.clearTimeout — tracked versions for use by stdlib and
1105
1087
  // sandbox modules that don't have direct access to the Sandbox context.
1106
1088
  she.setTimeout = (fn, delay, ...args) => {
1107
- const id = setTimeout(fn, delay, ...args);
1089
+ const wrapped = args.length ? () => fn(...args) : fn;
1090
+ const id = setTimeout(() => _dispatch(name, wrapped), delay);
1108
1091
  _myTimers.add(id);
1109
1092
  return id;
1110
1093
  };
@@ -1497,6 +1480,26 @@ function loadDir(dir) {
1497
1480
  }
1498
1481
  }
1499
1482
  function start() {
1483
+ if (config.heartbeat?.enabled) {
1484
+ const _hbInterval = typeof config.heartbeat.interval === 'number' ? config.heartbeat.interval : 50;
1485
+ const _hbThreshold = typeof config.heartbeat.threshold === 'number' ? config.heartbeat.threshold : 300;
1486
+ let _lastBeat = Date.now();
1487
+ const _hbTimer = setInterval(() => {
1488
+ const now = Date.now();
1489
+ const lag = now - _lastBeat - _hbInterval;
1490
+ _lastBeat = now;
1491
+ if (lag > _hbThreshold) {
1492
+ const label = _blockingScript ?? 'unknown';
1493
+ if (lag > 2000) {
1494
+ log.warn(`event loop blocked ${lag}ms — script: ${label}`);
1495
+ } else {
1496
+ log.warn(`event loop lag ${lag}ms — last active script: ${label}`);
1497
+ }
1498
+ }
1499
+ }, _hbInterval);
1500
+ _hbTimer.unref();
1501
+ }
1502
+
1500
1503
  if (config.file) {
1501
1504
  if (typeof config.file === 'string') {
1502
1505
  loadScript(config.file);