import sys
import datetime
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtSql as qts
from PyQt5 import uic
MainWindow_Ui, MainWindow_Base = uic.loadUiType('CriticalAlarmListGUI.ui')
#class MainWindow(qtw.QMainWindow, Ui_MainWindow):
class MainWindow(MainWindow_Base, MainWindow_Ui):
events = {}
def __init__(self):
"""MainWindow constructor.
This widget will be the main window.
Define all the UI components in here.
"""
super().__init__()
# Main UI code goes here
self.setupUi(self)
# Connect to databases
#removed for security purposes
# Build the Query
StartDate = datetime.date(2020, 11, 1)
EndDate = datetime.date.today()
# Set the valid date range for the Calendar
self.Calendar.setDateRange(StartDate, EndDate)
# Format Dates for SQL
StartDate = "'" + str(StartDate.strftime("%m/%d/%Y")) + "'"
EndDate = "'" + str(EndDate.strftime("%m/%d/%Y")) + "'"
# Get the list of critical alarms within the date range
CriticalAlarms = self.get_Critical_Alarms(StartDate, EndDate)
# Loop through the Critical Alarm list and update the calendar date format of the results
for i in range(CriticalAlarms.rowCount()):
datetime_str = CriticalAlarms.record(i).value("EventStamp")
datetime_obj = datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S.%f0')
# Update the format of the date on the calendar day that the alarm occured
format.
setFont(qtg.
QFont('Times',
15)) date
= qtc.
QDate(datetime_obj
) self.Calendar.setDateTextFormat(date, format)
# Single Date Selected on the Calendar
self.Calendar.selectionChanged.connect(self.config_Critical_Alarms)
# Single record selected on the CriticalAlarmList widget
self.CriticalAlarmList.selectionModel().selectionChanged.connect(self.get_Critical_Alarm_Details)
# End main UI code
self.show()
def config_Critical_Alarms(self):
print ("config_Critical_Alarms")
CriticalAlarms = None
StartDate = self.Calendar.selectedDate()
EndDate = StartDate.addDays(1)
StartDate
= "'" + qtc.
QDate.
toString(StartDate,
'MM/dd/yyyy') + "'" EndDate
= "'" + qtc.
QDate.
toString(EndDate,
'MM/dd/yyyy') + "'" CriticalAlarms = self.get_Critical_Alarms(StartDate, EndDate)
def get_Critical_Alarms(self, StartDate, EndDate):
print ("get_Critical_Alarms")
alarm_state = "'UNACK_ALM'"
queryString = f'SELECT EventStamp, TagName, Description, Priority FROM v_AlarmHistory WHERE (AlarmState = {alarm_state}) '\
f'AND (EventStamp >= {StartDate}) AND (Priority <= 10) AND '\
f'(EventStamp <= {EndDate}) ORDER BY EventStamp DESC'
# Process the SQL Query
CriticalAlarms.setQuery(queryString)
self.CriticalAlarmList.setModel(CriticalAlarms)
self.CriticalAlarmList.resizeColumnsToContents()
self.StatusLabel.setText('Found {0} critical alarms between {1} and {2}'.format(CriticalAlarms.rowCount(), StartDate, EndDate))
return(CriticalAlarms)
def get_Critical_Alarm_Details(self, selected):
print ("get_Critical_Alarm_Details")
indexes = self.CriticalAlarmList.selectionModel().selectedRows()
model = self.CriticalAlarmList.model()
for index in indexes:
self.StatusLabel.setText('Selected row: {0}, Tag: {1}'.format(index.row(), model.data(model.index(index.row(), 1))))
def UiComponents(self, datetime_obj):
print ("UiComponents")
# Calendar day format
format.
setFont(qtg.
QFont('Times',
15)) date
= qtc.
QDate(datetime_obj
) value = self.Calendar.dateTextFormat()
self.label.setWordWrap(True)
self.label.setText("Date: " + str(value))
# Main
if __name__ == '__main__':
# Requirement to save a reference to MainWindow
# if it goes out of scope, it will be destroyed.
mw = MainWindow()
sys.exit(app.exec())
import sys
import datetime
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtSql as qts
from PyQt5 import uic
from PyQt5.QtSql import QSqlDatabase
MainWindow_Ui, MainWindow_Base = uic.loadUiType('CriticalAlarmListGUI.ui')
#class MainWindow(qtw.QMainWindow, Ui_MainWindow):
class MainWindow(MainWindow_Base, MainWindow_Ui):
events = {}
def __init__(self):
"""MainWindow constructor.
This widget will be the main window.
Define all the UI components in here.
"""
super().__init__()
# Main UI code goes here
self.setupUi(self)
# Connect to databases
#removed for security purposes
# Build the Query
StartDate = datetime.date(2020, 11, 1)
EndDate = datetime.date.today()
# Set the valid date range for the Calendar
self.Calendar.setDateRange(StartDate, EndDate)
# Format Dates for SQL
StartDate = "'" + str(StartDate.strftime("%m/%d/%Y")) + "'"
EndDate = "'" + str(EndDate.strftime("%m/%d/%Y")) + "'"
# Get the list of critical alarms within the date range
CriticalAlarms = self.get_Critical_Alarms(StartDate, EndDate)
# Loop through the Critical Alarm list and update the calendar date format of the results
for i in range(CriticalAlarms.rowCount()):
datetime_str = CriticalAlarms.record(i).value("EventStamp")
datetime_obj = datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S.%f0')
# Update the format of the date on the calendar day that the alarm occured
format = qtg.QTextCharFormat()
format.setFont(qtg.QFont('Times', 15))
date = qtc.QDate(datetime_obj)
self.Calendar.setDateTextFormat(date, format)
# Single Date Selected on the Calendar
self.Calendar.selectionChanged.connect(self.config_Critical_Alarms)
# Single record selected on the CriticalAlarmList widget
self.CriticalAlarmList.selectionModel().selectionChanged.connect(self.get_Critical_Alarm_Details)
# End main UI code
self.show()
def config_Critical_Alarms(self):
print ("config_Critical_Alarms")
CriticalAlarms = None
StartDate = self.Calendar.selectedDate()
EndDate = StartDate.addDays(1)
StartDate = "'" + qtc.QDate.toString(StartDate, 'MM/dd/yyyy') + "'"
EndDate = "'" + qtc.QDate.toString(EndDate, 'MM/dd/yyyy') + "'"
CriticalAlarms = self.get_Critical_Alarms(StartDate, EndDate)
def get_Critical_Alarms(self, StartDate, EndDate):
print ("get_Critical_Alarms")
alarm_state = "'UNACK_ALM'"
queryString = f'SELECT EventStamp, TagName, Description, Priority FROM v_AlarmHistory WHERE (AlarmState = {alarm_state}) '\
f'AND (EventStamp >= {StartDate}) AND (Priority <= 10) AND '\
f'(EventStamp <= {EndDate}) ORDER BY EventStamp DESC'
# Process the SQL Query
CriticalAlarms = qts.QSqlQueryModel()
CriticalAlarms.setQuery(queryString)
self.CriticalAlarmList.setModel(CriticalAlarms)
self.CriticalAlarmList.resizeColumnsToContents()
self.StatusLabel.setText('Found {0} critical alarms between {1} and {2}'.format(CriticalAlarms.rowCount(), StartDate, EndDate))
return(CriticalAlarms)
def get_Critical_Alarm_Details(self, selected):
print ("get_Critical_Alarm_Details")
indexes = self.CriticalAlarmList.selectionModel().selectedRows()
model = self.CriticalAlarmList.model()
for index in indexes:
self.StatusLabel.setText('Selected row: {0}, Tag: {1}'.format(index.row(), model.data(model.index(index.row(), 1))))
def UiComponents(self, datetime_obj):
print ("UiComponents")
# Calendar day format
format = qtg.QTextCharFormat()
format.setFont(qtg.QFont('Times', 15))
date = qtc.QDate(datetime_obj)
value = self.Calendar.dateTextFormat()
self.label.setWordWrap(True)
self.label.setText("Date: " + str(value))
# Main
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
# Requirement to save a reference to MainWindow
# if it goes out of scope, it will be destroyed.
mw = MainWindow()
sys.exit(app.exec())
To copy to clipboard, switch view to plain text mode
Bookmarks