Skip to content

Commit

Permalink
Adding informations in admin about powering off device
Browse files Browse the repository at this point in the history
Change functions to ES6 notation
Handle new event : 'unpaired' : cert and status are updated
Cleaning code
  • Loading branch information
louis49 committed Jan 4, 2022
1 parent 2815384 commit 86cdb6f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
}
]
],
"plugins": ["@babel/plugin-transform-modules-commonjs"]
"plugins": ["babel-plugin-transform-import-meta"]
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 25 additions & 13 deletions src/homebridge/DeviceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class DeviceManager extends EventEmitter {
}
}


console.log("End Load");
this.log.info("Devices loaded");
}

save() {
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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){
Expand Down
8 changes: 6 additions & 2 deletions src/homebridge/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ 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(){
return new Promise((resolve, reject) => {
let server = this.app.listen(this.port, () => {
resolve(server.address().port)
}).on('error', e => {
console.log(e);
console.error(e);
reject(e);
});
});
Expand Down
37 changes: 29 additions & 8 deletions src/homebridge/admin/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,45 @@
<title>Homebridge AndroidTV Plugin</title>
<script src="https://unpkg.com/[email protected]"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
<div id="devices" class="container-fluid">
<table class="table table-striped">
<div style="text-align: center"><h1 class="display-5 text-white bg-secondary">AndroidTV Homebridge Plugin</h1></div>

<div id="devices_list" class="container-fluid" >
<div class="alert alert-warning m-auto" role="alert" style="width: 30rem; text-align: center" v-if="devices_length === 0">
<h1>Oops !</h1>
<hr>
<h6>It seems that no AndroidTV is connected and powered on this network</h6>
<i class="bi bi-power text-warning" style="font-size: 3rem;"></i>
</div>
<table class="table table-striped" v-if="devices_length > 0">
<thead>
<tr>
<th v-for="column in columns" scope="col"> {{ column }}</th>
<th>actions</th>
<th v-for="column in columns" scope="col" style="text-align: center"> {{ column }}</th>
<th style="text-align: center">actions</th>
</tr>
</thead>
<tbody>
<tr v-for="device in devices">
<td v-for="key in columns">
<div v-if="key !== 'type'">
{{device[key]}}
<div v-if="key === 'host' || key === 'name' || key === 'app_package_current'">
{{device[key]}}
</div>
<div v-if="key === 'online' || key === 'paired' || key === 'started' || key === 'powered'" style="text-align: center;">
<i class="bi bi-check-circle text-success" style="font-size: 1.5rem;" v-if="device[key] === true"></i>
<i class="bi bi-x-circle text-danger" style="font-size: 1.5rem;" v-if="device[key] === false"></i>
</div>
<div v-else-if="key === 'type'">
<div v-if="key === 'type'" style="text-align: center">
<select v-model="device.type" v-on:change="set_type(device)">
<option v-for="type in types" v-bind:value="type.value">
{{ type.text }}
</option>
</select>
</div>
</td>
<td>
<td style="text-align: center">
<button class="btn btn-primary btn-sm" v-on:click="pair(device)" v-if="device.online && !device.paired && !device.pairing">Pair</button>
<button class="btn btn-warning btn-sm" v-on:click="power(device)" v-if="device.started">Power</button>
<div class="row" v-if="device.pairing">
Expand All @@ -44,8 +57,16 @@
</tr>
</tbody>
</table>
<div id="info" class="alert alert-primary m-auto" role="alert" style="width: 30rem; text-align: center" v-if="devices_length > 0">
<!--<span class="badge bg-primary rounded-pill" style="font-size: 1.2em;">Info</span> -->
<h4 class="alert-heading">Information</h4>
<hr>
<p>Please note that after restarting Homebridge while your devices are turned off, your devices will remain unavailable until you turn them on manually.</p>
</div>
</div>



<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.24.0/axios.js"></script>
<script src="index.js" charset="utf-8" ></script>
</body>
Expand Down
9 changes: 7 additions & 2 deletions src/homebridge/admin/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -48,4 +53,4 @@ const Devices = {
}


Vue.createApp(Devices).mount('#devices')
Vue.createApp(Devices).mount('#devices_list')
2 changes: 1 addition & 1 deletion src/homebridge/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 86cdb6f

Please sign in to comment.