update: relay control

This commit is contained in:
Your Name 2024-11-15 10:06:55 +01:00
parent 846fc4d210
commit 746f4bfda5
3 changed files with 107 additions and 0 deletions

Binary file not shown.

View File

@ -28,6 +28,8 @@ import cv2
import os import os
import sainsmartrelay
# db_config = { # db_config = {
# 'user': 'dbUser', # 'user': 'dbUser',
# 'password': 'dbPassword', # 'password': 'dbPassword',
@ -459,6 +461,14 @@ class Ui_MainWindow(object):
self.graphicsView.setGeometry(QtCore.QRect(10, int(1080/2), 661, int(480*1.05))) # position and size of camera frame # int(640*1.05) self.graphicsView.setGeometry(QtCore.QRect(10, int(1080/2), 661, int(480*1.05))) # position and size of camera frame # int(640*1.05)
self.graphicsView.setObjectName("graphicsView") self.graphicsView.setObjectName("graphicsView")
# relay control
self.startSpotlightBtn = QtWidgets.QPushButton(self.centralwidget)
self.startSpotlightBtn.setGeometry(QtCore.QRect(700+200, int(1080/2)+100, 161, 25)) #int(1080/2)+100
self.startSpotlightBtn.setObjectName("startSpotlightBtn")
self.stopSpotlightBtn = QtWidgets.QPushButton(self.centralwidget)
self.stopSpotlightBtn.setGeometry(QtCore.QRect(700+200, int(1080/2)+150, 161, 25))
self.stopSpotlightBtn.setObjectName("stopSpotlightBtn")
#self.myTestLambda = lambda: self.worker.checkWaageStartSignal.emit(einzelteilID, self.teileZuViel, self.auftragsnummer) #self.myTestLambda = lambda: self.worker.checkWaageStartSignal.emit(einzelteilID, self.teileZuViel, self.auftragsnummer)
''' '''
@ -561,6 +571,10 @@ class Ui_MainWindow(object):
self.camWorkFlowcheckBox.setText(_translate("MainWindow", "Camera Workflow")) self.camWorkFlowcheckBox.setText(_translate("MainWindow", "Camera Workflow"))
# self.camWorkFlowcheckBox.clicked.connect(self.onCheckboxCheck) # self.camWorkFlowcheckBox.clicked.connect(self.onCheckboxCheck)
self.modelComboBox self.modelComboBox
# relay control
self.startSpotlightBtn.setText(_translate("MainWindow", "Turn on light"))
self.stopSpotlightBtn.setText(_translate("MainWindow", "Turn off light"))
@ -1045,6 +1059,46 @@ class CameraStreamApp(QtWidgets.QMainWindow):
self.yolo_stream.cap.release() self.yolo_stream.cap.release()
event.accept() event.accept()
# new class for relay control
class RelayControl(QtWidgets.QMainWindow):
def __init__(self, ui):
super().__init__()
self.ui = ui
# self.timer = QtCore.QTimer(self)
# self.timer.timeout.connect(self.update_frame)
# # Populate the model dropdown menu
# self.populate_model_dropdown()
self.ui.startSpotlightBtn.clicked.connect(self.spot_on)
self.ui.stopSpotlightBtn.clicked.connect(self.spot_off)
# self.scene = QtWidgets.QGraphicsScene(self)
# self.ui.graphicsView.setScene(self.scene)
# def populate_model_dropdown(self):
# """Populate the dropdown menu with model files from the models directory."""
# models_dir = "models"
# model_files = [f for f in os.listdir(models_dir) if f.endswith(".pt")]
# self.ui.modelComboBox.clear()
# self.ui.modelComboBox.addItems(model_files)
def spot_on(self):
# Start the YOLOv8 camera stream (only if not already started)
# if self.yolo_stream is None:
# self.yolo_stream = YOLOv8CameraStream(model_path="models/yolov8m_seg_e300.pt", logging_level="high")
# # self.yolo_stream.start() # Start the YOLOv8 stream
# self.timer.start(30) # Start the timer to update the frame every 30ms (about 33 FPS)
sainsmartrelay.turn_on(1)
def spot_off(self):
# # Stop the camera stream and processing
# if self.yolo_stream is not None:
# self.timer.stop() # Stop the timer
# self.yolo_stream.cap.release() # Release the camera resource
# self.yolo_stream = None # Reset the YOLOv8 stream object
# self.scene.clear() # Clear the displayed frame from the graphicsView
# print("Camera stream stopped and resources released.")
sainsmartrelay.turn_off(1)
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys

53
sainsmartrelay.py Normal file
View File

@ -0,0 +1,53 @@
import subprocess
class SainsmartRelay:
def __init__(self, executable="sainsmartrelay"):
self.executable = executable
def _run_command(self, args):
try:
result = subprocess.run(
["sudo", self.executable] + args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr.strip()}")
return None
def turn_on(self, relays):
"""Turn on specified relays."""
relays_str = ','.join(map(str, relays)) if isinstance(relays, (list, tuple)) else str(relays)
return self._run_command(["--on", relays_str])
def turn_off(self, relays):
"""Turn off specified relays."""
relays_str = ','.join(map(str, relays)) if isinstance(relays, (list, tuple)) else str(relays)
return self._run_command(["--off", relays_str])
def get_status(self, relays="all"):
"""Get the status of specified relays."""
return self._run_command(["--status", str(relays)])
def find_all_devices(self):
"""Find all connected FTDI devices."""
return self._run_command(["--findall"])
# Example usage:
if __name__ == "__main__":
relay = SainsmartRelay()
# Turn on relay 1
print(relay.turn_on(1))
# Turn off all relays
print(relay.turn_off("all"))
# Get the status of all relays
print(relay.get_status())
# Find all FTDI devices
print(relay.find_all_devices())