forked from ecoen66/homebridge-epson-projector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (108 loc) · 3.67 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
const axios = require('axios');
const query_path = "/cgi-bin/json_query?jsoncallback=";
const timeout = 10000;
const interval = 15; // Minutes
const debug = false;
var Service;
var Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-epson-iprojection", "Epson iProjection", EpsonProjector);
};
function EpsonProjector(log, config) {
this.log = log;
this.ipAddress = config["ipAddress"];
this.model = config["model"] === undefined ? "" : config["model"];
this.serial = config["serial"] === undefined ? "" : config["serial"];
this.name = config["name"];
this.timeout = config["timeout"] === undefined ? timeout : config["timeout"];
this.refreshInterval = config["refreshInterval"] === undefined ? interval * 60000 : config["refreshInterval"] * 60000;
this.debug = config["debug"] === undefined ? debug : config["debug"];
this.state = false; // Track the power state
this.referer = "http://" + this.ipAddressAddress + "/cgi-bin/webconf";
this.api = axios.create({
headers: {'Referer': this.referer}
});
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Epson")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.switchService = new Service.Switch(this.name);
this.switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
this.wait = null;
this.timer = null;
this.poll()
}
EpsonProjector.prototype = {
updateUI: async function () {
setTimeout( () => {
this.switchService.getCharacteristic(Characteristic.On).updateValue(this.state);
this.log('Updated Characteristic value to %s', this.state);
}, 100)
},
free: async function () {
if(this.wait) clearTimeout(this.wait);
this.wait = null;
this.poll();;
},
poll: async function () {
if(this.timer) clearTimeout(this.timer);
this.timer = null;
if(!this.wait) {
try {
const resp = await this.api.get('http://' + this.ipAddress + query_path + 'PWR?')
.catch(err => {
this.log.error('Error getting power state %s',err)
});
// this.state = parseInt(resp.data.projector.feature.reply);
this.state = resp.data.projector.feature.reply === "01";
this.updateUI();
if (this.debug) {
this.log("http://" + this.ipAddress + query_path + "PWR?");
this.log("Projector response: " + resp.data.projector.feature.reply + " =", this.state);
}
} catch(err) {
this.log.error('Error getting power state %s',err)
}
}
this.timer = setTimeout(this.poll.bind(this), this.refreshInterval);
},
setPowerState: async function(powerOn, callback) {
if (!this.wait) {
this.state = powerOn;
this.updateUI();
if(this.timer) clearTimeout(this.timer)
this.timer = null
let command;
if (powerOn) {
command = "PWR ON";
} else {
command = "PWR OFF";
}
if (this.debug) {
this.log('powerOn = %s', powerOn);
this.log("http://" + this.ipAddress + query_path + command);
}
callback(null);
this.wait = setTimeout(this.free.bind(this), powerOn === true ? 60000 : 300000);
try {
const resp = await this.api({
method: 'get',
url: 'http://' + this.ipAddress + query_path + command,
timeout: 16000
}).catch(err => {
// this.log.error('Error setting power state %s',err)
});
}catch(err) {
this.log.error('Error setting powerState %s',err)
}
}
},
getServices: function () {
return [this.informationService, this.switchService];
}
};