import RPi.GPIO as GPIO
import json
import paho.mqtt.client as mqtt
import time
import asyncio

thread = None

score_topic = "foosball/score"
speed_topic = "foosball/speed"

# 192.168.195.7 was IR 829 Broker
broker_ip = "10.8.182.131"  # <--- Please change IP to match the location of your MQTT broker

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

ir = 15
ir2 = 18

mqttc = mqtt.Client()
mqttc.connect(broker_ip)
mqttc.loop_start()

GPIO.setup(ir, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(ir2, GPIO.IN, GPIO.PUD_DOWN)

start = 0
stop = 0


async def data_collect_ir():
    GPIO.add_event_detect(ir, GPIO.BOTH, callback=process_edge, bouncetime=5)


def process_edge(channel):
    if GPIO.input(channel):  # test if pin is high
        post_speed(channel)
    else:
        post_score(channel)


def post_score(channel):
    global start
    start = time.time()
    print("Start time is:")
    print(start)
    brokerMessage = {'Status': 'scored', 'Player': '2', 'Score': 1, 'Data': '0'}
    print("message sent")
    mqttc.publish(score_topic, json.dumps(brokerMessage))


def post_speed(channel):
    global stop
    stop = time.time()
    print("Stop time is:")
    print(stop)
    if stop > start:
        elapsed = stop - start
        print("Elapsed time is:")
        print(elapsed)
        speed = .0345 / elapsed  # meters per second
        mph = 2.23694 * speed  # convert meters/s to mph
        print("posting speed")
        print(mph)
        brokerMessage = {'Status': 'speed', 'Speed': mph}
        mqttc.publish(speed_topic, json.dumps(brokerMessage))


if __name__ == '__main__':
    # data_collect()
    loop = asyncio.get_event_loop()
    # tasks = [asyncio.ensure_future(data_collect_ir()), asyncio.ensure_future(data_collect_ir2())]
    tasks = [asyncio.get_event_loop().run_until_complete(data_collect_ir())]
    loop.run_forever()
    print("started")
    
import RPi.GPIO as GPIO
import json
import paho.mqtt.client as mqtt
import time
import asyncio

thread = None

score_topic = "foosball/score"
speed_topic = "foosball/speed"

# 192.168.195.7 was IR 829 Broker
broker_ip = "10.8.182.131"  # <--- Please change IP to match the location of your MQTT broker

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

ir = 15
ir2 = 18

mqttc = mqtt.Client()
mqttc.connect(broker_ip)
mqttc.loop_start()

GPIO.setup(ir, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(ir2, GPIO.IN, GPIO.PUD_DOWN)

start = 0
stop = 0


async def data_collect_ir():
    GPIO.add_event_detect(ir, GPIO.BOTH, callback=process_edge, bouncetime=5)


def process_edge(channel):
    if GPIO.input(channel):  # test if pin is high
        post_speed(channel)
    else:
        post_score(channel)


def post_score(channel):
    global start
    start = time.time()
    print("Start time is:")
    print(start)
    brokerMessage = {'Status': 'scored', 'Player': '2', 'Score': 1, 'Data': '0'}
    print("message sent")
    mqttc.publish(score_topic, json.dumps(brokerMessage))


def post_speed(channel):
    global stop
    stop = time.time()
    print("Stop time is:")
    print(stop)
    if stop > start:
        elapsed = stop - start
        print("Elapsed time is:")
        print(elapsed)
        speed = .0345 / elapsed  # meters per second
        mph = 2.23694 * speed  # convert meters/s to mph
        print("posting speed")
        print(mph)
        brokerMessage = {'Status': 'speed', 'Speed': mph}
        mqttc.publish(speed_topic, json.dumps(brokerMessage))


if __name__ == '__main__':
    # data_collect()
    loop = asyncio.get_event_loop()
    # tasks = [asyncio.ensure_future(data_collect_ir()), asyncio.ensure_future(data_collect_ir2())]
    tasks = [asyncio.get_event_loop().run_until_complete(data_collect_ir())]
    loop.run_forever()
    print("started")
    
# 192.168.195.7 was IR 829 Broker # <--- Please change IP to match the location of your MQTT broker # test if pin is high # meters per second # convert meters/s to mph # data_collect() # tasks = [asyncio.ensure_future(data_collect_ir()), asyncio.ensure_future(data_collect_ir2())]