Esempio n. 1
0
def run():
    entries_invoke_threaded('start')

    try:
        while True:
            run_step()
            system.sleep(1)

    except KeyboardInterrupt:
        logging.info('keyboard interruption detected')
    except:
        logging.exception('system failure on run')
Esempio n. 2
0
def _health_checker_timer(entry):
  # if system load_level is high i disable health_publish_checker and health_dead_checker. When the load returns low, i'll reset health checker data (to avoid fake health problems)
  health_disable_load_level = 0
  
  while not threading.currentThread()._destroyed:
    now = system.time()
    
    if node.load_level() > 0:
      health_disable_load_level = now
    elif health_disable_load_level > 0 and node.load_level() == 0 and now - health_disable_load_level > 60:
      health_disable_load_level = 0
      # if a moment ago the system was too load, and now is ok, i must consider health_dead_checker and health_publish_checker data as invalid and reset them (or i'll report a lot of fake health problems)
    
    if health_disable_load_level > 0:
      # health_dead_checker
      for entry_id in entry.health_dead_checker:
        source_entry = system.entry_get(entry_id)
        if source_entry:
          entry.health_dead_checker[entry_id] = (system.time() + entry.health_dead_checker[entry_id][1], entry.health_dead_checker[entry_id][1], entry.health_dead_checker[entry_id][2])

      # health_publish_checker
      for t in entry.health_publish_checker:
        for e in entry.health_publish_checker[t]:
          if entry.health_publish_checker[t][e]['last_published'] > 0:
            entry.health_publish_checker[t][e]['last_published'] = system.time()
    
    else:
      # health_dead_checker
      timeouts = [ entry_id for entry_id in entry.health_dead_checker if now > entry.health_dead_checker[entry_id][0] ]
      if timeouts:
        for entry_id in timeouts:
          source_entry = system.entry_get(entry_id)
          if source_entry:
            source_entry.health_dead = entry.health_dead_checker[entry_id][2]
            check_health_status(source_entry)
        entry.health_dead_checker = { entry_id: entry.health_dead_checker[entry_id] for entry_id in entry.health_dead_checker if entry_id not in timeouts }
      
      # health_publish_checker
      delay = system.broker().queueDelay() * 2 if not system.test_mode else 0
      for t in entry.health_publish_checker:
        for e in entry.health_publish_checker[t]:
          if entry.health_publish_checker[t][e]['last_published'] > 0 and now - entry.health_publish_checker[t][e]['last_published'] > entry.health_publish_checker[t][e]['interval'] + delay:
            target_entry = system.entry_get(e)
            if target_entry and t not in target_entry.health_publish:
              target_entry.health_publish[t] = [now, entry.health_publish_checker[t][e]['last_published'], entry.health_publish_checker[t][e]['interval'], delay]
              check_health_status(target_entry)

    system.sleep(entry.config['health-checker-secs'])
Esempio n. 3
0
def waitPublish(topic, payload, qos=0, retain=False, timeoutms=2000):
    """
  Publish a topic and wait it's received by node
  """
    global checks
    now = system.timems()
    checks.append({
        'unit': False,
        'name': False,
        'topic': topic,
        'payload': payload,
        'maxtime': now + timeoutms
    })
    system.broker().mqtt_subscribe_pause_on_topic = topic
    system.broker().publish(topic, payload, qos=qos, retain=retain)
    while len(checks) > 0:
        system.sleep(.1)
    system.broker().mqtt_subscribe_pause_on_topic = None
Esempio n. 4
0
def init(_storage_fetch_data=True):
    global node_config, storage_fetch_data, threads, _system_initialized
    storage_fetch_data = _storage_fetch_data

    storage.init(os.path.join(node_config['base_path'],
                              node_config['storage']))

    threads = {}

    system.on_entry_load(_on_system_entry_load)
    system.on_entry_load_batch(_on_system_entry_load_batch)
    system.on_entry_init(_on_system_entry_init)
    system.on_entry_init_batch(_on_system_entry_init_batch)
    system.on_entry_unload(_on_system_entry_unload)
    system.on_entries_change(_on_system_entries_change)
    system.on_message(_on_system_message)
    system.init(_on_system_initialized)
    while not _system_initialized:
        system.sleep(.5)
