-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcarelink_client2_cli.py
94 lines (83 loc) · 2.76 KB
/
carelink_client2_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
###############################################################################
#
# Carelink Client 2 CLI
#
# Description:
#
# This is the command line interface of the Carelink Client. It is used
# to download a patients recent pump and sensor data from the Carelink
# Cloud. The data is saved to a JSON file.
#
# Author:
#
# Ondrej Wisniewski (ondrej.wisniewski *at* gmail.com)
#
# Changelog:
#
# 31/12/2023 - Initial version
#
# Copyright 2023, Ondrej Wisniewski
#
###############################################################################
import carelink_client2
import argparse
import time
import json
import datetime
VERSION = "1.0"
def writeJson(jsonobj, name):
filename = name + "-" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".json"
try:
f = open(filename, "w")
f.write(json.dumps(jsonobj,indent=3))
f.close()
except Exception as e:
print("ERROR: failed to save %s (%s) " % (filename, str(e)))
return False
else:
return True
# Parse command line
parser = argparse.ArgumentParser()
parser.add_argument('--repeat', '-r', type=int, help='Repeat request times', required=False)
parser.add_argument('--wait', '-w', type=int, help='Wait minutes between repeated calls', required=False)
parser.add_argument('--data', '-d', help='Save recent data', action='store_true')
parser.add_argument('--verbose', '-v', help='Verbose mode', action='store_true')
args = parser.parse_args()
# Get parameters from CLI
repeat = 1 if args.repeat == None else args.repeat
wait = 5 if args.wait == None else args.wait
data = args.data
verbose = args.verbose
#print("repeat = " + str(repeat))
#print("wait = " + str(wait))
#print("data = " + str(data))
#print("verbose = " + str(verbose))
# Create client instance
client = carelink_client2.CareLinkClient()
if verbose:
print("Client created")
if client.init():
client.printUserInfo()
for i in range(repeat):
if verbose:
print("Starting download, count: %d" % (i+1))
try:
recentData = client.getRecentData()
if recentData != None and client.getLastResponseCode() == 200:
if(data):
if writeJson(recentData, "data"):
if verbose:
print("Data saved successfully")
# Error occured
else:
print("ERROR: failed to get data (response code %d)" % client.getLastResponseCode())
break
except Exception as e:
print(e)
break
if i < repeat - 1:
if verbose:
print("Waiting %d minutes before next download" % wait)
time.sleep(wait * 60)
else:
print("ERROR: failed to initialize client (response code %s)" % client.getLastResponseCode())