sysiddr5 1.0.1-beta.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sysiddr5 might be problematic. Click here for more details.

@@ -0,0 +1,229 @@
1
+ 'use strict';
2
+ // @ts-check
3
+ // ==================================================================================
4
+ // audio.js
5
+ // ----------------------------------------------------------------------------------
6
+ // Description: System Information - library
7
+ // for Node.js
8
+ // Copyright: (c) 2014 - 2023
9
+ // Author: Sebastian Hildebrandt
10
+ // ----------------------------------------------------------------------------------
11
+ // License: MIT
12
+ // ==================================================================================
13
+ // 17. bluetooth
14
+ // ----------------------------------------------------------------------------------
15
+
16
+ const exec = require('child_process').exec;
17
+ const execSync = require('child_process').execSync;
18
+ const path = require('path');
19
+ const util = require('./util');
20
+ const fs = require('fs');
21
+
22
+ let _platform = process.platform;
23
+
24
+ const _linux = (_platform === 'linux' || _platform === 'android');
25
+ const _darwin = (_platform === 'darwin');
26
+ const _windows = (_platform === 'win32');
27
+ const _freebsd = (_platform === 'freebsd');
28
+ const _openbsd = (_platform === 'openbsd');
29
+ const _netbsd = (_platform === 'netbsd');
30
+ const _sunos = (_platform === 'sunos');
31
+
32
+ function parseBluetoothType(str) {
33
+ let result = '';
34
+
35
+ if (str.indexOf('keyboard') >= 0) { result = 'Keyboard'; }
36
+ if (str.indexOf('mouse') >= 0) { result = 'Mouse'; }
37
+ if (str.indexOf('speaker') >= 0) { result = 'Speaker'; }
38
+ if (str.indexOf('headset') >= 0) { result = 'Headset'; }
39
+ if (str.indexOf('phone') >= 0) { result = 'Phone'; }
40
+ if (str.indexOf('macbook') >= 0) { result = 'Computer'; }
41
+ if (str.indexOf('imac') >= 0) { result = 'Computer'; }
42
+ if (str.indexOf('ipad') >= 0) { result = 'Tablet'; }
43
+ if (str.indexOf('watch') >= 0) { result = 'Watch'; }
44
+ if (str.indexOf('headphone') >= 0) { result = 'Headset'; }
45
+ // to be continued ...
46
+
47
+ return result;
48
+ }
49
+
50
+ function parseBluetoothManufacturer(str) {
51
+ let result = str.split(' ')[0];
52
+ str = str.toLowerCase();
53
+ if (str.indexOf('apple') >= 0) { result = 'Apple'; }
54
+ if (str.indexOf('ipad') >= 0) { result = 'Apple'; }
55
+ if (str.indexOf('imac') >= 0) { result = 'Apple'; }
56
+ if (str.indexOf('iphone') >= 0) { result = 'Apple'; }
57
+ if (str.indexOf('magic mouse') >= 0) { result = 'Apple'; }
58
+ if (str.indexOf('macbook') >= 0) { result = 'Apple'; }
59
+ // to be continued ...
60
+
61
+ return result;
62
+ }
63
+
64
+ function parseLinuxBluetoothInfo(lines, macAddr1, macAddr2) {
65
+ const result = {};
66
+
67
+ result.device = null;
68
+ result.name = util.getValue(lines, 'name', '=');
69
+ result.manufacturer = null;
70
+ result.macDevice = macAddr1;
71
+ result.macHost = macAddr2;
72
+ result.batteryPercent = null;
73
+ result.type = parseBluetoothType(result.name.toLowerCase());
74
+ result.connected = false;
75
+
76
+ return result;
77
+ }
78
+
79
+ function parseDarwinBluetoothDevices(bluetoothObject, macAddr2) {
80
+ const result = {};
81
+ const typeStr = ((bluetoothObject.device_minorClassOfDevice_string || bluetoothObject.device_majorClassOfDevice_string || bluetoothObject.device_minorType || '') + (bluetoothObject.device_name || '')).toLowerCase();
82
+
83
+ result.device = bluetoothObject.device_services || '';
84
+ result.name = bluetoothObject.device_name || '';
85
+ result.manufacturer = bluetoothObject.device_manufacturer || parseBluetoothManufacturer(bluetoothObject.device_name || '') || '';
86
+ result.macDevice = (bluetoothObject.device_addr || bluetoothObject.device_address || '').toLowerCase().replace(/-/g, ':');
87
+ result.macHost = macAddr2;
88
+ result.batteryPercent = bluetoothObject.device_batteryPercent || null;
89
+ result.type = parseBluetoothType(typeStr);
90
+ result.connected = bluetoothObject.device_isconnected === 'attrib_Yes' || false;
91
+
92
+ return result;
93
+ }
94
+
95
+ function parseWindowsBluetooth(lines) {
96
+ const result = {};
97
+
98
+ result.device = null;
99
+ result.name = util.getValue(lines, 'name', ':');
100
+ result.manufacturer = util.getValue(lines, 'manufacturer', ':');
101
+ result.macDevice = null;
102
+ result.macHost = null;
103
+ result.batteryPercent = null;
104
+ result.type = parseBluetoothType(result.name.toLowerCase());
105
+ result.connected = null;
106
+
107
+ return result;
108
+ }
109
+
110
+ function bluetoothDevices(callback) {
111
+
112
+ return new Promise((resolve) => {
113
+ process.nextTick(() => {
114
+ let result = [];
115
+ if (_linux) {
116
+ // get files in /var/lib/bluetooth/ recursive
117
+ const btFiles = util.getFilesInPath('/var/lib/bluetooth/');
118
+ btFiles.forEach((element) => {
119
+ const filename = path.basename(element);
120
+ const pathParts = element.split('/');
121
+ const macAddr1 = pathParts.length >= 6 ? pathParts[pathParts.length - 2] : null;
122
+ const macAddr2 = pathParts.length >= 7 ? pathParts[pathParts.length - 3] : null;
123
+ if (filename === 'info') {
124
+ const infoFile = fs.readFileSync(element, { encoding: 'utf8' }).split('\n');
125
+ result.push(parseLinuxBluetoothInfo(infoFile, macAddr1, macAddr2));
126
+ }
127
+ });
128
+ // determine "connected" with hcitool con
129
+ try {
130
+ const hdicon = execSync('hcitool con').toString().toLowerCase();
131
+ for (let i = 0; i < result.length; i++) {
132
+ if (result[i].macDevice && result[i].macDevice.length > 10 && hdicon.indexOf(result[i].macDevice.toLowerCase()) >= 0) {
133
+ result[i].connected = true;
134
+ }
135
+ }
136
+ } catch (e) {
137
+ util.noop();
138
+ }
139
+
140
+ if (callback) {
141
+ callback(result);
142
+ }
143
+ resolve(result);
144
+ }
145
+ if (_darwin) {
146
+ let cmd = 'system_profiler SPBluetoothDataType -json';
147
+ exec(cmd, function (error, stdout) {
148
+ if (!error) {
149
+ try {
150
+ const outObj = JSON.parse(stdout.toString());
151
+ if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_title'] && outObj.SPBluetoothDataType[0]['device_title'].length) {
152
+ // missing: host BT Adapter macAddr ()
153
+ let macAddr2 = null;
154
+ if (outObj.SPBluetoothDataType[0]['local_device_title'] && outObj.SPBluetoothDataType[0].local_device_title.general_address) {
155
+ macAddr2 = outObj.SPBluetoothDataType[0].local_device_title.general_address.toLowerCase().replace(/-/g, ':');
156
+ }
157
+ outObj.SPBluetoothDataType[0]['device_title'].forEach((element) => {
158
+ const obj = element;
159
+ const objKey = Object.keys(obj);
160
+ if (objKey && objKey.length === 1) {
161
+ const innerObject = obj[objKey[0]];
162
+ innerObject.device_name = objKey[0];
163
+ const bluetoothDevice = parseDarwinBluetoothDevices(innerObject, macAddr2);
164
+ result.push(bluetoothDevice);
165
+ }
166
+ });
167
+ }
168
+ if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_connected'] && outObj.SPBluetoothDataType[0]['device_connected'].length) {
169
+ const macAddr2 = outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address ? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':') : null;
170
+ outObj.SPBluetoothDataType[0]['device_connected'].forEach((element) => {
171
+ const obj = element;
172
+ const objKey = Object.keys(obj);
173
+ if (objKey && objKey.length === 1) {
174
+ const innerObject = obj[objKey[0]];
175
+ innerObject.device_name = objKey[0];
176
+ innerObject.device_isconnected = 'attrib_Yes';
177
+ const bluetoothDevice = parseDarwinBluetoothDevices(innerObject, macAddr2);
178
+ result.push(bluetoothDevice);
179
+ }
180
+ });
181
+ }
182
+ if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_not_connected'] && outObj.SPBluetoothDataType[0]['device_not_connected'].length) {
183
+ const macAddr2 = outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address ? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':') : null;
184
+ outObj.SPBluetoothDataType[0]['device_not_connected'].forEach((element) => {
185
+ const obj = element;
186
+ const objKey = Object.keys(obj);
187
+ if (objKey && objKey.length === 1) {
188
+ const innerObject = obj[objKey[0]];
189
+ innerObject.device_name = objKey[0];
190
+ innerObject.device_isconnected = 'attrib_No';
191
+ const bluetoothDevice = parseDarwinBluetoothDevices(innerObject, macAddr2);
192
+ result.push(bluetoothDevice);
193
+ }
194
+ });
195
+ }
196
+ } catch (e) {
197
+ util.noop();
198
+ }
199
+ }
200
+ if (callback) {
201
+ callback(result);
202
+ }
203
+ resolve(result);
204
+ });
205
+ }
206
+ if (_windows) {
207
+ util.powerShell('Get-CimInstance Win32_PNPEntity | select PNPClass, Name, Manufacturer | fl').then((stdout, error) => {
208
+ if (!error) {
209
+ const parts = stdout.toString().split(/\n\s*\n/);
210
+ parts.forEach((part) => {
211
+ if (util.getValue(part.split('\n'), 'PNPClass', ':') === 'Bluetooth') {
212
+ result.push(parseWindowsBluetooth(part.split('\n')));
213
+ }
214
+ });
215
+ }
216
+ if (callback) {
217
+ callback(result);
218
+ }
219
+ resolve(result);
220
+ });
221
+ }
222
+ if (_freebsd || _netbsd || _openbsd || _sunos) {
223
+ resolve(null);
224
+ }
225
+ });
226
+ });
227
+ }
228
+
229
+ exports.bluetoothDevices = bluetoothDevices;
package/lib/cli.js ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+
3
+ 'use strict';
4
+ // @ts-check
5
+ // ==================================================================================
6
+ // cli.js
7
+ // ----------------------------------------------------------------------------------
8
+ // Description: System Information - library
9
+ // for Node.js
10
+ // Copyright: (c) 2014 - 2023
11
+ // Author: Sebastian Hildebrandt
12
+ // ----------------------------------------------------------------------------------
13
+ // License: MIT
14
+ // ==================================================================================
15
+
16
+ // ----------------------------------------------------------------------------------
17
+ // Dependencies
18
+ // ----------------------------------------------------------------------------------
19
+ const si = require('./index');
20
+
21
+ // ----------------------------------------------------------------------------------
22
+ // Main
23
+ // ----------------------------------------------------------------------------------
24
+ (function () {
25
+ si.getStaticData().then(
26
+ ((data) => {
27
+ data.time = si.time();
28
+ console.log(JSON.stringify(data, null, 2));
29
+ }
30
+ ));
31
+ })();