@ipsme/msgenv-mqtt 0.0.21
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/README.md +1 -0
- package/dist/ipsme_msgenv.cjs.js +125 -0
- package/package.json +40 -0
- package/rollup.config.mjs +24 -0
- package/src/ipsme_msgenv.cjs +124 -0
- package/tests/test_ipsme_msgenv.cjs +30 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mqtt = require('mqtt');
|
|
4
|
+
const { BitLogr } = require ('@knev/bitlogr');
|
|
5
|
+
|
|
6
|
+
// https://www.tutorialsteacher.com/nodejs/nodejs-module-exports
|
|
7
|
+
|
|
8
|
+
//-------------------------------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
let LOGR_= new BitLogr();
|
|
11
|
+
|
|
12
|
+
const l_ = {
|
|
13
|
+
MsgEnv : 0b1 << 0, // MsgEnv
|
|
14
|
+
CXNS : 0b1 << 1, // connections
|
|
15
|
+
REFL : 0b1 << 2, // reflection
|
|
16
|
+
};
|
|
17
|
+
LOGR_.labels= l_;
|
|
18
|
+
|
|
19
|
+
// https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript
|
|
20
|
+
const __name = obj => Object.keys(obj)[0];
|
|
21
|
+
// console.log('OUT', __name({variableName}) );
|
|
22
|
+
|
|
23
|
+
//-------------------------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
var cfg_= (function() {
|
|
26
|
+
let _options= {};
|
|
27
|
+
|
|
28
|
+
// options= {
|
|
29
|
+
// channel : 'IPSME',
|
|
30
|
+
// prefix : '',
|
|
31
|
+
// logr : ...
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
get channel() {
|
|
36
|
+
return (_options.channel === undefined) ? 'IPSME' : _options.channel;
|
|
37
|
+
},
|
|
38
|
+
get prefix() {
|
|
39
|
+
return (_options.prefix === undefined) ? '' : _options.prefix;
|
|
40
|
+
},
|
|
41
|
+
get options() { return _options; },
|
|
42
|
+
set options(obj) {
|
|
43
|
+
_options= obj;
|
|
44
|
+
if (_options.logr && _options.logr[ __name(l_) ] )
|
|
45
|
+
LOGR_.toggled= _options.logr;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
|
|
50
|
+
//-------------------------------------------------------------------------------------------------
|
|
51
|
+
// MsgEnv:
|
|
52
|
+
|
|
53
|
+
let singleton;
|
|
54
|
+
|
|
55
|
+
const kstr_TOPIC = 'IPSME';
|
|
56
|
+
|
|
57
|
+
class Singleton {
|
|
58
|
+
constructor() {
|
|
59
|
+
this.client = mqtt.connect('mqtt://localhost:1883');
|
|
60
|
+
|
|
61
|
+
this.client.on('error', function (err) {
|
|
62
|
+
console.error('MQTT Connection Error:', err);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
this.client.on('connect', function () {
|
|
66
|
+
// console.log('MQTT Connected');
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static getInstance() {
|
|
71
|
+
if (!singleton) {
|
|
72
|
+
singleton = new Singleton();
|
|
73
|
+
}
|
|
74
|
+
return singleton;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Normally you can specify an object to filter on when subscribing, but in electron that is missing
|
|
79
|
+
//
|
|
80
|
+
function subscribe_(handler) {
|
|
81
|
+
if (handler.subscription_ID !== undefined)
|
|
82
|
+
return;
|
|
83
|
+
LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: subscribe');
|
|
84
|
+
// handler.subscription_ID= systemPreferences.subscribeNotification(cfg_.channel, function(event, userInfo, object) {
|
|
85
|
+
// LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
|
|
86
|
+
// this(userInfo.msg);
|
|
87
|
+
// }.bind(handler));
|
|
88
|
+
|
|
89
|
+
let instance = Singleton.getInstance();
|
|
90
|
+
handler.subscription_ID = function (topic, message) {
|
|
91
|
+
handler(message.toString());
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
instance.client.on('message', handler.subscription_ID);
|
|
95
|
+
instance.client.subscribe(kstr_TOPIC);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// we have to use the ID, rather than the handler itself to unsubsribe
|
|
99
|
+
function unsubscribe_(handler) {
|
|
100
|
+
LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: unsubscribe');
|
|
101
|
+
// systemPreferences.unsubscribeNotification(handler.subscription_ID);
|
|
102
|
+
let instance = Singleton.getInstance();
|
|
103
|
+
instance.client.removeListener('message', handler.subscription_ID);
|
|
104
|
+
instance.client.unsubscribe(kstr_TOPIC);
|
|
105
|
+
delete handler.subscription_ID;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Normally a {name, object, userInfo} tuple, but in electron it is apparently not possible
|
|
109
|
+
// to specify the (sender) object explicitly; gets set automagically?!
|
|
110
|
+
function publish_(msg) {
|
|
111
|
+
LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: postNotification: ', msg);
|
|
112
|
+
// systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
|
|
113
|
+
let instance = Singleton.getInstance();
|
|
114
|
+
instance.client.publish(kstr_TOPIC, msg);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//-------------------------------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
module.exports= {
|
|
120
|
+
config : cfg_,
|
|
121
|
+
subscribe : subscribe_,
|
|
122
|
+
unsubscribe : unsubscribe_,
|
|
123
|
+
publish : publish_,
|
|
124
|
+
l : l_,
|
|
125
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ipsme/msgenv-mqtt",
|
|
3
|
+
"version": "0.0.21",
|
|
4
|
+
"description": "",
|
|
5
|
+
"exports": {
|
|
6
|
+
"node": {
|
|
7
|
+
"require": "./dist/ipsme_msgenv.cjs.js"
|
|
8
|
+
},
|
|
9
|
+
"default": "./dist/ipsme_msgenv.cjs.js"
|
|
10
|
+
},
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "rollup --config",
|
|
14
|
+
"test": "node tests/test_ipsme_msgenv.cjs",
|
|
15
|
+
"clean": "del dist && echo OK"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/IPSME/npm-msgenv-MQTT.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"ipsme"
|
|
23
|
+
],
|
|
24
|
+
"author": "K.Nevelsteen, PhD",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/IPSME/npm-msgenv-MQTT/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/IPSME/npm-msgenv-MQTT#readme",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@knev/bitlogr": "^0.2.1",
|
|
32
|
+
"mqtt": "^5.1.4"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@rollup/plugin-commonjs": "^23.0.2",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
37
|
+
"del-cli": "^5.0.0",
|
|
38
|
+
"rollup": "^3.2.3"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
// import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
// import json from '@rollup/plugin-json';
|
|
4
|
+
|
|
5
|
+
export default [
|
|
6
|
+
{
|
|
7
|
+
input: 'src/ipsme_msgenv.cjs',
|
|
8
|
+
output: {
|
|
9
|
+
name: "ipsme_msgenv",
|
|
10
|
+
file: 'dist/ipsme_msgenv.cjs.js',
|
|
11
|
+
format: 'cjs'
|
|
12
|
+
},
|
|
13
|
+
// plugins: [resolve()]
|
|
14
|
+
},
|
|
15
|
+
// {
|
|
16
|
+
// input: 'src/ipsme_msgenv.cjs',
|
|
17
|
+
// output: {
|
|
18
|
+
// name: "ipsme_msgenv",
|
|
19
|
+
// file: 'dist/ipsme_msgenv.es.mjs',
|
|
20
|
+
// format: 'es'
|
|
21
|
+
// },
|
|
22
|
+
// plugins: [resolve(), commonjs()]
|
|
23
|
+
// }
|
|
24
|
+
];
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
|
|
2
|
+
const mqtt = require('mqtt');
|
|
3
|
+
const { BitLogr } = require ('@knev/bitlogr');
|
|
4
|
+
|
|
5
|
+
// https://www.tutorialsteacher.com/nodejs/nodejs-module-exports
|
|
6
|
+
|
|
7
|
+
//-------------------------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
let LOGR_= new BitLogr();
|
|
10
|
+
|
|
11
|
+
const l_ = {
|
|
12
|
+
MsgEnv : 0b1 << 0, // MsgEnv
|
|
13
|
+
CXNS : 0b1 << 1, // connections
|
|
14
|
+
REFL : 0b1 << 2, // reflection
|
|
15
|
+
}
|
|
16
|
+
LOGR_.labels= l_;
|
|
17
|
+
|
|
18
|
+
// https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript
|
|
19
|
+
const __name = obj => Object.keys(obj)[0];
|
|
20
|
+
// console.log('OUT', __name({variableName}) );
|
|
21
|
+
|
|
22
|
+
//-------------------------------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
var cfg_= (function() {
|
|
25
|
+
let _options= {};
|
|
26
|
+
|
|
27
|
+
// options= {
|
|
28
|
+
// channel : 'IPSME',
|
|
29
|
+
// prefix : '',
|
|
30
|
+
// logr : ...
|
|
31
|
+
// }
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
get channel() {
|
|
35
|
+
return (_options.channel === undefined) ? 'IPSME' : _options.channel;
|
|
36
|
+
},
|
|
37
|
+
get prefix() {
|
|
38
|
+
return (_options.prefix === undefined) ? '' : _options.prefix;
|
|
39
|
+
},
|
|
40
|
+
get options() { return _options; },
|
|
41
|
+
set options(obj) {
|
|
42
|
+
_options= obj;
|
|
43
|
+
if (_options.logr && _options.logr[ __name(l_) ] )
|
|
44
|
+
LOGR_.toggled= _options.logr
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
48
|
+
|
|
49
|
+
//-------------------------------------------------------------------------------------------------
|
|
50
|
+
// MsgEnv:
|
|
51
|
+
|
|
52
|
+
let singleton;
|
|
53
|
+
|
|
54
|
+
const kstr_TOPIC = 'IPSME';
|
|
55
|
+
|
|
56
|
+
class Singleton {
|
|
57
|
+
constructor() {
|
|
58
|
+
this.client = mqtt.connect('mqtt://localhost:1883');
|
|
59
|
+
|
|
60
|
+
this.client.on('error', function (err) {
|
|
61
|
+
console.error('MQTT Connection Error:', err);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
this.client.on('connect', function () {
|
|
65
|
+
// console.log('MQTT Connected');
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static getInstance() {
|
|
70
|
+
if (!singleton) {
|
|
71
|
+
singleton = new Singleton();
|
|
72
|
+
}
|
|
73
|
+
return singleton;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Normally you can specify an object to filter on when subscribing, but in electron that is missing
|
|
78
|
+
//
|
|
79
|
+
function subscribe_(handler) {
|
|
80
|
+
if (handler.subscription_ID !== undefined)
|
|
81
|
+
return;
|
|
82
|
+
LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: subscribe');
|
|
83
|
+
// handler.subscription_ID= systemPreferences.subscribeNotification(cfg_.channel, function(event, userInfo, object) {
|
|
84
|
+
// LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
|
|
85
|
+
// this(userInfo.msg);
|
|
86
|
+
// }.bind(handler));
|
|
87
|
+
|
|
88
|
+
let instance = Singleton.getInstance();
|
|
89
|
+
handler.subscription_ID = function (topic, message) {
|
|
90
|
+
handler(message.toString());
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
instance.client.on('message', handler.subscription_ID);
|
|
94
|
+
instance.client.subscribe(kstr_TOPIC);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// we have to use the ID, rather than the handler itself to unsubsribe
|
|
98
|
+
function unsubscribe_(handler) {
|
|
99
|
+
LOGR_.log(l_.CXNS, cfg_.prefix +'MsgEnv: unsubscribe');
|
|
100
|
+
// systemPreferences.unsubscribeNotification(handler.subscription_ID);
|
|
101
|
+
let instance = Singleton.getInstance();
|
|
102
|
+
instance.client.removeListener('message', handler.subscription_ID);
|
|
103
|
+
instance.client.unsubscribe(kstr_TOPIC);
|
|
104
|
+
delete handler.subscription_ID;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Normally a {name, object, userInfo} tuple, but in electron it is apparently not possible
|
|
108
|
+
// to specify the (sender) object explicitly; gets set automagically?!
|
|
109
|
+
function publish_(msg) {
|
|
110
|
+
LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: postNotification: ', msg);
|
|
111
|
+
// systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
|
|
112
|
+
let instance = Singleton.getInstance();
|
|
113
|
+
instance.client.publish(kstr_TOPIC, msg);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//-------------------------------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
module.exports= {
|
|
119
|
+
config : cfg_,
|
|
120
|
+
subscribe : subscribe_,
|
|
121
|
+
unsubscribe : unsubscribe_,
|
|
122
|
+
publish : publish_,
|
|
123
|
+
l : l_,
|
|
124
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
IPSME_MsgEnv_OS = require('../dist/ipsme_msgenv.cjs');
|
|
3
|
+
|
|
4
|
+
function sleep(ms) {
|
|
5
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function handler_(msg)
|
|
9
|
+
{
|
|
10
|
+
console.log(msg);
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async function demo() {
|
|
16
|
+
IPSME_MsgEnv_OS.subscribe( handler_ );
|
|
17
|
+
IPSME_MsgEnv_OS.publish('BOOYAH 1');
|
|
18
|
+
|
|
19
|
+
await sleep(2000);
|
|
20
|
+
|
|
21
|
+
IPSME_MsgEnv_OS.unsubscribe( handler_ );
|
|
22
|
+
IPSME_MsgEnv_OS.publish('BOOYAH 2.o');
|
|
23
|
+
|
|
24
|
+
await sleep(2000);
|
|
25
|
+
|
|
26
|
+
IPSME_MsgEnv_OS.subscribe( handler_ );
|
|
27
|
+
IPSME_MsgEnv_OS.publish('BOOYAH 3.o');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
demo();
|