-
Notifications
You must be signed in to change notification settings - Fork 2
/
pusher.js
160 lines (135 loc) · 4.62 KB
/
pusher.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const http = require('http')
, util = require('util')
, fs = require('fs')
, NodeRSA = require('node-rsa')
, cmd = require('node-cmd');
var next_update_in_ms = 0;
var FALLBACK_INTERVAL = 2*1000; // 2 seconds
var API_HOST = ['ec2-54-218-16-105.us-west-2.compute.amazonaws.com', 80];
var API_HOST_DEV = ['34.212.66.216', 8080];
var UPDATE_INTERVAL = 6*60*1000; // six minutes
// var API_HOST = ['localhost', 8080];
// var UPDATE_INTERVAL = 30*1000; // 30 seconds
var key_data = fs.readFileSync('keys/expresslanesapi.key.pub', 'utf8');
var publickey = new NodeRSA(key_data);
function parseSqlCmd(response){
var lines = response.split('\n');
var labels = lines[0].replace(/\s\s+/g, ' ').split(' ');
// console.log('labels : ', labels);
var split = lines[1].split(' ');
var counts = [];
for (var i=0; i < split.length; i++){
counts.push(split[i].length);
}
var res = [];
var label_index = 0;
for (var i=2; i<lines.length; i++){
var line = lines[i];
var obj = {};
var line_index = 0;
// console.log(line, line.length);
for (var j=0; j < counts.length; j++){
var end = line_index+counts[j];
var sliced = line.slice(line_index, end-1);
obj[labels[label_index]] = sliced.trim();
// console.log(labels[label_index]+' : '+obj[labels[label_index]]);
line_index += counts[j]+1;
label_index++;
}
res.push(obj);
label_index = 0
}
res.pop();
return res;
}
function pulse(){
console.log('running stored procedure...');
var this_update_in_ms = new Date().getTime();
//var dummy = [
// {
// "biCalSeqID": "35897",
// "Plaza_Name": "FSE",
// "Interval_Starting": (new Date()).getTime(),
// "Pricing_Module": "1.90",
// "Message_Module": "HOV 2+ NO TOLL",
// "User": "SYSTEM",
// "Algorithm_Mode": "EL Speed"
// },
// {
// "biCalSeqID": "35897",
// "Plaza_Name": "CLW",
// "Interval_Starting": (new Date()).getTime(),
// "Pricing_Module": "1.80",
// "Message_Module": "HOV 2+ NO TOLL",
// "User": "SYSTEM",
// "Algorithm_Mode": "EL Speed"
// }
//];
//sendData(dummy);
//return;
cmd.get(
'sqlcmd.exe -S "VTA00DB01" -d DMS -Q "DMS.dbo.uspGetSignMessageCurrent"',
function(data){
if (data.length === 0){
console.error('Failed to run the SQL query.');
return;
}
var data = parseSqlCmd(data);
var clean_data = [];
for (var i = 0; i < data.length; i++){
var obj = data[i];
obj['Interval_Starting'] = new Date(obj['Interval_Starting']).getTime();
this_update_in_ms = obj['Interval_Starting'];
clean_data.push(obj);
}
console.log('Got data that was updated at '+new Date(this_update_in_ms).toLocaleTimeString());
//Send to prod
sendData(clean_data, API_HOST, publickey);
//Send to Dev
sendData(clean_data, API_HOST_DEV, publickey);
// get new data five minutes from the time given
next_update_in_ms = (this_update_in_ms + UPDATE_INTERVAL) - (new Date()).getTime();
console.log('next update should be in '+next_update_in_ms+' ms (at '+new Date(next_update_in_ms + (new Date().getTime())).toLocaleTimeString()+')');
if (next_update_in_ms < 0) {
// if for some reason the data is old, then try again in 2 seconds.
console.log('data was old; trying again in two seconds instead.');
next_update_in_ms = FALLBACK_INTERVAL;
}
setTimeout(pulse, next_update_in_ms);
}
);
}
function sendData(data, api_host_var, publickey_var){
var toll_data = JSON.stringify(data);
console.log('data to encrypt: ', toll_data);
toll_data = publickey_var.encrypt(toll_data, 'base64');
console.log('encrypted to: ', toll_data);
var body = JSON.stringify({"tolls" : toll_data});
var request = new http.ClientRequest({
host: api_host_var[0],
port: api_host_var[1],
path: "/",
method: "PUT",
"agent":false,
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
}
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
request.on('response', function(response){
if (response.statusCode === 200){
console.log('response: OK');
}
if (response.statusCode === 400){
console.log('response from server : ', response.statusCode, response.statusMessage);
}
});
console.log(body);
request.end(body);
}
console.log('Running...');
pulse();