Esempio n. 5
0
def publish_bandwidth(entry, topic_rule, topic_definition):
    if not entry.config['bandwidth-enabled']:
        return

    data1 = _bandwidth_proc_net_dev_data(entry.config)
    if not data1:
        entry.publish('', {'error': 'error getting bandwidth'})
        return

    seconds = utils.read_duration(entry.config['bandwidth_check_time'])
    system.sleep(seconds)
    data2 = _bandwidth_proc_net_dev_data(entry.config)
    down_bps = (data2['down'] - data1['down']) * 8 / seconds
    up_bps = (data2['up'] - data1['up']) * 8 / seconds

    entry.publish(
        '', {
            'download_bps': round(down_bps),
            'download_mbps': round(down_bps / (1024 * 1024), 1),
            'upload_bps': round(up_bps),
            'upload_mbps': round(up_bps / (1024 * 1024), 1),
            'time': system.time(),
        })
def _thread_checker(installer_entry):
    while not threading.currentThread()._destroyed:
        status_check(installer_entry)
        system.sleep(
            utils.read_duration(installer_entry.config['connection_time']) /
            10)
Esempio n. 7
0
def _test_thread():
    global destroyed, assertsRunning, checks
    while not destroyed:
        now = system.timems()

        delete = []
        for check in checks:
            unitname = check['unit'] + '.' + check['name'] if check[
                'unit'] else False
            _delete = False
            if 'topic' in check:
                topic = check['topic']
                if 'maxtime' in check and now > check['maxtime']:
                    if not unitname:
                        logging.error(
                            "waitPublish of " + topic + ": " +
                            str(check['payload']) +
                            " exceed timeout, the process will continue but problems will probably occours"
                        )
                    elif 'events' in check:
                        _assertsRunningDone(
                            unitname, 'events', False,
                            'received NOTHING, want: ' + str(check['events']))
                    elif 'some_events' in check:
                        _assertsRunningDone(
                            unitname, 'some_events', False,
                            'received NOTHING, want (some): ' +
                            str(check['some_events']))
                    elif 'payload' in check:
                        _assertsRunningDone(
                            unitname, 'subscribe_payload', False,
                            'received NOTHING, want: ' + topic + ' = ' +
                            str(check['payload']))
                    elif 'some_payload' in check:
                        _assertsRunningDone(
                            unitname, 'some_subscribe_payload', False,
                            'received NOTHING, want (some): ' + topic + ' = ' +
                            str(check['some_payload']))
                    elif 'not' in check and check['not']:
                        _assertsRunningDone(unitname, 'not', True, None)
                    _delete = True
            elif 'notification' in check:
                if 'maxtime' in check and now > check['maxtime']:
                    if not unitname:
                        logging.error(
                            "waitPublish of " + topic + ": " +
                            str(check['payload']) +
                            " exceed timeout, the process will continue but problems will probably occours"
                        )
                    else:
                        _assertsRunningDone(
                            unitname, 'notification', False,
                            'received NOTHING, want: ' +
                            str(check['notification']))
                    _delete = True
            elif 'export' in check:
                if 'maxtime' in check and now > check['maxtime']:
                    if 'not' in check:
                        _assertsRunningDone(unitname,
                                            check['export'] + '.exports_not',
                                            True, None)
                    else:
                        _assertsRunningDone(
                            unitname, check['export'] + '.exports', False,
                            'exports NOT MATCHES, received: ' +
                            check['export'] + ' = ' +
                            (str(system.exports[check['export']]) if
                             check['export'] in system.exports else 'NULL') +
                            ', want: ' + check['export'] + ' = ' +
                            str(check['value']))
                    _delete = True
                elif check['export'] in system.exports and _data_match(
                        system.exports[check['export']], check['value']):
                    _assertsRunningDone(unitname, 'exports', True)
                    _delete = True
            elif 'child' in check:
                if 'maxtime' in check and now > check['maxtime']:
                    if 'not' in check:
                        _assertsRunningDone(unitname,
                                            check['child'] + '.child_not',
                                            True, None)
                    else:
                        _assertsRunningDone(
                            unitname, check['child'] + '.child', False,
                            'NOT received child asserts from: ' +
                            check['child'])
                    _delete = True

            if _delete:
                delete.append(check)
                if unitname and len(assertsRunning[unitname]['data']
                                    ) >= assertsRunning[unitname]['count']:
                    assertDone(unitname)

        for check in delete:
            checks.remove(check)

        #for unitname in assertsRunning:
        #  if 'maxtime' in assertsRunning[unitname] and now > assertsRunning[unitname]['maxtime']:
        #    assertDone(unitname, timeout = True)

        system.sleep(.5)
