Exemplo n.º 1
0
class CurrentCostReaderTest(TestCase):
    def setUp(self):
        current_cost_sensor.now = lambda: datetime(2012, 12, 13, 14, 15, 16)
        self.serial_device = DummySerial()
        self.publisher = QueuePublisher()
        self.current_cost_reader = AsyncCurrentCostReader(self.serial_device, self.publisher)

    def tearDown(self):
        self.current_cost_reader.remove_reader()
        self.serial_device.close()

    @async_coro
    def test_read_sensor(self):
        self.serial_device.serial.send(
            '<msg><src>CC128-v1.29</src><dsb>00302</dsb><time>02:57:28</time><tmpr>21.4</tmpr><sensor>1</sensor><id>00126</id><type>1</type><ch1><watts>00305</watts></ch1></msg>\n'.encode())

        event = yield from asyncio.wait_for(self.publisher.queue.get(), 1)
        self.assertDictEqual(event, {'date': (current_cost_sensor.now().isoformat()), 'watt': 305, 'temperature': 21.4})

    @async_coro
    def test_read_sensor_xml_error_dont_break_loop(self):
        self.serial_device.write('<malformed XML>\n'.encode())
        self.serial_device.serial.send(
            '<msg><src>CC128-v1.29</src><dsb>00302</dsb><time>02:57:28</time><tmpr>21.4</tmpr><sensor>1</sensor><id>00126</id><type>1</type><ch1><watts>00305</watts></ch1></msg>\n'.encode())

        event = yield from asyncio.wait_for(self.publisher.queue.get(), 1)
        self.assertDictEqual(event, {'date': (current_cost_sensor.now().isoformat()), 'watt': 305, 'temperature': 21.4})
Exemplo n.º 2
0
class TestRfxTrx433e(WithRedis):

    @asyncio.coroutine
    def setUp(self):
        yield from super().setUp()
        self.serial_device = DummySerial()

    def tearDown(self):
        self.serial_device.close()

    @asyncio.coroutine
    def test_read_data(self):
        rfxcom_emiter_receiver.now = lambda: datetime(2015, 2, 14, 15, 0, 0, tzinfo=timezone.utc)
        self.subscriber = yield from self.connection.start_subscribe()
        yield from self.subscriber.subscribe([rfxcom_emiter_receiver.RFXCOM_KEY])
        packet = DummyPacket().load(
            {'packet_length': 10, 'packet_type_name': 'Temperature and humidity sensors', 'sub_type': 1,
             'packet_type': 82, 'temperature': 22.2, 'humidity_status': 0, 'humidity': 0,
             'sequence_number': 1,
             'battery_signal_level': 128, 'signal_strength': 128, 'id': '0xBB02',
             'sub_type_name': 'THGN122/123, THGN132, THGR122/228/238/268'})

        RfxTrx433e(self.serial_device, RedisPublisher(self.connection, rfxcom_emiter_receiver.RFXCOM_KEY),
                   AsyncRedisSubscriber(self.connection, RfxTrx433eMessageHandler(), RFXCOM_KEY_CMD)).handle_temp_humidity(packet)

        message = yield from asyncio.wait_for(self.subscriber.next_published(), 1)
        self.assertDictEqual(dict(packet.data, date=rfxcom_emiter_receiver.now().isoformat()), loads(message.value))

    @asyncio.coroutine
    def test_write_data(self):
        subscriber = AsyncRedisSubscriber(self.connection, RfxTrx433eMessageHandler(), RFXCOM_KEY_CMD).start(for_n_messages=1)
        rfxcom_device = RfxTrx433eForTest(None, RedisPublisher(self.connection, rfxcom_emiter_receiver.RFXCOM_KEY), subscriber)

        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY_CMD, '{"code_device": "1234567", "value": "1"}')
        yield from asyncio.wait_for(subscriber.message_loop_task, 2)

        self.assertEqual(b'0b1100010123456702010f70', binascii.hexlify(rfxcom_device.rfxcom_transport.data_out.pop()))