def send_iot_device_messages(args):
    """Connects a device, sends data, and receives data."""
    # [START iot_mqtt_run]
    global minimum_backoff_time
    global MAXIMUM_BACKOFF_TIME

    # Publish to the events or state topic based on the flag.
    sub_topic = 'events' if args.message_type == 'event' else 'state'

    mqtt_topic = '/devices/{}/{}'.format(args.device_id, sub_topic)

    jwt_iat = datetime.datetime.utcnow()
    jwt_exp_mins = args.jwt_expires_minutes
    client = get_client(args.project_id, args.cloud_region, args.registry_id,
                        args.device_id, args.private_key_file, args.algorithm,
                        args.ca_certs, args.mqtt_bridge_hostname,
                        args.mqtt_bridge_port)

    # Publish num_messages messages to the MQTT bridge once per second.
    with open('data\iot_waste_tracking.tsv', newline='') as tsvfile:
        file_rec = csv.DictReader(tsvfile, delimiter='\t')
        # file_rec = csv.reader(tsvfile, delimiter='\t')
        for row in file_rec:

            payload = json.dumps(row)
            print(payload)

            # Process network events.
            client.loop()

            # [START iot_mqtt_jwt_refresh]
            seconds_since_issue = (datetime.datetime.utcnow() -
                                   jwt_iat).seconds
            if seconds_since_issue > 60 * jwt_exp_mins:
                print('Refreshing token after {}s'.format(seconds_since_issue))
                jwt_iat = datetime.datetime.utcnow()
                client.loop()
                client.disconnect()
                client = get_client(args.project_id, args.cloud_region,
                                    args.registry_id, args.device_id,
                                    args.private_key_file, args.algorithm,
                                    args.ca_certs, args.mqtt_bridge_hostname,
                                    args.mqtt_bridge_port)
            # [END iot_mqtt_jwt_refresh]
            # Publish "payload" to the MQTT topic. qos=1 means at least once
            # delivery. Cloud IoT Core also supports qos=0 for at most once
            # delivery.
            client.publish(mqtt_topic, payload, qos=1)

            # Send events every second. State should not be updated as often
            for i in range(0, 2):
                time.sleep(1)
                client.loop()
Beispiel #2
0
def test_state(test_topic, test_registry_id, test_device_id, capsys):  # noqa
    manager.get_device(service_account_json, project_id, cloud_region,
                       test_registry_id, test_device_id)

    sub_topic = 'state'
    mqtt_topic = '/devices/{}/{}'.format(test_device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              test_registry_id, test_device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 443)
    client.publish(mqtt_topic, 'state test', qos=1)
    client.loop_start()

    time.sleep(3)

    client.loop_stop()

    manager.get_state(service_account_json, project_id, cloud_region,
                      test_registry_id, test_device_id)

    out, _ = capsys.readouterr()
    assert 'on_publish' in out
    assert 'binary_data: "state test"' in out
Beispiel #3
0
def test_receive_command(test_registry_id, test_device_id, capsys):  # noqa
    # Exercize the functionality
    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              test_registry_id, test_device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 443)
    client.loop_start()

    # Pre-process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    manager.send_command(service_account_json, project_id, cloud_region,
                         test_registry_id, test_device_id, 'me want cookies')

    # Process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    out, _ = capsys.readouterr()
    assert 'on_connect' in out  # Verify can connect
    assert '\'me want cookies\'' in out  # Verify can receive command
def test_config(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_rs256_device(service_account_json, project_id, cloud_region,
                                registry_id, device_id, rsa_cert_path)

    manager.get_device(service_account_json, project_id, cloud_region,
                       registry_id, device_id)

    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              registry_id, device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 443)
    client.loop_start()

    time.sleep(5)

    client.loop_stop()

    manager.get_state(service_account_json, project_id, cloud_region,
                      registry_id, device_id)

    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, device_id)

    manager.delete_registry(service_account_json, project_id, cloud_region,
                            registry_id)

    out, _ = capsys.readouterr()
    assert "Received message" in out
    assert '/devices/{}/config'.format(device_id) in out
