131 lines
4.6 KiB
JavaScript
131 lines
4.6 KiB
JavaScript
|
|
|||
|
|
|||
|
|
|||
|
var IoTFconnector = function (orgId, api_key, auth_token, $rootScope) {
|
|||
|
|
|||
|
|
|||
|
//this.connected = '';
|
|||
|
this.clientId = "a:" + orgId + ":" + Date.now();
|
|||
|
|
|||
|
console.log("clientId: " + this.clientId);
|
|||
|
this.hostname = orgId + ".messaging.internetofthings.ibmcloud.com";
|
|||
|
this.client = '';
|
|||
|
|
|||
|
this.initialize = function () {
|
|||
|
|
|||
|
client = new Messaging.Client(this.hostname, 8883, this.clientId);
|
|||
|
|
|||
|
|
|||
|
client.onMessageArrived = function (msg) {
|
|||
|
console.log("Message from :" + msg.destinationName);
|
|||
|
};
|
|||
|
|
|||
|
var connectOptions = new Object();
|
|||
|
connectOptions.keepAliveInterval = 3600;
|
|||
|
connectOptions.useSSL = true;
|
|||
|
connectOptions.userName = api_key;
|
|||
|
connectOptions.password = auth_token;
|
|||
|
|
|||
|
connectOptions.onSuccess = function () {
|
|||
|
IoTFconnector.prototype.clientStatus.connected = true;
|
|||
|
// $rootScope.$broadcast("clientStatusUpdated", clientStatus);
|
|||
|
console.log("MQTT connected to host: " + client.host + " port : " + client.port + " at " + Date.now());
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
connectOptions.onFailure = function (e) {
|
|||
|
console.log("MQTT connection failed at " + Date.now() + "\nerror: " + e.errorCode + " : " + e.errorMessage);
|
|||
|
}
|
|||
|
|
|||
|
console.log("about to connect to " + client.host);
|
|||
|
client.connect(connectOptions);
|
|||
|
|
|||
|
//client = new Messaging.Client(this.hostname, 8883, this.clientId);
|
|||
|
|
|||
|
//// Initialize the Realtime Graph
|
|||
|
////var rtGraph = new RealtimeGraph();
|
|||
|
//client.onMessageArrived = function(msg) {
|
|||
|
// //var topic = msg.destinationName;
|
|||
|
|
|||
|
// //var payload = JSON.parse(msg.payloadString);
|
|||
|
// ////First message, instantiate the graph
|
|||
|
// //if (firstMessage) {
|
|||
|
// // $('#chart').empty();
|
|||
|
// // firstMessage = false;
|
|||
|
// // rtGraph.displayChart(null, payload);
|
|||
|
// //} else {
|
|||
|
// // rtGraph.graphData(payload);
|
|||
|
// //}
|
|||
|
// console.log("Message from :" + msg.destinationName);
|
|||
|
//};
|
|||
|
|
|||
|
//client.onConnectionLost = function(e) {
|
|||
|
// console.log("Connection Lost at " + Date.now() + " : " + e.errorCode + " : " + e.errorMessage);
|
|||
|
// this.connect(connectOptions);
|
|||
|
//}
|
|||
|
|
|||
|
//var connectOptions = new Object();
|
|||
|
//connectOptions.keepAliveInterval = 3600;
|
|||
|
//connectOptions.useSSL = true;
|
|||
|
//connectOptions.userName = api_key;
|
|||
|
//connectOptions.password = auth_token;
|
|||
|
|
|||
|
//connectOptions.onSuccess = function() {
|
|||
|
// IoTFconnector.prototype.connected = true;
|
|||
|
// console.log("MQTT connected to host: " + client.host + " port : " + client.port + " at " + Date.now());
|
|||
|
|
|||
|
//}
|
|||
|
|
|||
|
//connectOptions.onFailure = function(e) {
|
|||
|
// console.log("MQTT connection failed at " + Date.now() + "\nerror: " + e.errorCode + " : " + e.errorMessage);
|
|||
|
//}
|
|||
|
|
|||
|
//console.log("about to connect to " + client.host);
|
|||
|
//client.connect(connectOptions);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
this.initialize();
|
|||
|
|
|||
|
//var imageHTML = '<div class="iotdashboardtext">The selected device is not currently sending events to the Internet of Things Foundation</div><br><div class="iotdashboardtext">Select to view historical data or select a different device.</div> <img class="iotimagesMiddle" align="middle" alt="Chart" src="../../images/IOT_Icons_Thing02.svg">';
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
IoTFconnector.prototype.clientStatus = { connected: false, subscribeTopic: '' };;
|
|||
|
|
|||
|
IoTFconnector.prototype.disconnect = function disconnect() {
|
|||
|
client.disconnect();
|
|||
|
console.log("Connection closed");
|
|||
|
}
|
|||
|
|
|||
|
IoTFconnector.prototype.connect = function connect() {
|
|||
|
// this.initialize();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
IoTFconnector.prototype.subscribe = function subscribe(deviceId) {
|
|||
|
var subscribeOptions = {
|
|||
|
qos: 0,
|
|||
|
onSuccess: function () {
|
|||
|
console.log("subscribed to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
|
|||
|
},
|
|||
|
onFailure: function () {
|
|||
|
console.log("Failed to subscribe to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
|
|||
|
console.log("As messages are not available, visualization is not possible");
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
if (IoTFconnector.prototype.clientStatus.subscribeTopic != "") {
|
|||
|
console.log("Unsubscribing to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
|
|||
|
client.unsubscribe(IoTFconnector.prototype.clientStatus.subscribeTopic);
|
|||
|
};
|
|||
|
|
|||
|
IoTFconnector.prototype.clientStatus.subscribeTopic = "iot-2/type/iotsample-ti-cc3200/id/" + deviceId + "/evt/+/fmt/json";
|
|||
|
|
|||
|
client.subscribe(IoTFconnector.prototype.clientStatus.subscribeTopic, subscribeOptions);
|
|||
|
}
|