2021-01-24 11:45:37 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
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
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
vibration_settings = {
|
2021-02-16 05:04:48 +01:00
|
|
|
'interval_minutes': 20,
|
|
|
|
'duration_seconds': 10,
|
|
|
|
'type': 'random',
|
|
|
|
'heartrate_alarm_pct': 17
|
2021-02-02 08:25:59 +01:00
|
|
|
}
|
2021-01-24 11:45:37 +01:00
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
band = None
|
2021-01-31 07:21:05 +01:00
|
|
|
|
2021-01-24 11:45:37 +01:00
|
|
|
#-------------------------------------------------------------------------#
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
|
2021-01-31 03:54:23 +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:
|
2021-01-31 03:54:23 +01:00
|
|
|
hwaddr_search = re.search(regex_patterns.mac_regex_pattern, f.read().strip())
|
2021-01-25 17:53:45 +01:00
|
|
|
if hwaddr_search:
|
|
|
|
MAC_ADDR = hwaddr_search[0]
|
2021-01-24 11:45:37 +01:00
|
|
|
else:
|
2021-01-25 17:53:45 +01:00
|
|
|
print ("No valid MAC address found in {}".format(filename))
|
2021-01-24 11:45:37 +01:00
|
|
|
exit(1)
|
|
|
|
except FileNotFoundError:
|
2021-01-25 17:53:45 +01:00
|
|
|
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:
|
2021-01-31 03:54:23 +01:00
|
|
|
key_search = re.search(regex_patterns.authkey_regex_pattern, f.read().strip())
|
2021-01-25 17:53:45 +01:00
|
|
|
if key_search:
|
|
|
|
AUTH_KEY = bytes.fromhex(key_search[0])
|
2021-01-24 11:45:37 +01:00
|
|
|
else:
|
2021-01-25 17:53:45 +01:00
|
|
|
print ("No valid auth key found in {}".format(filename))
|
2021-01-24 11:45:37 +01:00
|
|
|
exit(1)
|
|
|
|
except FileNotFoundError:
|
2021-01-25 17:53:45 +01:00
|
|
|
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()
|
2021-02-02 08:25:59 +01:00
|
|
|
|
2021-01-31 03:54:23 +01:00
|
|
|
if not sleepdata.last_tick_time:
|
|
|
|
sleepdata.last_tick_time = time.time()
|
2021-02-02 08:25:59 +01:00
|
|
|
|
2021-02-05 22:20:53 +01:00
|
|
|
if data[0] == "GYRO_RAW":
|
2021-02-02 08:25:59 +01:00
|
|
|
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-01-25 17:53:45 +01:00
|
|
|
|
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))
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
|
2021-02-01 03:02:26 +01:00
|
|
|
def connect():
|
2021-01-24 11:45:37 +01:00
|
|
|
global band
|
|
|
|
success = False
|
2021-01-25 17:53:45 +01:00
|
|
|
timeout = 3
|
2021-01-31 03:54:23 +01:00
|
|
|
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:
|
2021-01-25 17:53:45 +01:00
|
|
|
print(msg.format(timeout))
|
|
|
|
time.sleep(timeout)
|
2021-01-24 11:45:37 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\nExit.")
|
|
|
|
exit()
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
|
2021-01-24 11:45:37 +01:00
|
|
|
def start_data_pull():
|
|
|
|
while True:
|
|
|
|
try:
|
2021-01-30 09:54:56 +01:00
|
|
|
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()
|
2021-01-25 17:53:45 +01:00
|
|
|
|
2021-01-30 09:54:56 +01:00
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
def start_vibration():
|
2021-01-30 09:54:56 +01:00
|
|
|
while True:
|
2021-02-02 08:25:59 +01:00
|
|
|
try:
|
2021-02-16 05:04:48 +01:00
|
|
|
#vibration.timed_vibration(vibration_settings)
|
|
|
|
vibration.heartrate_alarm(vibration_settings)
|
|
|
|
|
2021-02-02 08:25:59 +01:00
|
|
|
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__":
|
2021-02-01 03:02:26 +01:00
|
|
|
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()
|
2021-02-02 08:25:59 +01:00
|
|
|
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-31 03:54:23 +01:00
|
|
|
|
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
|
2021-01-27 08:00:00 +01:00
|
|
|
# comfort_lasttime = time.time()
|