-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparkingLot.js
54 lines (44 loc) · 1.04 KB
/
parkingLot.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
class VacantState {
constructor() {
}
vacant(context) {
}
occupied(context) {
context.sendOccupiedRequest('Lot is Occupied')
context.state = new OccupiedState()
}
}
class OccupiedState {
constructor() {
}
vacant(context) {
context.sendVacantRequest('Lot is Vacant')
context.state = new VacantState()
}
occupied(context) {
}
}
class ParkingLot{
constructor(client, parkingLotName) {
this.state = new VacantState()
this.client = client
this.parkingLotName = parkingLotName
}
sendOccupied() {
this.state.occupied(this)
}
sendVacant() {
this.state.vacant(this)
}
sendVacantRequest(data) {
console.log(data)
const payload = { lotName: this.parkingLotName, status: 0 }
this.client.publish('parkingLot/status-change', JSON.stringify(payload))
}
sendOccupiedRequest(data) {
console.log(data)
const payload = { lotName: this.parkingLotName, status: 1 }
this.client.publish('parkingLot/status-change', JSON.stringify(payload))
}
}
module.exports = ParkingLot