Logging und Tracker GUI weiterprogrammiert
This commit is contained in:
104
obp60v.py
104
obp60v.py
@@ -124,7 +124,9 @@ cfg = {
|
||||
'cfgfile': 'obp60v.conf',
|
||||
'logdir': '~/.local/share/obp60v',
|
||||
'logfile': 'obp60v.log',
|
||||
'loglevel': 3,
|
||||
'imgpath': os.path.join(sys.path[0], 'images'),
|
||||
'audiopath': os.path.join(sys.path[0], 'audio'),
|
||||
'deviceid': 100,
|
||||
'manufcode': 2046, # Open Boat Projects (OBP)
|
||||
'devfunc': 120, # Display
|
||||
@@ -212,7 +214,7 @@ def rxd_gps(devname, devspeed):
|
||||
try:
|
||||
ser = serial.Serial(devname, devspeed, timeout=3)
|
||||
except serial.SerialException as e:
|
||||
print("GPS serial port not available")
|
||||
log.error("GPS serial port not available")
|
||||
return
|
||||
setthreadtitle("GPSlistener")
|
||||
while not appdata.shutdown:
|
||||
@@ -624,7 +626,7 @@ def init_profile(config, cfg, boatdata):
|
||||
cls = getattr(pages, p['type'])
|
||||
except AttributeError:
|
||||
# Klasse nicht vorhanden, Seite wird nicht benutzt
|
||||
print(f"Klasse '{p['type']}' nicht gefunden")
|
||||
log.error(f"Klasse '{p['type']}' nicht gefunden")
|
||||
continue
|
||||
c = cls(i, cfg, appdata, boatdata, *[v for v in p['values'].values()])
|
||||
clist[i] = c
|
||||
@@ -643,7 +645,7 @@ def set_loglevel(nr):
|
||||
nr = 0
|
||||
return level[nr]
|
||||
|
||||
def init_logging(logdir, logfile='obp60v.log'):
|
||||
def init_logging(logdir, logfile='obp60v.log', loglevel=logging.INFO):
|
||||
global log
|
||||
os.makedirs(logdir, exist_ok=True)
|
||||
log = logging.getLogger(os.path.basename(sys.argv[0]))
|
||||
@@ -651,30 +653,16 @@ def init_logging(logdir, logfile='obp60v.log'):
|
||||
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
|
||||
hdlr.setFormatter(formatter)
|
||||
log.addHandler(hdlr)
|
||||
log.setLevel(logging.INFO)
|
||||
log.setLevel(loglevel)
|
||||
console = logging.StreamHandler()
|
||||
console.setFormatter(logging.Formatter('%(levelname)s:%(message)s'))
|
||||
console.setLevel(logging.INFO)
|
||||
console.setLevel(loglevel)
|
||||
log.addHandler(console)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
setproctitle("obp60v")
|
||||
|
||||
# Globale Daten, u.a. auch Shutdown-Indikator
|
||||
appdata = AppData()
|
||||
|
||||
owndevice = Device(100)
|
||||
# Hardcoding device, not intended to change
|
||||
owndevice.manufacturercode = cfg['manufcode']
|
||||
owndevice.industrygroup = cfg['industrygroup']
|
||||
owndevice.deviceclass = cfg['devclass']
|
||||
owndevice.devicefunction = cfg['devfunc']
|
||||
|
||||
boatdata = BoatData()
|
||||
boatdata.addTank(0)
|
||||
boatdata.addEngine(0)
|
||||
|
||||
# Basiskonfiguration aus Datei lesen
|
||||
config = configparser.ConfigParser()
|
||||
config_path = os.path.join(sys.path[0], cfg['cfgfile'])
|
||||
@@ -683,11 +671,11 @@ if __name__ == "__main__":
|
||||
print("Konfigurationsdatei '{}' konnte nicht gelesen werden!".format(cfg['cfgfile']))
|
||||
sys.exit(1)
|
||||
cfg['_config'] = config # Objekt zum späteren schreiben
|
||||
cfg['loglevel'] = config.getint('system', 'loglevel')
|
||||
cfg['deviceid'] = config.getint('system', 'deviceid')
|
||||
cfg['simulation'] = config.getboolean('system', 'simulation')
|
||||
cfg['histpath'] = os.path.expanduser(config.get('system', 'histpath'))
|
||||
cfg['guistyle'] = config.get('system', 'guistyle')
|
||||
print("Setting GUI style to '{}'".format(cfg['guistyle']))
|
||||
cfg['mouseptr'] = config.getboolean('system', 'mouseptr')
|
||||
try:
|
||||
cfg['win_x'] = config.getint('gui', 'win_x')
|
||||
@@ -724,13 +712,15 @@ if __name__ == "__main__":
|
||||
boatdata.addHistory(history, "press")
|
||||
|
||||
# Tracker data
|
||||
cfg['tracker']['type'] = config.get('tracker', 'type')
|
||||
cfg['tracker']['type'] = config.get('tracker', 'type').upper()
|
||||
cfg['tracker']['host'] = config.get('tracker', 'host')
|
||||
cfg['tracker']['port'] = config.getint('tracker', 'port')
|
||||
cfg['tracker']['username'] = config.get('tracker', 'username')
|
||||
cfg['tracker']['password'] = config.get('tracker', 'password')
|
||||
cfg['tracker']['mqtt_host'] = config.get('tracker', 'mqtt_host')
|
||||
cfg['tracker']['mqtt_port'] = config.getint('tracker', 'mqtt_port')
|
||||
cfg['tracker']['mqtt_user'] = config.get('tracker', 'mqtt_user')
|
||||
cfg['tracker']['mqtt_pass'] = config.get('tracker', 'mqtt_pass')
|
||||
cfg['tracker']['orgname'] = config.get('tracker', 'orgname')
|
||||
cfg['tracker']['passcode'] = config.get('tracker', 'passcode')
|
||||
cfg['tracker']['logdir'] = cfg['logdir']
|
||||
cfg['tracker']['trace'] = config.getboolean('tracker', 'trace')
|
||||
|
||||
@@ -742,55 +732,76 @@ if __name__ == "__main__":
|
||||
cfg['boat']['club'] = config.get('boat', 'club')
|
||||
cfg['boat']['team'] = config.get('boat', 'team')
|
||||
|
||||
# Client UUID. Automatisch erzeugen wenn noch nicht vorhanden
|
||||
create_uuid = False
|
||||
try:
|
||||
cfg['tracker']['uuid'] = config.get('tracker', 'uuid')
|
||||
except configparser.NoOptionError:
|
||||
create_uuid = True
|
||||
if create_uuid or (len(cfg['tracker']['uuid']) != 36):
|
||||
cfg['tracker']['uuid'] = str(uuid.uuid4())
|
||||
config.set('tracker', 'uuid', cfg['tracker']['uuid'])
|
||||
with open(config_path, 'w') as fh:
|
||||
config.write(fh)
|
||||
|
||||
if cfg['simulation']:
|
||||
boatdata.enableSimulation()
|
||||
|
||||
# Protokollierung
|
||||
init_logging(os.path.expanduser(cfg['logdir']), cfg['logfile'])
|
||||
log.info("Logging initialized")
|
||||
loglevel = set_loglevel(cfg['loglevel'])
|
||||
init_logging(os.path.expanduser(cfg['logdir']), cfg['logfile'], loglevel)
|
||||
log.info("Client started")
|
||||
log.info("Setting GUI style to '{}'".format(cfg['guistyle']))
|
||||
|
||||
# Eindeutige Bootskennung UUID. Automatisch erzeugen wenn noch nicht vorhanden
|
||||
create_uuid = False
|
||||
try:
|
||||
cfg['boat']['uuid'] = config.get('boat', 'uuid')
|
||||
except configparser.NoOptionError:
|
||||
create_uuid = True
|
||||
if create_uuid or (len(cfg['boat']['uuid']) != 36):
|
||||
cfg['boat']['uuid'] = str(uuid.uuid4())
|
||||
config.set('boat', 'uuid', cfg['boat']['uuid'])
|
||||
with open(config_path, 'w') as fh:
|
||||
config.write(fh)
|
||||
log.info("Created new boat UUID: {}".format(cfg['boat']['uuid']))
|
||||
|
||||
# Globale Daten, u.a. auch Shutdown-Indikator
|
||||
appdata = AppData(log, cfg)
|
||||
|
||||
owndevice = Device(100)
|
||||
# Hardcoding device, not intended to change
|
||||
owndevice.manufacturercode = cfg['manufcode']
|
||||
owndevice.industrygroup = cfg['industrygroup']
|
||||
owndevice.deviceclass = cfg['devclass']
|
||||
owndevice.devicefunction = cfg['devfunc']
|
||||
|
||||
boatdata = BoatData()
|
||||
boatdata.addTank(0)
|
||||
boatdata.addEngine(0)
|
||||
|
||||
# Ggf. Simulationsdaten einschalten
|
||||
if cfg['simulation']:
|
||||
boatdata.enableSimulation()
|
||||
|
||||
# Gerät initialisieren u.a. mit den genutzten Seiten
|
||||
profile = init_profile(config, cfg, boatdata)
|
||||
|
||||
# Schnittstellen aktivieren
|
||||
# Schnittstellen aktivieren, jew. eigener Thread
|
||||
if cfg['can']:
|
||||
print("CAN enabled")
|
||||
log.info("CAN enabled")
|
||||
t_rxd_n2k = threading.Thread(target=rxd_n2k, args=(cfg['can_intf'],))
|
||||
t_rxd_n2k.start()
|
||||
if cfg['nmea0183']:
|
||||
print("NMEA0183 enabled, library version {}".format(pynmea2.version))
|
||||
log.info("NMEA0183 enabled, library version {}".format(pynmea2.version))
|
||||
t_rxd_0183 = threading.Thread(target=nmea0183.rxd_0183, args=(appdata,boatdata,cfg['0183_port'],))
|
||||
t_rxd_0183.start()
|
||||
if cfg['gps']:
|
||||
print("GPS enabled (local)")
|
||||
log.info("GPS enabled (local)")
|
||||
t_rxd_gps = threading.Thread(target=rxd_gps, args=(cfg['gps_port'],))
|
||||
t_rxd_gps.start()
|
||||
if cfg['network']:
|
||||
print("Networking enabled")
|
||||
log.info("Networking enabled")
|
||||
t_rxd_net = threading.Thread(target=rxd_network, args=(cfg['net_port'],cfg['net_addr']))
|
||||
t_rxd_net.start()
|
||||
if cfg['tracker']['type'] != 'NONE':
|
||||
appdata.track.set_type( cfg['tracker']['type'])
|
||||
log.info(f"Tracking enabled, mode {cfg['tracker']['type']}")
|
||||
#appdata.track.set_type( cfg['tracker']['type'])
|
||||
t_tracker = threading.Thread(target=appdata.track.mqtt_tracker, args=(cfg['tracker'],cfg['boat'],appdata,boatdata))
|
||||
t_tracker.start()
|
||||
if not cfg['simulation']:
|
||||
if cfg['bme280']:
|
||||
log.info("Environment sensor enabled")
|
||||
t_data = threading.Thread(target=datareader, args=(cfg, history))
|
||||
t_data.start()
|
||||
else:
|
||||
print("Simulation mode enabled")
|
||||
log.info("Simulation mode enabled")
|
||||
|
||||
app = Frontend(cfg, appdata, owndevice, boatdata, profile)
|
||||
app.run()
|
||||
@@ -808,5 +819,6 @@ if __name__ == "__main__":
|
||||
if not cfg['simulation'] and cfg['bme280']:
|
||||
t_data.join()
|
||||
|
||||
log.info("Client terminated")
|
||||
print(boatdata)
|
||||
print("Another fine product of the Sirius Cybernetics Corporation.")
|
||||
|
||||
Reference in New Issue
Block a user