Created
December 11, 2017 03:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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