-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (119 loc) · 3.92 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
var express = require('express')
var bodyParser = require('body-parser')
var mongoose = require('mongoose')
var app = express()
var interval
var interval_switch
var urlencodeParser = bodyParser.urlencoded({extended: false})
var decider = false
var time
//Import
let Simulation = require('./models/simulate')
let SuitSwitch = require('./models/suitswitch')
//Database connector
//mongoose.connect("mongodb+srv://Manny_Carr:Tvstudent1!@nasa-suits-2020-whk7y.mongodb.net/test?retryWrites=true&w=majority")
mongoose.connect("mongodb+srv://SUITS-tech_team:Tvstudent1!@cluster0-rqtoy.mongodb.net/test?retryWrites=true&w=majority")
//mongoose.connect('mongodb://localhost/spacesuit');
//EJS framework for website display
app.set('view engine', 'ejs')
app.use('/assets', express.static('assets'))
//ROUTES
app.get('/',function(req, res){
res.render('index')
})
//On start button, simulation starts
app.post('/', urlencodeParser, function(req, res){
console.log('--------------Simulation started--------------')
time = Date.now()
interval = setInterval(Simulation.suitTelemetry.bind(null, time, decider),1000)
interval_switch = setInterval(SuitSwitch.SuitSwitch.bind(null,decider),1000)
res.render('error_ready',{qs: ''})
})
app.post('/error-ready',urlencodeParser, function(req, res){
console.log('-> Error calculation active!')
//Stop standard simulation
clearInterval(interval)
clearInterval(interval_switch)
decider = true
//Start alternative simulation
interval = setInterval(Simulation.suitTelemetry.bind(null, time, decider),1000)
interval_switch = setInterval(SuitSwitch.SuitSwitch.bind(null,decider),1000)
res.render('error_resolver',{qs: req.query})
})
app.get('/error-ready',function(req, res){
res.render('error_ready',{qs: req.query})
})
app.post('/resolver',urlencodeParser, function(req, res){
console.log('-> Normal calculation active!')
//Stop standard simulation
clearInterval(interval)
clearInterval(interval_switch)
decider = false
//Start alternative simulation
interval = setInterval(Simulation.suitTelemetry.bind(null, time, decider),1000)
interval_switch = setInterval(SuitSwitch.SuitSwitch.bind(null,decider),1000)
res.render('contact',{qs: req.query})
})
app.get('/resolver',function(req, res){
res.render('error_resolver',{qs: req.query})
})
app.get('/contact',function(req, res){
res.render('contact',{qs: req.query})
})
app.post('/contact', urlencodeParser, function(req, res){
console.log('--------------Simulation stopped--------------')
clearInterval(interval)
clearInterval(interval_switch)
res.render('contact-success',{data: req.body})
})
console.log("before api/suit")
//Returns all simulated data from the database
app.get('/api/suit', function(req, res){
console.log("after api/suit")
Simulation.getSuitTelemetry(function (err, data) {
console.log("after getSuitTelemetry function")
if (err) {
throw err
console.log(err)
}
res.json(data)
})
})
console.log("before api/suit/recent")
app.get('/api/suit/recent', function(req, res){
console.log("after api/suit/recent")
Simulation.getSuitTelemetryByDate(function (err, data) {
console.log("after getSuitTelemetryByDate function")
if (err) {
throw err
console.log(err)
}
res.json(data)
})
})
console.log("before api/suitswitch")
app.get('/api/suitswitch', function(req, res){
console.log("after api/suitswitch")
SuitSwitch.getSuitSwitch(function (err, data) {
console.log("after getSuitSwitch function")
if (err) {
throw err
console.log(err)
}
res.json(data)
})
})
console.log("before api/suitswitch/recent")
app.get('/api/suitswitch/recent', function(req, res){
console.log("code afer api/suitswitch/recent")
SuitSwitch.getSuitSwitchByDate(function (err, data) {
console.log("after getSuitSwitchByDate function")
if (err) {
throw err
console.log(err)
}
res.json(data)
})
})
app.listen(process.env.PORT || 3000)
console.log('Server is running on port 3000...')