Esempio n. 8
0
def waitRunning():
    global assertsRunning
    while len(assertsRunning) > 0:
        system.sleep(.5)
Esempio n. 9
0
def test_run(entries):
  #print("DIST: " + str(entries['presence@TEST'].module.locations_distance((45.123,12.123), (45.127,12.123))))
  
  # Mary enters via device
  t1 = system.time()
  test.assertx('t1', 
    assertSubscribeSomePayload = {
      'home/presence/in': {'name': 'mary', 'before_someone_inside': False, 'time': ('d', t1)},
      'home/presence': {'occupants': {'mary': {"firstseen": ('d', t1), "lastseen": ('d', t1), "methods": ["connected/device1@TEST"]}}, "time": ('d', t1)}, },
    assertNotification = ['info', 'mary has entered', 'home/presence/in' ],
      # ['debug', ('re', 'People detected:.*mary.*'), 'home/presence' ],
    assertEventsTopic = 'home/presence/in', assertEvents = {
      'presence-in': {'who': 'mary', 'before_someone_inside': False, 'region': 'home' }, 
      'clock': {'value': ('d', t1)}},
    assertExports = {'presence_someone_inside': True, 'presence_no_one_inside': False},
    wait = False)
  entries['net_sniffer_scapy@TEST'].module.sniff_callback(entries['net_sniffer_scapy@TEST'], '01:02:03:04:05')
  test.waitRunning()

  # ... via location region
  t2 = system.time()
  test.assertPublish('t2a', 'owntracks/device/xxx', '{"_type":"location","acc":78,"alt":0,"batt":86,"conn":"w","inregions":["Region"],"lat":35.123,"lon":22.123,"tid":"ic","tst":1546871086,"vac":0,"vel":0}', 
    assertEvents = {
      'location': {'latitude': 35.123, 'longitude': 22.123, 'altitude': 0, 'radius': 78, 'radius:unit': 'm', 'regions': ['Region'], 'source': 'owntracks'},
      'clock': {'value': 1546871086}
    },
    assertSubscribeNotReceive = ['home/presence/in'])

  test.assertx('t2b', 
    assertSubscribeSomePayload = {
      'home/presence': {'occupants': {'mary': {"firstseen": ('d', t1), "lastseen": ('d', t2), "methods": ["connected/device1@TEST", "location_region/owntracks"]}}, "time": ('d', system.time())}, },
    assertNotification = [ 'debug', ('re', 'People detected:.*mary.*'), 'home/presence' ],
    assertSubscribeNotReceive = ['home/presence/in'],
    wait = False)
  entries['presence@TEST'].module.publish(entries['presence@TEST'], 'home/presence', entries['presence@TEST'].definition['publish']['home/presence'])
  test.waitRunning()
  
  # ... via location position
  t3 = system.time()
  test.assertPublish('t3a', 'owntracks/device/xxx', '{"_type":"location","acc":78,"alt":0,"batt":86,"conn":"w","inregions":["Out"],"lat":45.127,"lon":12.123,"tid":"ic","tst":1546871086,"vac":0,"vel":0}', 
    assertSubscribeNotReceive = ['home/presence/in'])

  test.assertx('t3b', 
    assertSubscribeSomePayload = {
      'home/presence': {'occupants': {'mary': {"firstseen": ('d', t1), "lastseen": ('d', t3), "methods": ["connected/device1@TEST", "location_region/owntracks", "location/owntracks"]}}, "time": ('d', system.time())}, },
    assertNotification = [ 'debug', ('re', 'People detected:.*mary.*'), 'home/presence' ],
    assertSubscribeNotReceive = ['home/presence/in'],
    wait = False)
  entries['presence@TEST'].module.publish(entries['presence@TEST'], 'home/presence', entries['presence@TEST'].definition['publish']['home/presence'])
  test.waitRunning()
  
  # Also John enters via device
  t4 = system.time()
  test.assertx('t4', 
    assertSubscribeSomePayload = {
      'home/presence/in': {'name': 'john', 'before_someone_inside': True, 'time': ('d', t4)},
      'home/presence': {'occupants': {'mary': {"firstseen": ('d', t1), "lastseen": ('d', t3), "methods": ["connected/device1@TEST", "location_region/owntracks", "location/owntracks"]}, "john": {"firstseen": ('d', t4), "lastseen": ('d', t4), "methods": ["connected/device2@TEST"]}}, "time": ('d', t4)}, },
    assertNotification = [ 'info', 'john has entered', 'home/presence/in' ],
      # 'debug', ('re', 'People detected:.*mary.*john.*'), 'home/presence'
    assertEventsTopic = 'home/presence/in', assertEvents = {
      'presence-in': {'who': 'john', 'before_someone_inside': True, 'region': 'home' }, 
      'clock': {'value': ('d', t4)}},
    assertExports = {'presence_someone_inside': True, 'presence_no_one_inside': False},
    wait = False)
  entries['net_sniffer_scapy@TEST'].module.sniff_callback(entries['net_sniffer_scapy@TEST'], '0A:0B:0C:0D:0E')
  test.waitRunning()
  
  # Mary should exit if we wait connection_time/presence_location_session_duration
  system.time_offset(40)
  t5 = system.time()
  test.assertx('t5', 
    assertSubscribeSomePayload = {
      'home/presence/out': {'name': 'mary', 'after_someone_inside': True, 'time': ('d', t5)},
      'home/presence': {'occupants': {"john": {"firstseen": ('d', t4), "lastseen": ('d', t4), "methods": ["connected/device2@TEST"]}}, "time": ('d', t5)}, },
    assertNotification = [ 'info', 'mary has gone away', 'home/presence/out' ],
      # 'debug', ('re', 'People detected:.*john.*'), 'home/presence'
    assertEventsTopic = 'home/presence/out', assertEvents = {
      'presence-out': {'who': 'mary', 'after_someone_inside': True, 'region': 'home' }, 
      'clock': {'value': ('d', t5)}},
    wait = False)
  entries['net_sniffer_scapy@TEST'].module.sniff_callback(entries['net_sniffer_scapy@TEST'], '0A:0B:0C:0D:0E') # John should stay there
  entries['net_sniffer_scapy@TEST'].module.status_check(entries['net_sniffer_scapy@TEST'])
  system.sleep(1) # To ensure the execution of events callbacks
  entries['presence@TEST'].module.publish(entries['presence@TEST'], 'home/presence', entries['presence@TEST'].definition['publish']['home/presence'])
  test.waitRunning()

  # And now its time for John to exit too
  system.time_offset(40)
  t6 = system.time()
  test.assertx('t6', 
    assertSubscribeSomePayload = {
      'home/presence/out': {'name': 'john', 'after_someone_inside': False, 'time': ('d', t6)},
      'home/presence': {'occupants': {}, "time": ('d', t6)}, },
    assertNotification = [ 'info', 'john has gone away', 'home/presence/out' ],
      # 'debug', 'No people detected', 'home/presence'
    assertEventsTopic = 'home/presence/out', assertEvents = {
      'presence-out': {'who': 'john', 'after_someone_inside': False, 'region': 'home' }, 
      'clock': {'value': ('d', t6)}},
    assertExports = {'presence_someone_inside': False, 'presence_no_one_inside': True},
    wait = False)
  entries['net_sniffer_scapy@TEST'].module.status_check(entries['net_sniffer_scapy@TEST'])
  system.sleep(1) # To ensure the execution of events callbacks
  entries['presence@TEST'].module.publish(entries['presence@TEST'], 'home/presence', entries['presence@TEST'].definition['publish']['home/presence'])
  test.waitRunning()