fix issues with apu

rework: create utils.BoxInfo
This commit is contained in:
2024-03-20 08:47:47 +01:00
parent 9c9719bff4
commit 7f661bb314
4 changed files with 92 additions and 120 deletions

View File

@ -14,37 +14,51 @@ else:
os.system(cmd)
def get_config(section=None):
"""get content of box configuration
class BoxInfo:
TOOLS = '/home/l_samenv/boxtools'
CFGPATH = f'{TOOLS}/cfg/%s_%06x.cfg'
BOX_TYPES = {
'00:0d:b9': 'apu', # bare apu or control box
'b8:27:eb': 'cm3', # iono pi
'd8:3a:dd': 'cm4', # dual-eth-rpi
}
:param section: if not None, return only given section
:return: configuration as dict
"""
parser = ConfigParser()
cfgfiles = glob('/home/l_samenv/boxtools/cfg/%s_*.cfg' % socket.gethostname())
if not cfgfiles:
# look by id
boxid = None
for ifname in ('enp1s0', 'eth0'):
try:
with open(f'/sys/class/net/{ifname}/address') as f:
addr = f.read().strip().lower()
except FileNotFoundError:
def __init__(self):
self.id = None
self.typ = None
self.hostname = socket.gethostname()
self.change_if_names = False
self.cfgfile = None
self.config = None
self.macaddr = {}
self.main_if = None
for ifdev in glob('/sys/class/net/*/address'):
ifname = ifdev.split('/')[-2]
if ifname == 'lo': # do not consider loopback interface
continue
boxid = int(''.join(addr.split(':')[-3:]), 16) & 0xffffff
break
if boxid:
cfgfiles = glob('/home/l_samenv/boxtools/cfg/*_%6.6x.cfg' % boxid)
if len(cfgfiles) != 1:
raise ValueError('there must be one and only one single cfgfile %r' % cfgfiles)
parser.read(cfgfiles[0])
try:
result = {k: dict(parser[k]) for k in ([section] if section else parser.sections())}
except KeyError:
return {}
if section:
return result[section]
return result
if ifname.startswith('enp'):
self.change_if_names = True
ifname = f'eth{int(ifname[3]) - 1}'
print(ifname)
with open(ifdev) as f:
self.macaddr[ifname] = addr = f.read().strip().lower()
if ifname in ('eth0', 'enp1s0'):
self.id = int(''.join(addr.split(':')[-3:]), 16) & 0xffffff
self.typ = self.BOX_TYPES.get(addr[:8])
self.main_if = ifname
def get_macaddr(self):
return self.macaddr.get(self.main_if)
def read_config(self):
cfgfiles = glob(self.CFGPATH % ('*', self.id))
if len(cfgfiles) != 1:
raise ValueError('there must be one and only one single cfgfile %r' % cfgfiles)
if cfgfiles:
self.cfgfile = cfgfiles[0]
parser = ConfigParser()
parser.read(self.cfgfile)
return {k: dict(parser[k]) for k in parser.sections()}
def gethostthread(ip, event, result):