Skip to content

Instantly share code, notes, and snippets.

@minato128 minato128/awair-to-mackerel.js Secret
Last active Sep 13, 2019

Embed
What would you like to do?
send awair metrics to mackerel
// https://docs.developer.getawair.com/?version=latest#tiers--quotas
const request = require('request');
module.exports = async (context) => {
const timeStamp = new Date().toISOString();
// setting
const deviceType = 'DEVICE';
const deviceId = 1234;
const awairApiKey = 'APIKEY';
const mackerelApiKey = 'APIKEY';
// get metrics from awair
const options = {
url: `https://developer-apis.awair.is/v1/users/self/devices/${deviceType}/${deviceId}/air-data/latest`,
headers: {
Authorization: `Bearer ${awairApiKey}`,
},
};
request.get(options, (error, response, body) => {
if (error) {
console.log(error);
console.log(response);
return;
}
if (body && body.includes('Too many requests')) {
console.log(body);
return;
}
const now = Math.floor(new Date().getTime() / 1000);
// console.log(body);
const json = JSON.parse(body);
const payload = [{ name: 'score.awair1', time: now, value: json.data[0].score }];
json.data[0].sensors.map((x) => payload.push({ name: `${x.comp}.awair1`, time: now, value: x.value }));
// context.log(payload);
// send metrics to mackarel
const options2 = {
url: 'https://api.mackerelio.com/api/v0/services/awair/tsdb',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': mackerelApiKey,
},
json: payload,
};
request(options2, (error, response, body) => {
if (error) {
console.log(error);
return;
}
// context.log(body);
});
});
context.log('triggered', timeStamp);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.