Skip to content

Commit

Permalink
not working
Browse files Browse the repository at this point in the history
  • Loading branch information
MartwyJez committed Oct 10, 2020
1 parent ba8a0a1 commit 828eb10
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
59 changes: 41 additions & 18 deletions chandelier/bt_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ 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 = pexpect.spawn("sudo hciconfig hci0 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 +43,25 @@ 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:
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 +106,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 +130,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.__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!")
def __del__(self):
print("disconnect")
#if self.__debug_on__:
# 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
@@ -1,13 +1,13 @@
import asyncio
from retry import retry
from time import sleep
from retry import retry
import evdev
import pulsectl
from . import bt_device, utilities

class BluetoothSpeaker(bt_device.BluetoothDevice):

@retry(Exception, tries=5, delay=3)
@retry(Exception, tries=20, delay=1)
def __get_pulse_audio_sink__(self):
with pulsectl.Pulse('list-sinks') as pulse:
for sink in pulse.sink_list():
Expand All @@ -29,13 +29,15 @@ 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):
Expand Down
19 changes: 14 additions & 5 deletions chandelier/chandelier.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
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=20, delay=1)
def connect_to_remote(self, addr):
return bt_devices.BluetoothRemote(addr)

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.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 +98,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 +146,21 @@ def wait_for_pick_up(self):
self.press_duration = "short"


def main():

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
"0C:A6:94:62:67:40", "2A:07:98:10:32:05") # sensors, leds, speaker, remote
chandelier.wait_for_beggining()
chandelier.led_white()
sleep(1000)
chandelier.play_ringtone(1)
chandelier.wait_for_pick_up()
#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 828eb10

Please sign in to comment.