blaaahblah.py
| 1 |
import time |
|---|---|
| 2 |
import ttn |
| 3 |
import sys |
| 4 |
import logging |
| 5 |
|
| 6 |
logging.basicConfig(level=logging.DEBUG) |
| 7 |
|
| 8 |
config = {
|
| 9 |
'apps': {
|
| 10 |
'ami_rama': "ttn-account-v2.6UEpQT1kb58BCouvltr5HLOP7CGOwZsLTWYpTo2nK3I", |
| 11 |
'baraka': "ttn-account-v2.eo-VseYHokva5d5h7J0b9b1fMQqWPU1dzOeBGXdqwrw" |
| 12 |
}, |
| 13 |
'timeout': 10 |
| 14 |
} |
| 15 |
|
| 16 |
def uplink_callback(msg, client): |
| 17 |
logging.debug("Values Received from appId: {}, devId: {}, wLevel: {}, wPressure: {}, temp: {}".format(msg.dev_id, msg.app_id, msg.payload_fields.wLevel, msg.payload_fields.wPressure, msg.payload_fields.tempC))
|
| 18 |
|
| 19 |
handlers = {}
|
| 20 |
mqtt_clients = {}
|
| 21 |
|
| 22 |
for app_id, access_key in config['apps'].items(): |
| 23 |
try:
|
| 24 |
handlers[app_id] = ttn.HandlerClient(app_id, access_key) |
| 25 |
except Exception as err: |
| 26 |
logging.debug(str(err))
|
| 27 |
logging.warning(err._state.details) |
| 28 |
else:
|
| 29 |
# using mqtt client
|
| 30 |
logging.info('Connected to {}'.format(app_id))
|
| 31 |
mqtt_clients[app_id] = handlers[app_id].data() |
| 32 |
mqtt_clients[app_id].set_uplink_callback(uplink_callback) |
| 33 |
mqtt_clients[app_id].connect() |
| 34 |
|
| 35 |
if len(handlers) == 0: |
| 36 |
sys.exit(0)
|
| 37 |
else:
|
| 38 |
time.sleep(600)
|
| 39 |
|
| 40 |
for name, mqtt_client in mqtt_clients.items(): |
| 41 |
logging.info('Closing connection to {}'.format(name))
|
| 42 |
mqtt_client.close() |
| 43 |
|
| 44 |
# using application manager client
|
| 45 |
# app_client = handler.application()
|
| 46 |
# my_app = app_client.get()
|
| 47 |
# print(my_app)
|
| 48 |
# my_devices = app_client.devices()
|
| 49 |
# print(my_devices)
|
| 50 |
|