-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
174 lines (149 loc) · 3.55 KB
/
routes.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
module.exports = function(app, session, io, client, redis, gamePage, gamePageHome){
var sess;
//THE DATA
var userInitData = {
'cMessage':{
userName:'',
data:[
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
},
{
name:'',
postpone:'',
startTimes:'',
endTimes:'',
days:''
}
]
},
'vMessage':''
};
app.get('/', function(req, res) {
sess = req.session;
if(sess.username){
res.redirect('/event');
}else{
res.render('main.html');
}
});
app.get('/getCurrentUserData', function(req, res){
res.send(userInitData);
});
app.post('/login', function(req, res) {
sess = req.session;
sess.username = req.body.user;
var user_name=req.body.user;
var password=req.body.password;
console.log('Checking db for rpi id = '+user_name+' and password = '+password);
// C part ;)
client.get("cMessage_" + user_name, function(error,value){
if(error){
console.log("error getting val", error);
}
console.log("cMessage_" + user_name, value);
var parsedValue = JSON.parse(value);
if(parsedValue != null){
userInitData.cMessage = parsedValue;}
userInitData.cMessage.userName = user_name;
});
//V part
//want randomSequenceId's all data
var randomSequenceId = Math.floor((Math.random() * 9000) + 1000);
client.publish("123456789_REQUEST", randomSequenceId + ' SOCKET GET ALL CURRENT_STATE',function(){console.log('publish cb');});
//subscribe in order to listen the response for that rpi_id with randomSequenceId
client.subscribe('123456789_REPLY');
//if that message ever arrives from volcano check the rpi_id und randomSequenceId
client.on("message", function (channel, message) {
var msgArray = message.split(' ');
var rpi_id = msgArray[0];
console.log("client channel " + channel + ": " + message);
userInitData.vMessage = message;
if(rpi_id == randomSequenceId){
res.end("yes");
}else{
//change this to no for prod******************************
res.end("yes");
}
client.unsubscribe("123456789_REPLY");
});
});
app.get('/home', function(req, res) {
sess = req.session;
console.log("inside get home")
if(sess.username){
res.send(gamePageHome);
console.log(JSON.stringify(userInitData));
}else{
//this can be replaced with a page
res.write('<h1>Please login first.</h1>');
res.end('<a href='+'/'+'>Login</a>');
}
});
app.get('/logout',function(req,res){
req.session.destroy(function(err){
if(err){
console.log("sess destroy error: "+err);
}else{
res.redirect('/');
}
});
});
app.post('/socketDataSave',function(req,res){
console.log('boday \n'+ JSON.stringify(req.body));
//set on redis
client.set("cMessage_" + req.body.userName, JSON.stringify(req.body) , redis.print);
//set locally
userInitData.cMessage = req.body;
res.sendStatus(200);
});
client.on('error', function(error){
console.log('error, ' + error);
});
}