-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·86 lines (77 loc) · 3.78 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
from PyQt5.QtWidgets import QApplication
import importlib
import os
from nexus import Bus, BusRider, GenericSensor, GenericActuator
from gui import MainWindow, DeviceRow, AutoPoller, AutoPollerRow, QLoggingHandler
import settings
# Should be compatable with any slcan CANBus interface on Linux
import logging
log = logging.getLogger(__name__)
if __name__ == '__main__':
formatstr = "%(asctime)s [%(name)-16.16s] [%(levelname)-5.5s] %(message)s"
consolehandler = QLoggingHandler()
consolehandler.setFormatter(logging.Formatter(formatstr))
if not os.path.isdir("log"):
os.mkdir("log")
logging.basicConfig(
level=logging.DEBUG,
format=formatstr,
handlers=[
logging.FileHandler("log/debug.log"),
logging.StreamHandler(),
consolehandler
]
)
log.debug("Hi!")
# Set up all the things
with Bus(settings.sender, settings.bitrate, packetprinting=False, packetlogging=False) as bus:
with AutoPoller(bus=bus, interval=0.5, autostart=False) as ap:
app = QApplication(sys.argv)
window = MainWindow(loghandler=consolehandler, autopoller=ap)
# Load all devices from settings
for deviceDesc in settings.devices:
# We don't know what type the device is, so try a bunch of devices and see if we can find it
deviceClass = None
devicePrefix = None
for prefix in ["sensors", "actuators", "nexus"]:
try:
m = importlib.import_module(prefix)
deviceClass = getattr(m, deviceDesc["class"])
break
except Exception as e:
continue
# Check if we found the class
if deviceClass is None:
raise ImportError(f"Cannot find a device of type {deviceDesc['class']} to add")
# Make sure the class is an allowable class
if not issubclass(deviceClass, BusRider):
raise ValueError(f"Device {deviceClass.__name__} must extend BusRider")
# Prepare device configuration
config = deviceDesc["config"] if "config" in deviceDesc else {}
# Initialize the device
device = deviceClass(deviceDesc["address"], deviceDesc["name"], **config)
bus.addRider(device)
# Find the display for the device
isVisibleOnList = deviceDesc["display"] is not None and deviceDesc["display"] != 'None'
display = None
if isVisibleOnList:
deviceDisplayClass = None
# Search for the class
for prefix in ["sensorgui", "actuatorgui"]:
try:
m = importlib.import_module(prefix)
deviceDisplayClass = getattr(m, deviceDesc["display"])
except Exception as e:
continue
# Check if we actually found a class
if deviceDisplayClass is None:
raise ImportError(f"Cannot find a display of type {deviceDesc['display']} to add")
# Make sure the class is an allowable class
if not issubclass(deviceDisplayClass, DeviceRow):
raise ValueError(f"Device {deviceDisplayClass.__name__} must extend DeviceRow")
displayConfig = deviceDesc["displayConfig"] if "displayConfig" in deviceDesc else {}
display = deviceDisplayClass(device, **displayConfig)
window.addDevice(device, display)
window.show()
app.exec()