fix: make hidcodes and on/off work

This commit is contained in:
Martin Giger 2021-07-18 13:26:04 +02:00
parent 92c8542a24
commit df6074cb3c
Signed by: freaktechnik
GPG key ID: AE530058EFE7FD60
2 changed files with 19 additions and 14 deletions

View file

@ -1,3 +1,3 @@
# irusb-adapter # irusb-adapter
webthings.io gateway adapter for the Video Storm IrUSB device. Allows control of Android TV devices like nVidia Shield over Network. webthings.io gateway adapter for the [Video Storm IrUSB device](https://www.video-storm.com/proddetail.php?prod=IRUSB). Allows control of Android TV devices like nVidia Shield over Network.

View file

@ -28,23 +28,23 @@ const HID_MAP = {
modifier: '000' modifier: '000'
}, },
RIGHT: { RIGHT: {
code: '069', code: '079',
type: 'consumer', type: 'keyboard',
modifier: '000' modifier: '000'
}, },
LEFT: { LEFT: {
code: '068', code: '080',
type: 'consumer', type: 'keyboard',
modifier: '000' modifier: '000'
}, },
DOWN: { DOWN: {
code: '067', code: '081',
type: 'consumer', type: 'keyboard',
modifier: '000' modifier: '000'
}, },
UP: { UP: {
code: '066', code: '082',
type: 'consumer', type: 'keyboard',
modifier: '000' modifier: '000'
}, },
BACK: { BACK: {
@ -238,9 +238,12 @@ class IRUSBDevice extends Device {
this.responseQueue = []; this.responseQueue = [];
this.socket.connect(port, ip, () => { this.socket.connect(port, ip, () => {
this.connectedNotify(true); this.connectedNotify(true);
this.interval = setInterval(() => this.updateState(), 5000);
this.adapter.handleDeviceAdded(this); this.adapter.handleDeviceAdded(this);
}); });
this.socket.on('close', () => { this.socket.on('close', () => {
clearInterval(this.interval);
this.interval = undefined;
this.connectedNotify(false); this.connectedNotify(false);
}); });
this.socket.on('data', (data) => { this.socket.on('data', (data) => {
@ -254,23 +257,22 @@ class IRUSBDevice extends Device {
//TODO event for incoming IR //TODO event for incoming IR
console.log('Unexpected packet', data); console.log('Unexpected packet', data);
}); });
this.interval = setTimeout(() => this.updateState(), 5000);
this.updateState(); this.updateState();
} }
async updateState() { async updateState() {
const playing = await this.sendCommand('GETPLAY'); const playing = await this.sendCommand('GETPLAY');
const app = await this.sendCommand('GETFG'); const app = await this.sendCommand('GETFG');
console.log(playing, app); const isPlaying = playing === '1';
const isPlaying = playing === '1\r';
this.findProperty('playing').setCachedValueAndNotify(isPlaying); this.findProperty('playing').setCachedValueAndNotify(isPlaying);
this.findProperty('app').setCachedValueAndNotify(app); this.findProperty('app').setCachedValueAndNotify(app);
// app !== screensaver // app !== screensaver
this.findProperty('power').setCachedValueAndNotify(isPlaying && app); this.findProperty('power').setCachedValueAndNotify(isPlaying || app);
} }
destroy() { destroy() {
clearInterval(this.interval); clearInterval(this.interval);
this.interval = undefined;
this.socket.destroy(); this.socket.destroy();
} }
@ -278,7 +280,7 @@ class IRUSBDevice extends Device {
let pressType = long ? 'long' : 'short'; let pressType = long ? 'long' : 'short';
const keyInfo = HID_MAP[keyName]; const keyInfo = HID_MAP[keyName];
const startCode = CONTROL_CODE[keyInfo.type][pressType]; const startCode = CONTROL_CODE[keyInfo.type][pressType];
return this.sendCommand(`HIDCODE${startCode}${keyInfo.code}${keyInfo.modifier}`); return this.sendCommand(`HIDCODE${startCode}${keyInfo.modifier}${keyInfo.code}`);
} }
sendCommand(command) { sendCommand(command) {
@ -296,6 +298,9 @@ class IRUSBDevice extends Device {
reconnect(ip, port) { reconnect(ip, port) {
if(this.socket.readyState !== 'open') { if(this.socket.readyState !== 'open') {
if(!this.interval) {
this.interval = setInterval(() => this.updateState(), 5000);
}
this.socket.connect(port, ip, () => { this.socket.connect(port, ip, () => {
this.connectedNotify(true); this.connectedNotify(true);
}); });