Skip to content

Commit

Permalink
Merge pull request #32 from oskarpearson/comms_detection
Browse files Browse the repository at this point in the history
Comms detection
  • Loading branch information
oskarpearson committed May 21, 2016
2 parents a04b70e + efc20c0 commit 4110ffb
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
23 changes: 23 additions & 0 deletions bin/mmeowlink-any-pump-comms.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions mmeowlink/cli/any_pump_comms_app.py
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions mmeowlink/detect_radio_comms.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 13 additions & 5 deletions mmeowlink/vendors/mmeowlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions mmeowlink/vendors/serial_rf_spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?")
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 4110ffb

Please sign in to comment.