Commit d36920e0 authored by Agrigor's avatar Agrigor

Attributes are now private and server cleanup() finished

parent 5dde531f
......@@ -27,7 +27,6 @@ class Comunication:
def __init__(self, ip, port):
self.__ip = ip
self.__port = port
self.__socket = ""
def check_ip(self):
try:
......
......@@ -29,36 +29,44 @@ PORT = 50000
class Communication:
def __init__(self, ip, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.msg = ""
self.target_ip = ip
self.target_port = port
def close(self):
self.socket.close()
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.__target_ip = ip
self.__target_port = port
def send(self, message):
self.socket.sendto(str.encode(message), (self.target_ip, self.target_port))
self.__socket.sendto(str.encode(message), (self.__target_ip, self.__target_port))
def cleanup(self):
self.__socket.close()
def observe_buzzers(self, buzzers):
for buzzer in buzzers:
buzzer.register_observer(self)
def notify(self, buzzer):
self.msg = "Button " + str(buzzer.number) + " pressed"
self.__msg = "Button " + str(buzzer.number) + " pressed"
self.send(self.msg)
print(self.msg + " -> " + self.target_ip)
print(self.msg + " -> " + self.__target_ip)
class Buzzer:
def __init__(self, number, board_pin, bounce, observers=set()):
self.number = number
self.pin = board_pin
self.bounce = bounce
self.observers = observers
self.__bounce = bounce
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.create_event()
self.observers = observers
def create_event(self):
GPIO.add_event_detect(self.pin,
GPIO.RISING,
callback=self.notify_observer,
bouncetime=self.__bounce)
def cleanup(self):
GPIO.cleanup()
def register_observer(self, observer):
self.observers.add(observer)
......@@ -67,9 +75,6 @@ class Buzzer:
for observer in self.observers:
observer.notify(self)
def create_event(self):
GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.notify_observer, bouncetime=self.bounce)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -89,8 +94,8 @@ class Server:
sys.exit(1)
def cleanup(self):
GPIO.cleanup()
self.UDP.close()
self.UDP.cleanup()
self.buzzers[0].cleanup()
print("\n Bye Bye")
sys.exit(0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment