Commit 0baccebb authored by Agrigor's avatar Agrigor

Added a simulation mode for server

parent 347f62e1
......@@ -9,6 +9,8 @@ import RPi.GPIO as GPIO
import sys
import socket
import threading
import time
import random
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -18,7 +20,6 @@ import threading
BUZZER_PINS = [35, 36, 37, 38]
BOUNCE = 300
PORT = 50000
......@@ -50,14 +51,14 @@ class Communication:
class Buzzer:
def __init__(self, number, board_pin, bounce, observers=set()):
def __init__(self, number, board_pin, bounce):
self.number = number
self.pin = board_pin
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
self.observers = set()
def create_event(self):
GPIO.add_event_detect(self.pin,
......@@ -82,10 +83,11 @@ class Buzzer:
class Server:
def __init__(self, ip, port):
def __init__(self, ip, port, simulation=0):
self.UDP = Communication(ip, port)
self.buzzers = [Buzzer(i, pin, BOUNCE) for i, pin in enumerate(BUZZER_PINS, start=1)]
self.UDP.observe_buzzers(self.buzzers)
self.sim = int(simulation)
# Initialize target ip address
try:
socket.inet_aton(ip)
......@@ -99,20 +101,35 @@ class Server:
print("\n Bye Bye")
sys.exit(0)
def send_simulation(self):
msg = "Button " + str(random.randint(1,4)) + " pressed"
print(msg)
self.UDP.send(msg)
def main(self):
try:
threading.Event().wait()
if self.sim == 1:
while True:
self.send_simulation()
time.sleep(1)
else:
threading.Event().wait()
finally:
self.cleanup()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Test if second script argument exists and
# set simulation mode with "1" or unset with "0"
if len(sys.argv) == 3:
SIM = sys.argv[2]
else:
SIM = 0
# Start Server
server = Server(sys.argv[1], PORT)
server = Server(sys.argv[1], PORT, SIM)
server.main()
......
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