import logging
import multiprocessing

from BusClient import BusClient
from nodes.Beamformer import Beamformer
from nodes.Broker import Broker
from nodes.Device import Device
from nodes.ImageCV import ImageCV
from nodes.ImageFFMPEG import ImageFFMPEG
from nodes.JoyStick import Joystick
from nodes.Loader import Loader
from nodes.Mi import Mi
from nodes.Midi import Midi
from nodes.Monitor import Monitor
from nodes.Muxer import Muxer
from nodes.Recorder import Recorder
from nodes.Robot import Robot
from qtonly import kde_pyqt6_mainui
from utils.Msg import KillMsg

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    multiprocessing.set_start_method('spawn')
    multiprocessing.Pool()
    pps = []
    ps = [
        Broker(),
        kde_pyqt6_mainui,
        Device(),
        ImageFFMPEG(),
        ImageCV(level=logging.DEBUG),
        Beamformer(level=logging.DEBUG),
        Loader(),
        Muxer(level=logging.DEBUG),
        # Midi(),
        Joystick(),
        Robot(),
        Recorder(),
        # Monitor(),
        Mi(),
    ]
    for p in ps:
        pps.append(multiprocessing.Process(target=p))
    for p in pps:
        p.start()

    c = BusClient(KillMsg)
    while True:
        x: KillMsg = c.recv()
        if x.name == '':
            break
    for p in pps:
        p.kill()
