summaryrefslogtreecommitdiff
path: root/fakegeld.py
blob: bc7e53e4126540d24a09179b82b05feea50aab85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import threading
import os
import time

TIMEOUT = 30

class MoneyTransaction:
	def __init__(self, end_callback, add_money_callback) -> None:
		self.end_callback = end_callback
		self.add_money_callback = add_money_callback
		self.lock = threading.Lock()
		self.lock.acquire_lock(blocking=True)
		self.timer = None
		self.timer_lock = threading.Lock()
	
	def __end_callback(self, _ = None):
		print("End Callback")
		self.end_callback()
		with self.timer_lock:
			if self.timer is not None:
				self.timer.cancel()
			self.timer = None
		self.lock.release_lock()
		
	def start(self):
		print("Start Transaction")
		with self.timer_lock:
			self.timer = threading.Timer(TIMEOUT, self.__end_callback) # create Timeout
			self.timer.start()
		try:
			os.mkfifo("/tmp/fakegeld")
		except Exception as e:
			print(e)
		thread = threading.Thread(target = self.__loop)
		thread.start()
	def __loop(self):
		f = open("/tmp/fakegeld", "r")
		while True:
			time.sleep(0.1)
			line = f.readline()
			if line == "": continue
			print("read '%s'" % line)
			try:
				amount = float(line)
				print("got amount %s" % amount)
				self.add_money_callback(amount)
			except Exception as e:
				print(e)
			
	def stop(self):
		self.__end_callback()