From 9f1b6ef43d307b05da777a949ab6f6bc35f6e3ab Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Mon, 24 Aug 2020 15:07:42 +0200 Subject: [PATCH] observe() added function to observe/watch system parameters --- CHANGELOG.md | 1 + README.md | 5 ++-- docs/history.html | 5 ++++ docs/index.html | 4 +-- docs/main.js | 2 +- docs/statsfunctions.html | 54 +++++++++++++++++++++++++++++++++++++++- lib/index.js | 15 +++++++++++ 7 files changed, 80 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8d7160..ed110de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 4.27.0 | 2020-08-24 | `observe()` added function to observe/watch system parameters | | 4.26.12 | 2020-08-21 | `versions()` fixed issue windows | | 4.26.11 | 2020-08-20 | `cpuTemperature()` fixed issue windows | | 4.26.10 | 2020-07-16 | `networkStats()` fixed issue blocking windows | diff --git a/README.md b/README.md index 0c4354d..538418b 100644 --- a/README.md +++ b/README.md @@ -87,13 +87,13 @@ si.cpu() (last 7 major and minor version releases) +- Version 4.27.0: `observe()` added observe / watch function - Version 4.26.0: `diskLayout()` added full S.M.A.R.T data (linux) - Version 4.25.0: `get()` added function to get partial system info - Version 4.24.0: `networkInterfaces()` added subnet mask ip4 and ip6 - Version 4.23.0: `versions()` added param to specify which program/lib versions to detect - Version 4.22.0: `services()` added pids (windows) - Version 4.21.0: added npx copmpatibility -- Version 4.20.0: `battery()` added designcapacity, voltage, unit - ... You can find all changes here: [detailed changelog][changelog-url] @@ -633,7 +633,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | [0].timeOffset | X | X | X | X | X | time Offset | | | [0].RTC | X | X | X | X | X | RTC | -#### 14. "Get All at once" - functions +#### 14. "Get All / Observe" - functions | Function | Result object | Linux | BSD | Mac | Win | Sun | Comments | | --------------- | ------------- | ----- | ------- | --- | --- | --- | -------- | @@ -641,6 +641,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | si.getDynamicData(srv,iface,cb) | {...} | X | X | X | X | X | all dynamic data at once
Specify services and interfaces to monitor
Defaults to first external network interface
Pass "*" for ALL services (linux/win only)
Pass "*" for ALL network interfaces | | si.getAllData(srv,iface,cb) | {...} | X | X | X | X | X | all data at once
Specify services and interfaces to monitor
Defaults to first external network interface
Pass "*" for ALL services (linux/win only)
Pass "*" for ALL network interfaces | | si.get(valueObject,cb) | {...} | X | X | X | X | X | get partial system info data at once
In valueObject you can define
all values, you want to get back
(see documentation for details) | +| si.observe(valueObject,interval,cb) | - | X | X | X | X | X | Observe a defined value object
call callback on changes
polling interval in milliseconds | ### cb: Asynchronous Function Calls (callback) diff --git a/docs/history.html b/docs/history.html index ad76a74..cb89605 100644 --- a/docs/history.html +++ b/docs/history.html @@ -83,6 +83,11 @@ + + 4.27.0 + 2020-08-24 + observe() added function to observe/watch system parameters + 4.26.12 2020-08-21 diff --git a/docs/index.html b/docs/index.html index 3330e04..9d92863 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,7 +168,7 @@
systeminformation
-
Current Version: 4.26.12
+
Current Version: 4.27.0
@@ -207,7 +207,7 @@
Downloads last month
-
330
+
333
Dependends
diff --git a/docs/main.js b/docs/main.js index 33a1c58..96fd76a 100644 --- a/docs/main.js +++ b/docs/main.js @@ -15,10 +15,10 @@ function createMenu() { [1, 'wifi', 'Wifi'], [1, 'docker', 'Docker'], [1, 'vbox', 'Virtual Box'], + [1, 'statsfunctions', 'Observers / Stats'], [0, '', 'More'], [1, 'history', 'Version history'], [1, 'issues', 'Known Issues'], - [1, 'statsfunctions', 'Stats Functions'], [1, 'copyright', 'Copyright & License'], [1, 'contributors', 'Contributors'], [1, 'trademarks', 'Trademarks'], diff --git a/docs/statsfunctions.html b/docs/statsfunctions.html index c0f4bf7..00f4d90 100644 --- a/docs/statsfunctions.html +++ b/docs/statsfunctions.html @@ -46,7 +46,7 @@
Stats Functions
-

Getting correct stats values

+

Getting correct stats values

In fsStats(), disksIO(), currentLoad() and networkStats() the results / sec. values (rx_sec, IOPS, ...) are calculated correctly beginning with the second call of the function. @@ -65,6 +65,58 @@ setInterval(function() { }) }, 1000)

Beginning with the second call, you get network transfer values per second.

+

Observe System Parameters

+

systeminformation now allows you to easily observe system poarameters: First you define a result object of system parameters you want to abserve (see also secription of the si.get() function here):

+

Then you just call an si.observe() function with three parameters: your result object, the polling interval (in miliseconds) and a callback function. Systeminformation will now observe the result object. Every time the result changes, this callback function is called. Your callback function also gets the current value of all observed system parameters.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionResult objectLinuxBSDMacWinSunComments
si.observe(valueObject,interval,cb)-XXXXXObserve the defined value object
call callback on changes:
+
Example
+
const si = require('systeminformation');
+
+// define all values, you want to get back
+valueObject = {
+  battery: 'acconnected'
+}
+
+function usersCallback(data) {
+  console.log('Power usage now: ' + (data.battery.acconnected ? 'AC' : 'battery'));
+}
+
+// now define the observer function
+let observer = si.observe(valueObject, 1000, usersCallback);
+
+// In this example we stop our observer function after 30 seconds
+setTimeout(() => {
+  clearInterval(observer)
+}, 30000);
+
+

The key names of the valueObject must be exactly the same as the representing function within systeminformation.

diff --git a/lib/index.js b/lib/index.js index 9db5ec1..4d7aee4 100755 --- a/lib/index.js +++ b/lib/index.js @@ -349,6 +349,20 @@ function get(valueObject, callback) { }); } +function observe(valueObject, interval, callback) { + let _data = null; + + const result = setInterval(() => { + get(valueObject).then(data => { + if (JSON.stringify(_data) !== JSON.stringify(data)) { + _data = { ...data }; + callback(data); + } + }) + }, interval); + return result; +} + // ---------------------------------------------------------------------------------- // export all libs // ---------------------------------------------------------------------------------- @@ -416,4 +430,5 @@ exports.getStaticData = getStaticData; exports.getDynamicData = getDynamicData; exports.getAllData = getAllData; exports.get = get; +exports.observe = observe;