Beispiel #5
0
def test_event(test_topic, test_registry_id, test_device_id, capsys):  # noqa
    manager.get_device(service_account_json, project_id, cloud_region,
                       test_registry_id, test_device_id)

    sub_topic = "events"
    mqtt_topic = "/devices/{}/{}".format(test_device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(
        project_id,
        cloud_region,
        test_registry_id,
        test_device_id,
        rsa_private_path,
        "RS256",
        ca_cert_path,
        "mqtt.googleapis.com",
        443,
    )

    client.loop_start()
    client.publish(mqtt_topic, "just test", qos=1)
    time.sleep(2)
    client.loop_stop()

    manager.get_state(service_account_json, project_id, cloud_region,
                      test_registry_id, test_device_id)

    out, _ = capsys.readouterr()
    assert "on_publish" in out
Beispiel #6
0
def test_config(test_topic, test_registry_id, test_device_id, capsys):  # noqa
    manager.get_device(service_account_json, project_id, cloud_region,
                       test_registry_id, test_device_id)

    client = cloudiot_mqtt_example.get_client(
        project_id,
        cloud_region,
        test_registry_id,
        test_device_id,
        rsa_private_path,
        "RS256",
        ca_cert_path,
        "mqtt.googleapis.com",
        443,
    )
    client.loop_start()

    time.sleep(5)

    client.loop_stop()

    manager.get_state(service_account_json, project_id, cloud_region,
                      test_registry_id, test_device_id)

    out, _ = capsys.readouterr()
    assert "Received message" in out
    assert "/devices/{}/config".format(test_device_id) in out
def transmit_image(
    cloud_region,
    registry_id,
    device_id,
    rsa_private_path,
    ca_cert_path,
    image_path,
    project_id,
    service_account_json,
):
    """Send an inage to a device registry"""

    with io.open(image_path, "rb") as image_file:
        image_data = base64.b64encode(image_file.read()).decode("utf-8")

    sub_topic = "events"
    mqtt_topic = "/devices/{}/{}".format(device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(
        project_id,
        cloud_region,
        registry_id,
        device_id,
        rsa_private_path,
        "RS256",
        ca_cert_path,
        "mqtt.googleapis.com",
        8883,
    )

    client.loop_start()
    client.publish(mqtt_topic, image_data)
    time.sleep(2)
    client.loop_stop()
def test_state(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_rs256_device(service_account_json, project_id, cloud_region,
                                registry_id, device_id, rsa_cert_path)

    manager.get_device(service_account_json, project_id, cloud_region,
                       registry_id, device_id)

    sub_topic = 'state'
    mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              registry_id, device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 443)
    client.publish(mqtt_topic, 'state test', qos=1)
    time.sleep(3)
    client.loop_stop()

    manager.get_state(service_account_json, project_id, cloud_region,
                      registry_id, device_id)

    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, device_id)

    manager.delete_registry(service_account_json, project_id, cloud_region,
                            registry_id)

    out, _ = capsys.readouterr()
    assert 'on_publish' in out
    assert 'c3RhdGUgdGVzdA' in out
Beispiel #9
0
def test_send_command(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    exists = False
    devices = manager.list_devices(
            service_account_json, project_id, cloud_region, registry_id)
    for device in devices:
        if device.get('id') == device_id:
            exists = True

    if not exists:
        manager.create_rs256_device(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, rsa_cert_path)

    # Exercize the functionality
    client = cloudiot_mqtt_example.get_client(
        project_id, cloud_region, registry_id, device_id,
        rsa_private_path, 'RS256', ca_cert_path,
        'mqtt.googleapis.com', 443)
    client.loop_start()
    out, _ = capsys.readouterr()

    # Pre-process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    manager.send_command(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, 'me want cookies')
    out, _ = capsys.readouterr()

    # Process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    # Clean up
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    assert 'Sending command to device' in out
    assert '400' not in out
def test_send_command(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    exists = False
    devices = manager.list_devices(
            service_account_json, project_id, cloud_region, registry_id)
    for device in devices:
        if device.get('id') == device_id:
            exists = True

    if not exists:
        manager.create_rs256_device(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, rsa_cert_path)

    # Exercize the functionality
    client = cloudiot_mqtt_example.get_client(
        project_id, cloud_region, registry_id, device_id,
        rsa_private_path, 'RS256', ca_cert_path,
        'mqtt.googleapis.com', 443)
    client.loop_start()
    out, _ = capsys.readouterr()

    # Pre-process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    manager.send_command(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, 'me want cookies')
    out, _ = capsys.readouterr()

    # Process commands
    for i in range(1, 5):
        client.loop()
        time.sleep(1)

    # Clean up
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    assert 'Sending command to device' in out
    assert '400' not in out
def test_state(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    manager.create_rs256_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, rsa_cert_path)

    manager.get_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    sub_topic = 'state'
    mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(
        project_id, cloud_region, registry_id, device_id,
        rsa_private_path, 'RS256', ca_cert_path,
        'mqtt.googleapis.com', 443)
    client.publish(mqtt_topic, 'state test', qos=1)
    client.loop_start()

    time.sleep(3)

    client.loop_stop()

    manager.get_state(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'on_publish' in out
    assert 'c3RhdGUgdGVzdA' in out
Beispiel #12
0
def transmit_image(cloud_region, registry_id, device_id, rsa_private_path,
                   ca_cert_path, image_path, project_id, service_account_json):
    """Send an inage to a device registry"""

    with io.open(image_path, 'rb') as image_file:
        image_data = base64.b64encode(image_file.read()).decode('utf-8')

    sub_topic = 'events'
    mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic)

    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              registry_id, device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 8883)

    client.loop_start()
    client.publish(mqtt_topic, image_data, qos=1)
    time.sleep(2)
    client.loop_stop()
def test_receive_command(capsys):
    device_id = device_id_template.format('RSA256')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_rs256_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, rsa_cert_path)

    # Exercize the functionality
    client = cloudiot_mqtt_example.get_client(
        project_id, cloud_region, registry_id, device_id,
        rsa_private_path, 'RS256', ca_cert_path,
        'mqtt.googleapis.com', 443)
    client.loop_start()

    # Pre-process commands
    for i in range(1, 3):
        client.loop()
        time.sleep(1)

    manager.send_command(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, 'me want cookies')

    # Process commands
    for i in range(1, 3):
        client.loop()
        time.sleep(1)

    # Clean up
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'on_connect' in out  # Verify can connect
    assert '\'me want cookies\'' in out  # Verify can receive command
def test_config(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    manager.create_rs256_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, rsa_cert_path)

    manager.get_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    client = cloudiot_mqtt_example.get_client(
        project_id, cloud_region, registry_id, device_id,
        rsa_private_path, 'RS256', ca_cert_path,
        'mqtt.googleapis.com', 443)
    client.loop_start()

    time.sleep(5)

    client.loop_stop()

    manager.get_state(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert "Received message" in out
    assert '/devices/{}/config'.format(device_id) in out
def test_receive_command(capsys):
    device_id = device_id_template.format('RSA256')
    manager.create_registry(service_account_json, project_id, cloud_region,
                            pubsub_topic, registry_id)
    manager.create_rs256_device(service_account_json, project_id, cloud_region,
                                registry_id, device_id, rsa_cert_path)

    # Exercize the functionality
    client = cloudiot_mqtt_example.get_client(project_id, cloud_region,
                                              registry_id, device_id,
                                              rsa_private_path, 'RS256',
                                              ca_cert_path,
                                              'mqtt.googleapis.com', 443)
    client.loop_start()

    # Pre-process commands
    for i in range(1, 3):
        client.loop()
        time.sleep(1)

    manager.send_command(service_account_json, project_id, cloud_region,
                         registry_id, device_id, 'me want cookies')

    # Process commands
    for i in range(1, 3):
        client.loop()
        time.sleep(1)

    # Clean up
    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, device_id)
    manager.delete_registry(service_account_json, project_id, cloud_region,
                            registry_id)

    out, _ = capsys.readouterr()
    assert 'on_connect' in out  # Verify can connect
    assert '\'me want cookies\'' in out  # Verify can receive command