From ca123e863d535d612f7e5011923d7322616c377a Mon Sep 17 00:00:00 2001 From: Oskar Pearson Date: Sun, 17 Apr 2016 00:44:33 +0100 Subject: [PATCH 1/3] [comms_detection] Added mmeowlink-any-pump-comms.py So that we can have two devices attempting to communicate with the pump at any one time, we now include a command mmeowlink-any-pump-comms.py that can check to see if there are ongoing pump comms - and return status appropriately. If the state is 0 then it means that there *ARE* pump comms, as per the unix convention that means it returned successfully. eg: mmeowlink-any-pump-comms.py --port /dev/ttyACM0 --wait-for 3 --ignore-wake && echo "no comms" If there have not been any communications in 3 seconds, this would return '1', which means the `echo "no comms"` command would execute. --- bin/mmeowlink-any-pump-comms.py | 23 ++++++++++++++ mmeowlink/cli/any_pump_comms_app.py | 32 ++++++++++++++++++++ mmeowlink/detect_radio_comms.py | 47 +++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100755 bin/mmeowlink-any-pump-comms.py create mode 100644 mmeowlink/cli/any_pump_comms_app.py create mode 100644 mmeowlink/detect_radio_comms.py diff --git a/bin/mmeowlink-any-pump-comms.py b/bin/mmeowlink-any-pump-comms.py new file mode 100755 index 0000000..7f0f2a3 --- /dev/null +++ b/bin/mmeowlink-any-pump-comms.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# PYTHON_ARGCOMPLETE_OK + +""" +Checks to see if there are any pump comms going on. If there are: exits with an error status +""" + +import sys + +from mmeowlink.cli.any_pump_comms_app import AnyPumpCommsApp + +if __name__ == '__main__': + app = AnyPumpCommsApp() + + app.run(None) + + # app.run doesn't return the call status, so we need to interrogate the object: + if app.app_result == 0: + print("No comms detected") + else: + print("Comms with pump detected") + + sys.exit(app.app_result) diff --git a/mmeowlink/cli/any_pump_comms_app.py b/mmeowlink/cli/any_pump_comms_app.py new file mode 100644 index 0000000..313abd7 --- /dev/null +++ b/mmeowlink/cli/any_pump_comms_app.py @@ -0,0 +1,32 @@ +import sys +import time + +from mmeowlink.detect_radio_comms import DetectRadioComms + +from base_mmeowlink_app import BaseMMeowlinkApp + +class AnyPumpCommsApp(BaseMMeowlinkApp): + """ + Waits for any pump communications, up to the timeout specified by wait_for + """ + + # Override the parser since we don't want the standard commands + def customize_parser(self, parser): + parser = super(self.__class__, self).configure_radio_params(parser) + + parser.add_argument('--wait-for', default=5, type=int, help="How long to wait for other comms") + parser.add_argument('--ignore-wake', action='store_true', help="Ignore 'wake' commands") + + return parser + + def prelude(self, args): + # When running mmtune, we don't want the code to try and send + # prelude packets or auto-init the pump, since they duplicate what + # we are about to do + args.no_rf_prelude = True + + super(AnyPumpCommsApp, self).prelude(args) + + def main(self, args): + self.detector = DetectRadioComms(link=self.link, wait_for=int(args.wait_for), ignore_wake=args.ignore_wake) + self.app_result = self.detector.detect() diff --git a/mmeowlink/detect_radio_comms.py b/mmeowlink/detect_radio_comms.py new file mode 100644 index 0000000..d010391 --- /dev/null +++ b/mmeowlink/detect_radio_comms.py @@ -0,0 +1,47 @@ +import time + +from exceptions import CommsException +from hex_handling import hexify + +from vendors.mmcommander_link import MMCommanderLink +from vendors.subg_rfspy_link import SubgRfspyLink + +class DetectRadioComms(object): + def __init__(self, link=None, wait_for=5, ignore_wake=False): + self.link = link + self.wait_for = wait_for + self.ignore_wake = ignore_wake + + def detect(self): + start = time.time() + assert self.wait_for >= 1 + + # We wait for packets 1 second at a time so that we don't exceed the + # firmware timeout value with 0.6 firmware: + while time.time() <= start + self.wait_for: + hex_string = None + + try: + if type(self.link) == SubgRfspyLink: + resp = self.link.get_packet(timeout=1) + hex_string = hexify(resp['data']).upper() + elif type(self.link) == MMCommanderLink: + resp = self.link.read(timeout=1) + hex_string = hexify(resp).upper() + except CommsException as e: + pass + + # EG: A7 12 31 23 22 5D .. .. + # POS: 01234567890123456789 + # 'A7' indicates comms with the pump + if hex_string: + if (hex_string[0:2] == 'A7'): + if hex_string[15:16] == '5D' and self.ignore_wake: + pass + else: + return(1) + else: + print('Picked up something other than pump comms - ignoring: %s' % hex_string) + + # No comms picked up + return(0) From 2f0dfd1c143f3d12bd07404dfeed27bc3c9ce708 Mon Sep 17 00:00:00 2001 From: Ben West Date: Mon, 2 May 2016 20:19:38 -0700 Subject: [PATCH 2/3] fix logging handling --- mmeowlink/vendors/mmeowlink.py | 18 +++++++++++++----- mmeowlink/vendors/serial_rf_spy.py | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mmeowlink/vendors/mmeowlink.py b/mmeowlink/vendors/mmeowlink.py index 80e5c1e..06480cd 100644 --- a/mmeowlink/vendors/mmeowlink.py +++ b/mmeowlink/vendors/mmeowlink.py @@ -2,6 +2,9 @@ """ mmeowlink - openaps driver for cc1111/cc1110 devices """ +import logging +import logging.handlers + from openaps.uses.use import Use from openaps.uses.registry import Registry from openaps.configurable import Configurable @@ -47,14 +50,21 @@ def main (args, app): def setup_logging (self): log = logging.getLogger(decocare.__name__) - level = getattr(logging, self.device.get('logLevel', 'WARN')) + mmlog = logging.getLogger('mmeowlink') + level = getattr(logging, self.device.get('DECOCARE_LOG_LEVEL', 'WARN')) + mmlevel = getattr(logging, self.device.get('logLevel', 'INFO')) address = self.device.get('logAddress', '/dev/log') log.setLevel(level) + mmlog.setLevel(mmlevel) for previous in log.handlers[:]: log.removeHandler(previous) + for previous in mmlog.handlers[:]: + mmlog.removeHandler(previous) log.addHandler(logging.handlers.SysLogHandler(address=address)) + mmlog.addHandler(logging.handlers.SysLogHandler(address=address)) def setup_medtronic_link (self): + setup_logging(self) serial = self.device.get('serial') radio_type = self.device.get('radio_type') port = self.device.get('port') @@ -65,8 +75,6 @@ def setup_medtronic_link (self): link = builder.build(radio_type, port) self.pump = Pump(link, serial) -import logging -import logging.handlers @use( ) class mmtune (medtronic.MedtronicTask): @@ -80,7 +88,7 @@ class mmtune (medtronic.MedtronicTask): requires_session = False def setup_medtronic (self): - setup_logging(self) + # setup_logging(self) setup_medtronic_link(self) serial = self.device.get('serial') self.mmtune = MMTune(self.pump.link, serial) @@ -90,7 +98,7 @@ def main (self, args, app): class MedtronicTask (medtronic.MedtronicTask): def setup_medtronic (self): - setup_logging(self) + # setup_logging(self) setup_medtronic_link(self) return diff --git a/mmeowlink/vendors/serial_rf_spy.py b/mmeowlink/vendors/serial_rf_spy.py index 6df5798..ae7da48 100644 --- a/mmeowlink/vendors/serial_rf_spy.py +++ b/mmeowlink/vendors/serial_rf_spy.py @@ -106,12 +106,12 @@ def sync(self): self.send_command(self.CMD_GET_STATE) status = self.get_response(timeout=1) if status == "OK": - print "subg_rfspy status: " + status + log.info("subg_rfspy status: " + status) self.send_command(self.CMD_GET_VERSION) version = self.get_response(timeout=1) if len(version) >= 3: - print "Version: " + version + log.info("Version: " + version) if not status or not version: raise CommsException("Could not get subg_rfspy state or version. Have you got the right port/device and radio_type?") From efc20c0ce9a20a0052331c65e78f8091e45114fd Mon Sep 17 00:00:00 2001 From: Oskar Pearson Date: Sat, 21 May 2016 17:47:01 +0100 Subject: [PATCH 3/3] Added mmeowlink-any-pump-comms.py to setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index d447f6e..fa91347 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,7 @@ ], scripts = [ 'bin/mmeowlink-bolus.py', + 'bin/mmeowlink-any-pump-comms.py', 'bin/mmeowlink-rf-dump.py', 'bin/mmeowlink-send.py', 'bin/mmtune.py'