-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
34 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
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,106 @@ | ||
"""Test mqtt messages exchanges between a client and the cc3200 board | ||
""" | ||
import pytest | ||
import os | ||
import asyncio | ||
import pynais as ns | ||
|
||
# the Leds message | ||
import commands as cmd | ||
|
||
# the firmware test machinery | ||
import pynais.iottest as iot | ||
|
||
import hbmqtt.client as mqtt | ||
from hbmqtt.mqtt.constants import QOS_0, QOS_1, QOS_2 | ||
import hbmqtt.broker | ||
|
||
# import firmware configuration | ||
import tc | ||
|
||
import time | ||
|
||
events = ( | ||
('board init', 'BOARD_INIT'), | ||
('board ready', 'BOARD_READY') | ||
) | ||
|
||
def handle_event(event, uart): | ||
if (event.name == 'BOARD_INIT'): | ||
profile = ns.msg.Profile( | ||
os.environ['TEST_UID'], pwd=os.environ['TEST_PWD']) | ||
uart.write(ns.marshall(profile)) | ||
|
||
board_cfg = ns.msg.Config(host=ns.my_ip_address(), | ||
network=tc.network, board=tc.board) | ||
uart.write(ns.marshall(board_cfg)) | ||
|
||
return 1 | ||
|
||
if (event.name == 'BOARD_READY'): | ||
return 1 | ||
|
||
if (event.name == 'SERIAL_TIMEOUT'): | ||
assert 0 | ||
|
||
|
||
async def start_broker(): | ||
config = { | ||
'listeners': { | ||
'default': { | ||
'type': 'tcp', | ||
'bind': '0.0.0.0:1883', | ||
} | ||
}, | ||
'sys_interval': 10 | ||
} | ||
|
||
# start broker | ||
broker = hbmqtt.broker.Broker(config) | ||
await broker.start() | ||
|
||
#await a little bit for board connection ... | ||
await asyncio.sleep(4) | ||
|
||
return broker | ||
|
||
|
||
def test_init_board(): | ||
|
||
# check that wlan env variables username and password are set | ||
tc.check_test_requirements() | ||
|
||
iot.run('nais', tc.fw_deploy_ctx, events=events, handler=handle_event) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_add_profile(): | ||
broker = await start_broker() | ||
|
||
profile = ns.msg.Profile(uid='baba', pwd='there_is_no_secret_for_me') | ||
msg = ns.marshall(profile) | ||
|
||
cli = mqtt.MQTTClient() | ||
await cli.connect(tc.broker) | ||
|
||
await cli.publish('{}/{}'.format(tc.network, tc.board), msg, qos=QOS_0) | ||
|
||
await cli.subscribe([('hb/{}/{}'.format(tc.network, tc.board), QOS_0)]) | ||
|
||
try: | ||
# wait for the ack | ||
message = await cli.deliver_message(timeout=3) | ||
packet = message.publish_packet | ||
payload = packet.payload.data | ||
print("%s => %s" % | ||
(packet.variable_header.topic_name, str(payload))) | ||
|
||
ack = ns.unmarshall(payload) | ||
print(ack) | ||
except asyncio.TimeoutError: | ||
assert 0 | ||
finally: | ||
await cli.disconnect() | ||
await broker.shutdown() | ||
|
||
|