From 86cdb6f7c51760c2f397fae9fa1325b73590d28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Desnos=20Benoi=CC=82t?= Date: Tue, 4 Jan 2022 13:13:35 +0100 Subject: [PATCH] Adding informations in admin about powering off device Change functions to ES6 notation Handle new event : 'unpaired' : cert and status are updated Cleaning code --- .babelrc | 2 +- package.json | 7 +++-- src/homebridge/DeviceManager.js | 38 +++++++++++++++++--------- src/homebridge/admin/index.js | 8 ++++-- src/homebridge/admin/static/index.html | 37 +++++++++++++++++++------ src/homebridge/admin/static/index.js | 9 ++++-- src/homebridge/platform.js | 2 +- 7 files changed, 73 insertions(+), 30 deletions(-) diff --git a/.babelrc b/.babelrc index 0f32876..3531911 100644 --- a/.babelrc +++ b/.babelrc @@ -11,5 +11,5 @@ } ] ], - "plugins": ["@babel/plugin-transform-modules-commonjs"] + "plugins": ["babel-plugin-transform-import-meta"] } diff --git a/package.json b/package.json index 1066451..acab686 100644 --- a/package.json +++ b/package.json @@ -21,14 +21,15 @@ "author": "@louis49", "license": "ISC", "dependencies": { - "androidtv-remote": "^1.0.7", + "androidtv-remote": "^1.0.8", "bonjour": "^3.5.0", - "express": "^4.17.2", - "core-js": "^3.20.2" + "core-js": "^3.20.2", + "express": "^4.17.2" }, "devDependencies": { "@babel/cli": "^7.16.7", "@babel/preset-env": "^7.0.0", + "babel-plugin-transform-import-meta": "^2.1.0", "homebridge": "^1.3.9" }, "type": "commonjs", diff --git a/src/homebridge/DeviceManager.js b/src/homebridge/DeviceManager.js index a8fa99f..3e91141 100644 --- a/src/homebridge/DeviceManager.js +++ b/src/homebridge/DeviceManager.js @@ -30,8 +30,7 @@ class DeviceManager extends EventEmitter { } } - - console.log("End Load"); + this.log.info("Devices loaded"); } save() { @@ -77,12 +76,12 @@ class DeviceManager extends EventEmitter { listen(){ bonjour().find({ type : ["androidtvremote2"] - }, async function (service){ + }, async (service) => { const name = service.name; const address = service.addresses[0]; const port = service.port; - this.log.debug('Finding device : ', name, address, port) + this.log.info('Finding online device : ', name, address, port) let device; @@ -95,17 +94,17 @@ class DeviceManager extends EventEmitter { } device.online = true; - device.android_remote.on('secret', function (){ + device.android_remote.on('secret', () => { console.info('Pairing', this.devices[address].name); this.devices[address].pairing = true; - }.bind(this)); + }); - device.android_remote.on('powered',function (powered){ + device.android_remote.on('powered', (powered) => { device.powered = powered; this.emit('powered', device); - }.bind(this)); + }); - device.android_remote.on('volume',function (volume){ + device.android_remote.on('volume',(volume) => { device.volume_max = volume.maximum; if(device.volume_current !== volume.level){ device.volume_current = volume.level; @@ -122,11 +121,16 @@ class DeviceManager extends EventEmitter { else{ device.volume_muted = volume.muted; } - }.bind(this)); + }); - device.android_remote.on('ready',function () { + device.android_remote.on('ready', () => { this.emit('discover', device); - }.bind(this)); + }); + + device.android_remote.on('unpaired', () => { + this.unpair(device.host); + this.log.info("The device", device.name, "had a problem with certificates and was unpaired"); + }); if(device.paired){ let result = await device.android_remote.start(); @@ -135,7 +139,15 @@ class DeviceManager extends EventEmitter { this.save(); } } - }.bind(this)); + }); + } + + unpair(host){ + let device = this.get(host); + device.started = false; + device.paired = false; + device.android_remote.cert = {}; + this.save(); } async pair(host){ diff --git a/src/homebridge/admin/index.js b/src/homebridge/admin/index.js index c6cae57..9e7cbda 100644 --- a/src/homebridge/admin/index.js +++ b/src/homebridge/admin/index.js @@ -2,13 +2,17 @@ import express from "express"; import path from "path"; import { api } from "./api.js"; +import { fileURLToPath } from 'url'; +import { dirname } from "path"; +const directory = dirname(fileURLToPath(import.meta.url)); + class AdminServer { constructor(port, deviceManager) { this.port = port; this.deviceManager = deviceManager; this.app = express(); this.app.use('/api', api(this.deviceManager)); - this.app.use('/', express.static(path.join(__dirname,'static'))); + this.app.use('/', express.static(path.join(directory,'static'))); } listen(){ @@ -16,7 +20,7 @@ class AdminServer { let server = this.app.listen(this.port, () => { resolve(server.address().port) }).on('error', e => { - console.log(e); + console.error(e); reject(e); }); }); diff --git a/src/homebridge/admin/static/index.html b/src/homebridge/admin/static/index.html index 9c6a9a6..2f5197c 100644 --- a/src/homebridge/admin/static/index.html +++ b/src/homebridge/admin/static/index.html @@ -4,24 +4,37 @@ Homebridge AndroidTV Plugin + -
- +

AndroidTV Homebridge Plugin

+ +
+ +
- - + + -
{{ column }}actions {{ column }}actions
-
- {{device[key]}} +
+ {{device[key]}} +
+
+ +
-
+
+
@@ -44,8 +57,16 @@
+
+ + diff --git a/src/homebridge/admin/static/index.js b/src/homebridge/admin/static/index.js index 630830a..32562e9 100644 --- a/src/homebridge/admin/static/index.js +++ b/src/homebridge/admin/static/index.js @@ -2,9 +2,14 @@ const Devices = { mounted() { axios.get('api/devices').then((response) => (this.devices = response.data)); }, + computed: { + devices_length: function() { + return Object.keys(this.devices).length; + } + }, data() { return { - devices: [], + devices: {}, columns:["host", "name", "online", "paired", "started", "powered", "app_package_current", "type"], code: "", types: [ @@ -48,4 +53,4 @@ const Devices = { } -Vue.createApp(Devices).mount('#devices') +Vue.createApp(Devices).mount('#devices_list') diff --git a/src/homebridge/platform.js b/src/homebridge/platform.js index 0f6d590..af8e658 100644 --- a/src/homebridge/platform.js +++ b/src/homebridge/platform.js @@ -65,7 +65,7 @@ class AndroidTV { discover(device){ - console.log("Discover : ", device.toJSON()); + this.log.info("Discover : ", device.toJSON()); const tvName = device.name; const uuid = this.api.hap.uuid.generate('homebridge:androidtv-' + tvName);