Esempio n. 1
0
 def test_group_properties_are_optional(self):
     group = Group(name="foo")
     self.assertEqual(group.name, 'foo')
     self.assertIsNone(group.description)
     self.assertIsNone(group.source_keys)
     self.assertIsNone(group.id)
     self.assertIsNone(group.key)
     self.assertIsNone(group.feeds)
     self.assertIsNone(group.properties)
Esempio n. 2
0
 def test_group_properties_are_optional(self):
     group = Group(name="foo")
     self.assertEqual(group.name, 'foo')
     self.assertIsNone(group.description)
     self.assertIsNone(group.source_keys)
     self.assertIsNone(group.id)
     self.assertIsNone(group.key)
     self.assertIsNone(group.feeds)
     self.assertIsNone(group.properties)
     """ Let's make sure feeds are explicitly set from within the model:
Esempio n. 3
0
    def test_create_feed_in_group(self):
        """Tests creating a feed within a group.

        """
        io = self.get_client()
        self.ensure_feed_deleted(io, 'testfeed')
        self.ensure_group_deleted(io, 'testgroup')

        group = io.create_group(Group(name='testgroup'))
        feed = Feed(name='testfeed')
        result = io.create_feed(feed, "testgroup")
        self.assertEqual(result.key, "testgroup.testfeed")

        io.delete_feed(result.key)
        io.delete_group('testgroup')
Esempio n. 4
0
 def test_receive_group_by_key(self):
     io = self.get_client()
     self.ensure_group_deleted(io, 'grouprx')
     group = io.create_group(Group(name='grouprx'))
     response = io.groups(group.key)
     self.assertEqual(response.key, 'grouprx')
Esempio n. 5
0
 def test_delete_group(self):
     io = self.get_client()
     self.ensure_group_deleted(io, 'groupdeletetest')
     group = io.create_group(Group(name='groupdeletetest'))
     io.delete_group('groupdeletetest')
     self.assertRaises(RequestError, io.groups, 'groupdeletetest')
Esempio n. 6
0
 def test_groups_retrieves_requested_group(self):
     io = self.get_client()
     self.ensure_group_deleted(io, 'grouptest')
     response = io.create_group(Group(name='grouptest'))
     self.assertEqual(response.name, 'grouptest')
     self.assertEqual(response.key, 'grouptest')
Esempio n. 7
0
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient, Group

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = ENV_DICT['ADAFRUIT_IO_KEY']

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = ENV_DICT['ADAFRUIT_IO_USERNAME']

# Set to the ID of the feed to subscribe to for updates.
FEED_ID = 'pi-lcd.message1'
#group_name = 'pi-lcd'
group_name = Group(name="pi-lcd")


# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print('Connected to Adafruit IO!  Listening for {0} changes...'.format(
        group_name))
    # Subscribe to changes on a feed named DemoFeed.
    #client.subscribe(FEED_ID)
    client.subscribe_group(group_name)

Esempio n. 8
0
    def __init__(self, update_sec=30, cfg_file="aiocfg.txt"):

        # Update period
        # Rate limit is 30 updates every 60 seconds = 6 feeds every 12 seconds
        if update_sec > 60/(30/6):
            self.update_sec = datetime.timedelta(seconds=update_sec)
        else:
            self.update_sec = datetime.timedelta(seconds=12)

        # Force feed update when the send_data_all_feeds() is called for the first time
        self._crt_time  = datetime.datetime.utcnow()
        self._last_time = datetime.datetime.utcnow() - self.update_sec

        # Read the access info
        self.cfg_file = os.path.join(Path(__file__).parent.absolute(), cfg_file)
        try:
            with open(self.cfg_file,'r') as f:
                aio_info = f.read().split('\n')

                # Access
                self.aio_acc = aio_info[0].split(',',2)

                # Feed group
                self.aio_grp = aio_info[1].split(',',2)

                if DEBUG:
                    print(f"Read config file {self.cfg_file}:")
                    print(self.aio_grp)

                # Feed names
                self.aio_feednames = aio_info[2].split(',',6)

                if DEBUG:
                    print(self.aio_feednames)

                # Location (lat, lon and ele)
                self.aio_loc = aio_info[3].split(',',3)

                if DEBUG:
                    print(self.aio_loc)

        except IOError:
            print(f"Adafruit IO:: Configuration file '{self.cfg_file}' read error (not found or cannot be read)! Exiting!")
            raise

        # Create an instance of the REST client.
        self.aio = Client(self.aio_acc[0], self.aio_acc[1])

        # Get/Create the group
        try:
            # Get the WS group
            self.group  = self.aio.groups(self.aio_grp[1])

        except errors.RequestError:
            # The returned object will contain all the details about the created group.
            self.group = self.aio.create_group(Group(name=self.aio_grp[0]))
            pass

        # Get/Create the feeds
        if len(self.group.feeds):

            # Get the feed names
            self._feednames = dict()
            for _idx in range(len(self.group.feeds)):
                self._feednames[self.group.feeds[_idx].name] = _idx

            if DEBUG > 1:
                print(self._feednames)

            self._direction   = self.group.feeds[self._feednames[self.aio_feednames[0]]]
            self._temperature = self.group.feeds[self._feednames[self.aio_feednames[1]]]
            self._speed       = self.group.feeds[self._feednames[self.aio_feednames[2]]]
            self._pressure    = self.group.feeds[self._feednames[self.aio_feednames[3]]]
            self._humidity    = self.group.feeds[self._feednames[self.aio_feednames[4]]]
            self._rssi        = self.group.feeds[self._feednames[self.aio_feednames[5]]]

            if DEBUG:
                print(f"Got group {self.group.name} with {len(self.group.feeds)} feeds.")

        else:

            # Create feeds in the group
            feed = Feed(name=self.aio_feednames[0])
            self._direction = self.aio.create_feed(feed, self.group.name)
            feed = Feed(name=self.aio_feednames[1])
            self._temperature = self.aio.create_feed(feed, self.group.name)
            feed = Feed(name=self.aio_feednames[2])
            self._speed = self.aio.create_feed(feed, self.group.name)
            feed = Feed(name=self.aio_feednames[3])
            self._pressure = self.aio.create_feed(feed, self.group.name)
            feed = Feed(name=self.aio_feednames[4])
            self._humidity = self.aio.create_feed(feed, self.group.name)
            feed = Feed(name=self.aio_feednames[5])
            self._rssi = self.aio.create_feed(feed, self.group.name)

            if DEBUG:
                print(f"Created group {self.group.name} with {len(self.group.feeds)} feeds.")