-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish-fx.ts
More file actions
79 lines (70 loc) · 2.58 KB
/
publish-fx.ts
File metadata and controls
79 lines (70 loc) · 2.58 KB
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
#!/usr/bin/env ts-node
import request from 'request';
import commandLineArgs from 'command-line-args';
import * as diffusion from 'diffusion';
const truefxURL = 'http://webrates.truefx.com/rates/connect.html';
const pollFreq = 1000;
const optionDefinitions = [
{ name: 'host', alias: 'h', type: String, defaultValue: 'localhost'},
{ name: 'port', alias: 'p', type: Number, defaultValue: 8080},
{ name: 'username', alias: 'U', type: String, defaultValue: 'control'},
{ name: 'password', alias: 'P', type: String, defaultValue: 'password'},
{ name: 'topic', alias: 't', type: String, defaultValue: 'Demos/Fx'}
];
const options = commandLineArgs(optionDefinitions);
const spec = new diffusion.topics.TopicSpecification(diffusion.topics.TopicType.JSON, {
COMPRESSION: 'false'
});
function placeTrueFxRequest(session: diffusion.Session, topicRoot: string) {
request({
url: truefxURL,
method: "GET",
qs: {f: 'csv'}
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
const topicValues: any[] = processCurrencyPairs(body);
const promises = topicValues.map((topicValue, i) =>
session.topicUpdate.set(
topicRoot + '/raw/' + i,
diffusion.datatypes.json(),
topicValue,
{specification: spec})
);
Promise.all(promises).then(() => {
console.log(`Updated ${promises.length} topics under ${topicRoot}/raw`);
}).catch((error) => console.error);
} else {
console.error(`Unexpected response from ${truefxURL}, statusCode: ${response.statusCode}`);
}
}
);
}
function processCurrencyPairs(csvData: string): any[] {
const lines = csvData.trim().split("\n");
return lines.map((line) => {
const [pairName, millis,
bidBig, bidPoints,
offerBig, offerPoints,
high, low, open] = line.split(",");
return {
pairName: pairName.replace('\/', ':'),
timestamp: millis,
bid: {big: bidBig, points: bidPoints},
offer: {big: offerBig, points: offerPoints},
high: high,
low: low,
open: open
}
});
}
diffusion.connect({
host : options.host,
port: options.port,
principal : options.username,
credentials : options.password
}).then((session) => {
console.log(`Connected to ${options.host}:${options.port}`);
setInterval(() => placeTrueFxRequest(session, options.topic), pollFreq);
}, (error) => {
console.error(`Cannot connect to Diffusion: ${error}`);
});