Commit 7cccc2bd authored by AJ Fite's avatar AJ Fite 🛫

Work on making file handlers thread safe

parent 6602545c
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QGraphicsScene
import os
from PyQt5.QtCore import QThread, pyqtSignal
from watchdog.events import PatternMatchingEventHandler
from datetime import date
from .config import Config
from watchdog.observers import Observer
# Computes where the lepton grabber is currently dumping output
......@@ -18,57 +20,34 @@ class FileHandlerUtils:
# TODO: This needs to be thread safe
# Good example:
# https://github.com/yesworkflow-org/yw-gui/blob/master/main_ui.py
class ImageHandler(PatternMatchingEventHandler):
patterns = ["*.png"]
class ImageThread(QThread):
new_image = pyqtSignal(str)
def __init__(self, mainwindow):
super().__init__()
self.main = mainwindow
self.current = 0
self.sceneN = QGraphicsScene()
self.main.imgN.setScene(self.sceneN)
self.sceneNE = QGraphicsScene()
self.main.imgNE.setScene(self.sceneNE)
self.sceneE = QGraphicsScene()
self.main.imgE.setScene(self.sceneE)
self.sceneSE = QGraphicsScene()
self.main.imgSE.setScene(self.sceneSE)
self.sceneS = QGraphicsScene()
self.main.imgS.setScene(self.sceneS)
self.sceneSW = QGraphicsScene()
self.main.imgSW.setScene(self.sceneSW)
self.sceneW = QGraphicsScene()
self.main.imgW.setScene(self.sceneW)
self.sceneNW = QGraphicsScene()
self.main.imgNW.setScene(self.sceneNW)
self.sceneCenter = QGraphicsScene()
self.main.imgCenter.setScene(self.sceneCenter)
class ImageHandler(PatternMatchingEventHandler):
patterns = ["*.png"]
def on_created(self, event):
print("Update image")
pix = QPixmap(event.src_path)
if self.current == 0:
self.sceneN.addPixmap(pix)
elif self.current == 1:
self.sceneNE.addPixmap(pix)
elif self.current == 2:
self.sceneE.addPixmap(pix)
elif self.current == 3:
self.sceneSE.addPixmap(pix)
elif self.current == 4:
self.sceneS.addPixmap(pix)
elif self.current == 5:
self.sceneCenter.addPixmap(pix)
elif self.current == 6:
self.sceneSW.addPixmap(pix)
elif self.current == 7:
self.sceneW.addPixmap(pix)
elif self.current == 8:
self.sceneNW.addPixmap(pix)
self.current = (self.current + 1) % Config.dbg_lepton_set
def __init__(self, event_thread):
super().__init__()
self.event_thread = event_thread
def on_created(self, event):
print("Update image")
self.event_thread.new_image.emit(event.src_path)
def run(self):
datadir = FileHandlerUtils.compute_current_data_dir()
if not os.path.exists(datadir):
# Wait for lepton-grabber to make the directory rather than failing
# here or waiting for the data directory to be created just create
# it ourselves
os.mkdir(datadir)
observer = Observer()
observer.schedule(self.ImageHandler(self), path=datadir)
observer.start()
observer.join()
# TODO: This needs to be thread safe
......
#!/usr/bin/env python
import os
import sys
import webbrowser
from PyQt5 import uic
import pkg_resources
from PyQt5.QtCore import QProcess, QProcessEnvironment, Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
from watchdog.observers import Observer
from .filehandlers import FileHandlerUtils, ImageHandler
from .filehandlers import ImageThread
from .sudo import PasswordWindow
from .about import AboutDialog
from .config import Config, ConfigEditor
......@@ -15,7 +13,6 @@ from .config import Config, ConfigEditor
class MainWindow(QMainWindow):
simfProcess = QProcess()
observer = Observer()
def __init__(self):
super().__init__(flags=Qt.Window)
......@@ -27,6 +24,9 @@ class MainWindow(QMainWindow):
self.configdialog = None
self.aboutdialog = None
self.png_watcher = ImageThread()
self.png_watcher.new_image.connect(self.update_image)
# Register Events
self.startCapButton.clicked.connect(self.start_capture)
self.stopCapButton.clicked.connect(self.stop_capture)
......@@ -54,6 +54,9 @@ class MainWindow(QMainWindow):
self.cameraCount.display("OFF")
self.solarIrradiance.display("OFF")
def update_image(self, path):
print("New Image! " + path)
# Opens a link
@staticmethod
def open_link(link):
......@@ -106,19 +109,7 @@ class MainWindow(QMainWindow):
# Triggered when the QProcess that runs the lepton-grabber runs
def process_started(self):
self.button_toggle(True)
# Start the file observers
if not self.observer.is_alive():
self.observer.start()
datadir = FileHandlerUtils.compute_current_data_dir()
if not os.path.exists(datadir):
# Wait for lepton-grabber to make the directory rather than failing
# here or waiting for the data directory to be created just create
# it ourselves
os.mkdir(datadir)
self.observer.schedule(ImageHandler(self), path=datadir)
self.png_watcher.start()
# Triggered when the QProcess that runs the lepton-grabber dies for any
# reason
......@@ -128,8 +119,7 @@ class MainWindow(QMainWindow):
self.console_write()
self.console_write_line("Capture Ended")
# Unschedule the file observers while capture isn't running
self.observer.unschedule_all()
self.png_watcher.terminate()
# Fired when the observer started in process_started
# detects a new image from the lepton grabbers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment