Commit ef6b97c6 authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Refatorar sistema de log e adicionar modal

parent 5ae6567e
from random import random
from util.Logger import Logger
class Mock(object):
def __init__(self, logger, location, channels):
def __init__(self, location, channels):
self.logger = Logger()
self.logger.writeLog("mock", "teste!!!")
self.logger.writeLog("mock", "deu certo!!!")
self.channels = len(channels)
logger("[MOCK]", "Generating fake data...")
self.reading = 10
def read(self, channel):
......
from random import random
from time import sleep
from sacada import SACADA as _SACADA
from util.Logger import Logger
class SACADA(object):
def __init__(self, logger, location, channels):
def __init__(self, location, channels):
self.logger = Logger()
self.channels = channels
logger("[SACADA]", "Opening SACADA at {}".format(location))
self.s = _SACADA(location)
self.s.sendSCPICommand("IN:SCALE A0,V0256")
logger("[SACADA]", "Opening SACADA at {}".format(location))
logger("[SACADA]", str(self.s.identify()))
logger("[SACADA]", "Valor do zero: {}".format(self.s.zero()))
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>460</width>
<height>422</height>
</rect>
</property>
<property name="windowTitle">
<string>Configuração da aquisição</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>370</y>
<width>401</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>19</x>
<y>9</y>
<width>421</width>
<height>351</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="amostraLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Amostra:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="amostraEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="pressaoLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Pressão</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="temperaturaLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Temperatura</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="pressaoEdit"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="temperaturaEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="gaxetaLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Gaxeta</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="gaxetaEdit"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="operadorLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Operador</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="operadorEdit"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="observacoesLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Observações</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QTextEdit" name="observacoesEdit"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
from util.Logger import Logger
class ConfigThread(object):
def __init__(self, ui):
self.ui = ui
self.logger = Logger()
self.ui.buttonBox.accepted.connect(self.save)
self.ui.buttonBox.rejected.connect(self.quit)
def save(self):
self.logger.writeLog("dados", "Amostra: " + self.ui.amostraEdit.text())
self.logger.writeLog("dados", "Pressão: " + self.ui.pressaoEdit.text())
self.logger.writeLog("dados", "Temperatura: " + self.ui.temperaturaEdit.text())
self.logger.writeLog("dados", "Gaxeta: " + self.ui.gaxetaEdit.text())
self.logger.writeLog("dados", "Operador: " + self.ui.operadorEdit.text())
self.logger.writeLog("dados", "Observações: " + self.ui.observacoesEdit.toPlainText())
def quit(self):
print("Quit!")
......@@ -4,12 +4,13 @@ from PyQt5.QtCore import QTimer
from pyqtgraph import AxisItem, InfiniteLine, mkColor, mkPen, setConfigOption
from interfaces import *
from config import Config
from util.Logger import Logger
import pyqtgraph as pg
class Graph():
def __init__(self, ui, interval, logger, parent=None):
def __init__(self, ui, interval, parent=None):
self.running = True
self.logger = logger
self.logger = Logger()
self.interval = interval#/1000.0
self.dataX = []
self.dataY = []
......@@ -17,26 +18,22 @@ class Graph():
self.graph = ui.mainGraph
self.plots = []
self.axis = []
try:
self.config = Config().data
except FileNotFoundError:
logger("[FATAL]", "Arquivo de configuração não encontrado!")
#try:
self.config = Config().data
#except FileNotFoundError:
# logger("[FATAL]", "Arquivo de configuração não encontrado!")
self.device = self.config["device"]
today = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
home = str(Path.home())
self.logFile = open("{}/logs/{}-calib.csv".format(home, today), "w")
logger("[LOGS]", "Salvando dados em: {}/logs/{}-calib.csv".format(home, today))
self._configurePlots()
self.lastupdated = 0
pg.setConfigOptions(useOpenGL=True)
try:
self.interface = INTERFACES[self.device["type"]](logger, self.device["location"], self.device["channels"])
except:
self.running = False
logger("[FATAL]", "Erro ao abrir a interface!")
#try:
self.interface = INTERFACES[self.device["type"]](self.device["location"], self.device["channels"])
#except:
# self.running = False
# logger("[FATAL]", "Erro ao abrir a interface!")
print(ui.samplingBox)
ui.min_amostra.valueChanged.connect(self.updateScale)
......@@ -47,16 +44,14 @@ class Graph():
self.graph.setBackground((240, 240, 240))
def updateGraphs(self):
self.logFile.write("\n")
for i, channel in enumerate(self.device["channels"]):
self.dataX[i].append(self.dataX[i][-1] + self.interval/1000.0)
self.dataY[i].append(self.interface.read(self.device["channels"][i]))
#self.plots[i].getViewBox().setRange(xRange=(self.dataX[0][-1] - 10, self.dataX[0][-1]))
self.plots[i].setData(self.dataX[i], self.dataY[i])
self.logFile.write("{},".format(self.dataY[i][-1]))
self.combinedPlot.setData(self.dataY[1], self.dataY[0])
self.logFile.write("{}".format(self.dataX[0][-1]))
self.logFile.flush()
dados = "{}, {}, {}".format(self.dataY[0][-1], self.dataY[1][-1], self.dataX[0][-1])
self.logger.writeLog("acquisicao", dados, isCSV=True)
self.ui.forceLabel.setText("{:.0f} bar".format(self.dataY[1][-1]))
self.ui.calibratorLabel.setText("{:.2f} mV".format(self.dataY[0][-1] * 1000))
......@@ -67,7 +62,7 @@ class Graph():
minY = self.ui.min_amostra.value() / 1000.0
maxX = self.ui.max_pressao.value()
maxY = self.ui.max_amostra.value() / 1000.0
self.combinedPlot.getViewBox().setRange(xRange=(minX, maxX), yRange=(minY, maxY))
def setInterval(self, interval):
......@@ -84,8 +79,10 @@ class Graph():
self.graph.getPlotItem().addItem(marker)
def _configurePlots(self):
self.logger("[GRAPH]", "Found new device {}".format(self.config["device"]["type"]))
self.debugLayout = self.graph.addLayout()
header = ""
for i, channel in enumerate(self.device["channels"]):
plotItem = self.debugLayout.addPlot(row=i, col=1)
plotItem.setMenuEnabled(False) # Previne acesso ao menu do pyqtgraph pelos usuários
......@@ -96,15 +93,15 @@ class Graph():
self.dataX.append([0])
self.dataY.append([0])
plotItem.setLabel("left", text=channel["name"], units=channel["unit"])
self.logger("[GRAPH]", "Added channel {}".format(channel["name"]))
self.logFile.write("{},".format(channel["name"]))
header += "{},".format(channel["name"])
self.ui.min_amostra.setSuffix(self.device["channels"][0]["unit"])
self.ui.min_pressao.setSuffix(self.device["channels"][1]["unit"])
self.ui.max_amostra.setSuffix(self.device["channels"][0]["unit"])
self.ui.max_pressao.setSuffix(self.device["channels"][1]["unit"])
self.logFile.write("timestamp")
header += "timestamp"
self.logger.writeLog("acquisicao", header, isCSV=True)
# Faz com que ambos eixos X se movam juntos sempre
# TODO: escrever de forma mais limpa
......
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'resources/UI/ConfigDialog.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setWindowModality(QtCore.Qt.ApplicationModal)
Dialog.resize(460, 422)
Dialog.setModal(True)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 370, 401, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setCenterButtons(False)
self.buttonBox.setObjectName("buttonBox")
self.formLayoutWidget = QtWidgets.QWidget(Dialog)
self.formLayoutWidget.setGeometry(QtCore.QRect(19, 9, 421, 351))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.amostraLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.amostraLabel.setFont(font)
self.amostraLabel.setObjectName("amostraLabel")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.amostraLabel)
self.amostraEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.amostraEdit.setObjectName("amostraEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.amostraEdit)
self.pressaoLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pressaoLabel.setFont(font)
self.pressaoLabel.setObjectName("pressaoLabel")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.pressaoLabel)
self.temperaturaLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.temperaturaLabel.setFont(font)
self.temperaturaLabel.setObjectName("temperaturaLabel")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.temperaturaLabel)
self.pressaoEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.pressaoEdit.setObjectName("pressaoEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.pressaoEdit)
self.temperaturaEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.temperaturaEdit.setObjectName("temperaturaEdit")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.temperaturaEdit)
self.gaxetaLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.gaxetaLabel.setFont(font)
self.gaxetaLabel.setObjectName("gaxetaLabel")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.gaxetaLabel)
self.gaxetaEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.gaxetaEdit.setObjectName("gaxetaEdit")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.gaxetaEdit)
self.operadorLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.operadorLabel.setFont(font)
self.operadorLabel.setObjectName("operadorLabel")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.operadorLabel)
self.operadorEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.operadorEdit.setObjectName("operadorEdit")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.operadorEdit)
self.observacoesLabel = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.observacoesLabel.setFont(font)
self.observacoesLabel.setObjectName("observacoesLabel")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.observacoesLabel)
self.observacoesEdit = QtWidgets.QTextEdit(self.formLayoutWidget)
self.observacoesEdit.setObjectName("observacoesEdit")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.observacoesEdit)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Configuração da aquisição"))
self.amostraLabel.setText(_translate("Dialog", "Amostra:"))
self.pressaoLabel.setText(_translate("Dialog", "Pressão"))
self.temperaturaLabel.setText(_translate("Dialog", "Temperatura"))
self.gaxetaLabel.setText(_translate("Dialog", "Gaxeta"))
self.operadorLabel.setText(_translate("Dialog", "Operador"))
self.observacoesLabel.setText(_translate("Dialog", "Observações"))
......@@ -2,7 +2,9 @@ import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer
from .GenericoW import Ui_Generico
from .ConfigDialog import Ui_Dialog
from threads.MainThread import MainThread
from threads.ConfigThread import ConfigThread
class Ui_MainWindow(object):
......@@ -12,15 +14,19 @@ class Ui_MainWindow(object):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) #use highdpi icons
self.app = QtWidgets.QApplication(sys.argv)
self.dialog = QtWidgets.QMainWindow()
self.mainWindow = QtWidgets.QMainWindow()
self.dialog = QtWidgets.QDialog(self.mainWindow)
self.ui = Ui_Generico()
self.ui.setupUi(self.dialog)
self.ui.setupUi(self.mainWindow)
print(self.ui)
self.configDialog = Ui_Dialog()
self.configDialog.setupUi(self.dialog)
self.configThread = ConfigThread(self.configDialog)
self.dialog.exec_()
self.timer = QTimer(self.app)
self.main_thread = MainThread(self.ui, self.timer)
self.dialog.showMaximized()
self.mainWindow.showMaximized()
#main_thread.finished.connect(self.app.exit)
#main_thread.start()
# Essa classe deve ser usada como singleton
# O objetivo dela é prover uma interface centralizada para salvar os dados
# das aquisições de forma organizada
#
# Todos os arquivos de log devem ser gerados através dela para manter consistência
# Para conhecer mais sobre o pattern de singleton utilizado, acesse
# https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html
# Classe externa, apenas interface para a interna
from datetime import datetime
from pathlib import Path
class Logger(object):
# Classe de fato
# Essa é uma classe privada que não pode ser acessada diretamente
class __Logger(object):
def __init__(self):
today = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
home = Path.home()
self.logDir = Path.home() / "logs" / today
self.logDir.mkdir(exist_ok=True)
self.logs = {}
def writeLog(self, name, text, timestamp=False, isCSV = False):
if not name in self.logs:
path = self.logDir / name
if isCSV:
path = path.with_suffix(".csv")
else:
path = path.with_suffix(".txt")
self.logs[name] = path.open("a")
log = self.logs[name]
if timestamp:
now = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
log.write("[{}] {} \n".format(now, text))
else:
log.write("{} \n".format(text))
def __del__(self):
for key in self.logs:
self.logs[key].close()
instance = None
def __init__(self):
# Se não existe um Logger já criado, cria um
if not Logger.instance:
Logger.instance = Logger.__Logger()
def __getattr__(self, name):
return getattr(self.instance, name)
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