server.js
function parseMQTTMessage (topic, message) {
    var contents = message.toString();
    winston.info('Incoming message from MQTT: %s = %s', topic, contents);

    // Remove the preface from the topic before splitting it
    var pieces = topic.substr(config.mqtt.preface.length + 1).split('/'),
        device = pieces[0],
        property = pieces[1],
        topicReadState = getTopicFor(device, property, TOPIC_READ_STATE),
        topicWriteState = getTopicFor(device, property, TOPIC_WRITE_STATE),
        topicSwitchState = getTopicFor(device, 'switch', TOPIC_READ_STATE),
        topicLevelCommand = getTopicFor(device, 'level', TOPIC_COMMAND);

    // Deduplicate only if the incoming message topic is the same as the read state topic
    if (topic === topicReadState) {
        if (history[topic] === contents) {
            winston.info('Skipping duplicate message from: %s = %s', topic, contents);
            return;
        }
    }
    history[topic] = contents;

    // If sending level data and the switch is off, don't send anything
    // SmartThings will turn the device on (which is confusing)
    if (property === 'level' && history[topicSwitchState] === 'off') {
        winston.info('Skipping level set due to device being off');
        return;
    }

    // If sending switch data and there is already a nonzero level value, send level instead
    // SmartThings will turn the device on
    if (property === 'switch' && contents === 'on' &&
        history[topicLevelCommand] > 0) {
        winston.info('Passing level instead of switch on');
        property = 'level';
        contents = history[topicLevelCommand];
    }

    request.post({
        url: 'http://' + callback,
        json: {
            name: device,
            type: property,
            value: contents,
            command: (!pieces[2] || pieces[2] && pieces[2] === config.mqtt[SUFFIX_COMMAND])
        }
    }, function (error, resp) {
        if (error) {
            // @TODO handle the response from SmartThings
            winston.error('Error from SmartThings Hub: %s', error.toString());
            winston.error(JSON.stringify(error, null, 4));
            winston.error(JSON.stringify(resp, null, 4));
        }
    });
}
function parseMQTTMessage (topic, message) {
    var contents = message.toString();
    winston.info('Incoming message from MQTT: %s = %s', topic, contents);

    // Remove the preface from the topic before splitting it
    var pieces = topic.substr(config.mqtt.preface.length + 1).split('/'),
        device = pieces[0],
        property = pieces[1],
        topicReadState = getTopicFor(device, property, TOPIC_READ_STATE),
        topicWriteState = getTopicFor(device, property, TOPIC_WRITE_STATE),
        topicSwitchState = getTopicFor(device, 'switch', TOPIC_READ_STATE),
        topicLevelCommand = getTopicFor(device, 'level', TOPIC_COMMAND);

    // Deduplicate only if the incoming message topic is the same as the read state topic
    if (topic === topicReadState) {
        if (history[topic] === contents) {
            winston.info('Skipping duplicate message from: %s = %s', topic, contents);
            return;
        }
    }
    history[topic] = contents;

    // If sending level data and the switch is off, don't send anything
    // SmartThings will turn the device on (which is confusing)
    if (property === 'level' && history[topicSwitchState] === 'off') {
        winston.info('Skipping level set due to device being off');
        return;
    }

    // If sending switch data and there is already a nonzero level value, send level instead
    // SmartThings will turn the device on
    if (property === 'switch' && contents === 'on' &&
        history[topicLevelCommand] > 0) {
        winston.info('Passing level instead of switch on');
        property = 'level';
        contents = history[topicLevelCommand];
    }

    request.post({
        url: 'http://' + callback,
        json: {
            name: device,
            type: property,
            value: contents,
            command: (!pieces[2] || pieces[2] && pieces[2] === config.mqtt[SUFFIX_COMMAND])
        }
    }, function (error, resp) {
        if (error) {
            // @TODO handle the response from SmartThings
            winston.error('Error from SmartThings Hub: %s', error.toString());
            winston.error(JSON.stringify(error, null, 4));
            winston.error(JSON.stringify(resp, null, 4));
        }
    });
}
function parseMQTTMessage ( topic , message ) { var contents = message . toString ( ) ; winston . info ( 'Incoming message from MQTT: %s = %s' , topic , contents ) ; // Remove the preface from the topic before splitting it var pieces = topic . substr ( config . mqtt . preface . length + 1 ) . split ( '/' ) , device = pieces [ 0 ] , property = pieces [ 1 ] , topicReadState = getTopicFor ( device , property , TOPIC_READ_STATE ) , topicWriteState = getTopicFor ( device , property , TOPIC_WRITE_STATE ) , topicSwitchState = getTopicFor ( device , 'switch' , TOPIC_READ_STATE ) , topicLevelCommand = getTopicFor ( device , 'level' , TOPIC_COMMAND ) ; // Deduplicate only if the incoming message topic is the same as the read state topic if ( topic === topicReadState ) { if ( history [ topic ] === contents ) { winston . info ( 'Skipping duplicate message from: %s = %s' , topic , contents ) ; return ; } } history [ topic ] = contents ; // If sending level data and the switch is off, don't send anything // SmartThings will turn the device on (which is confusing) if ( property === 'level' && history [ topicSwitchState ] === 'off' ) { winston . info ( 'Skipping level set due to device being off' ) ; return ; } // If sending switch data and there is already a nonzero level value, send level instead // SmartThings will turn the device on if ( property === 'switch' && contents === 'on' && history [ topicLevelCommand ] > 0 ) { winston . info ( 'Passing level instead of switch on' ) ; property = 'level' ; contents = history [ topicLevelCommand ] ; } request . post ( { url : 'http://' + callback , json : { name : device , type : property , value : contents , command : ( ! pieces [ 2 ] || pieces [ 2 ] && pieces [ 2 ] === config . mqtt [ SUFFIX_COMMAND ] ) } } , function ( error , resp ) { if ( error ) { // @TODO handle the response from SmartThings winston . error ( 'Error from SmartThings Hub: %s' , error . toString ( ) ) ; winston . error ( JSON . stringify ( error , null , 4 ) ) ; winston . error ( JSON . stringify ( resp , null , 4 ) ) ; } } ) ; }
Parse incoming message from MQTT
@method parseMQTTMessage
@param  {String} topic   Topic channel the event came from
@param  {String} message Contents of the event
Parse incoming message from MQTT