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()
Exemple #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)
Exemple #3
0
def main():
    cleanup = True

    tbapi = TbApi(motherShipUrl, username, password)

    # Get a definition of our template dashboard
    template_dash = tbapi.get_dashboard_by_name(dashboard_template_name)
    dash_def = tbapi.get_dashboard_definition(tbapi.get_id(template_dash))

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

    if cust_lat is None or cust_lon is None:
        print("Must have valid lat/lon in order to add device!")
        exit(1)

    # Create new customer and device records on the server
    customer = tbapi.add_customer(cust_name, cust_address, cust_address2,
                                  cust_city, cust_state, cust_zip,
                                  cust_country, cust_email, cust_phone)

    server_attributes = {"latitude": cust_lat, "longitude": cust_lon}

    shared_attributes = {"LED": "Unknown", "nonce": 0}
    device = tbapi.add_device(make_device_name(cust_name), sensor_type,
                              shared_attributes, server_attributes)
    device_id = tbapi.get_id(device)

    # We need to store the device token as a server attribute so our REST services can get access to it
    device_token = tbapi.get_device_token(device_id)

    server_attributes = {"device_token": device_token}

    tbapi.set_server_attributes(device_id, server_attributes)

    # Upate the dash def. to point at the device we just created (modifies dash_def)
    update_dash_def(dash_def, cust_name, device_id)

    # Create a new dash with our definition, and assign it to the new customer
    dash = tbapi.create_dashboard_for_customer(cust_name + ' Dash', dash_def)
    tbapi.assign_dash_to_user(tbapi.get_id(dash), tbapi.get_id(customer))

    if cleanup:
        # input("Press Enter to continue...")   # Don't run from Sublime with this line enabled!!!

        print("Cleaning up!")
        tbapi.delete_dashboard(tbapi.get_id(dash))
        tbapi.delete_device(device_id)
        tbapi.delete_customer_by_id(tbapi.get_id(customer))
Exemple #4
0
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"
}