From 3b9cc41a4b24c86e12152943497324332b4a2603 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 13 Mar 2023 16:34:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + RFCTCMLD20230215125003(1)(1).dat_bak | 1 + backup/ID0001(1).txt | 81 ------------- backup/ID0002.txt | 84 -------------- conf.ini | 4 +- ctc数据推送.py | 167 ++++++++++++++++++--------- requirements.txt | 1 + 7 files changed, 118 insertions(+), 222 deletions(-) create mode 100644 RFCTCMLD20230215125003(1)(1).dat_bak delete mode 100644 backup/ID0001(1).txt delete mode 100644 backup/ID0002.txt create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 7ee6edb..3133c5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.exe *.spec +*.zip +*.rar venv/ dist/ build/ \ No newline at end of file diff --git a/RFCTCMLD20230215125003(1)(1).dat_bak b/RFCTCMLD20230215125003(1)(1).dat_bak new file mode 100644 index 0000000..3956fe1 --- /dev/null +++ b/RFCTCMLD20230215125003(1)(1).dat_bak @@ -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" diff --git a/backup/ID0001(1).txt b/backup/ID0001(1).txt deleted file mode 100644 index 4d952cc..0000000 --- a/backup/ID0001(1).txt +++ /dev/null @@ -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" - } - ] -} diff --git a/backup/ID0002.txt b/backup/ID0002.txt deleted file mode 100644 index b647506..0000000 --- a/backup/ID0002.txt +++ /dev/null @@ -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" - } - ] -} diff --git a/conf.ini b/conf.ini index fd5f6d0..5f325e2 100644 --- a/conf.ini +++ b/conf.ini @@ -1,2 +1,4 @@ [ctc] -serverUrl=http://127.0.0.1:1234 \ No newline at end of file +format=TestPro,SampleNo,SiO2,Al2O3,Fe2O3,TiO2,CaO,MgO,SO3,K2O,Na2O,MnO,P2O5,Cr2O3,Cl-,TestDate +server=10.0.11.236 +port=8080 \ No newline at end of file diff --git a/ctc数据推送.py b/ctc数据推送.py index bc9ab86..7114a5e 100644 --- a/ctc数据推送.py +++ b/ctc数据推送.py @@ -4,74 +4,129 @@ import threading from PIL import Image from pystray import MenuItem import time -import requests +# import requests +from datetime import datetime import json -import shutil from configparser import ConfigParser 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): - global isWorking - isWorking = False - icon.stop() + def check_one_instance(self): + try: + global s + 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(): - try: - global s - s = socket.socket() - host = socket.gethostname() - s.bind((host, 20309)) - return True - except Exception: - print('already has an instance') - return False + def run(self): + if not self.is_one_instance: + return + 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(): - conf = ConfigParser() # 需要实例化一个ConfigParser对象 - conf.read('conf.ini') - global isWorking - isWorking = True - serverUrl = conf['ctc']['serverUrl'] - currentPath = os.getcwd() - backupPath = os.path.join(currentPath, 'backup') - if not os.path.exists(backupPath): - os.mkdir(backupPath) - - menu = (MenuItem('退出', close), ) - image = Image.open("favicon.ico") - title = "ctc数据推送" - icon = pystray.Icon(title, image, "ctc数据推送", menu) - threading.Thread(target=icon.run, daemon=True).start() - - while isWorking: - files = os.listdir(currentPath) - for file in files: - 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) + server_addr = (server, int(port)) + self.is_working = True + filename_pattern = "*.dat" + # 获取当前工作目录 + current_dir = os.getcwd() + while self.is_working: + files = glob.glob(os.path.join(current_dir, filename_pattern)) + print(files) + for file in files: + # 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 + with open(file, 'r') as f: + post_data = self.format_data(f.read()) + if post_data: try: - shutil.move(file_path, backupPath) - except Exception: - os.remove(file_path) - except Exception as e: - err = '服务器错误:{}'.format(e.__str__()[0:80]) - icon.notify(message=err) - time.sleep(3) + # requests.post(url=serverUrl, json=post_data) + tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp_socket.settimeout(3) + tcp_socket.connect(server_addr) + tcp_socket.sendall(post_data.encode('utf-8')) + os.rename(file, file+'_bak') + 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 check_one_instance(): - main() - + conf = ConfigParser() # 需要实例化一个ConfigParser对象 + 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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4500175 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pystray==0.19.4 \ No newline at end of file