Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahees/e56d88e054aceb25028956344cb8e7d4 to your computer and use it in GitHub Desktop.
Save mahees/e56d88e054aceb25028956344cb8e7d4 to your computer and use it in GitHub Desktop.
converts web calls to mqtt calls. im using this with aws lambda (as its cheap) and i dont have a dedicated server.google-assistant-ifttt-maker-webhook-awslambda-mqtt
'use strict';
//lambda endpoint - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/prod/whatever-you-call-it?text={{TextField}}&time={{CreatedAt}}
//https://github.com/pmorjan/paho-node
/*
git clone https://github.com/pmorjan/paho-node.git
cd paho-node
npm install
wget https://raw.githubusercontent.com/eclipse/paho.mqtt.javascript/v1.0.2/src/mqttws31.js
git apply mqttws31.js.patch
node example.js
*/
var Paho = require('./mqttws31');
//TODO: change to location1
var MQTT_CLIENT = {
config: {
hostname: '',
port: 0,
qos: 1,
retain: false
}
};
var client = null;
var initMQTTConnection = function() {
return new Promise(function(resolve, reject) {
var clientID = "mobile" + new Date().getTime();
client = new Paho.MQTT.Client(MQTT_CLIENT.config.hostname, Number(MQTT_CLIENT.config.port), clientID);
client.onConnectionLost = function(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
};
client.onMessageArrived = function(message) {
console.log("onMessageArrived:" + message.payloadString, message.destinationName);
};
function onConnect() {
console.log("onConnect");
// client.subscribe("#");
// publish("topic/aaaa", "message");
resolve();
}
function doFail(e) {
console.log(e);
reject();
}
var options = {
useSSL: true,
userName: "",
password: "",
onSuccess: onConnect,
onFailure: doFail
}
client.connect(options);
})
};
var publish = function(topic, mess, options) {
return new Promise(function(resolve, reject) {
options = options || {};
var message = new Paho.MQTT.Message(mess);
message.destinationName = topic;
if (options.retained) { message.retained = options.retained; }
if (options.qos) { message.retained = options.qos; }
client.send(message);
return resolve();
});
}
exports.handler = (event, context, callback) => {
if (event.httpMethod != 'GET') {
return callback(null, {
statusCode: 403,
body: JSON.stringify({ message: `Unsupported method "${event.httpMethod}"` }),
headers: {
'Content-Type': 'application/json',
}
})
}
if (!event.queryStringParameters || !event.queryStringParameters.text) {
return callback(null, {
statusCode: 403,
body: JSON.stringify({ message: `Unsupported query "${event.httpMethod}"` }),
headers: {
'Content-Type': 'application/json',
}
})
}
var text = event.queryStringParameters.text;
var publishMessage = {
topic: null,
message: null
}
if (text.includes('on the living room light')) {
publishMessage.topic = 'living-room/lights/main';
publishMessage.message = '1';
} else if (text.includes('off the living room light')) {
publishMessage.topic = 'living-room/lights/main';
publishMessage.message = '0';
} else if (text.includes('on the secondary living room light')) {
publishMessage.topic = 'living-room/lights/secondary';
publishMessage.message = '1';
} else if (text.includes('off the secondary living room light')) {
publishMessage.topic = 'living-room/lights/secondary';
publishMessage.message = '0';
} else if (text.includes('on the outside light')) {
publishMessage.topic = 'outside/lights/main';
publishMessage.message = '1';
} else if (text.includes('off the outside light')) {
publishMessage.topic = 'outside/lights/main';
publishMessage.message = '0';
}
if (!publishMessage.topic || !publishMessage.message) {
return callback(null, {
statusCode: 403,
body: JSON.stringify({ message: `Unsupported text "${event.httpMethod}"` }),
headers: {
'Content-Type': 'application/json',
}
})
}
initMQTTConnection().then(function() {
publish(publishMessage.topic, publishMessage.message).then(function() {
client.disconnect();
callback(null, {
statusCode: 200,
body: JSON.stringify({ message: `good mon`, event: event }),
headers: {
'Content-Type': 'application/json'
}
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment