blesleep/bluesleep.py

142 lines
3.8 KiB
Python
Raw Normal View History

2021-01-24 11:45:37 +01:00
#!/usr/bin/env python3
import time, re, threading
2021-01-24 11:45:37 +01:00
from bluepy.btle import BTLEDisconnectError
from miband import miband
2021-02-06 02:14:25 +01:00
import sleepdata
from vibrate import Vibrate
2021-01-24 11:45:37 +01:00
auth_key_filename = 'auth_key.txt'
mac_filename = 'mac.txt'
2021-02-05 22:20:53 +01:00
maximize_graph = False
vibration_settings = {
2021-02-16 05:04:48 +01:00
'interval_minutes': 20,
'duration_seconds': 10,
'type': 'random',
'heartrate_alarm_pct': 17
}
2021-01-24 11:45:37 +01:00
band = None
2021-01-31 07:21:05 +01:00
2021-01-24 11:45:37 +01:00
#-------------------------------------------------------------------------#
class regex_patterns():
mac_regex_pattern = re.compile(r'([0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})')
authkey_regex_pattern = re.compile(r'([0-9a-fA-F]){32}')
2021-01-24 11:45:37 +01:00
def get_mac_address(filename):
try:
with open(filename, "r") as f:
hwaddr_search = re.search(regex_patterns.mac_regex_pattern, f.read().strip())
if hwaddr_search:
MAC_ADDR = hwaddr_search[0]
2021-01-24 11:45:37 +01:00
else:
print ("No valid MAC address found in {}".format(filename))
2021-01-24 11:45:37 +01:00
exit(1)
except FileNotFoundError:
print ("MAC file not found: {}".format(filename))
2021-01-24 11:45:37 +01:00
exit(1)
return MAC_ADDR
def get_auth_key(filename):
try:
with open(filename, "r") as f:
key_search = re.search(regex_patterns.authkey_regex_pattern, f.read().strip())
if key_search:
AUTH_KEY = bytes.fromhex(key_search[0])
2021-01-24 11:45:37 +01:00
else:
print ("No valid auth key found in {}".format(filename))
2021-01-24 11:45:37 +01:00
exit(1)
except FileNotFoundError:
print ("Auth key file not found: {}".format(filename))
2021-01-24 11:45:37 +01:00
exit(1)
return AUTH_KEY
2021-01-31 07:21:05 +01:00
def average_data(tick_time):
if (tick_time - sleepdata.last_tick_time) >= sleepdata.tick_seconds:
sleepdata.average_raw_data(tick_time)
sleepdata.last_tick_time = time.time()
2021-02-16 05:04:48 +01:00
2021-01-24 11:45:37 +01:00
def sleep_monitor_callback(data):
tick_time = time.time()
if not sleepdata.last_tick_time:
sleepdata.last_tick_time = time.time()
2021-02-05 22:20:53 +01:00
if data[0] == "GYRO_RAW":
sleepdata.process_gyro_data(data[1], tick_time)
elif data[0] == "HR":
sleepdata.process_heartrate_data(data[1], tick_time)
2021-01-31 07:21:05 +01:00
average_data(tick_time)
2021-02-16 05:04:48 +01:00
vibration.heartrate_increase_pct = sleepdata.analyze_heartrate(10)
print("HR increase percent: {}".format(vibration.heartrate_increase_pct))
def connect():
2021-01-24 11:45:37 +01:00
global band
success = False
timeout = 3
msg = 'Connection to the band failed. Trying again in {} seconds'
2021-01-24 11:45:37 +01:00
MAC_ADDR = get_mac_address(mac_filename)
AUTH_KEY = get_auth_key(auth_key_filename)
while not success:
try:
band = miband(MAC_ADDR, AUTH_KEY, debug=True)
success = band.initialize()
except BTLEDisconnectError:
print(msg.format(timeout))
time.sleep(timeout)
2021-01-24 11:45:37 +01:00
except KeyboardInterrupt:
print("\nExit.")
exit()
2021-01-24 11:45:37 +01:00
def start_data_pull():
while True:
try:
band.start_heart_and_gyro(sensitivity=1, callback=sleep_monitor_callback)
2021-01-24 11:45:37 +01:00
except BTLEDisconnectError:
band.gyro_started_flag = False
connect()
def start_vibration():
while True:
try:
2021-02-16 05:04:48 +01:00
#vibration.timed_vibration(vibration_settings)
vibration.heartrate_alarm(vibration_settings)
except BTLEDisconnectError:
print("Vibration thread waiting for band reconnect...")
time.sleep(1)
2021-01-24 22:04:28 +01:00
2021-01-24 11:45:37 +01:00
if __name__ == "__main__":
connect()
2021-02-06 02:14:25 +01:00
vibration = Vibrate(band)
2021-01-31 07:21:05 +01:00
threading.Thread(target=start_data_pull).start()
threading.Thread(target=start_vibration).start()
2021-02-05 22:20:53 +01:00
sleepdata.init_graph(maximize=maximize_graph, graph_displaytime_mins=5)
2021-01-24 11:45:37 +01:00
#import simpleaudio as sa
# comfort_wav = 'comfort.wav'
# wave_obj = sa.WaveObject.from_wave_file(comfort_wav)
# comfort_delay = 30
# comfort_lasttime = time.time()