main.py
import sys
# 导入创建的文件模块
import test
import dcservice
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ =='__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = test.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
test.py
# -*- coding: utf-8 -*-
import sys
import time
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QTableWidgetItem, QHeaderView, QMessageBox, QMainWindow, QApplication
import dcservice
openflag = 1
class QMainWindow():
"""
重写closeEvent方法
"""
def closeEvent(self, event):
result = QtWidgets.QMessageBox.question(self, "标题", "亲,你确定想关闭我?别后悔!!!'_'",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if (result == QtWidgets.QMessageBox.Yes):
event.accept()
# 通知服务器的代码省略,这里不是重点...
else:
event.ignore()
class Ui_MainWindow(QMainWindow):
def closeEvent(self, event):
result = QMainWindow.QMessageBox.question(self, "标题", "亲,你确定想关闭我?别后悔!!!'_'",
QMainWindow.QMessageBox.Yes | QMainWindow.QMessageBox.No)
if (result == QMainWindow.QMessageBox.Yes):
event.accept()
# 通知服务器的代码省略,这里不是重点...
else:
event.ignore()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(867, 700)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 10, 851, 581))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(560, 600, 271, 51))
self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton1.setGeometry(QtCore.QRect(30, 600, 271, 51))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(18)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton1.setFont(font)
self.pushButton1.setObjectName("pushButton1")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 867, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
MainWindow.destroyed.connect(self.close) # type: ignore
self.pushButton.clicked.connect(self.CloseService) # type: ignore
self.pushButton1.clicked.connect(self.OpenService) # type: ignore
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.InitTableWdiget()
def InitTableWdiget(self):
self.tableWidget.setRowCount(7) # 设置表格行数
self.tableWidget.setColumnCount(4) # 设置表格列数
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
# self.tableWidget.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
# 合并相同数据单元格
# 合并第3列的第1-5行
# (0表示第1行,2表示第3列,5表示跨越5行<1、2、3、4、5行>,1表示跨越1列)
self.tableWidget.setSpan(2, 1, 1, 3)
self.tableWidget.setSpan(3, 1, 1, 3)
self.tableWidget.setSpan(4, 1, 1, 3)
self.tableWidget.setSpan(5, 1, 1, 3)
self.tableWidget.setSpan(6, 1, 1, 3)
self.tableWidget.setRowHeight(0, 60)
self.tableWidget.setRowHeight(1, 60)
self.tableWidget.setRowHeight(2, 60)
self.tableWidget.setRowHeight(3, 60)
self.tableWidget.setRowHeight(4, 60)
self.tableWidget.setRowHeight(5, 60)
self.tableWidget.setRowHeight(6, 120)
data = QTableWidgetItem(str('1状态')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(0, 0, data)
data = QTableWidgetItem(str('2状态')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(1, 0, data)
data = QTableWidgetItem(str('1产量')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(2, 0, data)
data = QTableWidgetItem(str('2产量')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(3, 0, data)
# data.setBackground(QtGui.QBrush(QtGui.QColor("yellow")))
data = QTableWidgetItem(str('上传数据状态:')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(4, 0, data)
data = QTableWidgetItem(str('服务状态:')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
self.tableWidget.setItem(5, 0, data)
data = QTableWidgetItem(str('已启动')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 25))
data.setForeground(QtGui.QBrush(QtGui.QColor("green"))) # 设置单元格文本颜色
self.tableWidget.setItem(5, 1, data)
data = QTableWidgetItem(str('已连接')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
data.setForeground(QtGui.QBrush(QtGui.QColor("green"))) # 设置单元格文本颜色
self.tableWidget.setItem(0, 1, data)
data = QTableWidgetItem(str('已断开')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 20))
data.setForeground(QtGui.QBrush(QtGui.QColor("red"))) # 设置单元格文本颜色
self.tableWidget.setItem(1, 1, data)
self.tableWidget.setRowHeight(6, 200)
data = QTableWidgetItem(str('异常日志:')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setForeground(QtGui.QBrush(QtGui.QColor("red"))) # 设置单元格文本颜色
data.setFont(QFont("Arial", 20))
data.setTextAlignment(Qt.AlignCenter)
self.tableWidget.setItem(6, 0, data)
dcservice._startService()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "关闭服务器"))
self.pushButton1.setText(_translate("MainWindow", "打开服务器"))
def closeEvent(self, event):
pass
def close(self):
print('==============')
pass
def CloseService(self):
global openflag
if openflag == 1:
openflag =0
dcservice._stopService()
data = QTableWidgetItem(str('已关闭')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 25))
data.setForeground(QtGui.QBrush(QtGui.QColor("red"))) # 设置单元格文本颜色
self.tableWidget.setItem(5, 1, data)
def OpenService(self):
global openflag
if(openflag == 0):
openflag = 1
dcservice._startService()
data = QTableWidgetItem(str('已启动')) # 转换后可插入表格
data.setTextAlignment(Qt.AlignCenter)
data.setFont(QFont("Arial", 25))
data.setForeground(QtGui.QBrush(QtGui.QColor("green"))) # 设置单元格文本颜色
self.tableWidget.setItem(5, 1, data)
def close(self):
# self.CloseService()
print('--------------------------------')
print('00000000000000000000000000000000')
sys.exit(-1)
dcservice.py
# -*- coding: UTF-8 -*-
# !/usr/bin/python
# @time :2019/10/12 11:11
# @author :Mo
# @function :service of flask
import ctypes
import inspect
import sys
import threading
import time
from flask import Flask, request, jsonify
from threading import Thread
import os
app = Flask(__name__)
app1 = Flask(__name__ + '1')
# mes交互需要的数据
################################################
# 接口名字
m_Cmd = 'getstatus'
# 工作台
m_table = 'Left'
# 机台设备号
m_EqNo = 'AE0001'
# 结果
m_result = 'true'
# 设备运行状态
m_process_state = 'Run'
# 错误消息
m_msg = '无'
# 产能
m_capicity = 1000
# 当前工序
m_currentProcess = ""
#设备名称
m_device_name=""
################################################
g_a = 100
g_b = 200
g_c = 300
t1=0
t2=0
@app.route('/dcservice', methods=["GET","POST"])
def calculate():
global m_Cmd,m_table,m_EqNo,m_result ,m_process_state,m_msg,m_capicity,m_currentProcess
if request.method == 'GET':
params = params = request.args
m_Cmd = params.get("Cmd", 0)
if m_Cmd == 'getstatus':#获取设备机台状态接口
return jsonify(content_type='application/json;charset=utf-8',
reason='success',
charset='utf-8',
status='200',
Cmd=m_Cmd,
EquNo=m_EqNo,
result=m_result,
Data=m_data,
Table = m_table,
msg = m_msg)
elif m_Cmd == 'getcapicity':#获取产能
return jsonify(content_type='application/json;charset=utf-8',
reason='success',
charset='utf-8',
status='200',
Cmd=m_Cmd,
EquNo=m_EqNo,
result=m_result,
Data=m_capicity,
Table=m_table,
msg=m_msg)
else:
return jsonify(
result="true",
Data="没找到对应指令",
msg="")
elif request.method == 'GET':
pass
else:
return jsonify(
result="true",
Data="没找到对应指令",
msg="")
@app1.route('/hometoservice', methods=["GET","POST"])
def calculate():
global m_Cmd,m_table,m_EqNo,m_result ,m_process_state,m_msg,m_capicity,m_currentProcess,m_device_name
if request.method == 'POST':
params = params = request.args
m_Cmd = params.get("Cmd", 0)
if m_Cmd == 'set_ process':#设置当前工序
m_currentProcess = params.get("current_ process", 0)
return jsonify(
result="true",
Data="",
msg = "")
elif m_Cmd == 'set_state':#设置设备状态
m_process_state = params.get("current_state", 0)
return jsonify(
result="true",
Data="",
msg="")
elif m_Cmd == 'Updata_baseInfo':#上传产能
m_EqNo = params.get("EqNo", 0)
m_capicity = params.get("capacity", 0)
return jsonify(
result="true",
Data="",
msg="")
elif request.method == 'GET':
params = params = request.args
m_Cmd = params.get("Cmd", 0)
if m_Cmd == 'Get_Lock_device': # 请求机器锁定信号
m_device_name = params.get("DC_wafer_bonding_machine", 0)
return jsonify(
result="true",
Data="",
msg="")
elif m_Cmd == 'Get_Sheet': # 请求配方
m_device_name = params.get("DC_wafer_bonding_machine", 0)
return jsonify(
result="true",
Config= "GJ_NAME",
Data="",
msg="")
else:
return jsonify(
result="true",
Data="没找到对应指令",
msg="")
else:
return jsonify(
result="true",
Data="没找到对应指令",
msg="")
def _startService():
global t1
global t2
t1 = threading.Thread(target=lambda: app1.run(host='0.0.0.0', threaded=True,debug=False, port=5002))
t2 = threading.Thread(target=lambda: app.run(host='0.0.0.0', threaded=True, debug=False, port=5001))
t1.start()
t2.start()
def _stopService():
global t1
global t2
stop_thread(t1)
stop_thread(t2)
def _async_raise(tid, exctype):
if not inspect.isclass(exctype):
raise TypeError("Only types can be raised (not instances)")
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
if __name__ == '__main__':
print('start')
_startService()
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » PYQT + flask httpserver 服务器提供简单的MES服务
发表评论 取消回复