-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderGateway.js
162 lines (125 loc) · 3.63 KB
/
orderGateway.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
161
162
// Created by Max Angelma on 5.4.2017
// Modified for FIS course on 23.4.2017
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var request = require('request');
var port = 4099;
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var tilaus;
// JSON-order is sent here
function send(order) {
console.log("sending:");
console.log(order);
request.post({
headers: { "content-type" : "application/json" },
url: 'http://localhost:4107/order',
json: order
}, function(error, response, body){
//console.log(body);
if (error) { console.log(error); }
});
}
// JSON-order is sent here
function schedule(order) {
console.log("scheduling execution");
console.log("Order lenght: " + order.length);
//console.log(order[0]);
console.log(order);
/*
request.post({
headers: { "content-type" : "application/json" },
url: 'http://localhost:4107/order',
json: order
}, function(error, response, body){
//console.log(body);
if (error) { console.log(error); }
});
*/
}
function releaseNext() {
//console.log(tilaus);
//console.log(tilaus);
if (tilaus.length >= 1) {
send(tilaus[tilaus.length-1]);
tilaus.pop();
console.log("next released");
} else {
console.log("empty queue")
}
//console.log(tilaus);
}
function pollSimulator() {
request.get({
url: 'http://localhost:3000/RTU/CNV7/data/S1'
}, function(error, response, body){
//console.log(body);
if (error) {
var fault = error.code;
console.log(fault);
io.emit('simulatorState', fault);
//throw TypeError ("Simulator is offline, please come next year");
} else {
//console.log("simulator is alive");
io.emit('simulatorState', "OK");
}
});
}
// listening to port 4099
http.listen(port, function(){
console.log('The order gateway is listening to ' + port);
console.log('\n');
});
// GET sends index.html
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var i = 0;
// Socket.io magic happens here
io.on('connection', function(socket){
socket.on('moveToProd', function(order){
//console.log("MovetoProd socket io received");
//console.log(order);
if (order != "0") {
i++;
//send(order);
tilaus = order;
releaseNext();
//console.log("Order: " + i);
//io.emit('emitOrder', order);
}
});
socket.on('queue', function(order){
console.log("Queue socket io received, not executing anything. Should I do something?");
//console.log(order);
/*
if (order != "0") {
i++;
send(order);
console.log("Order: " + i);
setTimeout(function() {
io.emit('emitOrder', order);
},1000);
} */
});
});
// POSTs handled here, no functionality yet
app.post('/', function(req, res){
console.log("order ready?");
console.log(req.body);
io.emit('finishedOrder', req.body);
setTimeout(function() {
releaseNext();
}, 3000);
res.end('\n ordering system received information');
});
// POSTs handled here, no functionality yet
app.post('/update', function(req, res){
console.log("/update received post with body: ");
console.log(req.body);
io.emit('updateOrder', req.body);
res.end('\n updating...');
});
setInterval(pollSimulator, 5000);