-
Notifications
You must be signed in to change notification settings - Fork 26
/
ibm.iotfoundation.plugin.js
155 lines (144 loc) · 5.71 KB
/
ibm.iotfoundation.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
149
150
151
152
153
154
155
// # A Freeboard Plugin for IBM's IoT Foundation service; https://internetofthings.ibmcloud.com/
(function()
{
// ### Datasource Definition
// Please replace the external_scripts location with a local replica of the Paho MQTT client when possible
// -------------------
freeboard.loadDatasourcePlugin({
"type_name" : "ibm_iotf",
"display_name": "IBM IoT Foundation",
"description" : "Receive data from your devices in IBM IoT Foundation.",
"external_scripts" : [
"https://rawgit.com/benjaminchodroff/freeboard-mqtt/paho-mqtt-default/mqttws31.js"
],
"settings" : [
{
"name" : "org_id",
"display_name" : "Organisation",
"type" : "text",
"description" : "Your IoT Foundation organisation.",
"required" : true
},
{
"name" : "device_id",
"display_name": "Device",
"type" : "text",
"description" : "The device id to read data from.\nIf left empty data will be read for all devices in your organisation.",
"required" : false
},
{
"name" : "api_key",
"display_name": "API Key",
"description" : "An IoT Foundation API key for your organisation",
"type" : "text",
"required" : true
},
{
"name" : "api_auth_token",
"display_name": "API Auth Token",
"description" : "The Auth Token to match the API key",
"type" : "text",
"required" : true
}
],
// **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 iotfDatasourcePlugin(settings, updateCallback));
}
});
// ### Datasource Implementation
//
// -------------------
var iotfDatasourcePlugin = function(settings, updateCallback)
{
var self = this;
var data = {};
var currentSettings = settings;
function onConnect() {
console.log("Connected");
var topic;
if (currentSettings.device_id === undefined) {
topic = 'iot-2/type/+/id/+/evt/+/fmt/json';
} else {
topic = 'iot-2/type/+/id/' + currentSettings.device_id + '/evt/+/fmt/json';
}
console.log(topic);
client.subscribe(topic);
};
function onConnectionLost(responseObject) {
console.log("Connection Lost");
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:"+responseObject.errorMessage);
client.connect({onSuccess:onConnect,
userName: currentSettings.api_key,
password: currentSettings.api_auth_token,
useSSL: true,
timeout: 10,
cleanSession: true,
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
});
};
function onMessageArrived(message) {
var device = message.destinationName.split('/')[4];
var msg = JSON.parse(message.payloadString);
data[device] = msg;
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.api_key,
password: currentSettings.api_auth_token,
useSSL: true
});
}
// **updateNow()** (required) : A public function we must implement that will be called when the user wants to manually refresh the datasource
self.updateNow = function()
{
console.log("Forcing Update");
client.connect({onSuccess:onConnect,
userName: currentSettings.api_key,
password: currentSettings.api_auth_token,
useSSL: true,
timeout: 10,
cleanSession: true,
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
});
}
// **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 = {};
}
console.log((new Date().getTime()).toString());
var client = new Paho.MQTT.Client(currentSettings.org_id + '.messaging.internetofthings.ibmcloud.com',
8883, 'a:'+currentSettings.org_id+':'+currentSettings.api_key + (new Date().getTime()).toString());
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect,
userName: currentSettings.api_key,
password: currentSettings.api_auth_token,
useSSL: true,
timeout: 10,
cleanSession: true,
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
});
}
}());