<s> """<STR_LIT>""" <EOL> from openaps . uses . use import Use <EOL> from openaps . uses . registry import Registry <EOL> import dexcom_reader <EOL> from dexcom_reader import readdata <EOL> from datetime import timedelta <EOL> from datetime import datetime <EOL> import dateutil <EOL> from dateutil import relativedelta <EOL> from dateutil . parser import parse <EOL> def set_config ( args , device ) : <EOL> return <EOL> def display_device ( device ) : <EOL> return '<STR_LIT>' <EOL> use = Registry ( ) <EOL> get_uses = use . get_uses <EOL> @ use ( ) <EOL> class scan ( Use ) : <EOL> """<STR_LIT>""" <EOL> def scanner ( self ) : <EOL> return readdata . Dexcom . FindDevice ( ) <EOL> def before_main ( self , args , app ) : <EOL> self . port = self . scanner ( ) <EOL> self . dexcom = self . port and readdata . Dexcom ( self . port ) or None <EOL> def main ( self , args , app ) : <EOL> return self . port or '<STR_LIT>' <EOL> @ use ( ) <EOL> class battery ( scan ) : <EOL> def main ( self , args , app ) : <EOL> battery = dict ( level = self . dexcom . ReadBatteryLevel ( ) <EOL> , status = self . dexcom . ReadBatteryState ( ) <EOL> ) <EOL> return battery <EOL> @ use ( ) <EOL> class ReadBatteryLevel ( scan ) : <EOL> def main ( self , args , app ) : <EOL> return self . dexcom . ReadBatteryLevel ( ) <EOL> @ use ( ) <EOL> class ReadBatteryState ( scan ) : <EOL> def main ( self , args , app ) : <EOL> return self . dexcom . ReadBatteryState ( ) <EOL> @ use ( ) <EOL> class ReadManufacturingData ( scan ) : <EOL> def main ( self , args , app ) : <EOL> data = self . dexcom . ReadManufacturingData ( ) <EOL> result = data . attrib <EOL> return result <EOL> @ use ( ) <EOL> class GetFirmwareHeader ( scan ) : <EOL> def main ( self , args , app ) : <EOL> data = self . dexcom . GetFirmwareHeader ( ) <EOL> result = data . attrib <EOL> return result <EOL> def parse_clock ( candidate ) : <EOL> if candidate . lower ( ) in [ '<STR_LIT>' ] : <EOL> return datetime . now ( ) <EOL> return parse ( candidate ) <EOL> @ use ( ) <EOL> class UpdateTime ( scan ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( input = args . input , to = args . to ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT:input>' , nargs = '<STR_LIT:?>' , default = None ) <EOL> parser . add_argument ( '<STR_LIT>' , default = None ) <EOL> def upload_program ( self , program ) : <EOL> if not program . get ( '<STR_LIT>' , None ) or '<STR_LIT>' not in program : <EOL> print "<STR_LIT>" <EOL> raise Exception ( "<STR_LIT>" . format ( program . get ( '<STR_LIT>' ) ) ) <EOL> result = self . dexcom . WriteDisplayTimeOffset ( offset = program [ '<STR_LIT>' ] ) <EOL> new_offset = self . dexcom . ReadDisplayTimeOffset ( ) <EOL> return dict ( requested = dict ( ** program ) , result = result , offset = new_offset . total_seconds ( ) ) <EOL> def get_program ( self , args ) : <EOL> params = self . get_params ( args ) <EOL> program = dict ( clock = None ) <EOL> if params . get ( '<STR_LIT:to>' , None ) is None and params . get ( '<STR_LIT:input>' ) : <EOL> program . update ( clock = parse ( json . load ( argparse . FileType ( '<STR_LIT:r>' ) ( params . get ( '<STR_LIT:input>' ) ) ) ) ) <EOL> else : <EOL> if params . get ( '<STR_LIT:to>' ) : <EOL> program . update ( clock = parse_clock ( params . get ( '<STR_LIT:to>' ) ) ) <EOL> current_utc = self . dexcom . ReadSystemTime ( ) <EOL> offset = program [ '<STR_LIT>' ] - current_utc <EOL> program . update ( offset = offset . total_seconds ( ) ) <EOL> return program <EOL> def main ( self , args , app ) : <EOL> program = self . get_program ( args ) <EOL> results = self . upload_program ( program ) <EOL> program . update ( enacted_at = datetime . now ( ) , ** results ) <EOL> return program <EOL> @ use ( ) <EOL> class WriteChargerCurrentSetting ( scan ) : <EOL> MAP = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_params ( self , args ) : <EOL> return dict ( status = args . status ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT:status>' , choices = self . MAP ) <EOL> for key in self . MAP : <EOL> flag = "<STR_LIT>" . format ( key ) <EOL> parser . add_argument ( flag , dest = '<STR_LIT:status>' , action = '<STR_LIT>' , const = key ) <EOL> def main ( self , args , app ) : <EOL> params = self . get_params ( args ) <EOL> status = params . get ( '<STR_LIT:status>' ) <EOL> requested = dict ( ** params ) <EOL> if not status : <EOL> raise Exception ( "<STR_LIT>" . format ( status ) ) <EOL> result = self . dexcom . WriteChargerCurrentSetting ( status ) <EOL> updated = self . dexcom . ReadChargerCurrentSetting ( ) <EOL> result . update ( enacted_at = datetime . now ( ) , status = updated , requested = requested ) <EOL> return result <EOL> @ use ( ) <EOL> class DescribeClocks ( scan ) : <EOL> """<STR_LIT>""" <EOL> def main ( self , args , app ) : <EOL> system = dict ( offset = self . dexcom . ReadSystemTimeOffset ( ) . total_seconds ( ) <EOL> , utc = self . dexcom . ReadSystemTime ( ) ) <EOL> display = dict ( offset = self . dexcom . ReadDisplayTimeOffset ( ) . total_seconds ( ) <EOL> , clock = self . dexcom . ReadDisplayTime ( ) ) <EOL> rtc = dict ( epoch = self . dexcom . ReadRTC ( ) ) <EOL> clocks = dict ( system = system , display = display , rtc = rtc ) <EOL> return clocks <EOL> @ use ( ) <EOL> class ReadTransmitterId ( scan ) : <EOL> def main ( self , args , app ) : <EOL> result = self . dexcom . ReadTransmitterId ( ) <EOL> return result <EOL> class SameNameCommand ( scan ) : <EOL> def pass_result ( self , result ) : <EOL> return result <EOL> def main ( self , args , app ) : <EOL> name = self . __class__ . __name__ . split ( '<STR_LIT:.>' ) . pop ( ) <EOL> result = getattr ( self . dexcom , name ) ( ** self . get_params ( args ) ) <EOL> return self . pass_result ( result ) <EOL> @ use ( ) <EOL> class ReadLanguage ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadRTC ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadSystemTime ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadSystemTimeOffset ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> def pass_result ( self , result ) : <EOL> return result . total_seconds ( ) <EOL> @ use ( ) <EOL> class ReadDisplayTime ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadDisplayTimeOffset ( ReadSystemTimeOffset ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadGlucoseUnit ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadClockMode ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadDeviceMode ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadBlindedMode ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadHardwareBoardId ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadSetupWizardState ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class ReadChargerCurrentSetting ( SameNameCommand ) : <EOL> """<STR_LIT>""" <EOL> @ use ( ) <EOL> class glucose ( scan ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def prerender_stdout ( self , data ) : <EOL> return self . prerender_text ( data ) <EOL> def prerender_text ( self , data ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> for item in data : <EOL> line = map ( str , [ item [ field ] for field in self . TEXT_COLUMNS ] ) <EOL> out . append ( '<STR_LIT:U+0020>' . join ( line ) ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def prerender_json ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> def main ( self , args , app ) : <EOL> """<STR_LIT>""" <EOL> records = self . dexcom . ReadRecords ( self . RECORD_TYPE ) <EOL> out = [ ] <EOL> for item in records : <EOL> out . append ( item . to_dict ( ) ) <EOL> return out <EOL> @ use ( ) <EOL> class iter_glucose ( glucose ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> def get_params ( self , args ) : <EOL> return dict ( count = int ( args . count ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT:count>' , type = int , nargs = '<STR_LIT:?>' , default = <NUM_LIT:100> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( self . RECORD_TYPE ) : <EOL> records . append ( item . to_dict ( ) ) <EOL> if len ( records ) >= self . get_params ( args ) [ '<STR_LIT:count>' ] : <EOL> break <EOL> return records <EOL> @ use ( ) <EOL> class iter_glucose_hours ( glucose ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( hours = float ( args . hours ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , type = float , nargs = '<STR_LIT:?>' , default = <NUM_LIT:1> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> params = self . get_params ( args ) <EOL> delta = relativedelta . relativedelta ( hours = params . get ( '<STR_LIT>' ) ) <EOL> now = datetime . now ( ) <EOL> since = now - delta <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( self . RECORD_TYPE ) : <EOL> if item . display_time >= since : <EOL> records . append ( item . to_dict ( ) ) <EOL> else : <EOL> break <EOL> return records <EOL> @ use ( ) <EOL> class sensor ( glucose ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class iter_sensor ( iter_glucose , sensor ) : <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class iter_sensor_hours ( iter_glucose_hours , iter_sensor ) : <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class meter_data ( sensor ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class iter_meter_data ( iter_sensor , meter_data ) : <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = meter_data . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class iter_meter_data_hours ( iter_sensor_hours , iter_meter_data ) : <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = meter_data . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class insertion_time ( sensor ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class iter_insertion_time ( iter_sensor , insertion_time ) : <EOL> RECORD_TYPE = insertion_time . RECORD_TYPE <EOL> TEXT_COLUMNS = insertion_time . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class iter_insertion_time_hours ( iter_sensor_hours , iter_insertion_time ) : <EOL> RECORD_TYPE = insertion_time . RECORD_TYPE <EOL> TEXT_COLUMNS = insertion_time . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class user_event_data ( sensor ) : <EOL> """<STR_LIT>""" <EOL> RECORD_TYPE = '<STR_LIT>' <EOL> TEXT_COLUMNS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ use ( ) <EOL> class iter_user_event_data ( iter_sensor , user_event_data ) : <EOL> RECORD_TYPE = user_event_data . RECORD_TYPE <EOL> TEXT_COLUMNS = user_event_data . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class iter_user_event_data_hours ( iter_sensor_hours , iter_user_event_data ) : <EOL> RECORD_TYPE = user_event_data . RECORD_TYPE <EOL> TEXT_COLUMNS = user_event_data . TEXT_COLUMNS <EOL> @ use ( ) <EOL> class sensor_insertions ( scan ) : <EOL> """<STR_LIT>""" <EOL> def prerender_stdout ( self , data ) : <EOL> return self . prerender_text ( data ) <EOL> def prerender_text ( self , data ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> for item in data : <EOL> line = map ( str , [ <EOL> item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> ] ) <EOL> out . append ( '<STR_LIT:U+0020>' . join ( line ) ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def prerender_json ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> def main ( self , args , app ) : <EOL> """<STR_LIT>""" <EOL> records = self . dexcom . ReadRecords ( '<STR_LIT>' ) <EOL> out = [ ] <EOL> for item in records : <EOL> out . append ( item . to_dict ( ) ) <EOL> return out <EOL> @ use ( ) <EOL> class iter_sensor_insertions ( sensor_insertions ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( count = int ( args . count ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT:count>' , type = int , nargs = '<STR_LIT:?>' , default = <NUM_LIT:10> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( '<STR_LIT>' ) : <EOL> records . append ( item . to_dict ( ) ) <EOL> if len ( records ) >= self . get_params ( args ) [ '<STR_LIT:count>' ] : <EOL> break <EOL> return records <EOL> @ use ( ) <EOL> class iter_sensor_insertions_hours ( sensor_insertions ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( hours = float ( args . hours ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , type = float , nargs = '<STR_LIT:?>' , default = <NUM_LIT:1> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> params = self . get_params ( args ) <EOL> delta = relativedelta . relativedelta ( hours = params . get ( '<STR_LIT>' ) ) <EOL> now = datetime . now ( ) <EOL> since = now - delta <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( '<STR_LIT>' ) : <EOL> if item . system_time >= since : <EOL> records . append ( item . to_dict ( ) ) <EOL> else : <EOL> break <EOL> return records <EOL> @ use ( ) <EOL> class calibrations ( scan ) : <EOL> """<STR_LIT>""" <EOL> def prerender_stdout ( self , data ) : <EOL> return self . prerender_text ( data ) <EOL> def prerender_text ( self , data ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> for item in data : <EOL> line = map ( str , [ <EOL> item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> , item [ '<STR_LIT>' ] <EOL> ] ) <EOL> out . append ( '<STR_LIT:U+0020>' . join ( line ) ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def prerender_json ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> def main ( self , args , app ) : <EOL> """<STR_LIT>""" <EOL> records = self . dexcom . ReadRecords ( '<STR_LIT>' ) <EOL> out = [ ] <EOL> for item in records : <EOL> out . append ( item . to_dict ( ) ) <EOL> return out <EOL> @ use ( ) <EOL> class iter_calibrations ( calibrations ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( count = int ( args . count ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT:count>' , type = int , nargs = '<STR_LIT:?>' , default = <NUM_LIT:10> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( '<STR_LIT>' ) : <EOL> records . append ( item . to_dict ( ) ) <EOL> if len ( records ) >= self . get_params ( args ) [ '<STR_LIT:count>' ] : <EOL> break <EOL> return records <EOL> @ use ( ) <EOL> class iter_calibrations_hours ( calibrations ) : <EOL> """<STR_LIT>""" <EOL> def get_params ( self , args ) : <EOL> return dict ( hours = float ( args . hours ) ) <EOL> def configure_app ( self , app , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , type = float , nargs = '<STR_LIT:?>' , default = <NUM_LIT:1> , <EOL> help = "<STR_LIT>" ) <EOL> def main ( self , args , app ) : <EOL> params = self . get_params ( args ) <EOL> delta = relativedelta . relativedelta ( hours = params . get ( '<STR_LIT>' ) ) <EOL> now = datetime . now ( ) <EOL> since = now - delta <EOL> records = [ ] <EOL> for item in self . dexcom . iter_records ( '<STR_LIT>' ) : <EOL> if item . system_time >= since : <EOL> records . append ( item . to_dict ( ) ) <EOL> else : <EOL> break <EOL> return records </s>
