同PLC的通讯改为两条长连接,分别读写。本次优化部分

This commit is contained in:
Mr.V 2024-07-17 18:52:38 +08:00
parent 3d4b918a93
commit 23d0de4e38
5 changed files with 30 additions and 13 deletions

11
app.py
View File

@ -313,6 +313,16 @@ def runReadPLC():
logger.error("数据 读取&上传 异常:%s", traceback.print_exc())
continue
def writePLC_heartbeat():
while True:
try:
detect.writePLC_heartbeat()
except Exception as e:
logger.error("心跳PLC时间读取数据 读取&上传 异常:%s", traceback.print_exc())
continue
finally:
time.sleep(30)
if __name__ == '__main__':
# --- 运行方式一 ---
@ -327,6 +337,7 @@ if __name__ == '__main__':
pool = eventlet.GreenPool()
pool.spawn_n(runWebSocket)
pool.spawn_n(runReadPLC)
pool.spawn_n(writePLC_heartbeat)
pool.waitall()

View File

@ -4,17 +4,17 @@ track_num = 2
[log]
log_path = ./logs
stdout_log_level = INFO
file_log_level = INFO
stdout_log_level = DEBUG
file_log_level = DEBUG
[plc]
ip_address = 10.227.181.106
ip_address = 192.168.1.150
slot = 0
timeout = 0.9
interval_plc = 6000
interval_plc = 2000
[upload]
upload_type = 1
upload_type = 0
login_web_url = http://192.168.2.167:20004/api/blade-auth/oauth/token
upload_pinch_coal_url = http://xxxx
upload_gross_weight_url = http://xxxx

View File

@ -253,6 +253,11 @@ def upload_load_type(loadingDict: dict):
else:
return -1, ""
def writePLC_heartbeat():
global g_write_plc
if g_write_plc is None:
g_write_plc = PLC_Tool(config.ip_address, config.slot)
g_write_plc.get_plc_time()
def interval_read_PLC_up_web():
"""
@ -262,6 +267,7 @@ def interval_read_PLC_up_web():
global g_read_plc
if g_read_plc is None:
g_read_plc = PLC_Tool(config.ip_address, config.slot)
interface = interfaceToWeb()
login_result = ""

View File

@ -31,7 +31,7 @@ class interfaceToWeb:
"grant_type": "password"}
try:
res = requests.post(url=url, headers=headers, data=data)
res = requests.post(url=url, headers=headers, data=data, timeout=1)
if res.status_code == requests.codes.ok:
jsonData = json.loads(res.text)
@ -68,7 +68,7 @@ class interfaceToWeb:
"poundNo": track_name}
try:
res = requests.post(url=url, headers=headers, json=data)
res = requests.post(url=url, headers=headers, json=data, timeout=1)
if res.status_code == requests.codes.ok:
jsonData = json.loads(res.text)
@ -108,7 +108,7 @@ class interfaceToWeb:
"houseStatus": 4,
"poundNo": track_name}
try:
res = requests.post(url=url, headers=headers, json=data)
res = requests.post(url=url, headers=headers, json=data, timeout=1)
if res.status_code == requests.codes.ok:
jsonData = json.loads(res.text)
@ -145,7 +145,7 @@ class interfaceToWeb:
"houseStatus": house_status,
"poundNo": track_name}
try:
res = requests.post(url=url, headers=headers, json=data)
res = requests.post(url=url, headers=headers, json=data, timeout=1)
if res.status_code == requests.codes.ok:
jsonData = json.loads(res.text)
@ -172,7 +172,7 @@ class interfaceToWeb:
"""
headers = {'Content-Type': 'application/json', 'blade-auth': self.token}
try:
res = requests.post(url=url, headers=headers, json=data)
res = requests.post(url=url, headers=headers, json=data, timeout=1)
if res.status_code == requests.codes.ok:
jsonData = json.loads(res.text)

View File

@ -8,7 +8,7 @@ logger = Log(stdout_log_level=logging.FATAL,
fil_log_level=logging.FATAL).getLogger()
class PLC_Tool:
def __init__(self, plcIp: str, plcSlot: int = 0, timeOut: float = 1):
def __init__(self, plcIp: str, plcSlot: int = 0, timeOut: float = 2):
self.plcIp = plcIp
self.plcSlot = plcSlot
self.timeOut = timeOut
@ -305,9 +305,9 @@ class PLC_Tool:
"""
connect_count: int = 0
try:
logger.debug("开始获取PLC时间")
# logger.debug("开始获取PLC时间")
time = self.comm.GetPLCTime(raw)
logger.debug("获取PLC时间成功%s", str(time))
# logger.debug("获取PLC时间成功%s", str(time))
return time
except IOError as io_error:
if connect_count > 0: