-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToilet.js
More file actions
61 lines (54 loc) · 1.5 KB
/
Copy pathToilet.js
File metadata and controls
61 lines (54 loc) · 1.5 KB
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
let Toilet = class {
constructor(name, gender, location, status, sockets) {
this.name = name;
this.gender = gender;
this.location = location;
this.status = status;
this.sockets = sockets;
}
detail() {
return {
name: this.name,
gender: this.gender,
location: this.location,
status: this.status
}
}
publishChanges() {
this.sockets.emit('status', this.detail());
}
}
class DummyToilet extends Toilet {
constructor(name, gender, location, status, sockets) {
super(name, gender, location, status, sockets);
this.assignRandomStatus();
}
assignRandomStatus() {
const self = this,
statuses = ['busy', 'available'];
setInterval(function() {
self.status = statuses[Math.floor(Math.random() * statuses.length)];
self.publishChanges();
}, 2000);
}
}
class PhotonToilet extends Toilet {
constructor(particle, token, name, gender, location, status, sockets) {
super(name, gender, location, status, sockets);
this.particle = particle;
this.token = token;
this.setEventStream();
}
setEventStream() {
let self = this;
this.particle.getEventStream({deviceId: self.name, name: 'wcStatus', auth: this.token }).then(function(stream) {
stream.on('event', function(data) {
// console.log("Event: " + JSON.stringify(data));
self.status = data.data;
self.publishChanges();
});
});
}
}
exports.DummyToilet = DummyToilet;
exports.PhotonToilet = PhotonToilet;