koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame^] | 1 | from collections import namedtuple |
| 2 | |
| 3 | SensorInfo = namedtuple("SensorInfo", ['value', 'is_accumulated']) |
| 4 | |
| 5 | |
| 6 | def is_dev_accepted(name, disallowed_prefixes, allowed_prefixes): |
| 7 | dev_ok = True |
| 8 | |
| 9 | if disallowed_prefixes is not None: |
| 10 | dev_ok = all(not name.startswith(prefix) |
| 11 | for prefix in disallowed_prefixes) |
| 12 | |
| 13 | if dev_ok and allowed_prefixes is not None: |
| 14 | dev_ok = any(name.startswith(prefix) |
| 15 | for prefix in allowed_prefixes) |
| 16 | |
| 17 | return dev_ok |
| 18 | |
| 19 | |
| 20 | def delta(func, only_upd=True): |
| 21 | prev = {} |
| 22 | while True: |
| 23 | for dev_name, vals in func(): |
| 24 | if dev_name not in prev: |
| 25 | prev[dev_name] = {} |
| 26 | for name, (val, _) in vals.items(): |
| 27 | prev[dev_name][name] = val |
| 28 | else: |
| 29 | dev_prev = prev[dev_name] |
| 30 | res = {} |
| 31 | for stat_name, (val, accum_val) in vals.items(): |
| 32 | if accum_val: |
| 33 | if stat_name in dev_prev: |
| 34 | delta = int(val) - int(dev_prev[stat_name]) |
| 35 | if not only_upd or 0 != delta: |
| 36 | res[stat_name] = str(delta) |
| 37 | dev_prev[stat_name] = val |
| 38 | elif not only_upd or '0' != val: |
| 39 | res[stat_name] = val |
| 40 | |
| 41 | if only_upd and len(res) == 0: |
| 42 | continue |
| 43 | yield dev_name, res |
| 44 | yield None, None |