From ae6ba7a0310bfa430c15de333f3bf8686b02efb0 Mon Sep 17 00:00:00 2001 From: LIN SE Date: Mon, 15 Jan 2024 11:34:03 +0100 Subject: [PATCH] improve display - display config has only one line for startup text - faster display daeomon startup - added newer fonts (e.g. medium) to display.py --- display.py | 7 ++- install.py | 103 +++++++++++++++++--------------- servercfg/linse-box1_5b265c.cfg | 12 ++-- 3 files changed, 65 insertions(+), 57 deletions(-) diff --git a/display.py b/display.py index c141dd8..1aa70d2 100644 --- a/display.py +++ b/display.py @@ -37,12 +37,14 @@ SET_STARTUP = 0xf2 IDENT = 0xf3 -FONT_GEO = [(6, 8), (8, 16), (20, 40)] +FONT_GEO = [(6, 8), (8, 16), (20, 40), (38,64), (16, 16), (12, 24)] TOPGAP = 8 # upper part of display is not useable HEIGHT = 120 WIDTH = 480 +MAGIC = b'\xcb\xef\x20\x18' + def xy(x, y): x = min(480, int(x)) @@ -83,7 +85,7 @@ class Display: if daemon: threading.Thread(target=self.gethostthread, daemon=True).start() self.event.wait(1) # wait for thread to be started - self.refresh() + self.net_display(None) def reset(self): self.send(MODE_GRAPH) @@ -200,7 +202,6 @@ class Display: return if data[0] == TOUCH and len(data) == 3: x = (data[1] % 2) * 256 + data[2] - print(x) return x print('skipped', data) diff --git a/install.py b/install.py index ebbef16..f5e370b 100755 --- a/install.py +++ b/install.py @@ -14,13 +14,14 @@ import filecmp import shutil import serial import types +import socket from subprocess import Popen, PIPE from glob import glob from ipaddress import IPv4Interface from configparser import ConfigParser from os.path import join, getmtime, exists, basename -more_info = sys.argv[1] if len(sys.argv) > 1 else 'NONE' +more_info = False os.chdir('/root/aputools/to_system') DEL = '__to_delete__' @@ -96,6 +97,13 @@ ExecStart = /usr/bin/python3 /root/aputools/display.py -d WantedBy = multi-user.target """ +ifname_mapping = { + 'eth0': 'enp1s0', + 'eth1': 'enp2s0', + 'eth2': 'enp3s0', + 'eth3': 'enp4s0', +} + pip_requirements = { 'l_samenv': {}, 'root': {} @@ -108,7 +116,7 @@ main_info = { # info to be determined depending on cfg 'ifname': '', # name of interface of wan (dhcp) port 'addr': '', # addr of wan port 'current': None, - 'hostname': gethostname() # effective or given host name + 'hostname': socket.gethostname() # effective or given host name } for netif in os.scandir('/sys/class/net'): @@ -145,19 +153,15 @@ def router(**opts): def display_update(cfg): - text = '\n'.join(cfg.get(key, '') for key in ('line0', 'line1', 'line2')) + text = '\n'.join(cfg.get('startup_text', '').split('|')[:3]) text = text.replace('HOST', main_info['hostname']) \ .replace('ADDR', main_info['addr']) + '\n' - try: - with open(STARTUP_TEXT) as f: - if f.read() == text: - return - except FileNotFoundError: - pass - print('new startup text:') - print(text) - with open(STARTUP_TEXT + '.todo', 'w') as f: - f.write(text) + if write_when_new(STARTUP_TEXT, text): + print('change startup text') + if doit: + os.rename(STARTUP_TEXT, STARTUP_TEXT + '.todo') + return True + return False def display(**opts): @@ -230,7 +234,8 @@ def write_when_new(filename, content, ignore_reduction=False): fil.write(content) else: os.remove(filename) - elif filename.endswith(more_info): + elif more_info: + print('.' * 80) print('changes in', filename) old = [] if old is None else old.split('\n') top_lines = 0 # in case of empty loop @@ -254,6 +259,7 @@ def write_when_new(filename, content, ignore_reduction=False): print('>>>') print('\n'.join(old[-bottom_lines:-1])) print("===") + print('.' * 80) return content @@ -344,20 +350,21 @@ class Show(Walker): def diff(self, title, files): self.dirty = True - if more_info == 'NONE': - print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) - else: + if more_info: for f in files: if f.endswith(more_info): print('diff %s %s' % (join(self.dirpath, f), join(self.syspath, f))) + print('.' * 80) + else: + print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) def show(self, title, dirpath, files): - if more_info == 'NONE': - print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) - else: + if more_info: for f in files: - if f.endswith(more_info): - print('cat %s' % join(dirpath, f)) + print('cat %s' % join(dirpath, f)) + print('.' * 80) + else: + print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) def newer(self, files): self.show('get from', self.dirpath, files) @@ -392,6 +399,7 @@ class Do(Walker): def handle_config(): cfgfile = None cfgfiles = [] + dhcp_server_cfg.clear() for file in glob(CFGPATH % ('*', apuid)): cfgfiles.append(file) for i in [1, 2, 3]: @@ -400,7 +408,7 @@ def handle_config(): print('cfg files found with bad apu id (use net addr of leftmost plug)') print(bad) return False - newhostname = main['hostname'] + newhostname = main_info['hostname'] if not cfgfiles: # determine if display is present disp = serial.Serial('/dev/ttyS1', baudrate=115200, timeout=1) @@ -428,7 +436,7 @@ def handle_config(): print('ERROR: ambiguous cfg files: %s' % ', '.join(cfgfiles)) else: cfgfile = cfgfiles[0] - if cfgfile != CFGPATH % (hostname, apuid): + if cfgfile != CFGPATH % (newhostname, apuid): if cfgfile: newhostname = basename(cfgfile).rpartition('_')[0] if doit: @@ -445,12 +453,12 @@ def handle_config(): parser = ConfigParser() try: parser.read(cfgfile) - network = dict(parser['NETWORK']) + network = {ifname_mapping.get(k, k): v for k, v in parser['NETWORK'].items()} for ifname in net_addr: content = create_if(ifname, network.pop(ifname, 'off')) content = '\n'.join('%s=%s' % kv for kv in content.items()) todo = write_when_new('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname, content) - if todo and more_info == 'NONE': + if todo and not more_info: print('change', ifname) show.dirty = True to_start[ifname] = 'if_restart' @@ -472,7 +480,7 @@ def handle_config(): if todo: print('change dhcpd.conf') to_start['dhcpd'] = 'restart' - show.dirty = True + show.dirty = True elif doit: unix_cmd('systemctl stop dhcpd') unix_cmd('systemctl disable dhcpd') @@ -485,7 +493,8 @@ def handle_config(): content.append(COMMENT) section_dict = dict(parser[section].items()) if section == 'DISPLAY': - display_update(section_dict) + if display_update(section_dict): + to_start['display'] = 'restart' for key, value in section_dict.items(): content.append('%s=%s' % (key, value)) content.append('') @@ -551,30 +560,30 @@ def handle_config(): return '\n'.join(result) -more_info = 'NONE' -while True: - doit = False - print('---') - show = Show() - walk(show) - result = handle_config() +more_info = False +doit = False +print(' ') +show = Show() +walk(show) +result = handle_config() - if not result: - print('fix first above errors') - break - if show.dirty and more_info == 'NONE': +if not result: + print('fix first above errors') +else: + if show.dirty: print('---') - answer = input('enter y(es) to do above or a filename to check changes? ') - doit = True - if answer.lower() in {'y', 'yes'}: + answer = input('enter "y(es)" to do above or "m(ore)" for more info: ') + if not answer: + print('cancel') + elif 'yes'.startswith(answer.lower()): + doit = True handle_config() walk(Do()) with open('/root/aputools/current', 'w') as f: f.write(result) - break - if not answer: - print('cancel') - break + elif 'more'.startswith(answer.lower()): + more_info = True + walk(show) + result = handle_config() else: print('nothing to do') - break diff --git a/servercfg/linse-box1_5b265c.cfg b/servercfg/linse-box1_5b265c.cfg index 3a8dcbf..f808e75 100644 --- a/servercfg/linse-box1_5b265c.cfg +++ b/servercfg/linse-box1_5b265c.cfg @@ -1,12 +1,10 @@ [NETWORK] ; please refer to README.md for help -enp1s0=dhcp -enp2s0=192.168.1.1 -enp3s0=192.168.2.2 -enp4s0=192.168.3.3 +eth0=dhcp +eth1=192.168.1.1 +eth2=192.168.2.2 +eth3=192.168.3.3 [DISPLAY] ; please refer to README.md for help -line0=startup... -line1=HOST: linse-box1 -line2=ADDR: 00:0d:b9:5b:26:5c +startup_text=startup...|HOST|ADDR