split out config file reading
+ improve ip and no 'network' on display
This commit is contained in:
77
display.py
77
display.py
@@ -4,10 +4,16 @@ import time
|
||||
import serial
|
||||
import socket
|
||||
import threading
|
||||
import re
|
||||
import queue
|
||||
from utils import MainIf
|
||||
from subprocess import Popen, PIPE
|
||||
from configparser import ConfigParser
|
||||
|
||||
# display tty device
|
||||
tty = '/dev/ttyS1'
|
||||
STARTUP_TEXT = '/home/l_samenv/boxtools/startup_display.txt'
|
||||
TOOLS = '/home/l_samenv/boxtools'
|
||||
STARTUP_TEXT = f'{TOOLS}/startup_display.txt'
|
||||
|
||||
ESC = 0x1b
|
||||
ESCESC = bytes((ESC, ESC))
|
||||
@@ -46,6 +52,9 @@ WIDTH = 480
|
||||
MAGIC = b'\xcb\xef\x20\x18'
|
||||
|
||||
|
||||
mainif = MainIf()
|
||||
|
||||
|
||||
def xy(x, y):
|
||||
x = min(480, int(x))
|
||||
return bytes([(min(127, y + TOPGAP) << 1) + (x >> 8), x % 256])
|
||||
@@ -61,9 +70,10 @@ class Display:
|
||||
storage = None
|
||||
|
||||
def __init__(self, dev, timeout=0.5, daemon=False):
|
||||
self.event = threading.Event()
|
||||
# self.event = threading.Event()
|
||||
self.term = serial.Serial(dev, baudrate=115200, timeout=timeout)
|
||||
self.storage = bytearray()
|
||||
self.hostname = socket.gethostname()
|
||||
if daemon:
|
||||
todo_file = STARTUP_TEXT + '.todo'
|
||||
try:
|
||||
@@ -82,11 +92,8 @@ class Display:
|
||||
self.storage = None
|
||||
else:
|
||||
os.system('systemctl stop display')
|
||||
self.gethost(False)
|
||||
self.reset()
|
||||
if daemon:
|
||||
threading.Thread(target=self.gethostthread, daemon=True).start()
|
||||
self.event.wait(1) # wait for thread to be started
|
||||
self.net_display(None)
|
||||
|
||||
def reset(self):
|
||||
@@ -159,34 +166,10 @@ class Display:
|
||||
if row < 3:
|
||||
self.text(line, row)
|
||||
|
||||
def gethost(self, truehostname):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
s.connect(('8.8.8.8', 80)) # 8.8.8.8: google DNS
|
||||
self.hostip = s.getsockname()[0]
|
||||
except Exception as e:
|
||||
self.hostip = ''
|
||||
hostname = None
|
||||
if self.hostip and truehostname:
|
||||
try:
|
||||
hostname = socket.gethostbyaddr(self.hostip)[0]
|
||||
except Exception:
|
||||
pass
|
||||
self.hostname = hostname or socket.gethostname()
|
||||
|
||||
def gethostthread(self):
|
||||
self.gethost(True)
|
||||
self.event.set()
|
||||
time.sleep(1)
|
||||
while True:
|
||||
self.event.clear()
|
||||
self.gethost(True)
|
||||
self.event.wait(1)
|
||||
|
||||
def set_touch(self, on):
|
||||
self.send(TOUCH_MODE, on)
|
||||
self.touch = on
|
||||
|
||||
|
||||
def gettouch(self):
|
||||
if not self.touch:
|
||||
self.set_touch(1)
|
||||
@@ -207,44 +190,43 @@ class Display:
|
||||
return x
|
||||
print('skipped', data)
|
||||
|
||||
def menu_reboot(self, x):
|
||||
if x is None:
|
||||
def menu_reboot(self, xtouch):
|
||||
if xtouch is None:
|
||||
return
|
||||
if x < 120:
|
||||
if xtouch < 120:
|
||||
self.show('reboot ...')
|
||||
os.system('reboot now')
|
||||
else:
|
||||
self.std_display()
|
||||
|
||||
def menu_shutdown(self, x):
|
||||
if x is None:
|
||||
def menu_shutdown(self, xtouch):
|
||||
if xtouch is None:
|
||||
return
|
||||
if x < 120:
|
||||
if xtouch < 120:
|
||||
self.show('shutdown ...')
|
||||
os.system('shutdown now')
|
||||
else:
|
||||
self.std_display()
|
||||
|
||||
def menu_main(self, x):
|
||||
if x is None:
|
||||
def menu_main(self, xtouch):
|
||||
if xtouch is None:
|
||||
return
|
||||
print(x)
|
||||
if x < 160:
|
||||
if xtouch < 160:
|
||||
self.std_display()
|
||||
elif x < 320:
|
||||
elif xtouch < 320:
|
||||
self.menu = self.menu_reboot
|
||||
self.show('reboot?', '( OK ) (cancel)')
|
||||
else:
|
||||
self.menu = self.menu_shutdown
|
||||
self.show('shutdown?', '( OK ) (cancel)')
|
||||
|
||||
|
||||
def std_display(self):
|
||||
self.menu = self.net_display
|
||||
self.net_display(None)
|
||||
|
||||
def net_display(self, x):
|
||||
if x is None:
|
||||
hostip = self.hostip or 'no network'
|
||||
def net_display(self, xtouch):
|
||||
if xtouch is None:
|
||||
hostip = mainif.ip or 'no network'
|
||||
dotpos = hostip.find('.')
|
||||
if dotpos < 0:
|
||||
dotpos = len(hostip)
|
||||
@@ -252,14 +234,15 @@ class Display:
|
||||
self.send(SET_COLOR, 0, 0, 0, 11 + self.blink) # yellow / blue
|
||||
self.text('.', 0, dotpos, dotpos+1)
|
||||
self.color()
|
||||
self.text(self.hostname, 1)
|
||||
self.event.set()
|
||||
self.text(mainif.hostname or self.hostname, 1)
|
||||
# self.event.set()
|
||||
self.blink = not self.blink
|
||||
else:
|
||||
self.menu = self.menu_main
|
||||
self.show('(cancel)(reboot)(shdown)')
|
||||
|
||||
def refresh(self):
|
||||
mainif.getip()
|
||||
func = self.menu or self.net_display
|
||||
func(self.gettouch())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user