-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
363 lines (307 loc) · 10.7 KB
/
app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// applications
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const fs = require('fs');
const XLSX = require('xlsx');
const path = require('path');
const cors = require('cors');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const mqtt = require('mqtt');
const mysql = require('mysql2');
const flash = require("express-flash");
const session = require("express-session");
const methodOverride = require("method-override");
const { db, saveUserData, getUserByEmail, getUserById } = require('./models/saveduser.js');
const passport = require("passport");
const initializePassport = require("./passport-config");
initializePassport(passport,getUserByEmail,getUserById);
let dataInOut = false;
const port = 144;
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
// // routers
// const loginRouter = require('./controllers/userController');
// const dashboardRouter = require('./controllers/dashboardController');
// const registerRouter = require('./controllers/registerController');
// const forgotPasswordRouter = require('./controllers/forgotPasswordController');
// const userController = require('./controllers/userController');
// const dashboardController = require('./controllers/dashboardController')
// setup view engine
app.set('views',path.join(__dirname,'views'));
app.set('view engine','ejs')
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(session({
secret: 'yes',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride('_method'));
// Setup static files
app.use(express.static(path.join(__dirname, 'public')));
// Setup body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// app.use('/',loginRouter);
// app.use('/dashboard',dashboardRouter);
// app.use('/register',registerRouter);
// app.use('/forgotpassword',forgotPasswordRouter);
// // error handling
// app.use((req,res,next)=>{
// res.status(404).render('error',{message:"page not found"});
// console.log("page not found");
// })
// app.use((err,req,res,next)=>{
// res.status(500).render('error',{message:err.message});
// console.log("error 500");
// })
// Eksekusi query dan mengekspor ke file XLSX
function exportToXLSX(callback) {
connection.query('SELECT * FROM data', (err, results, fields) => {
if (err) {
console.error('Error executing query:', err);
callback(err);
return;
}
// Mengonversi hasil query menjadi format yang diperlukan oleh xlsx
const data = results.map(row => Object.values(row));
const header = fields.map(field => field.name);
const worksheet = XLSX.utils.aoa_to_sheet([header, ...data]);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'Praktikum');
// Menyimpan file XLSX
const filename = 'praktikum_data.xlsx';
const filePath = path.join(__dirname, '/public', filename);
XLSX.writeFile(workbook, filePath);
console.log(`Tabel berhasil diekspor ke file ${filename}`);
callback(null, filePath);
});
}
const connection = mysql.createConnection({
host:'localhost',
user: 'testuser',
password: 'iiot123',
database: 'livegrafik'
});
connection.connect((error) =>{
if (error){
console.error('Koneksi ke database gagal: '+ error.stack);
return;
}
console.log('Terhubung ke database MySQL dengan ID koneksi '+ connection.threadId);
});
// start server
const topicPiston = 'stepper_move';
// Variabel untuk menyimpan nilai topic
let topicValue = 0;
const mqttBroker = 'mqtt://iot.tf.itb.ac.id:1883'; // Ganti dengan alamat dan port MQTT broker Anda
const client = mqtt.connect(mqttBroker);
client.on('connect',function(){
// Subscribe ke lebih dari 1 topik
const topicsToSubscribe = ['db', 'tube_length','stepper_move']; // Ganti dengan topik-topik MQTT yang ingin Anda subscribe
topicsToSubscribe.forEach(topic => {
client.subscribe(topic, (err) => {
if (err) {
console.error(`Gagal subscribe ke topik ${topic}: ${err}`);
} else {
console.log(`Berhasil subscribe ke topik ${topic}`);
}
});
});
});
io.on('connection', (socket) => {
console.log('Klien terhubung');
socket.on('buttonClick', (value) => {
client.publish(topicPiston, value, (err) => {
if (err) {
console.error('Tombol Kontrol Piston gagal terhubung ke MQTT broker:', err);
}
});
});
socket.on('ResetData', (value) => {
// fungsi berisi syntax template untuk memasukan data ke tabel database
const query = `
TRUNCATE TABLE data
`;
//memanggil fungsi syntax template sebelumnya
connection.query(query,function(err){
if(err){
console.error(err.message);
} else {
console.log(`Reset Database Berhasil`);
}
});
});
// Event handler saat tombol ditekan
socket.on('exportData', () => {
exportToXLSX((err, filePath) => {
if (err) {
console.error('Error exporting data:', err);
return;
}
// Mengirim file XLSX sebagai respon ke client
socket.emit('downloadFile', filePath);
});
});
socket.on('getData', (value) => {
// console.log(` getData: ${}`);
if (value === '1'){
dataInOut = true;
console.log(` getData: ${value}`);
} else if(value === '0'){
dataInOut = false;
// console.log(` data stopped `);
console.log(` getData: ${value}`);
}
});
socket.on('disconnect', () => {
console.log('Klien terputus');
});
});
let datadb = 0;
let dataTubeLength = 0;
let dataBtn = 0;
let statusBtn = 'Diam'
client.on('message',function(topic,message){
const data = message.toString();
if(dataInOut){
console.log(` data started `);
console.log(`Menerima data dari topik ${topic}:`, data);
if(topic==='db'){
datadb = data;
console.log(data);
io.emit('valuedb', parseFloat(data)); // Kirim nilai topic ke semua client terhubung
// fungsi berisi syntax template untuk memasukan data ke tabel database
const query = `
INSERT INTO data(y1,y2,btn)
VALUES(?,?,?)
`;
const values = [datadb,dataTubeLength,statusBtn];
//memanggil fungsi syntax template sebelumnya
connection.query(query,values,function(err){
if(err){
console.error(err.message);
} else {
console.log(`inserted data: ${datadb} || ${dataTubeLength} || ${statusBtn}`);
}
});
} else if(topic==='tube_length'){
dataTubeLength = data;
console.log(data);
io.emit('valueTubeLength', parseFloat(data));
// fungsi berisi syntax template untuk memasukan data ke tabel database
const query = `
INSERT INTO data(y1,y2,btn)
VALUES(?,?,?)
`;
const values = [datadb,dataTubeLength,statusBtn];
//memanggil fungsi syntax template sebelumnya
connection.query(query,values,function(err){
if(err){
console.error(err.message);
} else {
console.log(`inserted data: ${datadb} || ${dataTubeLength} || ${statusBtn}`);
}
});
};
} else if (!dataInOut){
console.log(` data stopped `);
}
if(topic==='stepper_move'){
dataBtn = data;
console.log(data);
io.emit('valueBtn', parseInt(dataBtn));
if(data == '0'){
statusBtn = 'Diam';
} else if(data == '1'){
statusBtn = 'Maju';
} else if(data == '2'){
statusBtn = 'Mundur';
}
};
});
// Rute untuk ke dashboard
app.get('/', (req, res) => {
res.render('index');
});
// Rute untuk ke dashboard
app.get('/home', (req, res) => {
res.render('home');
});
app.get("/dashboard", checkAuthenticated, async (req, res) => {
try {
// Menunggu Promise req.user diselesaikan dan mengakses properti "nama"
const user = await req.user;
res.render("dashboard.ejs", { name: user.nama });
} catch (error) {
// Tangani jika ada error saat menyelesaikan Promise
console.error('Terjadi kesalahan:', error);
res.status(500).send('Terjadi kesalahan dalam mengambil data pengguna.');
}
});
app.get("/login", checkNotAuthenticated, (req, res) => {
res.render("login.ejs");
});
app.get("/register", checkNotAuthenticated, (req, res) => {
res.render("register.ejs");
});
app.post("/register", checkNotAuthenticated, async (req,res) => {
//try{
//const hashedPassword = await bcrypt.hash(req.body.password, 10);
const userData = {
nama:req.body.name,
email:req.body.email,
password:req.body.password };
saveUserData(userData)
.then((result) => {
console.log('Data berhasil disimpan ke database:', result);
console.log(userData);
res.redirect('/');
})
.catch((error) => {
console.error('Terjadi kesalahan menyimpan ke database:', error);
res.redirect('/register');
});
// } catch{
// res.redirect('/login');
// }
});
app.post("/login", checkNotAuthenticated, passport.authenticate('local',{
successRedirect: '/home',
failureRedirect: '/',
failureFlash: true
}));
app.delete('/logout', (req, res) => {
req.logout(function(err) {
if (err) {
// Tangani kesalahan jika terjadi
console.log(err);
return res.status(500).send('Internal Server Error');
}
// Logika setelah pengguna keluar
// Misalnya, arahkan pengguna ke halaman utama atau tampilkan pesan sukses
res.redirect('/');
});
});
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/dashboard');
}
next();
}
http.listen(port,()=>{
console.log(`server is running on http://localhost:${port}`);
});