forked from alsm/freeboard-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaho.mqtt.plugin.js
148 lines (137 loc) · 5.34 KB
/
paho.mqtt.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// # A Freeboard Plugin that uses the Eclipse Paho javascript client to read MQTT messages
(function()
{
// ### Datasource Definition
// Please replace the external_scripts location with a local replica of the Paho MQTT client when possible
// -------------------
freeboard.loadDatasourcePlugin({
"type_name" : "paho_mqtt",
"display_name": "Paho MQTT",
"description" : "Receive data from an MQTT server.",
"external_scripts" : [
"https://rawgit.com/benjaminchodroff/freeboard-mqtt/paho-mqtt-default/mqttws31.js"
],
"settings" : [
{
"name" : "server",
"display_name" : "MQTT Server",
"type" : "text",
"description" : "Hostname for your MQTT Server",
"required" : true
},
{
"name" : "port",
"display_name": "Port",
"type" : "number",
"description" : "The port to connect to the MQTT Server on",
"required" : true
},
{
"name" : "use_ssl",
"display_name": "Use SSL",
"type" : "boolean",
"description" : "Use SSL/TLS to connect to the MQTT Server",
"default_value": true
},
{
"name" : "client_id",
"display_name": "Client Id",
"type" : "text",
"default_value": "",
"required" : false
},
{
"name" : "username",
"display_name": "Username",
"type" : "text",
"default_value": "",
"required" : false
},
{
"name" : "password",
"display_name": "Password",
"type" : "text",
"default_value": "",
"required" : false
},
{
"name" : "topic",
"display_name": "Topic",
"type" : "text",
"description" : "The topic to subscribe to",
"required" : true
},
{
"name" : "json_data",
"display_name": "JSON messages?",
"type" : "boolean",
"description" : "If the messages on your topic are in JSON format they will be parsed so the individual fields can be used in freeboard widgets",
"default_value": false
}
],
// **newInstance(settings, newInstanceCallback, updateCallback)** (required) : A function that will be called when a new instance of this plugin is requested.
// * **settings** : A javascript object with the initial settings set by the user. The names of the properties in the object will correspond to the setting names defined above.
// * **newInstanceCallback** : A callback function that you'll call when the new instance of the plugin is ready. This function expects a single argument, which is the new instance of your plugin object.
// * **updateCallback** : A callback function that you'll call if and when your datasource has an update for freeboard to recalculate. This function expects a single parameter which is a javascript object with the new, updated data. You should hold on to this reference and call it when needed.
newInstance : function(settings, newInstanceCallback, updateCallback)
{
newInstanceCallback(new mqttDatasourcePlugin(settings, updateCallback));
}
});
var mqttDatasourcePlugin = function(settings, updateCallback)
{
var self = this;
var data = {};
var currentSettings = settings;
function onConnect() {
console.log("Connected");
client.subscribe(currentSettings.topic);
};
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
data.topic = message.destinationName;
if (currentSettings.json_data) {
data.msg = JSON.parse(message.payloadString);
} else {
data.msg = message.payloadString;
}
updateCallback(data);
};
// **onSettingsChanged(newSettings)** (required) : A public function we must implement that will be called when a user makes a change to the settings.
self.onSettingsChanged = function(newSettings)
{
client.disconnect();
data = {};
currentSettings = newSettings;
client.connect({onSuccess:onConnect,
userName: currentSettings.username,
password: currentSettings.password,
useSSL: currentSettings.use_ssl});
}
// **updateNow()** (required) : A public function we must implement that will be called when the user wants to manually refresh the datasource
self.updateNow = function()
{
// Don't need to do anything here, can't pull an update from MQTT.
}
// **onDispose()** (required) : A public function we must implement that will be called when this instance of this plugin is no longer needed. Do anything you need to cleanup after yourself here.
self.onDispose = function()
{
if (client.isConnected()) {
client.disconnect();
}
client = {};
}
var client = new Paho.MQTT.Client(currentSettings.server,
currentSettings.port,
currentSettings.client_id);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect,
userName: currentSettings.username,
password: currentSettings.password,
useSSL: currentSettings.use_ssl});
}
}());