Created
August 4, 2017 17:22
-
-
Save sunnygleason/e517000d4ea493420ed5c0469026f298 to your computer and use it in GitHub Desktop.
PubNub IoT Events FUNCTION w/ Mnubo
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
const XHR = require('xhr'); | |
const db = require('kvstore'); | |
const basicAuth = require('codec/auth'); | |
function fetchAccessToken(authUri, clientId, clientSecret) { | |
const auth = basicAuth.basic(clientId, clientSecret); | |
const httpOptions = { | |
method: 'POST', | |
body: 'grant_type=client_credentials&scope=ALL', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
Authorization: auth | |
}, | |
}; | |
return XHR.fetch(authUri, httpOptions).then((x) => { | |
const token = JSON.parse(x.body); | |
token.requestedAt = new Date(); | |
return token; | |
}); | |
} | |
// See Mnubo API | |
function retrieveAccessToken(authUri, clientId, clientSecret) { | |
const fetchAndStoreAccessToken = () => { | |
return fetchAccessToken(authUri, clientId, clientSecret).then((newToken) => { | |
const expiresInSeconds = newToken.expiresIn * 1000; | |
return db.set('token', { value: newToken }, expiresInSeconds).then(() => newToken); | |
}); | |
}; | |
return db.get('token').then((obj) => { | |
const storedToken = obj.value; | |
if (storedToken) { | |
const now = new Date(); | |
const requestedAt = new Date(storedToken.requestedAt); | |
const expiredTime = requestedAt.getTime() + storedToken.expires_in * 1000; | |
const hasExpired = now.getTime() > expiredTime; | |
if (hasExpired) { | |
return fetchAndStoreAccessToken(); | |
} else { | |
return storedToken; | |
} | |
} else { | |
return fetchAndStoreAccessToken(); | |
} | |
}); | |
} | |
// See Mnubo Events POST API | |
function sendEvents(eventsUri, token, events) { | |
const httpOptions = { | |
method: 'POST', | |
body: JSON.stringify(events), | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bearer ${token.access_token}` | |
}, | |
}; | |
return XHR.fetch(eventsUri, httpOptions); | |
} | |
function onRequest(request) { | |
const clientId = 'YOUR_CLIENT_ID'; | |
const clientSecret = 'YOUR_CLIENT_SECRET'; | |
const baseUri = 'https://rest.sandbox.mnubo.com'; | |
const authUri = `${baseUri}/oauth/token`; | |
const eventsUri = `${baseUri}/api/v3/events`; | |
return retrieveAccessToken(authUri, clientId, clientSecret) | |
.then((token) => { | |
const device = request.message.id; | |
const timestamp = request.message.timestamp; | |
const status = request.message.status; | |
const events = [ | |
{ | |
x_object: { x_device_id: device }, | |
x_event_type: 'system_health', | |
x_timestamp: timestamp, | |
// TODO: add 'status' to your Mnubo schema | |
// status: status | |
} | |
]; | |
return sendEvents(eventsUri, token, events); | |
}).then((r) => { | |
request.message.result = r; | |
return request.ok(); | |
}).catch((e) => { | |
console.error(e); | |
return request.ok(); | |
}); | |
} | |
export default onRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment