forked from LaserWeb/deprecated-LaserWeb3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-marlin.js
187 lines (157 loc) · 6.75 KB
/
server-marlin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
AUTHOR: Peter van der Walt openhardwarecoza.github.io/donate
RepRapWeb - A Web Based 3d Printer Controller
Copyright (C) 2015 Andrew Hodel
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var config = require('./config');
var serialport = require("serialport");
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
var static = require('node-static');
var EventEmitter = require('events').EventEmitter;
var url = require('url');
var qs = require('querystring');
var util = require('util');
var http = require('http');
var chalk = require('chalk');
var isConnected, port, isBlocked, lastsent = "", paused = false, blocked = false;
console.log(chalk.green('***************************************************************'));
console.log(chalk.green('* Notice: *'));
console.log(chalk.green('***************************************************************'));
console.log(chalk.green('*'),chalk.white(' Remember to update (: !!! '), chalk.green('*'));
console.log(chalk.green('* 1. Run ./update.sh or git pull *'));
console.log(chalk.green('* 2. or check the commit log on *'));
console.log(chalk.green('*'), chalk.yellow('https://github.com/openhardwarecoza/LaserWeb/commits/master'), chalk.green('*'));
console.log(chalk.green('***************************************************************'));
// Lets add a message so users know where to point their browser
require('dns').lookup(require('os').hostname(), function (err, add, fam) {
console.log(chalk.green('*'),chalk.white('Access the LaserWeb User Interface: '), chalk.green('*'));
console.log(chalk.green('* 1. Open Chrome *'));
console.log(chalk.green('* 2. Go to : *'));
console.log(chalk.green('*'), chalk.yellow(' http://'+add+':'+config.webPort+'/ '), chalk.green('*'));
console.log(chalk.green('***************************************************************'));
console.log(chalk.green(' '));
console.log(chalk.green(' '));
})
// Webserver
app.listen(config.webPort);
var fileServer = new static.Server('./public');
function handler (req, res) {
fileServer.serve(req, res, function (err, result) {
if (err) {
console.error(chalk.red('ERROR:'), chalk.yellow(' fileServer error:'+req.url+' : '), err.message);
}
});
}
function ConvChar( str ) {
c = {'<':'<', '>':'>', '&':'&', '"':'"', "'":''',
'#':'#' };
return str.replace( /[<&>'"#]/g, function(s) { return c[s]; } );
}
// Websocket <-> Serial
io.sockets.on('connection', function (socket) { // When we open a WS connection, send the list of ports
serialport.list(function (err, ports) {
socket.emit("ports", ports);
});
socket.on('refreshPorts', function(data) { // Or when asked
console.log(chalk.yellow('WARN:'), chalk.blue('Requesting Ports Refresh '));
serialport.list(function (err, ports) {
socket.emit("ports", ports);
});
});
socket.on('connectTo', function(data) { // If a user picks a port to connect to, open a Node SerialPort Instance to it
data = data.split(',');
console.log(chalk.yellow('WARN:'), chalk.blue('Connecting to Port ' + data));
if (!isConnected) {
port = new serialport(data[0], { parser: serialport.parsers.readline("\n"), baudrate: parseInt(data[1]) });
} else {
socket.emit("connectStatus", 'Already Connected');
port.write("?\n"); // Lets check if its LasaurGrbl?
port.write("M115\n"); // Lets check if its Marlin?
port.write("version\n"); // Lets check if its Smoothieware?
port.write("$fb\n"); // Lets check if its TinyG
}
port.on('open', function() {
socket.emit("connectStatus", 'Connected');
port.write("?\n"); // Lets check if its LasaurGrbl?
port.write("M115\n"); // Lets check if its Marlin?
port.write("version\n"); // Lets check if its Smoothieware?
port.write("$fb\n"); // Lets check if its TinyG
isConnected = true;
setInterval(function() {
port.write('?');
send1Q()
}, 100);
setInterval(function(){
socket.emit('qCount', gcodeQueue.length)
},500);
});
port.on('error', function(err) { // open errors will be emitted as an error event
console.log('Error: ', err.message);
socket.emit("data", data);
})
port.on("data", function (data) {
console.log('Recv: ' + data)
if(data.indexOf("ok") != -1 || data == "start\r" || data.indexOf('<') == 0){
if (data.indexOf("ok") == 0) { // Got an OK so we are clear to send
blocked = false;
}
socket.emit("data", data);
setTimeout(function(){
if(paused !== true){
send1Q()
}
},50);
} else {
console.log("Nope")
}
});
});
socket.on('firstLoad', function(data) {
socket.emit('config', config);
});
socket.on('serialSend', function(data) {
data = data.split('\n')
for (i=0; i<data.length; i++) {
addQ(data[i])
}
});
// End Websocket <-> Serial
// Queue
gcodeQueue = [];
function addQ(gcode) {
gcodeQueue.push(gcode);
}
function jumpQ(gcode) {
gcodeQueue.unshift(gcode)
}
function runQ() {
}
function send1Q() {
if (gcodeQueue.length > 0 && !blocked) {
var gcode = gcodeQueue.shift()
console.log('Sent: ' + gcode + ' Q: ' + gcodeQueue.length)
lastSent = gcode
port.write(gcode + '\n');
blocked = true;
}
}
// End Queue
});