new: added clean shutdown button to the web ui (closes #161)

This commit is contained in:
Simone Margaritelli 2019-10-07 19:42:29 +02:00
parent 0577972867
commit 4f694ddb83
4 changed files with 58 additions and 4 deletions

View File

@ -1,4 +1,8 @@
import subprocess import subprocess
import os
import logging
import time
import pwnagotchi.ui.view as view
version = '1.0.0a' version = '1.0.0a'
@ -46,3 +50,13 @@ def temperature(celsius=True):
temp = int(fp.read().strip()) temp = int(fp.read().strip())
c = int(temp / 1000) c = int(temp / 1000)
return c if celsius else ((c * (9 / 5)) + 32) return c if celsius else ((c * (9 / 5)) + 32)
def shutdown():
logging.warning("shutting down ...")
if view.ROOT:
view.ROOT.on_shutdown()
# give it some time to refresh the ui
time.sleep(5)
os.system("sync")
os.system("halt")

View File

@ -15,10 +15,28 @@ class VideoHandler(BaseHTTPRequestHandler):
_lock = Lock() _lock = Lock()
_index = """<html> _index = """<html>
<head> <head>
<title>%s</title> <title>%s</title>
<style>
.block {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
display: block;
cursor: pointer;
text-align: center;
}
</style>
</head> </head>
<body> <body>
<img src="/ui" id="ui"/> <div style="position: absolute; top:0; left:0; width:100%%;">
<img src="/ui" id="ui" style="width:100%%"/>
<br/>
<hr/>
<form action="/shutdown" onsubmit="return confirm('This will halt the unit, continue?');">
<input type="submit" class="block" value="Shutdown"/>
</form>
</div>
<script type="text/javascript"> <script type="text/javascript">
window.onload = function() { window.onload = function() {
@ -50,6 +68,9 @@ class VideoHandler(BaseHTTPRequestHandler):
except: except:
pass pass
elif self.path.startswith('/shutdown'):
pwnagotchi.shutdown()
elif self.path.startswith('/ui'): elif self.path.startswith('/ui'):
with self._lock: with self._lock:
self.send_response(200) self.send_response(200)

View File

@ -15,7 +15,7 @@ from pwnagotchi.ui.state import State
WHITE = 0xff WHITE = 0xff
BLACK = 0x00 BLACK = 0x00
ROOT = None
def setup_display_specifics(config): def setup_display_specifics(config):
width = 0 width = 0
@ -57,9 +57,12 @@ def setup_display_specifics(config):
class View(object): class View(object):
def __init__(self, config, state={}): def __init__(self, config, state={}):
global ROOT
self._render_cbs = [] self._render_cbs = []
self._config = config self._config = config
self._canvas = None self._canvas = None
self._frozen = False
self._lock = Lock() self._lock = Lock()
self._voice = Voice(lang=config['main']['lang']) self._voice = Voice(lang=config['main']['lang'])
@ -119,6 +122,8 @@ class View(object):
logging.warning("ui.fps is 0, the display will only update for major changes") logging.warning("ui.fps is 0, the display will only update for major changes")
self._ignore_changes = ('uptime', 'name') self._ignore_changes = ('uptime', 'name')
ROOT = self
def add_element(self, key, elem): def add_element(self, key, elem):
self._state.add_element(key, elem) self._state.add_element(key, elem)
@ -261,6 +266,12 @@ class View(object):
self.on_normal() self.on_normal()
def on_shutdown(self):
self.set('face', faces.SLEEP)
self.set('status', self._voice.on_shutdown())
self.update(force=True)
self._frozen = True
def on_bored(self): def on_bored(self):
self.set('face', faces.BORED) self.set('face', faces.BORED)
self.set('status', self._voice.on_bored()) self.set('status', self._voice.on_bored())
@ -323,6 +334,9 @@ class View(object):
def update(self, force=False): def update(self, force=False):
with self._lock: with self._lock:
if self._frozen:
return
changes = self._state.changes(ignore=self._ignore_changes) changes = self._state.changes(ignore=self._ignore_changes)
if force or len(changes): if force or len(changes):
self._canvas = Image.new('1', (self._width, self._height), WHITE) self._canvas = Image.new('1', (self._width, self._height), WHITE)

View File

@ -90,6 +90,11 @@ class Voice:
self._('Zzzzz'), self._('Zzzzz'),
self._('ZzzZzzz ({secs}s)').format(secs=secs)]) self._('ZzzZzzz ({secs}s)').format(secs=secs)])
def on_shutdown(self):
return random.choice([
self._('Good night.'),
self._('Zzz')])
def on_awakening(self): def on_awakening(self):
return random.choice(['...', '!']) return random.choice(['...', '!'])