Passed
Push — master ( f97c18...11c3c5 )
by Dave
51s
created

sse.connection   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 8
dl 0
loc 6
rs 10
nop 0
1
"use strict";
2
3
const express = require("express");
4
const serveStatic = require("serve-static");
5
const path = require("path");
0 ignored issues
show
Unused Code introduced by
The constant path seems to be never used. Consider removing it.
Loading history...
6
const SSE = require("sse");
7
const redis = require("redis");
0 ignored issues
show
Unused Code introduced by
The constant redis seems to be never used. Consider removing it.
Loading history...
8
const amqp = require("amqplib/callback_api");
9
10
const app = express();
11
12
// const passport = require('passport')    
13
// const BasicStrategy = require('passport-http').BasicStrategy
14
// passport.use(new BasicStrategy(
15
// 	function(username, password, done) {
16
// 		//todo: obviously needs to change
17
// 	  	if (username.valueOf() === "fullcycle" && password.valueOf() === "mining")
18
// 			return done(null, true);
19
// 		  else
20
// 		  {
21
// 			console.log("rejected!")
22
// 			return done(null, false);
23
// 		  }
24
// 	}
25
//   ));
26
27
const services = require("./services");
28
const messages = require("./messages");
0 ignored issues
show
Unused Code introduced by
The constant messages seems to be never used. Consider removing it.
Loading history...
29
30
var api = require("./api.js");
31
32
function bail(err, conn) {
33
  console.error("bailing...");
34
  console.error(err);
35
  if (conn) conn.close(function() {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
36
		// if (doexit)
37
		// 	process.exit(1); 
38
	});
39
}
40
41
42
//route all other calls to the home page. this is causing "path is not defined" in line 179
43
// app.get("/*", function(req, res) {
44
//   res.sendFile(path.join(__dirname, "/index.html"), function(err) {
45
//     if (err) {
46
//       res.status(500).send(err)
47
//     }
48
//   })
49
// });
50
51
//in production this serves up the react bundle
52
app.use(serveStatic("../web/build")
53
//,
54
//	passport.authenticate("basic", { session: false })
55
);
56
app.use("/api", api);
57
var server = app.listen(services.web.port, () => console.log(`Listening on port ${services.web.port}`));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
58
function onWebError(error) {
59
	if (error.syscall !== "listen") {
60
	  throw error;
61
	}
62
  
63
	var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
0 ignored issues
show
Bug introduced by
The variable port seems to be never declared. If this is a global, consider adding a /** global: port */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
  
65
	// handle specific listen errors with friendly messages
66
	switch (error.code) {
67
		case "EACCES":
68
			console.error(error.code + ":" + bind + " requires elevated privileges");
69
			process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
70
			break;
71
		case "EADDRINUSE":
72
			console.error(error.code + ":" + bind + " is already in use. Close the other app and try again");
73
			process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
74
			break;
75
		default:
76
			throw error;
77
	}
78
  }
79
  server.on("error", onWebError);
80
81
var busConnect = null;
82
83
function on_connect(err, conn) {
84
	if (err !== null) return bail(err);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
85
	process.once("SIGINT", function() { conn.close(); });
86
87
	busConnect = conn;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
88
89
}
90
91
//set up the full cycle alerts feed to send alerts to the browser
92
var sse = new SSE(server);
93
sse.on("connection", function (sseConnection) {
94
	console.log("new sse connection");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
95
	
96
	const qAlert = "alert";
97
	let alertChannel = null;
98
99
	function alertMessage(msg) {
100
		if (msg) {
101
			//msg.content.toString()
102
			console.log(" [x] '%s'", "received alert message");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
103
			sseConnection.send({
104
				event: "full-cycle-alert",
105
				data: msg.content.toString()
106
			});
107
		}
108
	}
109
110
	function on_channel_open_alert(err, ch) {
111
		if (err !== null) { return bail(err, busConnect); }
112
		alertChannel = ch;
113
		ch.on("error", function (err) {
114
			console.error(err);
115
			console.log("channel Closed");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
116
		});
117
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
118
			var q = ok.queue;
119
			ch.bindQueue(q, qAlert, "");
120
			ch.consume(q, alertMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
121
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
122
				console.log(" [*] Waiting for alert. To exit press CTRL+C.");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
123
			});
124
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
125
	}
126
127
	const qMiner = "statisticsupdated";
128
	let miner_channel = null;
129
130
	function minerMessage(msg) {
131
		if (msg) {
132
			//msg.content.toString()
133
			console.log(" [x] '%s'", "received miner message");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
134
			sseConnection.send({
135
				event: "full-cycle-miner",
136
				data: msg.content.toString()
137
			});
138
		}
139
	}
140
141
	function on_channel_open_miner(err, ch) {
142
		if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
143
		miner_channel = ch;
144
		ch.on("error", function (err) {
145
			console.error(err);
146
			console.log("miner channel Closed");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
147
		});
148
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
149
			var q = ok.queue;
150
			ch.bindQueue(q, qMiner, "");
151
			ch.consume(q, minerMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
152
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
153
				console.log(" [*] Waiting for miner stats. To exit press CTRL+C.");
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
154
			});
155
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
156
	}
157
158
	const qSensor = "sensor";
159
	let sensor_channel = null;
160
	function sensorMessage(msg) {
161
		if (msg) {
162
			//msg.content.toString()
163
			console.log(" [x] '%s'", "received sensor message");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
164
			sseConnection.send({
165
				event: "full-cycle-sensor",
166
				data: msg.content.toString()
167
			});
168
		}
169
	}
170
171
	function on_channel_open_sensor(err, ch) {
172
		if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
173
		sensor_channel = ch;
174
		ch.on("error", function (err) {
175
			console.error(err)
176
			console.log("sensor channel Closed");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
177
		});
178
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
179
			var q = ok.queue;
180
			ch.bindQueue(q, qSensor, "");
181
			ch.consume(q, sensorMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
182
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
183
				console.log(" [*] Waiting for sensor. To exit press CTRL+C.");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
184
			});
185
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
186
	}
187
188
	if (busConnect)
189
	{
190
		busConnect.createChannel(on_channel_open_alert);
191
		busConnect.createChannel(on_channel_open_miner);
192
		busConnect.createChannel(on_channel_open_sensor);
193
	}
194
	
195
  sseConnection.on("close", function () {
196
    console.log("lost sse connection");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
197
		if (alertChannel) alertChannel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
198
		if (miner_channel) miner_channel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
199
		if (sensor_channel) sensor_channel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
200
	});
201
	
202
});
203
204
try {
205
	amqp.connect(services.messagebus.connection, on_connect);
206
}
207
catch(error) {
208
	console.error(error);
209
}
210