You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DCS/UI/TrendManage/ActualTrendWidget.py

111 lines
3.3 KiB
Python

import sys
import time
import numpy as np
from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from PyQt5.QtCore import QThread
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
from protocol.Celery.MBTCPMaster import app as MBTCPMApp
from celery.schedules import crontab, timedelta
from celery.result import AsyncResult, GroupResult
from utils.Queue import Queue
import tkinter as tk
from model.ProjectModel.VarManage import ModbusVarManage
class ActualTrend(QtWidgets.QMainWindow):
def __init__(self, parent=None, varName = None):
super(ActualTrend, self).__init__(parent)
self.varName = varName
_main = QtWidgets.QWidget()
self.setCentralWidget(_main)
layout = QtWidgets.QVBoxLayout(_main)
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(dynamic_canvas)
self.addToolBar(QtCore.Qt.BottomToolBarArea,
NavigationToolbar(dynamic_canvas, self))
self.x = [] #建立空的x轴数组和y轴数组
self.y = []
self.xQueue = Queue(length = 20)
self.yQueue = Queue(length = 20)
# self.n = 0
self._dynamic_ax = dynamic_canvas.figure.subplots()
self._timer = dynamic_canvas.new_timer(
1000, [(self._update_canvas, (), {})])
self._timer.start()
def _update_canvas(self):
# self.n += 1
varName = self.varName
varMM = ModbusVarManage.getByName(varName)
minY = -100
maxY = 100
if varMM:
minY, maxY = varMM[-2], varMM[-1]
minY = float(minY)
maxY = float(maxY)
self._dynamic_ax.clear()
acttime = time.strftime('%H:%M:%S', time.localtime(time.time()))
# print(acttime)
if varName != '':
if MBTCPMApp.backend.get('ModBus') is not None:
uid = MBTCPMApp.backend.get('ModBus').decode('utf-8')
else:
return
# print(uid)
res = AsyncResult(uid) # 参数为task id
if res.result:
# print(res.result, res.date_done)
try:
yValue = (res.result[varName])
self.yQueue.enqueue(yValue)
self.xQueue.enqueue(acttime)
except Exception as e:
print(e)
name_list = self.xQueue.list
value_list = np.array(self.yQueue.list)
pos_list = np.arange(len(name_list))
self._dynamic_ax.set_xticks(pos_list)
self._dynamic_ax.set_xticklabels(name_list)
self._dynamic_ax.plot(pos_list, value_list, 'bo-', linewidth=2)
# self._dynamic_ax.set_xlim(0,10)
self._dynamic_ax.set_ylim(minY, maxY)
# print(id(self._dynamic_ax))
self._dynamic_ax.figure.canvas.draw()
def closeEvent(self, event):
# 判断是否点击了窗口的关闭按钮
if event.type() == event.Close:
self._timer.stop()
self.hide()
def showEvent(self, event):
self._timer.start()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ActualTrend()
app.show()
qapp.exec_()