Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
client.on('subscribe', function(packet) {
try {
// create a new client object to the new online broker
// subscribe
var newclient;
var topic = packet.subscriptions[0].topic;
if(conf.connector.mqtt.secure){
newclient = mqtt.createSecureClient(conf.connector.mqtt.port, conf.connector.mqtt.host, tlsArgs);
} else {
newclient = mqtt.createClient(conf.connector.mqtt.port, conf.connector.mqtt.host);
}
if(topic === 'data'){
newclient.subscribe(buildPath(metric_topic, [secret.accountId, deviceId]));
logger.info('Subscribed to topic:' + buildPath(metric_topic, [secret.accountId, deviceId]));
} else {
newclient.subscribe(buildPath(metric_topic, [secret.accountId, deviceId]) + '/' + topic);
logger.info('Subscribing to topic:' + buildPath(metric_topic, [secret.accountId, deviceId]) + '/' + topic);
}
newclient.on('message', function (topic, message) {
logger.info('Received a message on subscribed topic: ' + topic);
client.publish({"topic": topic, "payload": message});
});
]
};
// Add metrics to the message
msg.data_source[0].metrics[0] = makeItem(val.metric, val.value);
// If in debug than print to console, else send to broker
logger.info('Message: ', msg);
broker.publish(broker_topic, JSON.stringify(msg));
}
// ************************************************************
// Local variables
// ************************************************************
account_id = BROKER_OPTS.username;
broker_topic = BROKER_DATA_TOPIC + "/" + account_id + "/" + device_id
broker = mqtt.createSecureClient(BROKER_PORT, BROKER_HOST, BROKER_OPTS);
// ************************************************************
// REST Server
// ************************************************************
var rest = express();
logger.info('Starting REST broker on %s ...', SERVER_REST_PORT);
rest.configure(function() {
rest.use(express.favicon());
rest.use(express.logger('dev'));
rest.use(express.json());
rest.use(express.urlencoded());
rest.use(express.methodOverride());
rest.use(express.errorHandler());
});
rest.get('/', function (request, response) {
var makeItem = function(name, value){
var item_template = {
"name": "",
"sample": [ { "value": 0, "timestamp": 0 } ]
}
var item = cloneObject(item_template);
item.name = name;
item.sample[0].value = value;
item.sample[0].timestamp = getTimestamp();
return item;
}
// MQTT Client
var broker = mqtt.createSecureClient(BROKER_PORT, BROKER_HOST, BROKER_OPTS);
// Main function
var get_metrics = function(){
// get MAC address
mac.getMac(function(err, macAddress){
// array to hold the metrics
var metrics = [];
// clone message, new instance
var msg = {
"msg_type": "metrics_msg",
"sender_id": device_id,
"account_id": account_id,
"timestamp": getTimestamp(),
function initBroker(){
logger.info("Initializing MQTT broker [%s:%s] ",
config.mqtt.host, config.mqtt.port);
if (config.mqtt.secure){
logger.info("Creating secure MQTT subscription...");
broker = mqtt.createSecureClient(config.mqtt.port,
config.mqtt.host,
config.mqtt.args);
}else{
logger.info("Creating basic MQTT subscription...");
broker = mqtt.createClient(config.mqtt.port,
config.mqtt.host,
config.mqtt.args);
}
broker.subscribe(config.mqtt.topic)
.on("message", function(topic, message){
logger.info("Message for: ", topic);
backendEvents.emit("data", topic, message);
});
logger.info("MQTT broker initialized");
var MqttMessageEmitter = function (topic, config, logger){
var self = this;
self.logger = logger;
var broker = null;
if (config.mqtt.secure){
logger.info("Creating secure MQTT subscription...");
broker = mqtt.createSecureClient(config.mqtt.port,
config.mqtt.host,
config.mqtt.args);
}else{
logger.info("Creating basic MQTT subscription...");
broker = mqtt.createClient(config.mqtt.port,
config.mqtt.host,
config.mqtt.args);
}
logger.info("MQTT subscription created [%s:%s] ",
config.mqtt.host, config.mqtt.port);
broker.subscribe(topic)
.on('message', function(resultTopic, message){
self.logger.info("Message %s > %s ", topic, resultTopic);
self.emit(topic, resultTopic, message);
me.connect = function (done) {
var retries = 0;
try {
if ((me.client instanceof mqtt.MqttClient) === false) {
if (me.secure === false) {
me.logger.info("Non Secure Connection to "+ me.host + ":" + me.port);
me.client = mqtt.createClient(me.port, me.host, me.credential).on('error', function(e) {
logger.warn("Error in connection: " + JSON.stringify(e));
});
} else {
me.logger.info("Trying with Secure Connection to" + me.host + ":" + me.port);
me.logger.debug("with " + JSON.stringify(me.credential));
me.client = mqtt.createSecureClient(me.port, me.host, me.credential).on('error', function(e){
logger.warn("Error in secure connection: " + JSON.stringify(e));
});
}
}
} catch(e) {
logger.error("Error in connection ex: " + JSON.stringify(e));
done(new Error("Connection Error", 1002));
return;
}
var counter = 20000;
function waitForConnection() {
if (!me.client.connected) {
if(counter < 18000){
counter += 100;
setTimeout(waitForConnection, 100);
me.connect = function(done) {
var retries = 0;
try {
if (me.secure) {
me.logger.info("Trying with Secure Connection to", me.host, ":", me.port, "with ", me.tlsArgs);
me.client = mqtt.createSecureClient(me.port, me.host, me.tlsArgs);
}
else {
me.logger.info("Non Secure Connection to ", me.host, ":", me.port);
me.client = mqtt.createClient(me.port, me.host);
}
} catch(e) {
done(new Error("Connection Error", 1002));
return;
}
function waitForConnection() {
if (!me.client.connected) {
retries++;
me.logger.info("Waiting # ", retries);
if (retries < me.max_retries) {
setTimeout(waitForConnection, 500);
} else {