Skip to content

Commit

Permalink
Merge pull request #10 from SchoolGuy/fix-pep8-conventions
Browse files Browse the repository at this point in the history
Autoformat for following PEP8 conventions
  • Loading branch information
knoterich authored Jan 7, 2020
2 parents 500e5aa + 3c7a0eb commit ca91b72
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
57 changes: 30 additions & 27 deletions force-logger.py
Original file line number Diff line number Diff line change
@@ -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__()

Expand All @@ -21,79 +22,81 @@ 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])
self.ui.ui_lab_value_id2.setText(variables.list_scale_mom[2])
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_())



30 changes: 16 additions & 14 deletions handler.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -49,6 +51,6 @@ def handlerThread():
client_msg = None'''
except queue.Empty:
pass
client_id = 0
#time.sleep(0.1)

client_id = 0
# time.sleep(0.1)
41 changes: 19 additions & 22 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
Expand All @@ -19,45 +19,45 @@ 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:
if not variables.queues_send[client_id].empty():
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')

Expand All @@ -66,21 +66,18 @@ 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:
print("Terrible error!")
import traceback
traceback.print_exc()
soc.close()


18 changes: 9 additions & 9 deletions variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
lock_file_mes.append(lock)

0 comments on commit ca91b72

Please sign in to comment.