Skip to content

Instantly share code, notes, and snippets.

@tako2
Created December 11, 2017 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tako2/32ac4f5d8e9e392cda7b103b5cbc205c to your computer and use it in GitHub Desktop.
Save tako2/32ac4f5d8e9e392cda7b103b5cbc205c to your computer and use it in GitHub Desktop.
this file is node.js module, place the file to node_module/irkit/index.js and change "esp_irkit.local" to "irkit.local" if needed.
var request = require('request');
var fs = require('fs');
function postMessages(body, callback)
{
var options = {
uri: 'http://esp_irkit.local/messages',
method: 'POST',
headers: { 'Content-Type': 'application/json',
'Content-Length': body.length },
body: body
};
request(options, function(err, res, res_body) {
if (err) {
if (callback != null) {
callback('ERROR');
}
}
res_json = JSON.parse(res_body);
if (callback != null) {
callback(res_json.status);
}
});
}
function sendFile(json_file, callback)
{
var body = fs.readFileSync(json_file, 'utf-8');
postMessages(body, callback);
}
function send(json, callback)
{
var body = JSON.stringify(json);
postMessages(body, callback);
}
module.exports = {
send: send,
sendFile: sendFile
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment