Skip to content

Commit

Permalink
removed counter thread
Browse files Browse the repository at this point in the history
  • Loading branch information
gretzke committed Dec 10, 2019
1 parent 2b43608 commit f31dbc5
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions socket_py/socket_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ def __init__(self, parent=None):
def closeEvent(self, event):
print("closing")
# cleanup and end threads
try:
self.cs_thread.threadActive = False
self.cs_thread.wait()
except:
pass
print("cs_thread deactivated")
try:
self.ad_thread.threadActive = False
self.ad_thread.wait()
Expand Down Expand Up @@ -123,6 +117,7 @@ def updateInfoCenter(self, str):
self.ui.infoCenterLabel.setVisible(False)
return
self.ui.counterLabel.setVisible(False)
self.ws_thread.start_timestamp = 0
self.ui.infoCenterLabel.setText(str)
self.ui.infoCenterLabel.setVisible(True)

Expand Down Expand Up @@ -162,31 +157,6 @@ def run(self):
QThread().sleep(5)


class Counter(QThread):
def __init__(self, parent=window):
super(Counter, self).__init__(parent)
global window

self.threadActive = True
self.timestamp = time()

window.ui.counterLabel.setVisible(True)
window.ui.infoCenterLabel.setVisible(False)

def run(self):
global window
while self.threadActive:
if window.ui.counterLabel.isVisible() == False:
self.threadActive = False
break
time_passed = time()-self.timestamp
hrs = int(time_passed // 3600)
mnt = int(time_passed // 60)
sec = int(math.floor(time_passed % 60))
window.ui.counterLabel.setText("%02d:%02d:%02d" % (hrs, mnt, sec))
QThread().msleep(500)


# advertise socket service when no plug is connected
class Advertisement(QThread):
def __init__(self, parent=window):
Expand Down Expand Up @@ -224,6 +194,7 @@ def __init__(self, parent=window):
self.lastSignature = None
self.lastSignatureTmp = None
self.relayStatus = False
self.start_timestamp = 0

def run(self):
# start websocket server
Expand Down Expand Up @@ -264,6 +235,14 @@ async def wsServer(self, websocket, path):
if self.timestamp < time():
self.switchRelay(False)

# display counter if active
if self.start_timestamp > 0:
time_passed = time()-self.start_timestamp
hrs = int(time_passed // 3600)
mnt = int(time_passed // 60)
sec = int(math.floor(time_passed % 60))
window.ui.counterLabel.setText("%02d:%02d:%02d" % (hrs, mnt, sec))

await self.receiveMessage()

if self.paymentState != State.disconnected:
Expand Down Expand Up @@ -371,8 +350,9 @@ async def initialized_S(self):

async def active_P(self, parsedMessage):
if self.transactionCounter == 0:
window.cs_thread = Counter()
window.cs_thread.start()
self.start_timestamp = time()
window.ui.counterLabel.setVisible(True)
window.ui.infoCenterLabel.setVisible(False)
self.transactionCounter += 1
self.transactionValue = self.transactionCounter * \
self.pricePerSecond * self.secondsPerPayment
Expand Down

0 comments on commit f31dbc5

Please sign in to comment.