-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverElevator.js
125 lines (116 loc) · 2.61 KB
/
serverElevator.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
// Montardon & diodfr
var express= require('express');
var url= require('url');
var querystring = require('querystring');
var app= express();
var floor = 0;
var calls = [];
var directions = [];
var gos = [];
var userHasEntered = false;
var userHasExited = false;
var doorsOpened = false;
var reachingUser = false;
var releasingUser = false;
var go = -1;
var min = 0;
var max = 5;
var updateMinMax = function(call) {
if (call > max) {
max = call;
} else if (call < min) {
min = call;
}
};
app.get('/call', function(req,res) {
var params = querystring.parse(url.parse(req.url).query);
calls.push(params['atFloor']);
directions.push(params['to']);
console.log("call "+params['atFloor']+" "+params['to']);
console.log(calls);
console.log(directions);
updateMinMax(params['atFloor']);
res.writeHead(200);
res.end();
});
app.get('/go', function(req,res) {
var params = querystring.parse(url.parse(req.url).query);
go = params['floorToGo'];
gos.push(go);
console.log("go "+go);
res.writeHead(200);
res.end();
});
app.get('/userHasEntered', function(req,res) {
console.log("userHasEntered");
res.writeHead(200);
res.end();
userHasEntered = true;
});
app.get('/userHasExited', function(req,res) {
console.log("userHasExited");
userHasExited = true;
});
app.get('/reset', function(req,res) {
var params = querystring.parse(url.parse(req.url).query);
console.log("reset "+params['cause']+"==========================================");
calls = [];
directions = [];
floor = 0;
go = -1;
userHasEntered = false;
userHasExited = false;
res.writeHead(200);
res.end()
});
app.get('/nextCommand', function(req,res) {
console.log("nextCommand");
res.writeHead(200);
if (calls.length>0 || go >= 0) {
var dest = calls[0];
if (go >= 0) {
if (doorsOpened == true) {
dest = go;
res.end("CLOSE");
doorsOpened = false;
}
} else {
if (dest > floor) {
floor++;
console.log("UP");
res.end("UP");
} else if (dest == floor) {
// TODO: status door
if (userHasEntered == true) {
if (doorsOpened == true) {
console.log("CLOSE");
res.end("CLOSE");
userHasEntered = false;
doorsOpened = false;
}
} else
if (userHasExited == true) {
if (doorsOpened == true) {
console.log("CLOSE");
res.end("CLOSE");
userHasExited = false;
doorsOpened = false;
}
} else {
doorsOpened = true;
console.log("OPEN");
res.end("OPEN");
go = -1;
}
} else {
console.log("DOWN");
res.end("DOWN");
floor--;
}
}
} else {
console.log("NOTHING");
res.end("NOTHING");
}
});
app.listen(80);