Exemplo n.º 1
0
        for device in DevicePool.devices().values():
            try:
                response = http.get('http://{}/api/air/kick'.format(device.ip))
                if response.status == 204:
                    time.sleep(5)
                response = http.get('http://{}/api/air/state'.format(device.ip))
                if response.status == 200:
                    air_state = json.loads(response.body)
                    for sensor in air_state['air']['sensors']:
                        Client.event(
                            device.id,
                            'reading_{}'.format(sensor['type']),
                            json.dumps({
                                'value': sensor['value'],
                                'unit': 'µg/m³',
                                'time': '{}Z'.format(datetime.datetime.utcnow().isoformat())
                            }),
                            block=False
                        )
            except Exception as ex:
                logger.error(ex)
        time.sleep(20)

readings_scraper = Thread(target=pushReadings, name="Scraper")


if __name__ == '__main__':
    device_monitor = Monitor()
    connector_client = Client(device_manager=DevicePool)
    readings_scraper.start()
if 0 in tests:
    logger.info('------ populate device manager ------')
    device_manager.add(
        Device(id_1, 'iot#d66ec9bc-e37f-4f35-a788-027301aad6c2',
               'Dummy Device 1'))
    device_2 = Device(id_2, 'iot#d66ec9bc-e37f-4f35-a788-027301aad6c2',
                      'Dummy Device 2')
    device_2.addTag('type', 'Dummy')
    device_manager.add(device_2)
    device_manager.add(
        Device(id_3, 'iot#d66ec9bc-e37f-4f35-a788-027301aad6c2',
               'Dummy Device 3'))

if __name__ == '__main__':
    connector_client = Client(device_manager)

    logger.info('###### runtime phase ######')

    if 1 in tests:
        time.sleep(0.5)
        logger.info('------ add tag to existing device ------')
        device = device_manager.get(id_1)
        device.addTag('type', 'Dummy')
        Client.update(device)

    if 2 in tests:
        time.sleep(0.5)
        logger.info('------ change tag on existing device ------')
        device = device_manager.get(id_1)
        device.changeTag('type', 'dummy device')
Exemplo n.º 3
0
    logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(fn)
from coap_server import server
from pinger import pinger
from coap_executer import executer
#from discovery import mdns_discovery
#from discovery import coap_discovery

if __name__ == '__main__':
    # initiation phase nicht notwenidg, da alles was connected ist, schon mit connected status in db sthet
    # hochstens bei start das nochmal uberprufen, aber passiet eh weiter unten
    # devices method liefert eh nur connected devices
    # nochmal chekcen vorher ?! synchron aber damit erst danach client connecion aufbaut

    # Start Web Socket connection and synchronize connected devices
    connector_client = Client(DB.DB())

    # CoAP server listens on multicast and gets registration requests from devices
    # CoAP Server with registration und discovery endpoints
    coap_server = server.CoAPMulticastServer("0.0.0.0", 5683)
    coap_server.start()

    # CoAP Client for pining devices to update device manager
    coap_pinger = pinger.Pinger()
    coap_pinger.start()

    # Executer - Receive command and respond
    coap_executer = executer.Executer()
    coap_executer.start()

    # Observer - Gets data from connected devices and pushes them to the platform