mirror of
https://github.com/janishutz/BiogasControllerApp.git
synced 2025-11-25 22:04:24 +00:00
Improve Com class, continue writing test
This commit is contained in:
@@ -1,32 +1,26 @@
|
||||
from kivy.uix.screenmanager import Screen
|
||||
from kivy.lang import Builder
|
||||
from gui.popups.popups import QuitPopup, TwoActionPopup
|
||||
from lib.com import Com
|
||||
from lib.com import ComSuperClass
|
||||
|
||||
import configparser
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('./config.ini')
|
||||
|
||||
# This is the launch screen, i.e. what you see when you start up the app
|
||||
class HomeScreen(Screen):
|
||||
def __init__(self, com: Com, **kw):
|
||||
def __init__(self, com: ComSuperClass, **kw):
|
||||
self._com = com;
|
||||
super().__init__(**kw)
|
||||
|
||||
# Go to the main screen if we can establish connection or the check was disabled
|
||||
# in the configs
|
||||
def start(self):
|
||||
if config[ 'Dev Settings' ][ 'disableconnectioncheck' ] != "True":
|
||||
if self._com.connect():
|
||||
self.manager.current = 'main'
|
||||
self.manager.transition.direction = 'right'
|
||||
else:
|
||||
TwoActionPopup().open('Failed to connect', 'Details', self.open_details_popup)
|
||||
print('ERROR connecting')
|
||||
else:
|
||||
if self._com.connect():
|
||||
self.manager.current = 'main'
|
||||
self.manager.transition.direction = 'right'
|
||||
else:
|
||||
TwoActionPopup().open('Failed to connect', 'Details', self.open_details_popup)
|
||||
print('ERROR connecting')
|
||||
|
||||
# Open popup for details as to why the connection failed
|
||||
def open_details_popup(self):
|
||||
|
||||
@@ -10,7 +10,7 @@ import threading
|
||||
|
||||
# Load utilities
|
||||
from lib.instructions import Instructions
|
||||
from lib.com import Com
|
||||
from lib.com import ComSuperClass
|
||||
from lib.decoder import Decoder
|
||||
|
||||
|
||||
@@ -26,13 +26,13 @@ synced_queue: queue.Queue[List[str]] = queue.Queue()
|
||||
# ╰────────────────────────────────────────────────╯
|
||||
# Using a Thread to run this in parallel to the UI to improve responsiveness
|
||||
class ReaderThread(threading.Thread):
|
||||
_com: Com
|
||||
_com: ComSuperClass
|
||||
_decoder: Decoder
|
||||
_instructions: Instructions
|
||||
|
||||
# This method allows the user to set Com object to be used.
|
||||
# The point of this is to allow for the use of a single Com object to not waste resources
|
||||
def set_com(self, com: Com):
|
||||
def set_com(self, com: ComSuperClass):
|
||||
"""Set the Com object to be used in this
|
||||
|
||||
Args:
|
||||
@@ -41,6 +41,7 @@ class ReaderThread(threading.Thread):
|
||||
self._com = com
|
||||
self._run = True
|
||||
self._decoder = Decoder()
|
||||
self._instructions = Instructions(com)
|
||||
|
||||
# This method is given by the Thread class and has to be overriden to change
|
||||
# what is executed when the thread starts
|
||||
@@ -98,7 +99,7 @@ class MainScreen(Screen):
|
||||
|
||||
# The constructor if this class takes a Com object to share one between all screens
|
||||
# to preserve resources and make handling better
|
||||
def __init__(self, com: Com, **kw):
|
||||
def __init__(self, com: ComSuperClass, **kw):
|
||||
# Set some variables
|
||||
self._com = com
|
||||
self._event = None
|
||||
@@ -117,9 +118,11 @@ class MainScreen(Screen):
|
||||
def start(self):
|
||||
self.ids.status.text = "Connecting..."
|
||||
if self._com.connect():
|
||||
print("Acquired connection")
|
||||
self._has_connected = True
|
||||
# Start communication
|
||||
self._reader.start()
|
||||
print("Reader has started")
|
||||
Clock.schedule_interval(self._update_screen, 0.5)
|
||||
else:
|
||||
self.ids.status.text = "Connection failed"
|
||||
@@ -145,10 +148,18 @@ class MainScreen(Screen):
|
||||
pass
|
||||
self._com.close()
|
||||
self.ids.status.text = "Connection terminated"
|
||||
print("Connection terminated")
|
||||
|
||||
# A helper function to update the screen. Is called on an interval
|
||||
def _update_screen(self):
|
||||
update = synced_queue.get()
|
||||
def _update_screen(self, dt):
|
||||
update = []
|
||||
try:
|
||||
update = synced_queue.get_nowait()
|
||||
except:
|
||||
pass
|
||||
if len(update) == 0:
|
||||
# There are no updates to process, don't block and simply try again next time
|
||||
return
|
||||
if len(update) == 1:
|
||||
if update[0] == "ERR_HOOK":
|
||||
self.ids.status.text = "Hook failed"
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import Callable
|
||||
from kivy.uix.popup import Popup
|
||||
from kivy.lang import Builder
|
||||
|
||||
from lib.com import Com
|
||||
from lib.com import ComSuperClass
|
||||
|
||||
|
||||
# Just an empty function
|
||||
@@ -14,7 +14,7 @@ def empty_func():
|
||||
# ╰────────────────────────────────────────────────╯
|
||||
# Below, you can find various popups with various designs that can be used in the app
|
||||
class QuitPopup(Popup):
|
||||
def __init__(self, com: Com, **kw):
|
||||
def __init__(self, com: ComSuperClass, **kw):
|
||||
self._com = com;
|
||||
super().__init__(**kw)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<ProgramScreen>:
|
||||
name: "program"
|
||||
on_pre_enter: self.config_loader = root.load_config()
|
||||
on_enter: self.config_loader = root.load_config()
|
||||
canvas.before:
|
||||
Color:
|
||||
rgba: (50,50,50,0.2)
|
||||
|
||||
@@ -4,17 +4,17 @@ from kivy.lang import Builder
|
||||
from lib.decoder import Decoder
|
||||
from lib.instructions import Instructions
|
||||
from gui.popups.popups import SingleRowPopup, TwoActionPopup, empty_func
|
||||
from lib.com import Com
|
||||
from lib.com import ComSuperClass
|
||||
from kivy.clock import Clock
|
||||
|
||||
|
||||
# The below list maps 0, 1, 2, 3 to a, b, c and t respectively
|
||||
# This is used to set and read values of the UI
|
||||
name_map = [ "a", "b", "c", "t" ]
|
||||
name_map = ["a", "b", "c", "t"]
|
||||
|
||||
|
||||
class ProgramScreen(Screen):
|
||||
def __init__(self, com: Com, **kw):
|
||||
def __init__(self, com: ComSuperClass, **kw):
|
||||
self._com = com
|
||||
self._instructions = Instructions(com)
|
||||
self._decoder = Decoder()
|
||||
@@ -31,7 +31,18 @@ class ProgramScreen(Screen):
|
||||
# Load config for all four sensors
|
||||
for _ in range(4):
|
||||
# Receive 28 bytes of data
|
||||
received = self._com.receive(28)
|
||||
received = bytes()
|
||||
try:
|
||||
received = self._com.receive(28)
|
||||
except:
|
||||
TwoActionPopup().open(
|
||||
"Failed to connect to micro-controller, retry?",
|
||||
"Cancel",
|
||||
empty_func,
|
||||
"Retry",
|
||||
lambda: self._load(0),
|
||||
)
|
||||
return
|
||||
|
||||
# Create a list of strings to store the config for the sensor
|
||||
# This list has the following elements: a, b, c, temperature
|
||||
@@ -39,7 +50,9 @@ class ProgramScreen(Screen):
|
||||
|
||||
# Create the list
|
||||
for j in range(4):
|
||||
config_sensor_i.append(str(self._decoder.decode_float(received[7 * j:7 * j + 6])))
|
||||
config_sensor_i.append(
|
||||
str(self._decoder.decode_float(received[7 * j : 7 * j + 6]))
|
||||
)
|
||||
|
||||
# Add it to the config
|
||||
config.append(config_sensor_i)
|
||||
@@ -56,12 +69,14 @@ class ProgramScreen(Screen):
|
||||
def _set_ui(self, config: List[List[str]]):
|
||||
for sensor_id in range(4):
|
||||
for property in range(4):
|
||||
self.ids[f"s{sensor_id + 1}_{name_map[property]}"].text = config[sensor_id][property]
|
||||
self.ids[f"s{sensor_id + 1}_{name_map[property]}"].text = config[
|
||||
sensor_id
|
||||
][property]
|
||||
|
||||
# Read values from the UI. Returns the values as a list or None if the check was infringed
|
||||
def _read_ui(self, enforce_none_empty: bool = True) -> List[float] | None:
|
||||
data: List[float] = []
|
||||
|
||||
|
||||
# Iterate over all sensor config input fields and collect the data
|
||||
for sensor_id in range(4):
|
||||
for property in range(4):
|
||||
|
||||
Reference in New Issue
Block a user