main_window_XmSgj.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import sys
  2. import threading
  3. import time
  4. from datetime import datetime
  5. from PyQt5 import QtWidgets
  6. sys.path.append("..")
  7. import mytool.db_helper.db_operation as spmng
  8. from PyQt5.QtCore import QSize, QTimer, Qt
  9. from PyQt5.QtGui import QFont, QBrush, QColor, QPixmap
  10. from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem, QListWidget, QLabel, QMessageBox, QFrame, \
  11. QAbstractItemView
  12. from ui.ui import Ui_MainWindow
  13. class MainWindow(QMainWindow, Ui_MainWindow):
  14. def __init__(self, task_type, show_config, db_config):
  15. super().__init__()
  16. self.setupUi(self)
  17. # self.label.setText("上古街智能立体停车库取车排队信息")
  18. self.setWindowFlags(Qt.FramelessWindowHint)
  19. self.task_type = task_type
  20. self.top_number = show_config["top_number"]
  21. # self.image_label.setPixmap(QPixmap('./image/log.jpg'))
  22. # self.image_label.setScaledContents(True)
  23. # self.image_label.setMaximumHeight(140)
  24. # self.image_label.setMaximumWidth(600)
  25. self.db = spmng.DBOperation(db_config['db_ip'], db_config['db_port'], db_config['db_name'],
  26. db_config['db_user'], db_config['db_password'])
  27. self.unit_list = show_config["unit_list"][show_config["unit"]]
  28. self.font_size = show_config["font_size"]
  29. self.list_widget_dict = {}
  30. self.list_widget_thread = {}
  31. self.scroll_flag_dict = {}
  32. self.command_dict = {}
  33. self.last_command_dict = {}
  34. self.pick_wait_weight = {}
  35. self.last_pick_wait_weight = {}
  36. self.btn_positions = [(i, j) for i in range(0, show_config['row']) for j in range(show_config['column'])]
  37. # 创建listwidget与label 配置多少个单元生成多少对
  38. for i, position in zip(self.unit_list, self.btn_positions):
  39. vboxlayout = QtWidgets.QVBoxLayout()
  40. self.list_widget_dict[i] = QListWidget(self)
  41. self.list_widget_dict[i].setAutoScroll(True)
  42. self.scroll_flag_dict[i] = False
  43. self.list_widget_dict[i].setSelectionMode(QAbstractItemView.NoSelection)
  44. self.list_widget_dict[i].setStyleSheet("border:5px solid #c0181f;")
  45. self.list_widget_dict[i].setObjectName(('list_widget_%d' % (i + 1)))
  46. self.list_widget_thread[i] = threading.Thread(target=self.Scroll, args=(i,))
  47. self.list_widget_thread[i].start()
  48. label = QLabel(self.get_unit(i), self)
  49. label.setFont(QFont("黑体", 30, QFont.Bold)) # 设置字体、样式和大小
  50. label.setAlignment(Qt.AlignCenter)
  51. vboxlayout.addWidget(label)
  52. vboxlayout.addWidget(self.list_widget_dict[i])
  53. vboxlayout.setSpacing(0)
  54. # hframe = QFrame(self)
  55. # hframe.setFrameShape(QFrame.HLine) # 设置水平方向
  56. # hframe.setStyleSheet("QFrame{background:black;min-height:4px}");
  57. # vboxlayout.addWidget(hframe)
  58. self.main_gridLayout.addLayout(vboxlayout, *position)
  59. self.main_gridLayout.setVerticalSpacing(40)
  60. self.command_dict[i] = []
  61. self.pick_wait_weight[i] = {}
  62. # 数据库查询线程
  63. self.db_query = threading.Thread(target=self.db_query)
  64. self.db_query_isClose = False
  65. self.db_query.start()
  66. # 界面刷新定时器
  67. self.timer = QTimer()
  68. self.timer.timeout.connect(self.Switch)
  69. self.timer.start(200)
  70. # 断开连接的弹窗
  71. self.t_timer = QTimer(self)
  72. self.msg_box_hint = QMessageBox()
  73. self.msg_box_hint.setIcon(QMessageBox.Critical)
  74. self.msg_box_hint.setWindowTitle('错误')
  75. self.msg_box_hint.setText('连接断开,重新连接中...')
  76. self.t_timer.setSingleShot(True)
  77. self.t_timer.timeout.connect(self.msg_box_hint.close)
  78. def closeEvent(self, event):
  79. self.db_query_isClose = True
  80. self.db_query.join()
  81. event.accept() # 接受关闭事件
  82. # 定义自动关闭对话框函数
  83. def show_message_box(self):
  84. self.msg_box_hint.show()
  85. self.t_timer.start(1000)
  86. def db_query(self):
  87. while self.db_query_isClose is False:
  88. for i in self.unit_list:
  89. self.last_pick_wait_weight[i] = self.db.query_pick_wait_weight_in_unit(i)
  90. if i != 40:
  91. self.last_command_dict[i] = self.db.query_queue_condition_in_unit(i)
  92. else:
  93. self.last_command_dict[i] = self.db.query_command_top_number_in_type(self.task_type,
  94. self.top_number)
  95. # elif self.task_type != 0 and i == 40:
  96. # self.last_command_dict[i] = self.db.query_command_top_number_in_type(self.task_type,self.top_number)
  97. # elif self.task_type == 0 and i != 40:
  98. # self.last_command_dict[i] = self.db.query_queue_condition_in_unit(i)
  99. # else:
  100. # self.last_command_dict[i] = self.db.query_command_top_number(self.top_number)
  101. time.sleep(0.2)
  102. def get_time_weight(self):
  103. current_time_gour = datetime.now().hour
  104. if 0 <= current_time_gour < 7:
  105. return 0.6
  106. elif 7 <= current_time_gour < 8:
  107. return 0.9
  108. elif 8 <= current_time_gour < 9:
  109. return 1
  110. elif 9 <= current_time_gour < 11:
  111. return 0.9
  112. elif 11 <= current_time_gour < 12:
  113. return 0.8
  114. elif 12 <= current_time_gour < 14:
  115. return 0.7
  116. elif 11 <= current_time_gour < 15:
  117. return 0.9
  118. elif 15 <= current_time_gour < 16:
  119. return 0.8
  120. elif 16 <= current_time_gour < 21:
  121. return 0.7
  122. elif 21 <= current_time_gour < 24:
  123. return 0.6
  124. def get_listWidget_item(self, dict, index):
  125. item = QListWidgetItem()
  126. item.setFont(QFont('微软雅黑', self.font_size, QFont.Bold))
  127. show_str = ""
  128. if dict['type'] == 1: # 存车指令
  129. item.setBackground(QColor(185, 240, 240))
  130. if (dict['statu'] == 0): # 排队
  131. item.setForeground(QColor(80, 80, 80))
  132. show_str = "%s 排队中" % (dict['car_number'])
  133. elif (dict['statu'] == 1): # 工作
  134. item.setForeground(QColor('blue'))
  135. show_str = "%s 存车中" % (dict['car_number'])
  136. elif (dict['statu'] == 2): # 已完成
  137. item.setForeground(QColor('green'))
  138. show_str = "%s 已完成" % (dict['car_number'])
  139. elif (dict['statu'] == 3): # 已完成
  140. item.setForeground(QColor('red'))
  141. show_str = "%s 维护!" % (dict['car_number'])
  142. elif dict['type'] == 2: # 取车指令
  143. item.setBackground(QColor(250, 240, 200))
  144. if (dict['statu'] == 0): # 排队
  145. item.setForeground(QColor(80, 80, 80))
  146. show_str = "%s %s排队中" % (dict['car_number'], self.get_unit(dict['unit']))
  147. if index != 40:
  148. show_str = "%s 预计剩%d分钟" % (dict['car_number'],
  149. round((dict['row_number() over(order by queue_time)']) * 5 * self.get_time_weight() *
  150. self.pick_wait_weight[index][0]['wait_weight']))
  151. elif (dict['statu'] == 1): # 工作
  152. item.setForeground(QColor('blue'))
  153. show_str = "%s %s取车中" % (dict['car_number'], self.get_unit(dict['unit']))
  154. elif (dict['statu'] == 2): # 已完成
  155. item.setForeground(QColor('green'))
  156. show_str = "%s %s出口%d" % (dict['car_number'], self.get_unit(dict['unit']), dict['export_id'])
  157. elif (dict['statu'] == 3): # 已完成
  158. item.setForeground(QColor(80, 180, 200))
  159. show_str = "%s %s维护!" % (dict['car_number'], self.get_unit(dict['unit']))
  160. item.setText(show_str)
  161. return item
  162. def Switch(self):
  163. for i in self.unit_list:
  164. if (i in self.last_command_dict.keys()) is False or self.last_command_dict[i] is None or \
  165. (i in self.last_pick_wait_weight.keys()) is False or self.last_pick_wait_weight[i] is None:
  166. self.show_message_box()
  167. elif self.command_dict[i] != self.last_command_dict[i] or self.pick_wait_weight[i] != \
  168. self.last_pick_wait_weight[i]:
  169. self.pick_wait_weight[i] = self.last_pick_wait_weight[i]
  170. self.command_dict[i] = self.last_command_dict[i]
  171. self.list_widget_dict[i].clear()
  172. for dict in self.command_dict[i]:
  173. if dict['statu'] != 4 and dict['type'] == self.task_type:
  174. self.list_widget_dict[i].addItem(self.get_listWidget_item(dict, i))
  175. def Scroll(self, i):
  176. while True:
  177. for index in range(self.list_widget_dict[i].count()):
  178. # if self.scroll_flag_dict[i] is True:
  179. # self.scroll_flag_dict[i] = False
  180. # break
  181. self.list_widget_dict[i].setCurrentRow(index)
  182. time.sleep(0.7)
  183. time.sleep(2)
  184. def get_unit(self, unit):
  185. if unit == 1:
  186. return 'A'
  187. elif unit == 2:
  188. return 'B'
  189. elif unit == 11:
  190. return 'A1'
  191. elif unit == 12:
  192. return 'A2'
  193. elif unit == 13:
  194. return 'A3'
  195. elif unit == 14:
  196. return 'A4'
  197. elif unit == 21:
  198. return 'B1'
  199. elif unit == 22:
  200. return 'B2'
  201. elif unit == 23:
  202. return 'B3'
  203. elif unit == 24:
  204. return 'B4'
  205. elif unit == 25:
  206. return 'B5'
  207. elif unit == 31:
  208. return 'C1'
  209. elif unit == 32:
  210. return 'C2'
  211. elif unit == 40:
  212. return '最新%d条' % self.top_number
  213. return '未知'