diff --git a/app/mifit_to_influxdb.py b/app/mifit_to_influxdb.py
--- a/app/mifit_to_influxdb.py
+++ b/app/mifit_to_influxdb.py
#
@@ -520,6 +520,133 @@ def processODIEvent(record):
#
+def get_PAI_data(auth_info, config):
#
+ ''' Retrieve Personal Actitivity Intelligence scoring data
#
+ TODO: This could definitely be DRYer
#
+ ''' calculate the times that the api query should check between
#
+ today = datetime.datetime.today()
#
+ today_end = datetime.datetime.combine(today, datetime.datetime.max.time())
#
+ query_start_d = today - datetime.timedelta(days=config['QUERY_DURATION'])
#
+ # Make it midnight - the api doesn't seem to like mid-day queries
#
+ query_start = datetime.datetime.combine(query_start_d, datetime.datetime.min.time())
#
+ print("Retrieving PAI data")
#
+ band_data_url=f"https://api-mifit-de2.zepp.com/users/{auth_info['token_info']['user_id']}/events"
#
+ 'apptoken': auth_info['token_info']['app_token'],
#
+ "from" : query_start.strftime('%s000'),
#
+ "to" : today_end.strftime('%s000'),
#
+ "eventType" : "PaiHealthInfo",
#
+ "timeZone" : "Europe/London"
#
+ response=requests.get(band_data_url,params=data,headers=headers)
#
+ r_json = response.json()
#
+ if "items" not in r_json:
#
+ # Iterate through the daily entries
#
+ for item in r_json['items']:
#
+ timestamp_ms = int(item['timestamp'])
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "heart_rate" : int(item['maxHr']),
#
+ "PAI_measure" : "daily",
#
+ "hr_measure" : "PAI",
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "heart_rate" : int(item['restHr']),
#
+ "PAI_measure" : "daily",
#
+ "hr_measure" : "PAI",
#
+ "hr_state" : "resting"
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "activity_duration_m" : int(item['lowZoneMinutes']),
#
+ "pai_score_bound" : int(item['lowZoneLowerLimit']),
#
+ "pai_score" : float(item['lowZonePai'])
#
+ "PAI_measure" : "daily",
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "activity_duration_m" : int(item['mediumZoneMinutes']),
#
+ "pai_score_bound" : int(item['mediumZoneLowerLimit']),
#
+ "pai_score" : float(item['mediumZonePai'])
#
+ "PAI_measure" : "daily",
#
+ "PAI_bound" : "medium"
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "activity_duration_m" : int(item['highZoneMinutes']),
#
+ "pai_score_bound" : int(item['highZoneLowerLimit']),
#
+ "pai_score" : float(item['highZonePai'])
#
+ "PAI_measure" : "daily",
#
+ "timestamp": timestamp_ms * 1000000, # Convert to nanos
#
+ "scorable_activities" : len(item['activityScores']),
#
+ "pai_score" : float(item['dailyPai']),
#
+ "total_pai" : float(item['totalPai'])
#
+ "PAI_measure" : "daily",
#
+ "PAI_bound" : "daily"
#
def get_stress_data(auth_info, config):
#
''' Retrieve stress level information
#
@@ -650,6 +777,14 @@ def main():
#
print("Failed to collect blood oxygen data")
#
+ pai = get_PAI_data(auth_info, config)
#
+ result_set = result_set + pai
#
+ print("Failed to collect PAI information")
#
write_results(result_set, serial, config)