我在AWS中有一个lambda函数,只要从我的iot设备发送mqtt有效负载就应该调用它。我有一个iot规则设置,订阅mqtt主题,应该将有效负载从csv转换为json格式,但现在该函数永远不会被调用。
我已经检查过以确保mqtt主题是使用正确的有效负载发布的,而且肯定是。这是我的lambda函数的'index.js'。
var https = require('https');
exports.handler = (event, context, callback) => {
//split lines in csv
var lines = event.split("\n");
var result = [];
var headers = ["Timestamp","Temperature","humidity","counts_0.3","counts_0.5","counts_1","counts_2.5","counts_5","counts_10","pm1","pm2.5","pm10","pma1","pma2.5","pma10"];
for (var i = 0; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//store json Object in postData to be published to Losant
var postData = JSON.stringify(result); //JSON
var options = {
hostname: 'triggers.losant.com',
port: 443,
path: '/webhooks/nV2kS3GWzETodHeFin4vlLhoAwmqkFdtfdmGV7we',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
callback();
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
callback();
});
// write data to request body
req.write(postData);
req.end();
};
以下是我的物联网规则的一些截图。