-
Notifications
You must be signed in to change notification settings - Fork 0
/
octoprint-dashboard.js
150 lines (116 loc) · 4.41 KB
/
octoprint-dashboard.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const http = require('http');
const fs = require('fs');
const app = express();
/************************************************************************************
/ Setup these items
/************************************************************************************/
const port = 3000; //Webserver Port
let getheaders = {
'X-Api-Key' : '' //API key, needs to be generated in OctoPrint
};
let optionsGet = {
host : '', //Octopi server IP -- Server hostname was soemtimes causing issues, IP always seemed to work.
port : 80, //Octopi server default is part 80
path : '/api/server', //default server api
method : 'GET', //we only make GET calls
headers : getheaders,
webcamStream : true //true=Use webcam stream; false=Use screen shots
};
/************************************************************************************/
app.use(cors());
// Configuring body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Files
app.get('/', (req, res) => { //main html page
res.writeHead(200, { 'content-type': 'text/html' });
fs.createReadStream('dashboard.html').pipe(res);
});
app.get('/dashboard.js', (req, res) => { //javascript for calling API's and manipulating display
res.writeHead(200, { 'content-type': 'text/html' });
fs.createReadStream('dashboard.js').pipe(res);
});
app.get('/dashboard.css', (req, res) => { //CSS for styling dashboard
res.writeHead(200, { 'content-type': 'text/css' });
fs.createReadStream('dashboard.css').pipe(res);
});
app.get('/webcamoption', (req, res) => {
if (optionsGet.webcamStream === true) {
res.send('{"webcam":"stream"}');
} else {
res.send('{"webcam":"screenshot"}');
}
});
app.get('/webcam', (req, res) => { //have to call webcam in iframe because of CORS
res.send('<iframe id="webcam" style="width:640; height:480" src="http://'+optionsGet.host+'/webcam/?action=stream"></iframe>');
});
//Proxy Stream data
let target = 'http://'+optionsGet.host+'/webcam/?action=stream';
// figure out 'real' target if the server returns a 302 (redirect)
http.get(target, resp => {
if(resp.statusCode == 302) {
target = resp.headers.location;
}
});
app.get('/webcamstream', (req, res) => {
var request = require('request');
req.pipe(request.get(target)).pipe(res);
});
app.get('/webcamafter', (req, res) => { //Can only use this fomr the computer that is hosting node because of CORS
const ts = Date.now();
res.send('<img id="webcam" style="height:480px;" src="/view?'+ts+'" />'); //style="width:640; height:480"
});
//API pass through to prevent CORS rejection in browser
app.get('/version', (req, res) => {
optionsGet.path = '/api/version';
http.request(optionsGet, function(res1) {
res1.setEncoding('utf8');
res1.on('data', function (chunk) {
res.send(chunk);
});
}).end();
});
app.get('/server', (req, res) => {
optionsGet.path = '/api/server';
http.request(optionsGet, function(res1) {
res1.setEncoding('utf8');
res1.on('data', function (chunk) {
res.send(chunk);
});
}).end();
});
app.get('/printer', (req, res) => {
optionsGet.path = '/api/printer';
http.request(optionsGet, function(res1) {
res1.setEncoding('utf8');
res1.on('data', function (chunk) {
res.send(chunk);
});
}).end();
});
app.get('/job', (req, res) => {
optionsGet.path = '/api/job';
http.request(optionsGet, function(res1) {
res1.setEncoding('utf8');
res1.on('data', function (chunk) {
res.send(chunk);
});
}).end();
});
app.get('/view', (req, res) => {
//req.pipe(req.get('http://'+optionsGet.host+'/webcam/?action=snapshot')).pipe(res);
const file = fs.createWriteStream('octopi.jpeg');
const request = http.get('http://'+optionsGet.host+'/webcam/?action=snapshot', function(response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
res.writeHead(200, { 'content-type': 'image/jpeg' });
fs.createReadStream('octopi.jpeg').pipe(res);
});
});
});
app.listen(port, () => console.log(`Octoprint Dashboard listening on port ${port}!`));