-
Notifications
You must be signed in to change notification settings - Fork 57
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 #213 from telefonicaid/merge/tc
reMerge/tc
- Loading branch information
Showing
6 changed files
with
602 additions
and
6 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,103 @@ | ||
import sys, mosquitto, re, time | ||
from gw_configuration import MQTT_BROKER_HOSTNAME, MQTT_BROKER_PORT | ||
|
||
# Client configuration | ||
config = { | ||
'host': '', | ||
'port': '', | ||
'client_id': '', | ||
'apikey': '', | ||
'device_id': '', | ||
'num': 0, | ||
'num_commands': 0 | ||
} | ||
|
||
|
||
|
||
def mqtt_command(apikey, device_id, cmd_type, num): | ||
config['host'] = MQTT_BROKER_HOSTNAME; | ||
config['port'] = MQTT_BROKER_PORT; | ||
config['client_id'] = '3333'; | ||
config['apikey'] = apikey | ||
config['device_id'] = device_id | ||
config['num'] = num | ||
config['num_commands'] = 0 | ||
config['type'] = cmd_type | ||
global mqttc; | ||
global num_commands; | ||
mqttc = mosquitto.Mosquitto(config['client_id']) | ||
start(apikey, cmd_type); | ||
|
||
|
||
def on_connect(mosq, obj, rc): | ||
print("rc: "+str(rc)) | ||
topic = '+/+/cmd/+'; | ||
subscribe(topic); | ||
|
||
def on_message(mosq, obj, msg): | ||
message = str(msg.payload); | ||
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) | ||
m = re.match( r'(.*)/(.*)/(.*)/(.*)', msg.topic); | ||
if m: | ||
apikey = m.group(1); | ||
device = m.group(2) | ||
cmd_name = m.group(4); | ||
my_topic = apikey + '/' + device + '/cmdexe/' + cmd_name; | ||
#time.sleep(1) | ||
publish(my_topic, message+"Result|Command_OK"); | ||
config['num_commands']+= 1 | ||
if config['num_commands'] == config['num']: | ||
disconnect() | ||
|
||
def on_publish(mosq, obj, mid): | ||
print("mid: "+str(mid)) | ||
|
||
def on_subscribe(mosq, obj, mid, granted_qos): | ||
print("Subscribed: "+str(mid)+" "+str(granted_qos)) | ||
cmd = re.match( r'pull(.*)', config['type']) | ||
if cmd: | ||
topic = config['apikey'] + '/' + config['device_id'] + '/cmdget' | ||
payload = '11' | ||
publish(topic, payload) | ||
cmd2 = re.match( r'(.*)_not_resp', config['type']) | ||
if cmd2: | ||
time.sleep(2) | ||
disconnect() | ||
|
||
def on_log(mosq, obj, level, string): | ||
print(string) | ||
|
||
|
||
def on_disconnect(mosq, obj, rc): | ||
print("Disconnected successfully.") | ||
|
||
|
||
def start(user,cmd_type): | ||
cmd = re.match( r'(.*)_not_resp', cmd_type) | ||
if not cmd: | ||
mqttc.on_message = on_message | ||
mqttc.username_pw_set(user, "") | ||
mqttc.on_connect = on_connect | ||
mqttc.on_publish = on_publish | ||
mqttc.on_subscribe = on_subscribe | ||
# Uncomment to enable debug messages | ||
mqttc.on_log = on_log | ||
mqttc.on_disconnect = on_disconnect | ||
mqttc.connect(config['host'], config['port'], 60) | ||
it=0 | ||
while it<10: | ||
mqttc.loop() | ||
it+=1 | ||
|
||
def subscribe(topic): | ||
print 'Subscrito a topic: ' +topic | ||
mqttc.subscribe(topic) | ||
|
||
def publish(topic, content): | ||
mqttc.publish(topic, content, 1); | ||
|
||
def disconnect(): | ||
mqttc.disconnect() | ||
|
||
#if __name__ == "__main__": | ||
# mqtt_command() |
49 changes: 49 additions & 0 deletions
49
tests/e2e_tests/component/iot_mqtt/commands/send/send_cmd_mqtt.feature
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,49 @@ | ||
Feature: MQTT Command Send | ||
In order to include MQTT protocol into SBC | ||
As a MQTT subscriber | ||
I want to receive messages from SBC | ||
|
||
@iot_mqtt @iot_cmd_mqtt @IDAS-18382 | ||
Scenario: send a command with parameters | ||
Given a service with name "<service>" and protocol "<protocol>" created | ||
And a device with device id "<device_id>", device name "<device_name>", endpoint "<cmdURL>", protocol "<protocol>", command name "<command>" and command value "<cmd_value>" created | ||
When I send a command to the IoTAgent with service "<service>", device "<device_name>", command "<command>", cmd_type "<cmd_type>" and value "<cmd_name>" | ||
And I wait the pooling period | ||
Then the command of device "<device_name>" with response "<response>" and status "<status>" is received by context broker | ||
|
||
Examples: | ||
|device_id |device_name |service |protocol |command |cmd_name |cmd_value |cmd_type |cmdURL |status |response | | ||
|dev1 |room1 |servicemqtt |IoTMqtt |attr_1_dev_1 |ping1#param1=value1 |dev1@%s#%s |pull | |OK |dev1@ping1#param1=value1 | | ||
|dev1_1 |room1_1 |servicemqtt |IoTMqtt |attr_1_dev_1_1 |ping1_1#p1=v1#p2=v2 |dev1_1@%s#%s#%s |pull | |OK |dev1_1@ping1_1#p1=v1#p2=v2 | | ||
|dev1_2 |room1_2 |servicemqtt |IoTMqtt |attr_1_dev_1_2 |ping1_2#p1=v1#p2=v2#p3=v3#p4=v4|dev1_2@%s#%s#%s#%s#%s |pull | |OK |dev1_2@ping1_2#p1=v1#p2=v2#p3=v3#p4=v4 | | ||
|dev1_3 |room1_3 |servicemqtt |IoTMqtt |attr_1_dev_1_3 |ping1_3#p1=v1#p2=v2 |dev1_3@%s#%s#%s |push |http://myurl.com |OK |dev1_3@ping1_3#p1=v1#p2=v2 | | ||
|
||
@iot_mqtt @iot_cmd_mqtt @IDAS-18381 | ||
Scenario: send a command without parameters | ||
Given a service with name "<service>" and protocol "<protocol>" created | ||
And a device with device id "<device_id>", device name "<device_name>", endpoint "<cmdURL>", protocol "<protocol>", command name "<command>" and command value "<cmd_value>" created | ||
When I send a command to the IoTAgent with service "<service>", device "<device_name>", command "<command>", cmd_type "<cmd_type>" and value "<cmd_name>" | ||
And I wait the pooling period | ||
Then the command of device "<device_name>" with response "<response>" and status "<status>" is received by context broker | ||
|
||
Examples: | ||
|device_id |device_name |service |protocol |command |cmd_name |cmd_value |cmd_type |cmdURL |response |status | | ||
|dev2 |room2 |servicemqtt |IoTMqtt |attr_1_dev_2 |ping2 |dev2@%s |pull | |dev2@ping2Result#Command_OK |OK | | ||
|dev2_1 |room2_1 |servicemqtt |IoTMqtt |attr_1_dev_2_1 |ping2_1 |dev2_1@%s |push |http://myurl.com |dev2_1@ping2_1Result#Command_OK |OK | | ||
|
||
|
||
@iot_mqtt @iot_cmd_mqtt @IDAS-18380 | ||
Scenario: send a command without response | ||
Given a service with name "<service>" and protocol "<protocol>" created | ||
And a device with device id "<device_id>", device name "<device_name>", endpoint "<cmdURL>", protocol "<protocol>", command name "<command>" and command value "<cmd_value>" created | ||
When I send a command to the IoTAgent with service "<service>", device "<device_name>", command "<command>", cmd_type "<cmd_type>" and value "<cmd_name>" | ||
And I wait the command timeout "<timeout>" for "<cmd_type>" | ||
Then the command of device "<device_name>" with response "<response>" and status "<status>" is received by context broker | ||
|
||
Examples: | ||
|device_id |device_name |service |protocol |command |cmd_name |cmd_value |cmd_type |cmdURL |status |timeout | | ||
|dev3 |room3 |servicemqtt |IoTMqtt |attr_1_dev_3 |ping3 |dev3@%s |pull | |expired read |10 | | ||
|dev3_1 |room3_1 |servicemqtt |IoTMqtt |attr_1_dev_3_1 |ping3_1 |dev3_1@%s |push |http://myurl.com |delivered but no respond |10 | | ||
|dev3_2 |room3_2 |servicemqtt |IoTMqtt |attr_1_dev_3_2 |ping3_2 |dev3_2@%s |pull_not_resp | |delivered but no respond |10 | | ||
|dev3_3 |room3_3 |servicemqtt |IoTMqtt |attr_1_dev_3_3 |ping3_3 |dev3_3@%s |push_not_resp |http://myurl.com |delivered but no respond |10 | | ||
|
Oops, something went wrong.