#ThingsBoard data collection from tb_rest_client.rest_client_ce import * from tb_rest_client.rest import ApiException import logging logger = logging.getLogger('billing_reports') logger.setLevel(logging.INFO) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) def getDevices(rest_client, customers, page=0, pageSize=500): thingsboard_data = {} for c in customers.data: cname = c.name cid = c.id.id if cname not in ["Test Company", "Amerus Safety Solutions"]: #create new company if it doesn't exist if cname not in thingsboard_data: thingsboard_data[cname] = {} #get devices of a company devices = rest_client.get_customer_devices(customer_id=cid, page_size=pageSize, page=page) #go through each device and store its data in the dict for device in devices.data: cellular_cost = 20 #fix naming to work with JSON/dict if '"' in device.name: deviceName = device.name.replace('"', 'in') else: deviceName = device.name # Sort Device details if cname == "Chuda Resources": so = "Water Wells" price = 75 billing_type = "Stand-Alone" elif cname == "Henry Petroleum": so = "Check Meters" price = 50 billing_type = "Stand-Alone" elif cname == "Faskens": if device.type == "tankalarms": so = "Tanks" billing_type = "Stand-Alone" price = 50 elif device.type in ["advvfdipp", "plcfreshwater"]: so = "Water Wells" billing_type = "Stand-Alone" price = 50 elif device.type == "plcpond": so = "Ponds" billing_type = "Stand-Alone" price = 50 else: so = "HPSO-1" billing_type = "Stand-Alone" price = 50 elif cname == "Henry Resources": if deviceName == "Pearl Central": so = "Henry Resources" price = 300 billing_type = "Stand-Alone-WiFi" else: so = "Henry Resources" price = 275 billing_type = "Stand-Alone" elif cname == "ConocoPhillips": if device.type == "flowmeterskid": so = "Portable Meter" price = 50 billing_type = "Stand-Alone" elif device.type == "plcfreshwater": so = "Water Well" price = 50 cellular_cost = 0 billing_type = "Networked" elif device.type == "advvfdipp": so = "Santa Rosa" price = 50 billing_type = "Stand-Alone" elif device.type == "Gateway": so = "AP" price = 0 billing_type = "AP-bundled" elif device.type == "cpdualflowmeter": so = "Pond/Flowmeter" price = 50 billing_type = "Networked" elif cname == "Saulsbury Ventures": so = "Saulsbury Ventures" price = 50 billing_type = "Stand-Alone" else: so = "HPSO-1" price = 50 billing_type = "Stand-Alone" #make a new Sales Order if it doesn't exist if so not in thingsboard_data[cname]: thingsboard_data[cname][so] = {} #add device to Sales Order under Company thingsboard_data[cname][so][deviceName] = { "Sales Price": price, "Platform Cost": 0, "Platform": "ThingsBoard", # "thingsboard", "mistaway" "Cellular Cost": cellular_cost, "Billing Type": billing_type # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi" } return thingsboard_data def getThingsBoardData(url, username, password): # Creating the REST client object with context manager to get auto token refresh with RestClientCE(base_url=url) as rest_client: try: # Auth with credentials rest_client.login(username=username, password=password) # Get customers > get devices under a target customer > get keys for devices > get data for devices customers = rest_client.get_customers(page_size="100", page="0") thingsboard_data = getDevices(rest_client=rest_client, customers=customers) return thingsboard_data except ApiException as e: logger.error(e) return False