-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
96 lines (81 loc) · 2.74 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
const request = require("request");
const command_path = "/cgi-bin/directsend?";
const query_path = "/cgi-bin/json_query?jsoncallback=";
const timeout = 5000;
const debug = false;
var Service;
var Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-epson-projector", "EpsonProjector", EpsonProjector);
};
function EpsonProjector(log, config) {
this.log = log;
this.ip = config["ip"];
this.model = config["model"] === undefined ? "" : config["model"];
this.serial = config["serial"] === undefined ? "" : config["serial"];
this.name = config["name"];
}
EpsonProjector.prototype = {
getPowerState: function (callback) {
if (debug) {
console.log(error);
}
request.get({
uri: "http://" + this.ip + query_path + "PWR?",
headers: {
"Referer": "http://" + this.ip + "/cgi-bin/webconf",
},
timeout: timeout
}, function (error, response, body) {
if (error !== null) {
callback(error);
return;
}
try {
callback(null, JSON.parse(body)["projector"]["feature"]["reply"] === "01")
} catch (error) {
callback(error);
}
});
},
setPowerState: function(powerOn, callback) {
let command;
if (powerOn) {
command = "PWR=ON";
} else {
command = "PWR=OFF";
}
if (debug) {
console.log(error);
}
request.get({
uri: "http://" + this.ip + command_path + command,
headers: {
"Referer": "http://" + this.ip + "/cgi-bin/webconf",
},
timeout: timeout
}, function (error, response, body) {
if (debug) {
console.log(error);
}
callback();
});
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "EPSON")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
this.informationService = informationService;
this.switchService = switchService;
return [informationService, switchService];
}
};