diff options
author | Andrew Cady <d@jerkface.net> | 2017-01-04 12:25:49 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2017-01-04 20:49:34 +0000 |
commit | 1c7351722dab02b01b4d76565f5b16bdcbd61404 (patch) | |
tree | a62992e82421a7d8fe1b21af3de8fdb49a4c30a2 | |
parent | b4013cf3b68ad94d8e87cc68d4fef1cc54bd1d44 (diff) |
dexcom_dumper: new subcommand: --poll
-rw-r--r-- | dexcom_reader/dexcom_dumper.py | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/dexcom_reader/dexcom_dumper.py b/dexcom_reader/dexcom_dumper.py index f0bac36..4a14762 100644 --- a/dexcom_reader/dexcom_dumper.py +++ b/dexcom_reader/dexcom_dumper.py | |||
@@ -1,5 +1,8 @@ | |||
1 | import constants | 1 | import constants |
2 | import readdata | 2 | import readdata |
3 | from sys import stdout | ||
4 | from datetime import timedelta | ||
5 | from time import sleep | ||
3 | 6 | ||
4 | from optparse import OptionParser | 7 | from optparse import OptionParser |
5 | 8 | ||
@@ -9,11 +12,16 @@ DEFAULT_PAGE_COUNT = 2 | |||
9 | parser = OptionParser() | 12 | parser = OptionParser() |
10 | parser.add_option("--g4", action="store_false", dest="g5", default=G5_IS_DEFAULT, help="use Dexcom G4 instead of Dexcom G5") | 13 | parser.add_option("--g4", action="store_false", dest="g5", default=G5_IS_DEFAULT, help="use Dexcom G4 instead of Dexcom G5") |
11 | parser.add_option("--g5", action="store_true", dest="g5", default=G5_IS_DEFAULT, help="use Dexcom G5 instead of Dexcom G4") | 14 | parser.add_option("--g5", action="store_true", dest="g5", default=G5_IS_DEFAULT, help="use Dexcom G5 instead of Dexcom G4") |
12 | parser.add_option("-a", "--all", action="store_true", dest="dump_everything", default=False, help="dump all available records") | 15 | |
16 | parser.add_option("-a", "--all", action="store_const", dest="command", const="dump_everything", help="dump all available records") | ||
17 | parser.add_option("-p", "--poll", action="store_const", dest="command", const="poll", help="poll for latest CGM record") | ||
18 | |||
13 | parser.add_option("-n", type="int", dest="num_records", default=DEFAULT_PAGE_COUNT, help="number of pages of CGM records to display") | 19 | parser.add_option("-n", type="int", dest="num_records", default=DEFAULT_PAGE_COUNT, help="number of pages of CGM records to display") |
14 | 20 | ||
15 | (options, args) = parser.parse_args() | 21 | (options, args) = parser.parse_args() |
16 | 22 | ||
23 | command = options.command or "dump_cgm" | ||
24 | |||
17 | def get_dexcom_reader(): | 25 | def get_dexcom_reader(): |
18 | if options.g5: | 26 | if options.g5: |
19 | dd = readdata.DexcomG5.FindDevice() | 27 | dd = readdata.DexcomG5.FindDevice() |
@@ -24,13 +32,13 @@ def get_dexcom_reader(): | |||
24 | 32 | ||
25 | dr = get_dexcom_reader() | 33 | dr = get_dexcom_reader() |
26 | 34 | ||
27 | if options.dump_everything: | 35 | def dump_everything(): |
28 | 36 | ||
29 | # record_types = ['METER_DATA', 'INSERTION_TIME', 'USER_EVENT_DATA', 'CAL_SET', 'SENSOR_DATA'] | 37 | # record_types = ['METER_DATA', 'INSERTION_TIME', 'USER_EVENT_DATA', 'CAL_SET', 'SENSOR_DATA'] |
30 | 38 | ||
31 | unparseable = ['FIRMWARE_PARAMETER_DATA', 'RECEIVER_LOG_DATA', 'USER_SETTING_DATA', 'MAX_VALUE'] | 39 | unparseable = ['FIRMWARE_PARAMETER_DATA', 'RECEIVER_LOG_DATA', 'USER_SETTING_DATA', 'MAX_VALUE'] |
32 | parsed_to_xml = ['MANUFACTURING_DATA', 'PC_SOFTWARE_PARAMETER'] | 40 | parsed_to_xml = ['MANUFACTURING_DATA', 'PC_SOFTWARE_PARAMETER'] |
33 | skip = unparseable + parsed_to_xml + ['EGV_DATA'] | 41 | skip = unparseable + parsed_to_xml |
34 | record_types = filter(lambda v: not v in skip, constants.RECORD_TYPES) | 42 | record_types = filter(lambda v: not v in skip, constants.RECORD_TYPES) |
35 | 43 | ||
36 | for t in record_types: | 44 | for t in record_types: |
@@ -38,7 +46,45 @@ if options.dump_everything: | |||
38 | for r in dr.ReadRecords(t): | 46 | for r in dr.ReadRecords(t): |
39 | print r | 47 | print r |
40 | 48 | ||
41 | cgm_records = dr.ReadRecords('EGV_DATA', 0 if options.dump_everything else options.num_records) | 49 | def dump_cgm(): |
42 | for cr in cgm_records: | 50 | cgm_records = dr.ReadRecords('EGV_DATA', options.num_records) |
43 | if not cr.display_only: | 51 | for cr in cgm_records: |
44 | print cr | 52 | if not cr.display_only: |
53 | print cr | ||
54 | |||
55 | def recent(t): | ||
56 | now = dr.ReadSystemTime() | ||
57 | return t.system_time > now - timedelta(minutes=5) | ||
58 | |||
59 | def read_recent_egv_data(): | ||
60 | try: | ||
61 | r = dr.ReadRecords('EGV_DATA', options.num_records)[-1] | ||
62 | if recent(r) and not r.is_special and not r.display_only: | ||
63 | return r | ||
64 | else: | ||
65 | return None | ||
66 | except: | ||
67 | return None | ||
68 | |||
69 | def format_times(now, stamp): | ||
70 | diff = (stamp - now).total_seconds() | ||
71 | operand = '-' if diff < 0 else '+' # should always be - | ||
72 | return '%s %s %d' % (now.strftime('%s'), operand, abs(diff)) | ||
73 | |||
74 | def poll(): | ||
75 | while True: | ||
76 | r = read_recent_egv_data() | ||
77 | if r is None: | ||
78 | sleep(10) | ||
79 | else: | ||
80 | now = dr.ReadSystemTime() | ||
81 | print '%s: %s %s' % (format_times(now, r.system_time), r.glucose, r.trend_arrow) | ||
82 | stdout.flush() | ||
83 | next_reading = (r.system_time - now + timedelta(minutes=5, seconds=2)).total_seconds() | ||
84 | if (next_reading > 0): | ||
85 | sleep(next_reading) | ||
86 | |||
87 | {"dump_everything": dump_everything, | ||
88 | "dump_cgm": dump_cgm, | ||
89 | "poll": poll | ||
90 | }[command]() | ||