This repository was archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·182 lines (121 loc) · 4.29 KB
/
server.js
File metadata and controls
executable file
·182 lines (121 loc) · 4.29 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
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
const express = require('express'),
app = express(),
http = require('http'),
server = http.Server(app),
upload = require('multer')(),
io = require('socket.io')(server),
scss = require('node-sass-middleware'),
yaml = require('yamljs'),
//my own files
template = require('./utils/template.js')('public', app),
serial = require('./utils/serial.js'),
ip = require('./utils/ip.js'),
system = require('./utils/system.js');
//load config file
var config = yaml.load('config.yaml');
for (var i = 0; i < config.Profiles.length; i++) new system.Profile(config.Profiles[i]);
for (var i = 0; i < config.Printers.length; i++) new system.Printer(config.Printers[i]);
for (var i = 0; i < config.OctoPrints.length; i++) new system.OctoPrint(config.OctoPrints[i]);
//check arguments for testweb
if (process.argv[2] && process.argv[2] == 'testweb') {
console.log('(testweb mode)');
system.testweb(true);
if (process.argv[3] && process.argv[3] == 'skipsetup') {
system.ready(true);
for (var i = 0; i < system.OctoPrints.length; i++) system.OctoPrints[i].Printer = system.Printers[i];
}
}
//Assign each server a printer
serial.refresh(system.testweb())
.then(() => {
//if there are more ports than servers, throw an error
//if (serial.length > system.OctoPrints.length) console.log('too many serial devices');
//connect each server to a port
//TODO: leave octoprint servers connected
for (var i = 0; i < serial.length; i++) {
system.OctoPrints[i].disconnect();
//TODO: error handler: if the thing doesnt connect, try again a few times
system.OctoPrints[i].connect( serial[i] );
}
});
//rout scss
app.use(scss({
src: __dirname + '/public/style/scss',
dest: __dirname + '/public/style',
prefix: '/style',
outputStyle: 'compressed'
}));
//for reading the bodies of http posts
app.use(upload.any());
//set up
app.get('/setup', (req, res) => {
res.render('setup.html', {
ready: system.ready(),
local: true,
Printers: system.Printers,
OctoPrints: system.OctoPrints
});
});
//gets a list of potential printers for a given server
app.get('/setup/options/:server', (req, res) => {
var octoprint = system.OctoPrints[req.params.server];
var options = [];
for (var i = 0; i < system.Printers.length; i++) {
var printer = system.Printers[i];
if (printer.match(octoprint)) options.push(printer.name)
}
res.setHeader('Content-Type', 'application/json');
res.json(options);
});
//assign a printer to a server
app.post('/setup/set', (req, res) => {
var printer,
octoprint = system.OctoPrints[parseInt(req.body.octoprint)];
//determine printer from printername
for (var i = 0; i < system.Printers.length; i++) if(system.Printers[i].name == req.body.printer) printer = system.Printers[i];
octoprint.wiggle.stop();
printer.attach(octoprint);
var ready = true;
for (var i = 0; i < serial.length && i < system.OctoPrints.length; i++) {
if (!system.OctoPrints[i].Printer) {
ready = false;
break;
}
}
if (ready) {
system.ready(true);
res.status(200);
} else res.status(204);
res.end();
});
//wiggle whatever printer is connected to a server
app.post('/setup/wiggle', (req, res) => {
console.log('octoprint wiggled!');
var octoprint = system.OctoPrints[req.body.octoprint];
octoprint.wiggle.setTime(5);
res.status(204);
res.end();
});
//redirect to setup
//lets through any scripty or style things
app.use((req, res, next) => {
if ( system.ready() || req.path.includes('script/') || req.path.includes('style/') ) next();
else {
res.writeHead((req.method === 'GET') ? 302 : 310, {'Location': '/setup'});
res.end();
}
});
//index
app.get('/', (req, res) => {
res.render('index.html', {
Printers: system.Printers,
OctoPrints: system.OctoPrints
});
});
//just give whatever other files the client asks for
app.use(express.static(__dirname + '/public'));
var port = (system.testweb()) ? 8000:80;
server.listen(port, '0.0.0.0', () => {
console.log('----Server Created----');
console.log('\nGo to localhost:' + port + ' in your browser');
});