TVJR/index.js

113 lines
3.8 KiB
JavaScript

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
const util = require('util');
var bodyParser = require('body-parser');
var session = require('express-session');
var fonctionMaison = require('./fonction.js');
var registerController = require('./controllers/register-controller.js');
var loginController = require('./controllers/login-controller.js');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(session({ secret: 'fUZG4VjOx9U91Htq2AAA', cookie: { maxAge: 60000 }, saveUninitialized: true,resave: false}));
const config = require('./config.json');
var con = require('./bdd.js')
app.set('view engine', 'ejs');
app.use('/static/', express.static('static'));
app.get('/', function(req, res){
if(req.session.authStatus)
con.query('SELECT * FROM zone', function (err, result, fields) {
if (err) throw err;
var zone = JSON.stringify(result);
zone = JSON.parse(zone);
res.render('index', {zone:zone});
});
else
res.redirect('/login');
}).get('/TV', function(req, res){
res.render('TV');
}).get('/TV/zone/:zone', function(req, res){
con.query("SELECT * FROM com_active INNER JOIN zone ON com_active.ID_Zone = zone.ID_Zone INNER JOIN communication ON com_active.ID_Com = communication.ID_Com WHERE zone.ID_Zone = ? ", [req.params.zone], function(err, result){
if (err) throw err;
var pub = JSON.stringify(result);
pub = JSON.parse(pub);
fonctionMaison.listPubDansZone(req.params.zone,function(err, pub){
if (err) throw err;
res.render('TV', {zone:req.params.zone,pub:pub})
});
});
}).get('/TV/latlng/:lat/:lng', function(req,res){
con.query('CALL cherche_zones(?,?,?,?)', [req.params.lat,req.params.lat,req.params.lng,req.params.lng], function(err, result){
if (err) throw err;
var data = JSON.stringify(result);
data = JSON.parse(data);
var zone = fonctionMaison.choixZone(data[0]);
fonctionMaison.listPubDansZone(zone,function(err, pub){
if (err) throw err;
res.render('TV', {zone:zone.ID_Zone,pub:pub})
});
//console.log(util.inspect(data[0][0].Nom, false, null, true /* enable colors */))
});
}).get('/map', function(req, res){
con.query('SELECT * FROM zone', function (err, result, fields) {
if (err) throw err;
var zone = JSON.stringify(result);
zone = JSON.parse(zone);
res.render('map', {zone:zone});
});
}).get('/login', function(req, res){
res.render('login');
}).post('/login', loginController, function(req, res){
res.send('<!DOCTYPE html><html><body>'+req.session.authMessage+'<br><a href="http://nodejs.adriy.be">HOME</a></body></html>');
}).post('/register', registerController, function(req, res){
res.send('<!DOCTYPE html><html><body>'+res.locals.message+'<br><a href="http://nodejs.adriy.be">HOME</a></body></html>');
}).post('/addZone' , function(req, res){
var nord = req.body.N;
var sud = req.body.S;
var est = req.body.E;
var ouest = req.body.O;
var name = req.body.Name;
con.query('CALL add_zone(?,?,?,?,?,?)', [true, name,nord,sud,ouest,est], function(err, result){
});
});
io.on('connection', function(socket){
socket.on('JoinZone', function(zone){
console.log("Join "+zone)
socket.join(zone);
});
socket.on('Communication', function(CommunicationJSON){
var data = JSON.parse(CommunicationJSON);
console.log("JSON : "+CommunicationJSON);
console.log("ZONE "+data.zone);
io.sockets.in(data.zone).emit('Communication', CommunicationJSON);
});
});
http.listen(1234, function(){
console.log('listening on *:1234');
});