from time import sleep

# This script will receive data from a central dongle
# and echo it back to the central

my_dongle = None
connected_to_dongle = False
connected_to_central = False
message = ''

while not connected_to_dongle:
    try:
        # Specify the COM PORT connected to the dongle
        my_dongle = BleuIo()
        # Start the deamon (background process handler) for RX and TX data.
        my_dongle.start_daemon()

        connected_to_dongle = True
    except SerialException:
        print('Dongle not found. Please connect your dongle')
        sleep(5)

print('Connected to dongle\n\n'
      'Welcome to the BleuIO SPS example!\n\n')  # //TODO byt namn på de andra

# Starts advertising so it can be detected
my_dongle.at_advstart()

try:
    time_out_counter = 0
    i = 0
import time
from bleuio_lib.bleuio_funcs import BleuIo
import random

sender_dongle_port = "COM73"  # Change this to your dongle's COM port
mac_addr_to_repeater = (
    "[0]40:48:FD:E5:2D:74"  # Change this to your repeater dongle's mac address
)

sender_dongle = BleuIo(port=sender_dongle_port)
sender_dongle.start_daemon()

connected = False
data = ""
buffer = ""


def scan_and_get_results():
    """
    Starts a BLE scan for three seconds, looking for results including the 'FF' flag in the advertising data.
    Then it saves the results in a list and returns one of the results as a string.
    :return: string
    """
    print("Scanning...")
    return_data = ""
    result_list = []
    time.sleep(0.5)
    try:
        scanning = sender_dongle.at_findscandata("FF")
        if "SCANNING" in scanning[0]:
            time.sleep(4)
import time
from bleuio_lib.bleuio_funcs import BleuIo


reciever_dongle_port = "COM74"  # Change this to your dongle's COM port
mac_addr_to_repeater = (
    "[0]40:48:FD:E5:2D:74"  # Change this to your repeater dongle's mac address
)

buffer = ""
connected = False
connecting = ""

reciever_dongle = BleuIo(port=reciever_dongle_port)
reciever_dongle.start_daemon()

print("Dongle found.")


def save_msg(buffer):
    """
    Parses incomming data string for just the data and prints it out.
    """
    result = buffer
    result_array1 = result.split("\r\n")
    result_array = result_array1[2].split(" ")
    msg_to_save = str(result_array[0])
    print("Recieved = " + msg_to_save)


try:
import time
from bleuio_lib.bleuio_funcs import BleuIo

repeter_dongle_port = "COM38"  # Change this to your dongle's COM port

repeter_dongle = BleuIo(port=repeter_dongle_port)
repeter_dongle.start_daemon()

buffer = ""
num_of_connected_devices = 0
connection_list = []

print("Dongle found.")

repeter_dongle.at_dual()
repeter_dongle.at_advstart()
repeter_dongle.rx_state = "rx_waiting"
print("Waiting for other dongles to connect...")


def send_msg(buffer):
    """
    Parses incomming data string for just the data and sends it forward via the Serial Port Service.
    """
    try:
        result = buffer
        msg_to_send = ""
        result_array1 = result.split("\r\n")
        for line in result_array1:
            if "[Received]:" in line:
                msg_to_send = line.split(" ")