Skip to content

Commit

Permalink
added more logging for debugging the activity issue
Browse files Browse the repository at this point in the history
  • Loading branch information
janvda committed Nov 24, 2021
1 parent e76cea7 commit 1179fd2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
17 changes: 11 additions & 6 deletions miband.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def handleNotification(self, hnd, data):
self.device.queue.put((QUEUE_TYPES.RAW_HEART, data))
# The fetch characteristic controls the communication with the activity characteristic.
elif hnd == self.device._char_fetch.getHandle():
print(f"_char_fetch.getHandle(): {len(data)} bytes received, processing them ...")
if data[:3] == b'\x10\x01\x01':
# get timestamp from what date the data actually is received
year = struct.unpack("<H", data[7:9])[0]
Expand All @@ -62,25 +63,26 @@ def handleNotification(self, hnd, data):
hour = struct.unpack("b", data[11:12])[0]
minute = struct.unpack("b", data[12:13])[0]
self.device.first_timestamp = datetime(year, month, day, hour, minute)
print("Fetch data from {}-{}-{} {}:{}".format(year, month, day, hour, minute))
print(" > Fetch data from {}-{}-{} {}:{}".format(year, month, day, hour, minute))
self.pkg = 0 #reset the packing index
self.device._char_fetch.write(b'\x02', False)
elif data[:3] == b'\x10\x02\x01':
if self.device.last_timestamp > self.device.end_timestamp - timedelta(minutes=1):
print("Finished fetching")
print(" > Finished fetching")
return
print("Trigger more communication")
print(" > Trigger more communication")
time.sleep(1)
t = self.device.last_timestamp + timedelta(minutes=1)
self.device.start_get_previews_data(t)

elif data[:3] == b'\x10\x02\x04':
print("No more activity fetch possible")
print(" > No more activity fetch possible")
return
else:
print("Unexpected data on handle " + str(hnd) + ": " + str(data))
print(" > Unexpected data on handle " + str(hnd) + ": " + str(data))
return
elif hnd == self.device._char_activity.getHandle():
print(f"_char_activity.getHandle(): {len(data)} bytes received, processing them ...")
if len(data) % 4 == 1:
self.pkg += 1
i = 1
Expand All @@ -94,6 +96,8 @@ def handleNotification(self, hnd, data):
heart_rate = struct.unpack("B", data[i + 3:i + 4])[0]
if timestamp < self.device.end_timestamp:
self.device.activity_callback(timestamp,category,intensity,steps,heart_rate)
else:
print(f" >ignoring received data as timestamp={timestamp} >= end_timestamp={self.device.end_timestamp}")
i += 4

#music controls & lost device
Expand Down Expand Up @@ -549,7 +553,7 @@ def start_get_previews_data(self, start_timestamp):
if not self.activity_notif_enabled:
self._auth_previews_data_notif(True)
self.waitForNotifications(0.1)
print("Trigger activity communication")
print(f"start_get_previews_data({start_timestamp})...")
year = struct.pack("<H", start_timestamp.year)
month = struct.pack("b", start_timestamp.month)
day = struct.pack("b", start_timestamp.day)
Expand All @@ -558,6 +562,7 @@ def start_get_previews_data(self, start_timestamp):
ts = year + month + day + hour + minute
char = self.svc_1.getCharacteristics(UUIDS.CHARACTERISTIC_CURRENT_TIME)[0]
utc_offset = char.read()[9:11]
print(f" >uct_offset={utc_offset}")
trigger = b'\x01\x01' + ts + utc_offset
self._char_fetch.write(trigger, False)
self.active = True
Expand Down
6 changes: 3 additions & 3 deletions miband4_console.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ def found_device_callback():


def activity_log_callback(timestamp,c,i,s,h):
print("{}: category: {}; intensity {}; steps {}; heart rate {};\n".format( timestamp.strftime('%d.%m - %H:%M'), c, i ,s ,h))
print(" > {}: category: {}; intensity {}; steps {}; heart rate {}".format( timestamp.strftime('%Y-%m-%d %H:%M'), c, i ,s ,h))

#Needs auth
def get_activity_logs():
#gets activity log for this day.
temp = datetime.now()
band.get_activity_betwn_intervals(datetime(temp.year,temp.month,temp.day),datetime.now(),activity_log_callback)
band.get_activity_betwn_intervals(datetime(temp.year,temp.month,temp.day,temp.hour),datetime.now(),activity_log_callback)
while True:
band.waitForNotifications(0.2)

Expand Down Expand Up @@ -244,7 +244,7 @@ def get_activity_logs():
steps_item = FunctionItem("@ Get Steps/Meters/Calories/Fat Burned", get_step_count)
single_heart_rate_item = FunctionItem("@ Get Heart Rate", get_heart_rate)
real_time_heart_rate_item = FunctionItem("@ Get realtime heart rate data", get_realtime)
get_band_activity_data_item = FunctionItem("@ Get activity logs for a day", get_activity_logs)
get_band_activity_data_item = FunctionItem("@ Get activity logs for current hour", get_activity_logs)
set_time_item= FunctionItem("@ Set the band's time to system time", set_time)
update_watchface_item = FunctionItem("@ Update Watchface", update_watchface)
dfu_update_item = FunctionItem("@ Restore/Update Firmware", restore_firmware)
Expand Down
8 changes: 4 additions & 4 deletions miband_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ def post_wait_for_notifications():
@return_404_if_not_connected
@protect_by_miband_lock
def get_info():
info = { "software revision" : band.get_revision(),
"hardware revision" : band.get_hrdw_revision(),
"serial number" : band.get_serial(),
"battery level" : band.get_battery_info()['level'],
info = { "software_version" : band.get_revision(),
"hardware_version" : band.get_hrdw_revision(),
"serial_number" : band.get_serial(),
"battery_level" : band.get_battery_info()['level'],
"time" : band.get_current_time()['date'].isoformat()
}
return info
Expand Down

0 comments on commit 1179fd2

Please sign in to comment.