def save_devices(tb_url, save_file, tenant_admin_user=None, password=None, token=None): """ Save devices that belong to this user's tenant """ if not token: print("Obtaining token...") token, refreshToken = tb.getToken(tb_url, tenant_admin_user, password)[0:2] print("Loading devices...") devices_list, resp = tb.get_tenant_devices(tb_url, token, limit=DEVICE_LIMIT) if devices_list: print(f"{len(devices_list)} devices loaded.") save_entities(save_file, devices_list) print(f"Devices successfully saved to {save_file}") else: print("FAILURE: devices were not loaded") print(f"Response status: {resp.status_code}") print(resp.json()) return devices_list
def check_devices(folder, tb_access_file=None): device_file = devices_path(folder) if path.exists(device_file): devices = json.load(open(device_file, 'r')) elif tb_access_file: tb_params = tb.load_access_parameters(tb_access_file) tb_con = tb.TbConnection(tb_params['url'], tb_params['user'], tb_params['password']) devices, resp = tb.get_tenant_devices(tb_con.url, tb_con.get_token(), get_credentials=True) json.dump(devices, open(device_file, 'w')) else: raise ValueError("File devices.json is not present and access to Thingsboard is not provided") return dict([(d['id']['id'], d) for d in devices]) # def create_device_tables(devices): '''devices is the list of structures:
from sys import argv import time import random import tb_rest as tb WAIT_SEC = 10 if __name__ == "__main__": tb_params = tb.load_access_parameters(argv[1]) tb_con = tb.TbConnection(tb_params['url'], tb_params['user'], tb_params['password']) print(tb_con.get_token()) #list devices devices, resp = tb.get_tenant_devices(tb_con.url, tb_con.get_token()) #generate data for d in devices: d['token'] = tb.get_device_credentials(tb_con.url, tb_con.get_token(), d['id']['id'])['credentialsId'] num = int(argv[2]) for i in range(num): for d in devices: if d['type'] == 'thermostat': json_data = { 'T': random.randint(0, 50), 'C': random.random() * 10 } elif d['type'] == 'test': json_data = { 'K': random.randint(0, 100), 'P': 5 - random.random() * 10 }
params['start_time'] = dt.datetime.strptime(params['start_time'], TIME_FORMAT) if params['end_time']: params['end_time'] = dt.datetime.strptime(params['end_time'], TIME_FORMAT) else: params['end_time'] = dt.datetime.now() return params if __name__ == "__main__": params = get_config_params(sys.argv[1]) if "password" not in params.keys(): params['password'] = input("Enter password: "******"*** Purge started ***") print( f"Total interval: {params['start_time'].strftime(TIME_FORMAT)}-{params['end_time'].strftime(TIME_FORMAT)}" ) delete_all_data(tb_connection, params['devices'], params['keys'], params['start_time'], params['end_time']) print("*** Purge finished ***")
}, "createdTime": 1580738590506, "additionalInfo": null, "tenantId": { "entityType": "TENANT", "id": "84966700-4687-11ea-bd76-a73c738b665f" }, "customerId": { "entityType": "CUSTOMER", "id": "d1cf6e60-3e57-11e9-b50c-f987795349fe" }, "name": "(Ф. 00) Smart Meter 96", "type": "Mercury", "label": null, "token": "DEVICE_TOKEN" } """ if __name__ == "__main__": tb_params = tb.load_access_parameters(argv[1]) tb_con = tb.TbConnection(tb_params['url'], tb_params['user'], tb_params['password']) devices, resp = tb.get_tenant_devices(tb_con.url, tb_con.get_token(), get_credentials=True) device_by_types = group_by_types(devices) assign_labels(device_by_types) devices = [d for group in device_by_types.values() for d in group] upload_tb(tb_con, devices)