-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (114 loc) · 3.64 KB
/
index.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
var Service, Characteristic;
var net = require("net");
var timeout = 500;
var long_timeout = 3000;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-beam", "Beam", BeamAccessory);
};
function BeamAccessory(log, config) {
this.log = log;
this.config = config
// Got to make a guess since there is no way to
// determine the state of the beam projector.
this.on_state = false;
this.service = new Service.Switch(this.config.name);
this.service.getCharacteristic(Characteristic.On).on('set', this.setState.bind(this));
this.service.getCharacteristic(Characteristic.On).on('get', this.getState.bind(this));
}
function writeTo(client, command) {
return new Promise((resolve, reject) => {
client.write(command + "\n", () => {
resolve(client);
});
});
}
function wait(timeout, client) {
return new Promise((resolve, reject) => {
setTimeout(() => {resolve(client)}, timeout);
});
}
BeamAccessory.prototype.communicate = function() {
return new Promise((resolve, reject) => {
var client = new net.Socket();
client.connect(this.config.port, this.config.host, () => {
resolve(client);
});
client.on('close', () => { this.log("Connection closed to beam") });
client.on('data', (data) => {this.log("received:" + data) });
});
}
// turning off process: current APIs only offer a toggle, so we turn it on to turn it off
//
// connect to the beam (unfortunately this also turns on the beam grr)
// wait 3 seconds -- there is a huge variance in how long it takes to turn on the beam, so we wait...
// send the 'turn off' command
BeamAccessory.prototype.off = function() {
return this.communicate().then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "user;Homebridge;" + this.config.macAddress)
}).then(client => {
return wait(long_timeout, client)
}).then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "led;3;3")
}).then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "led;0;3")
}).then(client => {
return writeTo(client, "")
}).then(client => {
this.on_state = false;
this.log("Beam off");
client.destroy()
});
}
// turning on process:
//
// Connect to beam -- this actually turns on the beam, but we need to wait to make sure
// it actually turned on before handing control back to homebridge.
BeamAccessory.prototype.on = function() {
return this.communicate().then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "user;Homebridge;" + this.config.macAddress)
}).then(client => {
return wait(long_timeout, client)
}).then(client => {
return writeTo(client, "led;3;3")
}).then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "led;0;3")
}).then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "screen;0;0")
}).then(client => {
return wait(timeout, client)
}).then(client => {
return writeTo(client, "")
}).then(client => {
this.log("Beam on");
this.on_state = true;
client.destroy()
return this.on_state;
});
}
BeamAccessory.prototype.getState = function(callback) {
callback(null, this.on_state);
}
BeamAccessory.prototype.setState = function(state, callback) {
if(state) {
this.on().then((state) =>{ callback(null); });
} else {
this.off().then((state) => { callback(null) });
}
}
BeamAccessory.prototype.getServices = function() {
return [this.service];
}