Пример #1
0
def ensure_devices_exist(device_nums):
    """
    Ensure all devices we're going to process exist on both the source and destination machines
    """
    tbapi_old = TbApi(birdhouse_utils.make_mothership_url(old_server_ip),
                      thingsboard_username, thingsboard_password)
    tbapi_new = TbApi(birdhouse_utils.make_mothership_url(new_server_ip),
                      thingsboard_username, thingsboard_password)

    print(
        "Making sure every device we want to migrate exists on both source and desitnation machines"
    )

    for num in device_nums:
        print(".", end="")

        name = birdhouse_utils.make_device_name(num)

        old_device = tbapi_old.get_device_by_name(name)
        if old_device is None:
            print(f"\nDevice {name} does not exist on old server!")
            exit()

        new_device = tbapi_new.get_device_by_name(name)
        if new_device is None:
            print(f"\nDevice {name} does not exist on new server!")
            exit()

        old_key = tbapi_old.get_id(old_device)
        new_key = tbapi_new.get_id(new_device)

        if old_key != new_key:
            print("\nDevice keys are different on old and new servers!")
            exit()
    print()
Пример #2
0
def main():

    mothership_url = birdhouse_utils.make_mothership_url(args, config)
    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    print(f"Retrieving template dashboard {template_dash}... ",
          end='',
          flush=True)
    template_dash_def = tbapi.get_dashboard_definition(
        tbapi.get_dashboard_by_name(template_dash))

    # We also need the id of the device being swapped out
    template_device_id = tbapi.get_id(
        tbapi.get_device_by_name(
            birdhouse_utils.make_device_name(args["<device>"])))

    print(" done.")

    all_devices = tbapi.get_devices_by_name(copy_to_pattern)

    for device in all_devices:
        num = birdhouse_utils.get_device_number_from_name(device["name"])

        print(f"Updating dashboard for {device['name']}")
        dash_name_being_replaced = birdhouse_utils.make_dash_name(num)
        device_name = birdhouse_utils.make_device_name(num)
        device = tbapi.get_device_by_name(device_name)
        device_id = tbapi.get_id(device)

        # The dash we are replacing:
        dash_being_replaced = tbapi.get_dashboard_by_name(
            dash_name_being_replaced)
        dash_id_being_replaced = tbapi.get_id(dash_being_replaced)

        dash_def = tbapi.get_dashboard_definition(
            tbapi.get_dashboard_by_name(
                template_dash))  # dash_def will be modified
        birdhouse_utils.reassign_dash_to_new_device(dash_def,
                                                    dash_name_being_replaced,
                                                    template_device_id,
                                                    device_id, device_name)

        dash_def["id"]["id"] = dash_id_being_replaced

        # del_humidity(dash_def)
        # exit()

        tbapi.save_dashboard(dash_def)
Пример #3
0
def main():
    tbapi = TbApi(motherShipUrl, username, password)

    # Lookup missing fields, such as zip, lat, and lon
    birdhouse_utils.update_customer_data(cust_info)

    if cust_info["lat"] is None or cust_info["lon"] is None:
        print("Must have valid lat/lon to continue!")
        exit(1)

    name = birdhouse_utils.make_device_name(birdhouse_number)

    cust = tbapi.get_customer(name)
    devices = tbapi.get_customer_devices(cust)

    print(devices)

    device = tbapi.get_device_by_name(name)

    customer = tbapi.update_customer(cust, None, cust_info["address"],
                                     cust_info["address2"], cust_info["city"],
                                     cust_info["state"], cust_info["zip"],
                                     cust_info["country"])
    server_attributes = {
        "latitude": cust_info["lat"],
        "longitude": cust_info["lon"],
        "address": birdhouse_utils.one_line_address(cust_info)
    }
    tbapi.set_server_attributes(device, server_attributes)

    exit()

    cust = tbapi.get_customer(name)
Пример #4
0
def make_params(nums):
    mothership_url = birdhouse_utils.make_mothership_url(args)

    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    params = []

    for num in nums:
        print(f"Retrieving details for device {num}... ", end='', flush=True)
        device_name = birdhouse_utils.make_device_name(num)
        dash_name = birdhouse_utils.make_dash_name(num)

        device = tbapi.get_device_by_name(device_name)
        dash = tbapi.get_dashboard_by_name(dash_name)
        dash_url = tbapi.get_public_dash_url(dash)
        tiny_url = make_tiny_url(dash_url)

        if device is None:
            print(f"Failed.\nCould not find device {num}... Aborting.")
            exit()

        token = tbapi.get_device_token(device)

        params.append((
            birdhouse_utils.get_sensor_type(num)[1],
            birdhouse_utils.make_device_number(num),
            token,
            tiny_url
        ))
        print("done.")

    return params
Пример #5
0
def main():

    mothership_url = birdhouse_utils.make_mothership_url(args, config)
    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    for num in args["<num>"]:
        print(f"Retrieving details for device {num}... ", end='', flush=True)
        name = birdhouse_utils.make_device_name(num)
        device = tbapi.get_device_by_name(name)

        if device is None:
            print(f"Failed.\nCould not find device {num}... Aborting.")
            exit()

        token = tbapi.get_device_token(device)
        print("done.")
        print(token)
Пример #6
0
import logging
import deq_tools                                                    # pip install deq_tools

from thingsboard_api_tools import TbApi                             # pip install git+git://github.com/eykamp/thingsboard_api_tools.git --upgrade
from config import motherShipUrl, username, password, deq_logfile   # You'll need to create this... Be sure to gitignore it!

tbapi = TbApi(motherShipUrl, username, password)

device_name = 'DEQ (SEL)'
deq_tz_name = 'US/Pacific'

logging.basicConfig(filename=deq_logfile, format='%(asctime)s %(message)s', level=logging.INFO)    # WARN, INFO, DEBUG


# Data is stored as if it were coming from one of our devices
device = tbapi.get_device_by_name(device_name)
device_token = tbapi.get_device_token(tbapi.get_id(device))


# This is the earliest timestamp we're interested in.  The first time we run this script, all data since this date will be imported. 
# Making the date unnecessarily early will make this script run very slowly.
earliest_ts = "2018/04/28T00:00"        # DEQ uses ISO datetime format: YYYY/MM/DDTHH:MM

station_id = 2              # 2 => SE Lafayette, 7 => Sauvie Island, 51 => Gresham Learning Center.  See bottom of this file more more stations.


# Mapping of DEQ keys to the ones we'll use for the same data
key_mapping = {
    "PM2.5 Est": "pm25",
    "Ambient Temperature": "temperature",
    "Barometric Pressure": "pressure"