-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (117 loc) · 4.73 KB
/
index.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
const express = require('express');
const Ably = require('Ably');
const parser = require('body-parser');
const cors = require('cors');
const authRoutes = require('./routes/auth-routes');
const profileRoutes = require('./routes/profile');
const dataRoutes = require('./routes/data-form');
const helpRoutes = require('./routes/helpers');
const safetyRoutes = require('./routes/safety');
const passpostSetup = require('./config/passport-setup');
const mongoose = require('mongoose');
const keys = require('./config/keys');
const cookieSession = require('cookie-session');
const passport = require('passport');
const realtime = new Ably.Realtime({key: keys.ably.ablyKey });
const randomstring = require("randomstring");
const path = require('path');
const app= express();
const ejs = require('ejs');
//connected to database
//create cookie
app.use(cookieSession({
maxAge: 24*60*60*1000,
keys: [keys.cookieSession.cookieKey]
}));
//initialize passport
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static('public'));
app.set('view engine','ejs');
mongoose.connect(keys.mongodb.dbURI,()=>{
console.log('Database connected');
});
app.use(parser.json()); //should be written above below lines...parse json data
app.use(parser.urlencoded({extended:true}));
//app.use(express.static(path.join(__dirname,'public')));
app.use('*', function(req, res, next){ // to alllow cors error //mdn cors
res.set('Access-Control-Allow-Origin','*') //editing header
res.set('Access-Control-Allow-Headers','content-type'); //for chrome
next();
});
//setting up routes
app.use('/auth', authRoutes);
app.use('/profile', profileRoutes);
app.use('/report', dataRoutes);
app.use('/help', helpRoutes);
app.use('/safety', safetyRoutes);
app.get('/',(req,res)=>{
//renders homepage, object tells if user is logged in or not, show buttons accordingly
console.log('Waah');
res.sendFile(path.join(__dirname, '/public', 'home.html'));
});
var spawn = require('child_process').spawn;
app.get('/safety',(req,res)=>{
console.log("start");
/*Safety.findOne({longitude : req.body.longitude, latitude : req.body.latitude},(err,result)=>{
if(err) console.log('Invalid or Unavailable details');
else{
console.log("check");
res.json(result).status(200);
}
});*/
//var latitude = req.body.latitude,longitude = req.body.longitude;
var latitude = 23.34,longitude=87.65;
var date = new Date().getDate(),month = new Date().getMonth(),year = new Date().getFullYear(),time = new Date().getHours();
var process = spawn('python',["./finallcode.py",time,date,month,year,longitude,latitude]);
process.stdout.on('data',(data)=>{
var arrayAnswer = data.toString();
console.log("Hello!");
console.log("The answer"+arrayAnswer);
});
res.send("Hello");
//res.json({text:'Hello'}).status(200);
});
app.get('/safetyFromAndroid',(req,res)=>{
//console.log("start");
/*Safety.findOne({longitude : req.body.longitude, latitude : req.body.latitude},(err,result)=>{
if(err) console.log('Invalid or Unavailable details');
else{
console.log("check");
res.json(result).status(200);
}
});*/
//var latitude = req.body.latitude,longitude = req.body.longitude;
var safety_params = [0.21,0.12,0.06,0.58];
var latitude = Number(req.body.latitude),longitude=Number(req.body.longitude);
var date = new Date().getDate(),month = new Date().getMonth(),year = new Date().getFullYear(),time = new Date().getHours();
var process = spawn('python',["./finallcode.py",time,date,month,year,longitude,latitude]);
process.stdout.on('data',(data)=>{
var arrayAnswer = (data.toString());
//console.log("Hello!");
console.log(arrayAnswer);
res.join({"murder": arrayAnswer[0],"harassment":arrayAnswer[1],"rape":arrayAnswer[2],"theft":arrayAnswer[3],"safety_index":arrayAnswer[4]})
console.log(arrayAnswer[0]+","+arrayAnswer[1]+","+arrayAnswer[2]+","+arrayAnswer[3]+","+arrayAnswer[4]);
res.send(arrayAnswer[0]+","+arrayAnswer[1]+","+arrayAnswer[2]+","+arrayAnswer[3]+","+arrayAnswer[4]);
});
//res.json({text:'Hello'}).status(200);
});
app.get('/authFromAndroid', function (req, res) {
var tokenParams = {
'clientId': randomstring.generate({
length: 12,
})
};
realtime.auth.createTokenRequest(tokenParams, function(err, tokenRequest) {
if (err) {
res.status(500).send('Error requesting token: ' + JSON.stringify(err));
} else {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(tokenRequest));
}
});
});
var port = process.env.PORT || 3000;
app.listen(port,()=>{
console.log('Listening to 3000');
});