Exemplo n.º 1
0
 def _test_encode_decode(self, msg: OatmealMsg) -> OatmealMsg:
     """ Returns the message after being encoded and decoded """
     frame = msg.encode()
     msg2 = OatmealMsg.decode(frame)
     frame2 = msg2.encode()
     # Test frame is valid and that encoding/decoding worked
     self._assert_valid_frame(frame)
     self.assertEqual(msg, msg2)
     self.assertEqual(msg.args, msg2.args)
     self.assertEqual(frame, frame2)
     return msg2
Exemplo n.º 2
0
    def test_raw_bytes(self) -> None:
        # Test data encoding
        self._test_args(random_bytearray(0))
        self._test_args(random_bytearray(1))
        self._test_args(random_bytearray(2))
        self._test_args(random_bytearray(3))
        self._test_args(random_bytearray(50))
        self._test_args(random_bytearray(100))

        # Decode, then encode and compare
        long0 = b'<HRTBVU{a=5.1,avail_kb=247,b="hi",loop_ms=1,uptime=16}>BH'
        long1 = b'<HRTB0E{Itotal=0.372172,v1=F,v10=F,v2=F,v3=F,v4=F,v5=F,v6=F,v7=F,v8=F,v9=F}>yI'
        long2 = b'<DISAea"ValveCluster",0,"0031FFFFFFFFFFFF4E45356740010017","e5938cd">Hg'
        self.assertEqual(OatmealMsg.decode(bytearray(long0)).encode(), long0)
        self.assertEqual(OatmealMsg.decode(bytearray(long1)).encode(), long1)
        self.assertEqual(OatmealMsg.decode(bytearray(long2)).encode(), long2)
Exemplo n.º 3
0
 def test_properties(self) -> None:
     msg = OatmealMsg("TSTR", 1, "abc", [], token="xy")
     self.assertEqual(msg.opcode, "TSTR")
     self.assertEqual(msg.command, "TST")
     self.assertEqual(msg.flag, "R")
     self.assertEqual(msg.token, "xy")
     self.assertEqual(msg.args, [1, "abc", []])
     self.assertEqual(msg.checksums, "SM")
Exemplo n.º 4
0
 def test_comparison(self) -> None:
     msgs = [
         OatmealMsg("TSTR", token="aa"),
         OatmealMsg("TSTR", "arg", token="aa"),
         OatmealMsg("TSTR", token="ab"),
         OatmealMsg("TSTA", token="aa")
     ]
     # Assert that no pair of OatmealMsg from msgs is equal
     for a, b in itertools.combinations(msgs, 2):
         self.assertNotEqual(a, b)
     # Assert that each message is equal to itself and a clone of itself
     for x in msgs:
         self.assertEqual(x, x)
         self.assertEqual(x, OatmealMsg(x.opcode, *x.args, token=x.token))
     # Assert that there is a total ordering over all msgs
     sorted_msgs = sorted(msgs)
     for i in range(len(sorted_msgs) - 1):
         self.assertLess(sorted_msgs[i], sorted_msgs[i + 1])
Exemplo n.º 5
0
 def get_adc_values(self):
     """
     Returns a list of two intgers representing the values of ADC0 and ADC1
     """
     resp = self.send_and_ack(OatmealMsg("ADCR"))
     return resp.args
Exemplo n.º 6
0
 def set_led_state(self, led_state):
     self.send_and_ack(OatmealMsg("LEDR", bool(led_state)))
Exemplo n.º 7
0
 def get_all_vals(self):
     resp = self.send_and_ack(OatmealMsg("FETR"))
     return resp.args
Exemplo n.º 8
0
 def get_val(self, name):
     resp = self.send_and_ack(OatmealMsg("GETR", name))
     return resp.args[0]
Exemplo n.º 9
0
 def set_val(self, name, val):
     self.send_and_ack(OatmealMsg("SETR", name, val))
Exemplo n.º 10
0
 def _test_args(self, *args) -> None:
     msg = OatmealMsg("TSTR", *args, token="aa")
     msg2 = self._test_encode_decode(msg)
     self.assertEqual(msg2.args, list(args))
Exemplo n.º 11
0
 def test_checksum(self) -> None:
     """ Test checksum values match those we're pre-computed. """
     msg0 = OatmealMsg("DISR", token='XY')
     msg1 = OatmealMsg("RUNR", 1.23, True, "Hi!", [1, 2], token='aa')
     msg2 = OatmealMsg("XYZA", 101, [0, 42], token='zZ')
     msg3 = OatmealMsg("LOLR", 123, True, 99.9, token='Oh')
     self.assertEqual(msg0.encode(), b'<DISRXY>i_')
     self.assertEqual(msg1.encode(), b'<RUNRaa1.23,T,"Hi!",[1,2]>-b')
     self.assertEqual(msg2.encode(), b'<XYZAzZ101,[0,42]>SH')
     self.assertEqual(msg3.encode(), b'<LOLROh123,T,99.9>SS')
Exemplo n.º 12
0
    def test_dicts(self) -> None:
        # Test empty and nested dicts
        msg0 = OatmealMsg("TSTR", {}, token='XY')
        msg1 = OatmealMsg("TSTR", "", {}, [], token='aa')
        msg2 = OatmealMsg("TSTR",
                          "", {
                              'a': {
                                  'b': {}
                              },
                              'c': {}
                          }, [],
                          token='XY')

        # Test different data types in dicts
        msg3 = OatmealMsg("XYZA",
                          1,
                          2, {
                              'int': -1,
                              'float': 1.2,
                              'bool': True,
                              'str': "asdf",
                              'bytes': b'123',
                              'list': [1, 2, "hi"],
                              'none': None
                          },
                          None,
                          token='zZ')

        self.assertEqual(OatmealMsg.decode(msg0.encode()), msg0)
        self.assertEqual(OatmealMsg.decode(msg1.encode()), msg1)
        self.assertEqual(OatmealMsg.decode(msg2.encode()), msg2)
        self.assertEqual(OatmealMsg.decode(msg3.encode()), msg3)
Exemplo n.º 13
0
 def _test_parse_args_fails(self, args: str) -> None:
     """ Test that parsing the given arg string raises an exception """
     with self.assertRaises(OatmealParseError):
         OatmealMsg._parse_args(args.encode('ascii'))
Exemplo n.º 14
0
 def _test_parse_args(self, args: str, expected_res: list) -> None:
     """ Parse args and test that we get a given result """
     parsed_args = OatmealMsg._parse_args(args.encode('ascii'))
     self.assertEqual(parsed_args, expected_res)