-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollow_basic_catchall.py
75 lines (58 loc) · 2.67 KB
/
follow_basic_catchall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
# Copyright (c) 2017 Iotic Labs Ltd. All rights reserved.
from __future__ import unicode_literals, print_function
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s [%(name)s] {%(threadName)s} %(message)s', level=logging.WARNING)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
from IoticAgent.Core.compat import monotonic
from IoticAgent.ThingRunner import RetryingThingRunner
from IoticAgent import Datatypes, Units
class FollowBasicCatchall(RetryingThingRunner):
LOOP_TIMER = 10 # minimum number of seconds duration of the main loop
def __init__(self, config=None):
"""Instantiation code in here, after the call to super().__init__()
"""
super(FollowBasicCatchall, self).__init__(config=config)
self.__thing = None
@staticmethod
def __catchall(args):
logger.debug("Catchall data received. Shared at %s", args['time'])
# Used for any catchall data that can't be parsed
try:
logger.debug('Found recent data for key %s: value: %s', 'count', args['data']['count'])
except KeyError as exc:
logger.warning('Failed to find key %s in recent data %s', exc, args)
raise
@staticmethod
def __catchall_parsed(args):
logger.debug("Feed data received. Shared at %s", args['time'])
values = args['parsed'].filter_by(types=(Datatypes.INTEGER,), units=(Units.COUNTS_PER_MIN,), text=("random",))
if values:
logger.debug('Found parsed data for key %s: value: %s', values[0].label, values[0].value)
else:
logger.debug('Parsed data not found')
def on_startup(self):
"""Called once at the beginning, before main().
Use this method to create your things, rebind connections, setup hardware, etc.
"""
print("Started. Press CTRL+C to end")
self.__thing = self.client.create_thing('follow_basic')
# register catchall for any data that's been queued for you before you start
# or any feed data that's not got a specific callback
self.client.register_catchall_feeddata(self.__catchall, callback_parsed=self.__catchall_parsed)
def main(self):
"""Called after on_startup.
Use this method for your main loop (we don't need one here).
Set self.LOOP_TIMER for your regular tick
"""
while True:
start = monotonic()
# loop code in here
stop = monotonic()
if self.wait_for_shutdown(max(0, self.LOOP_TIMER - (stop - start))):
break
def main():
FollowBasicCatchall(config="agent2.ini").run()
if __name__ == '__main__':
main()