-
Notifications
You must be signed in to change notification settings - Fork 1
/
rateservice.js
executable file
·144 lines (131 loc) · 3.53 KB
/
rateservice.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
/**
* This is a service that pulls in rate information and republishes it
* over MQTT. See conf file rateservice.conf.
*/
const fs = require('fs')
const mqtt = require('mqtt')
const extend = require('extend') // To merge objects
const winston = require('winston') // Logging lib
const schedule = require('node-schedule') //Schedule Published Rates
const request = require('request') // HTTP Requests for APIs
const CONFIG_FILE = 'rateservice.conf'
// Default config that is extended (merged) with CONFIG_FILE
let config = {
logging: {
level: 'info'
},
debug: false,
mqtt: {
url: 'wss://getcanoe.io:1884/mqtt',
options: {
username: 'test',
password: 'gurka'
},
rates: {
topic: 'rates',
opts: {
qos: 2,
retain: true
}
}
}
}
// MQTT Client
let mqttClient = null
// Are we connected to MQTT?
var connected = false
// Read configuration
let configure = () => {
// Read config file if exists
if (fs.existsSync(CONFIG_FILE)) {
try {
let fileConfig = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'))
extend(true, config, fileConfig)
} catch (e) {
winston.error('Failed to parse config file: ' + CONFIG_FILE + e.message)
process.exit(1)
}
}
winston.level = config.logging.level
winston.debug('Configured')
}
// Connect to MQTT
let connectMQTT = () => {
winston.debug('Connecting MQTT')
mqttClient = mqtt.connect(config.mqtt.url, config.mqtt.options)
mqttClient.on('connect', () => {
winston.debug('Connected to MQTT server')
connected = true
})
}
let publishRates = (payload, callback) => {
mqttClient.publish(config.mqtt.rates.topic, payload, config.mqtt.rates.opts, callback)
}
// Want to notify before shutting down
let handleAppExit = (options, err) => {
if (err) {
winston.error(err.stack)
}
if (options.cleanup) {
winston.info('Cleaning up...')
mqttClient.end(true)
}
if (options.exit) {
winston.info('Calling exit...')
process.exit()
}
}
let configureSignals = () => {
// Handle the different ways an application can shutdown
process.on('exit', handleAppExit.bind(null, {
cleanup: true
}))
process.on('SIGINT', handleAppExit.bind(null, {
exit: true
}))
process.on('uncaughtException', handleAppExit.bind(null, {
exit: true
}))
}
let startScheduler = () => {
winston.debug('Scheduling job')
schedule.scheduleJob('0 * * * * *', () => {
winston.debug('Starting update ...')
if (connected) {
request('https://min-api.cryptocompare.com/data/price?fsym=XRB&tsyms=BTC,ETH', (error, response, btcPrice) => {
winston.debug('BTC price: ' + btcPrice)
btcPrice = JSON.parse(btcPrice)
request('https://bitpay.com/api/rates', (error, response, btcConversions) => {
btcConversions = JSON.parse(btcConversions)
btcConversions.forEach(function(pair) {
pair.rate = pair.rate * btcPrice.BTC
})
btcConversions.push({
code:"ETH",
name:"Ethereum",
rate: btcPrice.ETH
})
let rates = btcConversions.reduce(function(map, obj) {
map[obj.code] = {
name: obj.name,
rate: obj.rate
}
return map
}, {})
publishRates(JSON.stringify(rates), () => {
winston.info("Published Rates")
})
})
})
}
})
}
let main = () => {
winston.info('Started RateService')
configure()
configureSignals()
connectMQTT()
startScheduler()
}
main()