-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.js
39 lines (33 loc) · 1006 Bytes
/
sensor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Nomad = require('nomad-stream')
axios = require('axios')
moment = require('moment')
const nomad = new Nomad()
const QUAKE_DATA_URL = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson'
const POLL_INTERVAL_SECONDS = 60
let instance = null
nomad.prepareToPublish()
.then((node) => {
instance = node
return axios.get(QUAKE_DATA_URL)
.then(({ data }) => {
const message = JSON.stringify(data)
console.log('Root publish:', message)
return instance.publishRoot(message)
})
.catch((error) => {
console.log(error)
})
})
.then(() => {
setInterval(() => {
axios.get(QUAKE_DATA_URL)
.then(({ data }) => {
const message = JSON.stringify(data)
console.log('Regular publish:', message)
instance.publish(message)
})
.catch((error) => {
console.log(error)
})
}, POLL_INTERVAL_SECONDS * 1000)
})