diff --git a/force-logger.py b/force-logger.py index 4b4c0bb..eac46d9 100644 --- a/force-logger.py +++ b/force-logger.py @@ -1,16 +1,17 @@ from PyQt5 import QtWidgets, QtGui from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel - + import config_cells, main_window, conf_server_window from threading import Thread, Lock -import sys, socket, queue , time, datetime, os +import sys, socket, queue, time, datetime, os import server import handler import variables - + + class MainWindow(QMainWindow): - + def __init__(self): super(MainWindow, self).__init__() @@ -21,30 +22,31 @@ def __init__(self): self.qTimer.setInterval(1000) self.qTimer.timeout.connect(self.updateGui) self.qTimer.start() - + self.ui.actionConfLoadcells.triggered.connect(lambda: self.confLoadcells()) self.ui.actionConfServer.triggered.connect(lambda: self.confServer()) self.ui.actionConfGraph.triggered.connect(lambda: self.confGraph()) - - self.ui.ui_but_control_connect.clicked.connect(lambda: Thread(target=server.startServer, args=(), daemon = True).start()) - + + self.ui.ui_but_control_connect.clicked.connect( + lambda: Thread(target=server.startServer, args=(), daemon=True).start()) + self.ui.ui_lab_unit_id0.setText(variables.list_cell_units[0]) self.ui.ui_lab_unit_id1.setText(variables.list_cell_units[1]) self.ui.ui_lab_unit_id2.setText(variables.list_cell_units[2]) self.ui.ui_lab_unit_id3.setText(variables.list_cell_units[3]) self.ui.ui_lab_unit_id4.setText(variables.list_cell_units[4]) self.ui.ui_lab_unit_id5.setText(variables.list_cell_units[5]) - + self.ui.ui_but_blk_id0.clicked.connect(lambda: self.blnk(0)) self.ui.ui_but_blk_id1.clicked.connect(lambda: self.blnk(1)) self.ui.ui_but_blk_id2.clicked.connect(lambda: self.blnk(2)) self.ui.ui_but_blk_id3.clicked.connect(lambda: self.blnk(3)) self.ui.ui_but_blk_id4.clicked.connect(lambda: self.blnk(4)) self.ui.ui_but_blk_id5.clicked.connect(lambda: self.blnk(5)) - - #Thread(target=server.startServer, args=(), daemon = True).start() - Thread(target=handler.handlerThread, args=(), daemon = True).start() - + + # Thread(target=server.startServer, args=(), daemon = True).start() + Thread(target=handler.handlerThread, args=(), daemon=True).start() + def updateGui(self): self.ui.ui_lab_value_id0.setText(variables.list_scale_mom[0]) self.ui.ui_lab_value_id1.setText(variables.list_scale_mom[1]) @@ -52,48 +54,49 @@ def updateGui(self): self.ui.ui_lab_value_id3.setText(variables.list_scale_mom[3]) self.ui.ui_lab_value_id4.setText(variables.list_scale_mom[4]) self.ui.ui_lab_value_id5.setText(variables.list_scale_mom[5]) - + def blnk(self, id): print("Blinking " + str(id)) variables.queues_send[id].put("blk\n") - + def confLoadcells(self): self.SW = windowConfigLoadcells() self.SW.show() - + def confServer(self): self.SW = windowConfigServer() - self.SW.show() - + self.SW.show() + def confGraph(self): self.SW = windowConfigGraph() - self.SW.show() - + self.SW.show() + + class windowConfigLoadcells(QMainWindow): def __init__(self): super(windowConfigLoadcells, self).__init__() self.ui = config_cells.Ui_conf_cells_window() self.ui.setupUi(self) - + + class windowConfigServer(QMainWindow): def __init__(self): super(windowConfigServer, self).__init__() self.ui = conf_server_window.Ui_conf_server_window() self.ui.setupUi(self) - + + class windowConfigGraph(QMainWindow): def __init__(self): super(windowConfigGraph, self).__init__() self.ui = config_Graph.Ui_conf_Graph_window() self.ui.setupUi(self) - - + + if __name__ == '__main__': import sys + app = QApplication(sys.argv) MW = MainWindow() MW.show() sys.exit(app.exec_()) - - - \ No newline at end of file diff --git a/handler.py b/handler.py index 9813a36..ac7809a 100644 --- a/handler.py +++ b/handler.py @@ -1,36 +1,38 @@ import variables import queue -#import main_window + + +# import main_window def handlerThread(): client_id = 0 client_msg = None - + while True: for client_id in range(5): - #print("looping") + # print("looping") if variables.list_scale_id[client_id] == True: - + try: client_msg = str(variables.queues_recv[client_id].get(False)) variables.queues_recv[client_id].task_done() - + if client_msg != None: - - if str(client_msg).count("mes"): - parts = client_msg.split(":") + + if str(client_msg).count("mes"): + parts = client_msg.split(":") if variables.en_log == True: lock_file_mes[client_id].acquire() f = open("log/" + filename + "/" + filename + "_ID" + str(client_id) + ".log", "a+") f.write(parts[2] + ":" + parts[3] + "\n") f.close() lock_file_mes[client_id].release() - #print("writing to file") - print("test") + # print("writing to file") + print("test") variables.list_scale_mom[client_id] = parts[3] print(str(parts[3])) client_msg = "" - + elif str(client_msg).count("ack"): print("Acknowlege") variables.queues_ack[client_id].put(True) @@ -49,6 +51,6 @@ def handlerThread(): client_msg = None''' except queue.Empty: pass - - client_id = 0 - #time.sleep(0.1) \ No newline at end of file + + client_id = 0 + # time.sleep(0.1) diff --git a/server.py b/server.py index db5ed14..07d094c 100644 --- a/server.py +++ b/server.py @@ -10,7 +10,7 @@ def clientThread(client_id, conn, client_ip, port): while True: try: data += conn.recv(1024).decode() - #print(data) + # print(data) if data.count("#") > 1: parts = data.split("#") data = " " @@ -19,13 +19,13 @@ def clientThread(client_id, conn, client_ip, port): parts[i] = str(parts[i]).replace("#", "") variables.queues_recv[client_id].put(parts[i]) parts[i] = None - i +=1 - - elif str(data).count("#"): + i += 1 + + elif str(data).count("#"): data = data.replace("#", "") - variables.queues_recv[client_id].put(data) + variables.queues_recv[client_id].put(data) data = " " - + if not data: break except: @@ -33,31 +33,31 @@ def clientThread(client_id, conn, client_ip, port): send_data = variables.queues_send[client_id].get() variables.queues_send[client_id].task_done() conn.sendall(send_data.encode()) - + variables.list_scale_id[client_id] = False variables.list_scale_mom[client_id] = "NA" conn.close() print("connection closed") - + def startServer(): - scale_id=0 + scale_id = 0 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # this is for easy starting/killing the app - soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - #soc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + # soc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) print('Socket created') try: soc.bind(("", variables.port_server)) - + print('Socket bind complete') except socket.error as msg: import sys print('Bind failed. Error : ' + str(sys.exc_info())) sys.exit() - #Start listening on socket + # Start listening on socket soc.listen(10) print('Socket now listening') @@ -66,15 +66,14 @@ def startServer(): while True: conn, addr = soc.accept() ip, port = str(addr[0]), str(addr[1]) - while True: #checking for available scale ids - if variables.list_scale_id[scale_id] == False: - break - scale_id += 1 + while True: # checking for available scale ids + if variables.list_scale_id[scale_id] == False: + break + scale_id += 1 print('Accepting connection from ' + ip + ':' + port + ' Id is:' + str(scale_id)) try: - - - Thread(target=clientThread, args=(scale_id, conn, ip, port), daemon = True).start() + + Thread(target=clientThread, args=(scale_id, conn, ip, port), daemon=True).start() variables.list_scale_id[scale_id] = True scale_id = 0 except: @@ -82,5 +81,3 @@ def startServer(): import traceback traceback.print_exc() soc.close() - - \ No newline at end of file diff --git a/variables.py b/variables.py index 4a25ed7..24c103e 100644 --- a/variables.py +++ b/variables.py @@ -5,28 +5,28 @@ en_blink = False list_scale_id = [False, False, False, False, False] -list_client_ip = ["NC", "NC" , "NC", "NC", "NC"] +list_client_ip = ["NC", "NC", "NC", "NC", "NC"] list_cell_units = ["", "", "", "", "", ""] list_scale_mom = ["", "", "", "", "", ""] -port_server=4242 +port_server = 4242 -queues_recv =[] +queues_recv = [] for i in range(5): queues_recv.append(queue.Queue()) - -queues_send =[] + +queues_send = [] for i in range(5): queues_send.append(queue.Queue()) - + queues_ack = [] for i in range(5): queues_ack.append(queue.Queue()) - + queues_mes = [] for i in range(5): queues_mes.append(queue.LifoQueue()) - + lock_file_mes = [] for i in range(5): lock = Lock() - lock_file_mes.append(lock) \ No newline at end of file + lock_file_mes.append(lock)