Commit d36920e0 authored by Agrigor's avatar Agrigor

Attributes are now private and server cleanup() finished

parent 5dde531f
...@@ -27,7 +27,6 @@ class Comunication: ...@@ -27,7 +27,6 @@ class Comunication:
def __init__(self, ip, port): def __init__(self, ip, port):
self.__ip = ip self.__ip = ip
self.__port = port self.__port = port
self.__socket = ""
def check_ip(self): def check_ip(self):
try: try:
......
...@@ -29,36 +29,44 @@ PORT = 50000 ...@@ -29,36 +29,44 @@ PORT = 50000
class Communication: class Communication:
def __init__(self, ip, port): def __init__(self, ip, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.msg = "" self.__target_ip = ip
self.target_ip = ip self.__target_port = port
self.target_port = port
def close(self):
self.socket.close()
def send(self, message): 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): def observe_buzzers(self, buzzers):
for buzzer in buzzers: for buzzer in buzzers:
buzzer.register_observer(self) buzzer.register_observer(self)
def notify(self, buzzer): def notify(self, buzzer):
self.msg = "Button " + str(buzzer.number) + " pressed" self.__msg = "Button " + str(buzzer.number) + " pressed"
self.send(self.msg) self.send(self.msg)
print(self.msg + " -> " + self.target_ip) print(self.msg + " -> " + self.__target_ip)
class Buzzer: class Buzzer:
def __init__(self, number, board_pin, bounce, observers=set()): def __init__(self, number, board_pin, bounce, observers=set()):
self.number = number self.number = number
self.pin = board_pin self.pin = board_pin
self.bounce = bounce self.__bounce = bounce
self.observers = observers
GPIO.setmode(GPIO.BOARD) GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.create_event() 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): def register_observer(self, observer):
self.observers.add(observer) self.observers.add(observer)
...@@ -67,9 +75,6 @@ class Buzzer: ...@@ -67,9 +75,6 @@ class Buzzer:
for observer in self.observers: for observer in self.observers:
observer.notify(self) observer.notify(self)
def create_event(self):
GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.notify_observer, bouncetime=self.bounce)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -89,8 +94,8 @@ class Server: ...@@ -89,8 +94,8 @@ class Server:
sys.exit(1) sys.exit(1)
def cleanup(self): def cleanup(self):
GPIO.cleanup() self.UDP.cleanup()
self.UDP.close() self.buzzers[0].cleanup()
print("\n Bye Bye") print("\n Bye Bye")
sys.exit(0) 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