2019-05-19 18:31:25 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-02-13 21:36:27 +01:00
|
|
|
|
|
|
|
"""Export Pacer data to InfluxDB"""
|
|
|
|
|
2019-05-19 18:31:25 +02:00
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
import sqlite3 as lite
|
|
|
|
import argparse
|
2024-02-13 21:36:27 +01:00
|
|
|
from influxdb import InfluxDBClient
|
2019-05-19 18:31:25 +02:00
|
|
|
|
2024-02-13 21:36:27 +01:00
|
|
|
def main(args):
|
2019-05-19 18:31:25 +02:00
|
|
|
"""Instantiate the connection to the InfluxDB client."""
|
2024-02-13 21:36:27 +01:00
|
|
|
influx_db = InfluxDBClient(args.host, args.port, args.user, args.password, args.dbname)
|
|
|
|
measurementime_stamp = influx_db.get_list_measurementime_stamp()
|
|
|
|
time_stamp = 0
|
2022-02-27 13:07:51 +01:00
|
|
|
lastdate = 0
|
2024-02-13 21:36:27 +01:00
|
|
|
lastime_stampteps = 0
|
|
|
|
if measurementime_stamp != []:
|
|
|
|
lastentry = influx_db.query('SELECT LAST("steps") FROM "steps"')
|
|
|
|
pointime_stamp = lastentry.get_pointime_stamp('steps')
|
|
|
|
lastdate = list(pointime_stamp)[0]['time']
|
|
|
|
time_stamp = time.mktime(datetime.datetime.strptime(lastdate, '%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
.timetuple())
|
2019-05-19 18:43:00 +02:00
|
|
|
|
2024-02-13 21:36:27 +01:00
|
|
|
if time_stamp == datetime.datetime.now().timestamp() // 86400 * 86400 + time.timezone:
|
|
|
|
pointime_stamp = lastentry.get_pointime_stamp('steps')
|
|
|
|
lastime_stampteps = list(pointime_stamp)[0]['last']
|
2019-05-19 18:31:25 +02:00
|
|
|
|
2022-02-27 10:20:35 +01:00
|
|
|
if args.verbose:
|
2024-02-13 21:36:27 +01:00
|
|
|
print(f'last entry is {lastdate}, time_stamp is {time_stamp}, \
|
|
|
|
number of steps is {lastime_stampteps}\n')
|
2019-05-19 18:31:25 +02:00
|
|
|
|
2024-02-13 21:36:27 +01:00
|
|
|
con = lite.connect(args.dbfile)
|
2019-05-19 18:31:25 +02:00
|
|
|
with con:
|
|
|
|
cur = con.cursor()
|
2024-02-13 21:36:27 +01:00
|
|
|
cur.execute(f'SELECT recordedForDate,steps,distanceInMeters,activeTimeInSeconds,calories \
|
|
|
|
FROM dailyActivityLog \
|
|
|
|
WHERE recordedForDate >= {time_stamp} AND steps > {lastime_stampteps}')
|
2019-05-19 18:31:25 +02:00
|
|
|
while True:
|
|
|
|
row = cur.fetchone()
|
2024-02-13 21:36:27 +01:00
|
|
|
if row is None:
|
2019-05-19 18:31:25 +02:00
|
|
|
break
|
|
|
|
mytime = datetime.datetime.fromtimestamp(row[0]).strftime('%Y-%m-%dT%H:%M:%SZ')
|
2024-02-13 21:36:27 +01:00
|
|
|
data = [{"measurement":"steps",
|
2019-05-19 18:31:25 +02:00
|
|
|
"time":mytime,
|
|
|
|
"fields": {
|
|
|
|
"steps":row[1],
|
|
|
|
"distanceInMeters":row[2],
|
|
|
|
"activeTimeInSeconds":row[3],
|
|
|
|
"calories":row[4]
|
2024-02-13 21:36:27 +01:00
|
|
|
}
|
|
|
|
}]
|
2022-02-27 10:20:35 +01:00
|
|
|
if args.verbose:
|
2024-02-13 21:36:27 +01:00
|
|
|
print(f'writing data for {mytime}')
|
|
|
|
influx_db.write_pointime_stamp(data)
|
|
|
|
influx_db.close()
|
2019-05-19 18:31:25 +02:00
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
"""Parse the args from main."""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Export Pacer data to InfluxDB')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-v', '--verbose', required=False,
|
2022-02-27 10:20:35 +01:00
|
|
|
action="store_true",
|
|
|
|
help='verbose output')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-H', '--host', type=str, required=False,
|
2019-05-19 18:31:25 +02:00
|
|
|
default='localhost',
|
|
|
|
help='hostname of InfluxDB http API')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-u', '--user', type=str, required=False,
|
2019-05-19 18:31:25 +02:00
|
|
|
default='root',
|
|
|
|
help='username for InfluxDB http API')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-p', '--password', type=str, required=False,
|
2019-05-19 18:31:25 +02:00
|
|
|
default='root',
|
|
|
|
help='passworid for InfluxDB http API')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-P', '--port', type=int, required=False, default=8086,
|
2019-05-19 18:31:25 +02:00
|
|
|
help='port of InfluxDB http API')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-n', '--dbname', type=str, required=True, default='demo',
|
2019-05-19 18:31:25 +02:00
|
|
|
help='InfluxDB database name')
|
2022-02-27 10:26:09 +01:00
|
|
|
parser.add_argument('-f', '--dbfile', type=str, required=True, default='demo.db',
|
2019-05-19 18:31:25 +02:00
|
|
|
help='sqlite (pacer) database name')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-02-13 21:36:27 +01:00
|
|
|
main(parse_args())
|