forked from bewest/mmblelink
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from oskarpearson/comms_detection
Comms detection
- Loading branch information
Showing
6 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters