Skip to content

Commit

Permalink
not working
Browse files Browse the repository at this point in the history
  • Loading branch information
MartwyJez authored and akotwica committed Oct 11, 2020
1 parent ba8a0a1 commit 3482415
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
55 changes: 41 additions & 14 deletions chandelier/bt_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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.")
Expand All @@ -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!")
12 changes: 7 additions & 5 deletions chandelier/bt_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down
44 changes: 25 additions & 19 deletions chandelier/chandelier.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from time import sleep
import threading
from retry import retry
from chandelier import bt_devices, led, movement_sensor, pulseaudio

class Chandelier:
standby_on = True
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])

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit 3482415

Please sign in to comment.