bewest / decoding-carelink

python serial driver for mm comlink2 protocol
http://bewest.github.io/decoding-carelink
Other
71 stars 52 forks source link

Interfacing with decoding-carelink #30

Closed erobinson closed 9 years ago

erobinson commented 10 years ago

From a separate python program what would be the best way to interface with decoding-carelink? Currently I can use the CLI from the other program via os.system(), but I was wondering if there was a better way, such as importing a module and then calling commands.

Thanks.

bewest commented 10 years ago

This is a good question and deserves better documentation.

The existing scripts might be used as an example, if I walk through some of the important bits:

This example script imports the commandline helper class which automatically adds --init and some other options to your CLI.:

These are the steps to using it in a new commandline program:

# this lets create eg commands.ReadPumpModel, later on
from decocare import commands
# lib.hexdump can be useful, feel free to remove if you don't use it
from decocare import lib

# this decodes history data... we can refactor this to make it more pleasant.
from decocare.history import parse_record

# the cli helper
from decocare.helpers import cli

# we can create this thing and call .run
class MyApp (cli.CommandApp):
  # my methods and things, implement customize_parser to add options to your parser
  def do_my_stuff (self, args):
    # use self.exec_request to exchange selected command with the pump
    self.clock = self.exec_request(self.pump, commands.ReadRTC)
    self.time = lib.parse.date(self.clock.getData( ))
    self.since = self.time - self.delta
  def main (self, args):
    self.do_my_stuff(args)

Maybe that helps clear things up a bit. This enables the --port and --serial switches with the environment variable and auto-scanning behavior.

So if you want to create a very similar behaving script, I would point you to those places, if you want to write new scripts, I'm open to suggestions and can point out how to do things. In general, at the bottom of the files in the library, there is an if __name__ == '__main__' stanza that tries to show how to use the module.

bewest commented 9 years ago

In decocare.models, there's now a much much nicer API, see @openaps's vendors/medtronc.py for usage examples.

bewest commented 9 years ago

Closing this now, see the decocare.models api. Feel free to re-open for questions.