Skip to content

Commit

Permalink
Add "restart service" and "reboot Raspberry" buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
zhogov committed Sep 8, 2020
1 parent de4c10a commit 47dd763
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from bluetooth_devices import *
import asyncio
import concurrent.futures
import sys
import subprocess

from aiohttp_session import SimpleCookieStorage, session_middleware
from aiohttp_security import check_authorized, \
Expand Down Expand Up @@ -46,6 +48,8 @@ def __init__(self, loop: asyncio.AbstractEventLoop, adapter, bluetooth_devices:B
self.app = web.Application(middlewares=[middleware])
self.app.router.add_route('*', '/', self.root_handler)
self.app.router.add_route('POST', '/changepassword', self.change_password_handler)
self.app.router.add_route('POST', '/restartservice', self.restart_service_handler)
self.app.router.add_route('POST', '/reboot', self.reboot_handler)
self.app.router.add_route('POST', '/login', self.handler_login)
self.app.router.add_route('GET', '/authorised', self.handler_is_authorised)
self.app.router.add_route('POST', '/setdevicecapture', self.set_device_capture)
Expand Down Expand Up @@ -112,6 +116,14 @@ async def change_password_handler(self, request):
return web.HTTPError
return web.Response(text="Password successfully changed")

async def restart_service_handler(self, request):
await check_authorized(request)
sys.exit(1)

async def reboot_handler(self, request):
await check_authorized(request)
subprocess.Popen(['reboot'])

async def get_hid_devices_handler(self, request):
await check_authorized(request)
return web.Response(text=json.dumps(self.hid_devices.get_hid_devices_with_config()))
Expand Down
12 changes: 12 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ <h5>Pair devices</h5>

<div class="row" style="width: 100%; padding: 0pt 15pt 0pt 15pt;" id="settingsPanel">
<h4>Settings</h4>
<div>
<div class="section">
<a class="waves-effect waves-light btn" id="restartServiceButton">
Restart bthidhub service
</a>
<a class="waves-effect waves-light btn" id="rebootButton">
Reboot Raspberry
</a>
</div>
<div class="divider">
</div>
</div>
<div class=" col s12 m8 l6 xl5">
<div class="section">
<h5>Change 'pi' user password</h5>
Expand Down
22 changes: 22 additions & 0 deletions web/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Main{
$('#stopScanningButton').on('click', e => {that.changeScanningMode(false);});
$('#startDiscoverableButton').on('click', e => {that.changeDiscoverableMode(true);});
$('#stopDiscoverableButton').on('click', e => {that.changeDiscoverableMode(false);});
$('#restartServiceButton').on('click', e => {that.restartService();});
$('#rebootButton').on('click', e => {that.rebootRaspberry();});

this.scanning = false;
this.setScanningState();
Expand Down Expand Up @@ -349,6 +351,26 @@ class Main{
});
}

restartService(){
$.ajax({
url: "http://" + location.hostname + ":8080/restartservice",
type: 'POST', cache:false, contentType: false, processData: false,
error: function (jqXHR, textStatus, errorThrown){
M.toast({html: "Restarting service, reload this page..."});
}
});
}

rebootRaspberry(){
$.ajax({
url: "http://" + location.hostname + ":8080/reboot",
type: 'POST', cache:false, contentType: false, processData: false,
error: function (jqXHR, textStatus, errorThrown){
M.toast({html: "Rebooting Raspberry, reload this page..."});
}
});
}

setActiveView(view){
switch (view){
case views.SETTINGS:
Expand Down

0 comments on commit 47dd763

Please sign in to comment.