From 348241559721ae7f5779b3a494b0ccdb2ab3c8f2 Mon Sep 17 00:00:00 2001 From: arek152 Date: Sat, 10 Oct 2020 14:55:39 +0000 Subject: [PATCH] not working --- chandelier/bt_device.py | 55 ++++++++++++++++++++++++++++++---------- chandelier/bt_devices.py | 12 +++++---- chandelier/chandelier.py | 44 ++++++++++++++++++-------------- 3 files changed, 73 insertions(+), 38 deletions(-) diff --git a/chandelier/bt_device.py b/chandelier/bt_device.py index d1bb1e4..31ddd14 100644 --- a/chandelier/bt_device.py +++ b/chandelier/bt_device.py @@ -17,7 +17,19 @@ class BluetoothError(Exception): class DeviceNotFound(Exception): """Raised when given device can't be found""" - def check_if_connected(self): + def reset_config(self): + try: + hci_name = pexpect.spawn('hcitool dev | grep hc.. -o').read() + hci = pexpect.spawn("sudo hciconfig "+ hci_name +" reset") + if hci.expect([self.addr, pexpect.EOF]) == 0: + return True + else: + return False + except pexpect.ExceptionPexpect as exception: + errprint(exception) + return False + + def is_connected(self): try: hci = pexpect.spawn("hcitool con") if hci.expect([self.addr, pexpect.EOF]) == 0: @@ -32,21 +44,28 @@ def __end_connection__(self): self.__interface__.sendline("\ndisconnect") self.__interface__.expect("diconnected", timeout=5) - @retry(pexpect.exceptions.TIMEOUT, tries=20) - def __check_if_device_is_visible__(self): + @retry(pexpect.exceptions.TIMEOUT, tries=20, delay=1) + def __is_device_visible__(self): try: self.__interface__.sendline("\ndevices") self.__interface__.expect(self.addr) except self.DeviceNotFound as exception: raise exception - @retry(pexpect.exceptions.TIMEOUT, tries=120) + @retry(pexpect.exceptions.TIMEOUT, tries=10, delay=2) def __connect_to_device__(self): - if not self.check_if_connected: + if not self.is_connected(): self.__end_connection__() sleep(3) self.__interface__.sendline("\nconnect "+self.addr) self.__interface__.expect("Connection successful", timeout=5) + sleep(2) + if not self.is_connected(): + raise pexpect.exceptions.TIMEOUT + + def __trust__(self): + self.__interface__.sendline("\ntrust " + self.addr) + self.__interface__.expect("trust succeeded") def __pair__(self): try: @@ -91,12 +110,16 @@ def __prepare_bluetooth__(self, __agent__): def __connect__(self): try: - self.__check_if_device_is_visible__() + self.__is_device_visible__() stdprint("Device " + self.addr + " is visible") + sleep(1) + self.__trust__() self.__pair__() + sleep(1) stdprint("Device " + self.addr + " is paired") self.__connect_to_device__() - stdprint("Connection to " + self.addr + " are estabilished") + sleep(1) + stdprint("Connection to " + self.addr + " is estabilished") except self.DeviceNotFound as exception: errprint(exception) errprint("Rasberry couldn't find device or connect.") @@ -111,23 +134,27 @@ def __init__(self, addr, debug=False, agent="NoInputNoOutput"): self.addr = addr self.__debug_on__ = debug self.__agent__ = agent - if self.check_if_connected(): + if self.is_connected(): stdprint("Device already connected.") return None + self.reset_config() self.__interface__ = self.__open_interface__() self.__prepare_bluetooth__(self.__agent__) self.__connect__() self.__interface__.kill(15) except Exception as exception: + #self.__interface__.kill(15) print(exception) self.__interface__ = self.__open_interface__() #self.__end_connection__() raise exception #def __del__(self): - # self.__interface__ = self.__open_interface__() - # try: - # #self.__end_connection__() - # except: - # pass - # stdprint("Device " + self.addr + " is disconnected!") + # if self.__debug_on__: + # print("disconnect") + # self.__interface__ = self.__open_interface__() + # try: + # self.__end_connection__() + # except: + # pass + # stdprint("Device " + self.addr + " is disconnected!") diff --git a/chandelier/bt_devices.py b/chandelier/bt_devices.py index 24aae64..dea3a00 100644 --- a/chandelier/bt_devices.py +++ b/chandelier/bt_devices.py @@ -7,14 +7,14 @@ class BluetoothSpeaker(bt_device.BluetoothDevice): - @retry(Exception, tries=5, delay=3) + @retry(Exception, tries=20, delay=3) def __get_pulse_audio_sink__(self): with pulsectl.Pulse('list-sinks') as pulse: for sink in pulse.sink_list(): if sink.proplist.get('device.string') == self.addr: return sink.index, sink.name - def __init__(self, addr, debug=False, agent="NoInputNoOutput"): + def __init__(self, addr, debug=False, agent="NoInputNoOutput"): super().__init__(addr, debug, agent) sleep(3) try: @@ -29,15 +29,17 @@ class BluetoothRemote(bt_device.BluetoothDevice): __loop = "" remote_inputs = [] output = "" - @retry(Exception, tries=5, delay=3) + def __remote_devices__(self, addr): remote_address = addr devices = [evdev.InputDevice(path) for path in evdev.list_devices()] for device in devices: - if device.phys not in remote_address: + if device.phys == '': devices.remove(device) + if len(devices) == 0: + raise Exception return devices - + def stop_waiting_on_output(self): self.__loop.stop() diff --git a/chandelier/chandelier.py b/chandelier/chandelier.py index 0b4caf5..43a19de 100644 --- a/chandelier/chandelier.py +++ b/chandelier/chandelier.py @@ -1,5 +1,6 @@ from time import sleep import threading +from retry import retry from chandelier import bt_devices, led, movement_sensor, pulseaudio class Chandelier: @@ -7,10 +8,14 @@ class Chandelier: playing_voices = False press_duration = "" + @retry(Exception, tries=10, delay=2) + def connect_to_remote(self, addr): + remote = bt_devices.BluetoothRemote(addr) + return remote + def __init__(self, sensors_pins, led_pins, speaker_addr, remote_addr): - self.blt_speaker = bt_devices.BluetoothSpeaker( - speaker_addr, debug=False) - self.remote = bt_devices.BluetoothRemote(remote_addr) + self.blt_speaker = bt_devices.BluetoothSpeaker(speaker_addr) + self.remote = self.connect_to_remote(remote_addr) self.sensors = movement_sensor.MovementSensors(sensors_pins) self.led = led.Led(led_pins[0], led_pins[1], led_pins[2]) @@ -92,6 +97,7 @@ def wait_between_seq(self): ambient = pulseaudio.Play([(self.blt_speaker.pa_index, 0.8)], "../ambient1.wav") button_thread = threading.Thread(target=self.wait_for_pick_up) button_thread.join(175) + ambient.stop_playing() def wait_for_beggining(self): led_thread = threading.Thread(target=self.__led_pulsing_white) @@ -139,19 +145,19 @@ def wait_for_pick_up(self): self.press_duration = "short" -def main(): - - while True: - chandelier = None - chandelier = Chandelier([16, 19, 26], [20, 21, 21], - "0C:A6:94:62:67:40", "2A:07:98:10:34:2C") # sensors, leds, speaker, remote - chandelier.wait_for_beggining() - chandelier.led_white() - chandelier.play_ringtone(1) - chandelier.wait_for_pick_up() - chandelier.stop_playing_ringtone() - press_duration = chandelier.voices() - if press_duration == "long": - chandelier.fail() - else: - chandelier.success() +def main(): + chandelier = Chandelier([16, 19, 26], [20, 21, 21], + "0C:A6:94:62:67:40", "2A:07:98:10:32:05") # sensors, leds, speaker, remote + chandelier.wait_for_beggining() + chandelier.led_white() + sleep(2) + chandelier.play_ringtone(1) + chandelier.wait_for_pick_up() + chandelier.stop_playing_ringtone() + sleep(2) + press_duration = chandelier.voices() + if press_duration == "long": + chandelier.fail() + else: + chandelier.success() + chandelier.wait_between_seq() \ No newline at end of file