Created
February 26, 2017 10:56
-
-
Save mfalkvidd/04775ec0f9d98e535b3dbc3bd8443d24 to your computer and use it in GitHub Desktop.
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
export default (request) => { | |
const debugPrefix = ""; | |
//const debugPrefix = "debug/"; | |
const base64Codec = require('codec/base64'); | |
const pubnub = require('pubnub'); | |
const kvstore = require('kvstore'); | |
const xhr = require('xhr'); | |
console.log("Before:"); | |
var payload = JSON.parse(request.message); | |
console.log(payload); | |
console.log(payload.data); | |
var humidity = Math.round((base64Codec.atob(payload.data)).charCodeAt(0) / 2.55); | |
console.log(humidity); | |
var ts = (((new Date(payload.time)).getTime())); | |
var jsonts = ts / 1000000; | |
console.log(jsonts); | |
var deviceEui = payload.deviceEui; | |
var deviceLabel = ""; | |
if (deviceEui == "00-00-00-00-FE-07-CC-56"){ | |
deviceLabel = "Humidity office plant 1"; | |
} else { | |
deviceLabel = deviceEui; | |
} | |
pubnub.publish({ | |
"channel": debugPrefix + "humidity", | |
"message":{ | |
"eon": { | |
[deviceLabel]: humidity, | |
"_eonDatetime": jsonts | |
}}}).then((publishResponse) => { | |
console.log(`Publish Status: ${publishResponse[0]}:${publishResponse[1]} with TT ${publishResponse[2]}`); | |
}); | |
// Tech stuff | |
var rssi = payload.rssi; | |
var snr = payload.snr; | |
pubnub.publish({ | |
"channel":debugPrefix + "techdata", | |
"message":{ | |
"eon": { | |
[deviceEui + " RSSI"]: rssi, | |
[deviceEui + " SNR"]: snr, | |
"_eonDatetime": jsonts | |
}}}).then((publishResponse) => { | |
console.log(`Publish Status: ${publishResponse[0]}:${publishResponse[1]} with TT ${publishResponse[2]}`); | |
}); | |
return request.ok(); // Return a promise when you're done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment