Ejemplo n.º 1
0
    def test_read_mqtt_multi_device_abstract(self, fix_registered):
        "Test add/remove devices dynamically and read their data."
        import time
        from relayr import Client
        from relayr.resources import Device
        from relayr.dataconnection import MqttStream

        fixture = fix_registered.testset1

        c = Client(token=fixture['token'])

        # create channel with one device (microphone)
        mic = Device(id=fixture['deviceID'], client=c)
        stream = MqttStream(self.receive, [mic])
        stream.start()
        time.sleep(5)

        # add one device (temperature/humidity)
        temphum = Device(id=fixture['deviceID2'], client=c)
        stream.add_device(temphum)
        time.sleep(5)

        # remove one device (microphone)
        stream.remove_device(mic)
        time.sleep(5)

        stream.stop()
Ejemplo n.º 2
0
 def test_send_command_device(self, fix_registered):
     "Test send a command to a device."
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     c = Client(token=token)
     deviceID = fix_registered.testset1['deviceID']
     dev = Device(id=deviceID, client=c)
     dev.get_info()
     dev.switch_led_on(True)
Ejemplo n.º 3
0
    def test_add_list_remove_device_group_device(self, fix_registered):
        "Test add, list, update and remove device to/from device group."
        from relayr import Client
        from relayr.exceptions import RelayrApiException
        from relayr.resources import Group, Device
        token = fix_registered.testset1['token']
        c = Client(token=token)

        # create test group
        group_name = 'TestGroup'
        g = Group(client=c).create(name=group_name)
        g.get_info()
        assert g.name == group_name
        assert len(g.devices) == 0

        # add two WunderBar devices
        d1_id = fix_registered.testset1['deviceID']
        d1 = Device(d1_id)
        g.add_device(d1)
        d2_id = fix_registered.testset1['deviceID2']
        d2 = Device(d2_id)
        g.add_device(d2)

        # get test group, again
        g = Group(id=g.id, client=c).get_info()
        devices = g.devices
        assert len(devices) == 2
        assert devices[0].id == d1_id
        assert devices[1].id == d2_id

        # update last device's position in group to be first
        g.update_device(devices[1], 0)

        # get test group, again
        g = Group(id=g.id, client=c).get_info()
        devices = g.devices
        assert len(devices) == 2
        assert devices[0].id == d2_id
        assert devices[1].id == d1_id

        # remove first device in group
        g.remove_device(devices[0])

        # get test group, again
        g = Group(id=g.id, client=c).get_info()
        devices = g.devices
        assert len(devices) == 1
        assert devices[0].id == d1_id

        # delete test group
        g.delete()
Ejemplo n.º 4
0
    def test_read_mqtt_single_device(self, fix_registered):
        "Test connect to a single device via MQTT and read data."
        import time
        from relayr import Client
        from relayr.resources import Device
        import paho.mqtt.client as mqtt

        token = fix_registered.testset1['token']
        c = Client(token=token)
        deviceId = fix_registered.testset1['deviceID']
        d = Device(id=deviceId, client=c)

        creds = c.api.post_channel(d.id, 'mqtt')
        print('full channel creds:', creds)
        creds = creds['credentials']

        self.topics = [creds['topic'].encode('utf-8')]

        client = mqtt.Client(client_id=creds['clientId'])
        client.on_connect = self.on_connect
        client.on_message = self.on_message
        client.on_subscribe = self.on_subscribe
        client.username_pw_set(creds['user'], creds['password'])

        null_file = '/dev/null' if platform.system() != 'Windows' else 'nul'
        client.tls_set(ca_certs=null_file, cert_reqs=ssl.CERT_NONE)
        client.tls_insecure_set(True)

        client.connect('mqtt.relayr.io', port=8883, keepalive=60)
        try:
            client.loop_forever()
        except KeyboardInterrupt:
            print('')
Ejemplo n.º 5
0
    def get_public_devices(self, meaning=''):
        """
        Returns a generator for all devices on the relayr platform.

        A generator is returned since the called API method always
        returns the entire results list and not a paginated one.


        :arg meaning: The *meaning* (type) of the desired devices.
        :type meaning: string
        :rtype: A generator for :py:class:`relayr.resources.Device` objects.
        """

        for dev in self.api.get_public_devices(meaning=meaning):
            d = Device(dev['id'], client=self)
            d.get_info()
            yield d
Ejemplo n.º 6
0
 def test_access_device(self, fix_registered):
     "Test access a public device."
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     deviceID = fix_registered.testset1['deviceID']
     c = Client(token=token)
     dev = Device(id=deviceID)
Ejemplo n.º 7
0
    def get_device(self, id):
        """
        Returns the device with the specified ID.

        :arg id: the unique ID for the desired device.
        :type id: string
        :rtype: A :py:class:`relayr.resources.Device` object.
        """
        return Device(id=id, client=self)
Ejemplo n.º 8
0
def connect():
    "Connect to a device and read data until interrupted by CTRL+C."

    DSN = 'DSN=' + ODBC_DSN
    if ODBC_USER:
        DSN += ';UID=' + ODBC_USER
    if ODBC_PASSWORD:
        DSN += ';PWD=' + ODBC_PASSWORD

    # Connect to dtabase.
    cnxn = pyodbc.connect(DSN)
    cursor = cnxn.cursor()

    print("Connected to database")

    try:
        # Check if database table already exists.
        cursor.execute("SELECT id, time, sensor, value FROM wunderdata")
    except:  # catch all
        cursor.execute("""
            CREATE TABLE wunderdata (
              id     INT NOT NULL AUTO_INCREMENT,
              device CHAR(40) NOT NULL,
              sensor CHAR(20) NOT NULL,
              time   TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
              value  float NOT NULL,
              PRIMARY KEY (id),
              KEY data (device, sensor, time)
              )""")
        cursor.commit()

    c = Client(token=ACCESS_TOKEN)
    device = Device(id=SENSOR_ID, client=c).get_info()
    callbacks = Callbacks(device, cursor)
    print("Monitoring '%s' (%s) ..." % (device.name, device.id))
    try:
        # Loop until interrupted by keyboard.
        while True:
            stream = MqttStream(callbacks.sensor, [device], transport='mqtt')
            stream.start()
            time.sleep(10)
            stream.stop()
    except KeyboardInterrupt:
        print('')
        stream.stop()
    print("Stopped")

    # Close database connection.
    cnxn.close()
Ejemplo n.º 9
0
def connect():
    "Connect to a device and read data for some time."

    c = Client(token=ACCESS_TOKEN)
    mic = Device(id=MICROPHONE_ID, client=c).get_info()
    callbacks = Callbacks(mic)
    print("Monitoring '%s' (%s) for 60 seconds..." % (mic.name, mic.id))
    stream = MqttStream(callbacks.microphone, [mic], transport='mqtt')
    stream.start()
    try:
        time.sleep(60)
    except KeyboardInterrupt:
        print('')
    stream.stop()
    print("Stopped")
Ejemplo n.º 10
0
 def test_send_command_device(self, fix_registered):
     "Test send a command to a device."
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     c = Client(token=token)
     deviceID = fix_registered.testset1['deviceID']
     dev = Device(id=deviceID, client=c)
     dev.get_info()
     dev.switch_led_on(True)
Ejemplo n.º 11
0
 def test_public_device_credentials(self, fix_registered):
     "Test get credentials for subscribing to a public device."
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     c = Client(token=token)
     deviceID = fix_registered.testset1['deviceID']
     dev = Device(id=deviceID, client=c)
     dev.get_info()
     made_public = False
     if not dev.public:
         dev.update(public=True)
         made_public = True
     creds = c.api.post_devices_public_subscription(deviceID)
     if made_public:
         dev.update(public=False)
     for key in 'authKey subscribeKey cipherKey channel'.split():
         assert key in creds
Ejemplo n.º 12
0
 def test_read_pubnub_device_10s(self, fix_registered):
     "Test connect to a device via PubNub and read data for some time."
     import time
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     deviceID = fix_registered.testset1['deviceID']
     self.received_data = []
     c = Client(token=token)
     dev = Device(id=deviceID, client=c).get_info()
     usr = c.get_user()
     app = c.get_app()
     conn = usr.connect_device(app, dev, self.receive)
     conn.start()
     time.sleep(10)
     conn.stop()
     assert len(self.received_data) > 0
     for item in self.received_data:
         assert 'ts' in item
Ejemplo n.º 13
0
 def test_public_device_credentials(self, fix_registered):
     "Test get credentials for subscribing to a public device."
     from relayr import Client
     from relayr.resources import Device
     token = fix_registered.testset1['token']
     c = Client(token=token)
     deviceID = fix_registered.testset1['deviceID']
     dev = Device(id=deviceID, client=c)
     dev.get_info()
     made_public = False
     if not dev.public:
         dev.update(public=True)
         made_public = True
     creds = c.api.post_devices_public_subscription(deviceID)
     if made_public:
         dev.update(public=False)
     for key in 'authKey subscribeKey cipherKey channel'.split():
         assert key in creds
Ejemplo n.º 14
0
    def test_read_mqtt_single_device_short(self, fix_registered):
        "Test connect to a single device via MQTT and read data."

        import time
        from relayr import Client
        from relayr.resources import Device
        from relayr.dataconnection import MqttStream

        token = fix_registered.testset1['token']
        c = Client(token=token)
        deviceId = fix_registered.testset1['deviceID']
        dev = Device(id=deviceId, client=c)
        received = []

        def callback(topic, payload):
            received.append((topic, payload))

        conn = MqttStream(callback, [dev], transport='mqtt')
        conn.start()
        time.sleep(10)
        conn.stop()
        # dev.delete_channel(creds['channelId'])
        assert len(received) > 0