/
device.mjs
104 lines (83 loc) · 2.28 KB
/
device.mjs
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
import io from '../controllers/socket.mjs'
let devices = new Map();
class Device {
constructor(id, socket) {
this._id = id;
this._socket = socket;
this._status = {};
this._online = true;
if(socket) io.to('client').emit('device-network-sync', { id: id, online: true });
}
get id() { return this._id; }
get socket() { return this._socket; }
set socket(socket) { this._socket = socket; }
get status() { return this._status; }
get statusJSON() { return JSON.stringify(this._status); }
get online() { return this._online; }
set online(online) {
this._online = online;
io.to('client').emit('device-network-sync', { id: this.id, online: online, status: this.status });
}
get info() {
return {
id: this.id,
status: this.status,
online: this.online
}
}
}
class PassiveSwitch extends Device {
constructor(id, socket) {
super(id, socket);
this._type = 0;
// initialize power status
this._status.power = false;
this.sync();
}
get type() { return this._type; }
get power() { return this._status.power; }
set power(power) { this._status.power = power; }
toggle() { this.power = !this.power; }
sync() {
io.to(this.socket).emit('relay', { data: this.power ? '1' : '0' });
io.to('client').emit('device-control-sync', { target: this.id, data: this.power });
}
}
class PassiveTactSwitch extends Device {
constructor(id, socket) {
super(id, socket);
this._type = 1;
}
get type() { return this._type; }
push() {
io.to(this.socket).emit('push');
io.to('client').emit('device-control-sync', { target: this.id, data: true });
}
}
class LEDDisplay extends Device {
constructor(id, socket) {
super(id, socket);
this._type = 2;
this._status = {
power: true,
display: 'READY.',
align: 'center'
}
this.sync();
}
get type() { return this._type; }
set status(status) { }
}
const DeviceType = {
passiveSwitch: 0,
PassiveTactSwitch: 1,
ledDisplay: 2,
0: 'passiveSwitch',
1: 'passiveTactSwitch',
2: 'ledDisplay'
}
// set virtual devices
devices.set('wakeonlan0', new PassiveTactSwitch('wakeonlan0', null));
devices.set('sleeponlan0', new PassiveTactSwitch('sleeponlan0', null));
export default devices
export { PassiveSwitch, DeviceType }