修改完成
This commit is contained in:
parent
a176f2a771
commit
3b9cc41a4b
|
@ -1,5 +1,7 @@
|
||||||
*.exe
|
*.exe
|
||||||
*.spec
|
*.spec
|
||||||
|
*.zip
|
||||||
|
*.rar
|
||||||
venv/
|
venv/
|
||||||
dist/
|
dist/
|
||||||
build/
|
build/
|
|
@ -0,0 +1 @@
|
||||||
|
"样品各类","样品名",20.188,5.171,2.617,.243,61.438,5.132,3.471,1.011,.223,.109,.136,.019,.069,"11/12/2022 12:03:36 PM"
|
|
@ -1,81 +0,0 @@
|
||||||
{
|
|
||||||
"SampleNo":"ID0001",
|
|
||||||
"TestDate":"2023-02-24 11:53",
|
|
||||||
"TestVaule":[
|
|
||||||
{
|
|
||||||
"标准名称":"0.41"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"loss":"0.67"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SiO2":"0.34"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Al2O3":"0.00"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Fe2O3":"0.69"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"CaO":"0.24"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"MgO":"0.78"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"MnO":"0.58"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SO3":"0.62"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"P2O5":"0.64"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Cl-":"0.05"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"K2O":"0.45"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Na2O":"0.81"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"R2O":"0.27"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"f-CaO":"0.61"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"KH":"0.91"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"KH-":"0.95"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SM":"0.42"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"IM":"0.27"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"LSF":"0.36"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C2S":"0.91"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C3S":"0.04"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C3A":"0.02"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C4AF":"0.53"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SUM":"0.92"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,84 +0,0 @@
|
||||||
{
|
|
||||||
"SampleNo":"ID0002",
|
|
||||||
"TestDate":"2023-02-28 10:54",
|
|
||||||
"TestVaule":[
|
|
||||||
{
|
|
||||||
"检测项目":"0.62"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"loss":"0.57"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SiO2":"0.37"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Al2O3":"0.59"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Fe2O3":"0.23"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"CaO":"0.41"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"MgO":"0.29"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"MnO":"0.78"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"TiO2":"0.16"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SO3":"0.35"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"P2O5":"0.90"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Cl-":"0.42"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"K2O":"0.88"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Na2O":"0.06"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"R2O":"0.40"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"f-CaO":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"KH":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"KH-":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SM":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"IM":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"LSF":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C2S":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C3S":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C3A":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"C4AF":"null"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"SUM":"null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
4
conf.ini
4
conf.ini
|
@ -1,2 +1,4 @@
|
||||||
[ctc]
|
[ctc]
|
||||||
serverUrl=http://127.0.0.1:1234
|
format=TestPro,SampleNo,SiO2,Al2O3,Fe2O3,TiO2,CaO,MgO,SO3,K2O,Na2O,MnO,P2O5,Cr2O3,Cl-,TestDate
|
||||||
|
server=10.0.11.236
|
||||||
|
port=8080
|
167
ctc数据推送.py
167
ctc数据推送.py
|
@ -4,74 +4,129 @@ import threading
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from pystray import MenuItem
|
from pystray import MenuItem
|
||||||
import time
|
import time
|
||||||
import requests
|
# import requests
|
||||||
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import shutil
|
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
import socket
|
import socket
|
||||||
|
import glob
|
||||||
|
|
||||||
|
class SnPost:
|
||||||
|
|
||||||
|
def __init__(self, server, port, format_str) -> None:
|
||||||
|
self.is_one_instance = False
|
||||||
|
self.is_working = False
|
||||||
|
self.icon = None # 下标
|
||||||
|
self.server = server
|
||||||
|
self.port = port
|
||||||
|
self.format_str = format_str
|
||||||
|
self.check_one_instance()
|
||||||
|
|
||||||
|
|
||||||
def close(icon, item):
|
def check_one_instance(self):
|
||||||
global isWorking
|
try:
|
||||||
isWorking = False
|
global s
|
||||||
icon.stop()
|
s = socket.socket()
|
||||||
|
host = socket.gethostname()
|
||||||
|
s.bind((host, 20309))
|
||||||
|
self.is_one_instance = True
|
||||||
|
except Exception:
|
||||||
|
self.is_one_instance = False
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.is_working = False
|
||||||
|
self.icon.stop()
|
||||||
|
|
||||||
|
def format_data(self, data):
|
||||||
|
|
||||||
|
format_list = format_str.split(',')
|
||||||
|
data = data.replace('"', '').replace('\r', '').replace('\n', '')
|
||||||
|
data_list = data.split(',')
|
||||||
|
post_data = {'TestVaule':[]}
|
||||||
|
ALL_ELEMENT = ["loss", "SiO2", "Al2O3", '"Fe2O3"', "CaO", "MgO", "MnO", "TiO2", "SO3", "P2O5", "Cl-", "K2O", "Na2O", "R2O", "f-CaO", "KH", "KH-", "SM", "IM", "LSF", "C2S", "C3S", "C3A", "C4AF", "SUM"]
|
||||||
|
HAS_ELEMENT = []
|
||||||
|
try:
|
||||||
|
for index, value in enumerate(format_list):
|
||||||
|
if value == 'SampleNo':
|
||||||
|
post_data['SampleNo'] = data_list[index]
|
||||||
|
elif value == 'TestDate':
|
||||||
|
dt = datetime.strptime(data_list[index], '%m/%d/%Y %I:%M:%S %p')
|
||||||
|
post_data['TestDate'] = dt.strftime('%Y-%m-%d %H:%M')
|
||||||
|
elif value == 'TestPro':
|
||||||
|
post_data['TestVaule'].append({'检测项目':data_list[index]})
|
||||||
|
else:
|
||||||
|
HAS_ELEMENT.append(value)
|
||||||
|
value_p = data_list[index]
|
||||||
|
if value_p.startswith('.'):
|
||||||
|
value_p = '0' + value_p
|
||||||
|
post_data['TestVaule'].append({value:value_p})
|
||||||
|
difference = set(ALL_ELEMENT).difference(set(HAS_ELEMENT))
|
||||||
|
for i in difference:
|
||||||
|
post_data['TestVaule'].append({i:None})
|
||||||
|
return json.dumps(post_data, ensure_ascii=False)
|
||||||
|
except:
|
||||||
|
import traceback
|
||||||
|
print(traceback.format_exc())
|
||||||
|
self.icon.notify(message='数据格式解析失败', title="ctc数据推送")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def check_one_instance():
|
def run(self):
|
||||||
try:
|
if not self.is_one_instance:
|
||||||
global s
|
return
|
||||||
s = socket.socket()
|
|
||||||
host = socket.gethostname()
|
|
||||||
s.bind((host, 20309))
|
|
||||||
return True
|
|
||||||
except Exception:
|
|
||||||
print('already has an instance')
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
menu = (MenuItem('退出', self.close), )
|
||||||
|
image = Image.open("favicon.ico")
|
||||||
|
title = "ctc数据推送"
|
||||||
|
self.icon = pystray.Icon(title, image, "ctc数据推送", menu)
|
||||||
|
threading.Thread(target=self.icon.run, daemon=True).start()
|
||||||
|
|
||||||
def main():
|
server_addr = (server, int(port))
|
||||||
conf = ConfigParser() # 需要实例化一个ConfigParser对象
|
self.is_working = True
|
||||||
conf.read('conf.ini')
|
filename_pattern = "*.dat"
|
||||||
global isWorking
|
# 获取当前工作目录
|
||||||
isWorking = True
|
current_dir = os.getcwd()
|
||||||
serverUrl = conf['ctc']['serverUrl']
|
while self.is_working:
|
||||||
currentPath = os.getcwd()
|
files = glob.glob(os.path.join(current_dir, filename_pattern))
|
||||||
backupPath = os.path.join(currentPath, 'backup')
|
print(files)
|
||||||
if not os.path.exists(backupPath):
|
for file in files:
|
||||||
os.mkdir(backupPath)
|
# file_path = os.path.join(currentPath, file)
|
||||||
|
# if file_path.endswith('.txt'):
|
||||||
menu = (MenuItem('退出', close), )
|
# with open(file_path, 'r', encoding="utf-8") as f:
|
||||||
image = Image.open("favicon.ico")
|
# post_data = json.load(f)
|
||||||
title = "ctc数据推送"
|
# for i in post_data['TestVaule']:
|
||||||
icon = pystray.Icon(title, image, "ctc数据推送", menu)
|
# for k in i:
|
||||||
threading.Thread(target=icon.run, daemon=True).start()
|
# if i[k] == 'null':
|
||||||
|
# i[k] = None
|
||||||
while isWorking:
|
with open(file, 'r') as f:
|
||||||
files = os.listdir(currentPath)
|
post_data = self.format_data(f.read())
|
||||||
for file in files:
|
if post_data:
|
||||||
file_path = os.path.join(currentPath, file)
|
|
||||||
if file_path.endswith('.txt'):
|
|
||||||
with open(file_path, 'r', encoding="utf-8") as f:
|
|
||||||
post_data = json.load(f)
|
|
||||||
for i in post_data['TestVaule']:
|
|
||||||
for k in i:
|
|
||||||
if i[k] == 'null':
|
|
||||||
i[k] = None
|
|
||||||
try:
|
|
||||||
requests.post(url=serverUrl, json=post_data)
|
|
||||||
try:
|
try:
|
||||||
shutil.move(file_path, backupPath)
|
# requests.post(url=serverUrl, json=post_data)
|
||||||
except Exception:
|
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
os.remove(file_path)
|
tcp_socket.settimeout(3)
|
||||||
except Exception as e:
|
tcp_socket.connect(server_addr)
|
||||||
err = '服务器错误:{}'.format(e.__str__()[0:80])
|
tcp_socket.sendall(post_data.encode('utf-8'))
|
||||||
icon.notify(message=err)
|
os.rename(file, file+'_bak')
|
||||||
time.sleep(3)
|
except Exception as e:
|
||||||
|
err = '服务器错误:{}'.format(e.__str__()[0:80])
|
||||||
|
self.icon.notify(message=err, title="ctc数据推送")
|
||||||
|
finally:
|
||||||
|
tcp_socket.close()
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if check_one_instance():
|
conf = ConfigParser() # 需要实例化一个ConfigParser对象
|
||||||
main()
|
conf.read('conf.ini')
|
||||||
|
# serverUrl = conf['ctc']['serverUrl']
|
||||||
|
server = conf['ctc']['server']
|
||||||
|
port = conf['ctc']['port']
|
||||||
|
format_str = conf['ctc']['format']
|
||||||
|
snpost = SnPost(server, port, format_str)
|
||||||
|
snpost.run()
|
||||||
|
|
||||||
|
# 打包命令
|
||||||
# pyinstaller -F -w -i favicon.ico ctc数据推送.py
|
# pyinstaller -F -w -i favicon.ico ctc数据推送.py
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
pystray==0.19.4
|
Loading…
Reference in New Issue