Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added actionOpenClosed for contactSensors (simulated) #154

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
49 changes: 34 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*jslint node: true */
'use strict';

Expand Down Expand Up @@ -195,8 +196,12 @@ function handleSubscribeEvent (req, res) {
subscriptions = [];
Object.keys(req.body.devices).forEach(function (property) {
req.body.devices[property].forEach(function (device) {
subscriptions.push(getTopicFor(device, property, TOPIC_COMMAND));
subscriptions.push(getTopicFor(device, property, TOPIC_WRITE_STATE));
var cmdTopic = getTopicFor(device, property, TOPIC_COMMAND);
var writeTopic = getTopicFor(device, property, TOPIC_WRITE_STATE);
subscriptions.push(cmdTopic);
if (writeTopic !== cmdTopic) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

subscriptions.push(writeTopic);
}
});
});

Expand Down Expand Up @@ -251,14 +256,13 @@ function getTopicFor (device, property, type) {
*/
function parseMQTTMessage (topic, message) {
var contents = message.toString();
winston.info('Incoming message from MQTT: %s = %s', topic, contents);
//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);

Expand All @@ -270,6 +274,7 @@ function parseMQTTMessage (topic, message) {
}
}
history[topic] = contents;
winston.info('Incoming message from MQTT: %s = %s', topic, contents);

// If sending level data and the switch is off, don't send anything
// SmartThings will turn the device on (which is confusing)
Expand All @@ -287,24 +292,38 @@ function parseMQTTMessage (topic, message) {
contents = history[topicLevelCommand];
}

request.post({
var json = {
name: device,
type: property,
value: contents,
//command: (!pieces[2] || pieces[2] && pieces[2] === config.mqtt[SUFFIX_COMMAND])
command: (pieces[2] && pieces[2] === config.mqtt[SUFFIX_COMMAND]) || false
};

retryPost(json, true);
}

function retryPost(json, retry) {
request.post({
url: 'http://' + callback,
json: {
name: device,
type: property,
value: contents,
command: (!pieces[2] || pieces[2] && pieces[2] === config.mqtt[SUFFIX_COMMAND])
}
json: json
}, 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));
if ((error.code === "ETIMEDOUT") && retry) {
winston.error('Error connecting SmartThings, retrying');
retryPost(json);
}
else {
// @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));
}
}
});
}


// Main flow
async.series([
function loadFromDisk (next) {
Expand Down
Loading