Beispiel #1
0
def read():
    console.Console().write("{} Reading running_devices.txt".format(
        console.Tags.BLT_RAD.value))

    found_devices = list()
    with open("blueToothRadar/running_devices.txt", "r") as file:
        data = file.readlines()
        file.close()

    for known_device_address in devices.KNOW_DEVICES:
        for line in data:
            if line.find(known_device_address) != -1:
                found_devices.append(known_device_address)
                break

    console.Console().write("{} running_devices.txt contained {} {} {}".format(
        console.Tags.BLT_RAD.value, colors.Colors.YELLOW.value, found_devices,
        colors.Colors.RESET.value))

    return found_devices
Beispiel #2
0
 def off(self, relay_number):
     state = "off"
     pin_number = self.relay_number_to_GPIO[relay_number]
     state = self.relay_state_to_logical_state[state]
     self.set_pin_to_out(pin_number)
     GPIO.output(pin_number, state)
     console.Console().write("{} Switched pin {} {} {} to {} {} {}".format(
         console.Tags.REL_MOD.value, console.colors.Colors.YELLOW.value,
         pin_number, console.colors.Colors.RESET.value,
         console.colors.Colors.YELLOW.value, GPIO.input(pin_number),
         console.colors.Colors.RESET.value))
Beispiel #3
0
    def __init__(self):
        self.console = console.Console()
        self.relay_states = dict()
        self.relay = relay_controller.Relay()

        self.console.write("{} Initialized".format(console.Tags.LIGHTS.value))
Beispiel #4
0
def scanner():
    """
    scan for new devices
    if device was found function evaluate avarange of its rssi
    :return:
    """
    # it's file in which scanner holds btmgmt output
    tmp = tempfile.NamedTemporaryFile()
    with open(tmp.name, "w") as file:
        subprocess.run(["/usr/bin/btmgmt", "find"], stdout=file)
        file.close()
    # read results of command run
    with open(tmp.name, "r") as file:
        output = file.readlines()
        # print(output)
        result = dict()
        for line in output:
            words = line.split(" ")
            device_address = str()
            device_rssi = int()
            for i in range(len(words)):
                # loop through one line of output to find device name and corresponding rssi
                if words[i] == "dev_found:":
                    device_address = words[i + 1]
                elif words[i] == "rssi":
                    device_rssi = words[i + 1]
            # check if device was previously found
            if device_address in result:
                result[device_address].append(device_rssi)
            else:
                result[device_address] = [device_rssi]

        # print(result)

        # swap values in results form list of ints - rssi to it avg
        for device in result:
            result[device] = avg(result[device])

        # save results to file
        # and print to joint console
        with open("blueToothRadar/running_devices.txt", "w") as f:
            running_devices = list()
            for known_device in devices.KNOW_DEVICES:
                if known_device in result:
                    if result[known_device] > detection_threshold:
                        console.Console().write(
                            "{} Device {} {} {} with address {} {} {} detected {} its avarange RSSI is {} {} {}"
                            .format(
                                console.Tags.BLT_RAD.value,
                                colors.Colors.YELLOW.value,
                                devices.KNOW_DEVICES[known_device].get_name(),
                                colors.Colors.RESET.value,
                                colors.Colors.CYAN.value, known_device,
                                colors.Colors.GREEN.value,
                                colors.Colors.RESET.value,
                                colors.Colors.BLUE.value,
                                int(result[known_device]),
                                colors.Colors.RESET.value))

                        running_devices.append(known_device + "\n")
                else:
                    console.Console().write(
                        "{} Device {} {} {} with address {} {} {} not detected {}"
                        .format(console.Tags.BLT_RAD.value,
                                colors.Colors.YELLOW.value,
                                devices.KNOW_DEVICES[known_device].get_name(),
                                colors.Colors.RESET.value,
                                colors.Colors.CYAN.value, known_device,
                                colors.Colors.RED.value,
                                colors.Colors.RESET.value))

            f.writelines(running_devices)
            f.close()
        # print()

        file.close()