diff options
-rw-r--r-- | glucometerutils/drivers/lifescan_common.py | 20 | ||||
-rw-r--r-- | glucometerutils/drivers/otultra2.py | 21 | ||||
-rw-r--r-- | test/test_lifescan.py | 28 | ||||
-rw-r--r-- | test/test_otultra2.py | 8 |
4 files changed, 28 insertions, 49 deletions
diff --git a/glucometerutils/drivers/lifescan_common.py b/glucometerutils/drivers/lifescan_common.py index ada58e8..9a60ab6 100644 --- a/glucometerutils/drivers/lifescan_common.py +++ b/glucometerutils/drivers/lifescan_common.py @@ -26,23 +26,3 @@ class InvalidSerialNumber(exceptions.Error): """The serial number is not as expected.""" def __init__(self, serial_number): self.message = 'Serial number %s is invalid.' % serial_number - - -def calculate_checksum(bytestring): - """Calculate the checksum used by OneTouch Ultra and Ultra2 devices - - Args: - bytestring: the string of which the checksum has to be calculated. - - Returns: - A string with the hexdecimal representation of the checksum for the input. - - The checksum is a very stupid one: it just sums all the bytes, - modulo 16-bit, without any parity. - """ - checksum = 0 - - for byte in bytestring: - checksum = (checksum + byte) & 0xffff - - return checksum diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 3af78fc..8f21ac2 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -46,6 +46,25 @@ _DUMP_LINE_RE = re.compile( _RESPONSE_MATCH = re.compile(r'^(.+) ([0-9A-F]{4})\r$') +def _calculate_checksum(bytestring): + """Calculate the checksum used by OneTouch Ultra and Ultra2 devices + + Args: + bytestring: the string of which the checksum has to be calculated. + + Returns: + A string with the hexdecimal representation of the checksum for the input. + + The checksum is a very stupid one: it just sums all the bytes, + modulo 16-bit, without any parity. + """ + checksum = 0 + + for byte in bytestring: + checksum = (checksum + byte) & 0xffff + + return checksum + def _validate_and_strip_checksum(line): """Verify the simple 16-bit checksum and remove it from the line. @@ -64,7 +83,7 @@ def _validate_and_strip_checksum(line): try: checksum_given = int(checksum_string, 16) - checksum_calculated = lifescan_common.calculate_checksum( + checksum_calculated = _calculate_checksum( bytes(response, 'ascii')) if checksum_given != checksum_calculated: diff --git a/test/test_lifescan.py b/test/test_lifescan.py deleted file mode 100644 index 7252bec..0000000 --- a/test/test_lifescan.py +++ /dev/null @@ -1,28 +0,0 @@ -# -*- coding: utf-8 -*- -"""Tests for the LifeScan Common functions.""" - -__author__ = 'Diego Elio Pettenò' -__email__ = 'flameeyes@flameeyes.eu' -__copyright__ = 'Copyright © 2013, Diego Elio Pettenò' -__license__ = 'MIT' - -import os -import sys -import unittest - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -from glucometerutils.drivers import lifescan_common - - -class TestOTUltra2(unittest.TestCase): - def test_checksum(self): - checksum = lifescan_common.calculate_checksum(bytes('T', 'ascii')) - self.assertEqual(0x0054, checksum) - - checksum = lifescan_common.calculate_checksum( - bytes('T "SAT","08/03/13","22:12:00 "', 'ascii')) - self.assertEqual(0x0608, checksum) - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_otultra2.py b/test/test_otultra2.py index 5007ca7..4d59f6a 100644 --- a/test/test_otultra2.py +++ b/test/test_otultra2.py @@ -30,6 +30,14 @@ class TestOTUltra2(unittest.TestCase): def _set_return_string(self, string): self.mock_readline.return_value = bytes(string, 'ascii') + def test_checksum(self): + checksum = otultra2._calculate_checksum(bytes('T', 'ascii')) + self.assertEqual(0x0054, checksum) + + checksum = otultra2._calculate_checksum( + bytes('T "SAT","08/03/13","22:12:00 "', 'ascii')) + self.assertEqual(0x0608, checksum) + def test_missing_checksum(self): self._set_return_string('INVALID') |