def device_client_start(type, id): """ Connects with MQTT broker """ """ vne::tbd ep_config = endpoint.get_broker_details() srv_ip = ep_config[0] srv_port = ep_config[1] srv_keepalive = ep_config[2] """ global mqtt_profile mqtt_profile.update(config.get_mqtt_config()) srv_ip = mqtt_profile['broker_ip'] srv_port = mqtt_profile['broker_port'] srv_keepalive = mqtt_profile['broker_keepalive'] print 'connecting to broker:', srv_ip, ':', srv_port, ' ', srv_keepalive mqtt_client = mqtt.Client(config.get_platform_node_id(), mqtt_profile['clean_session'], mqtt_profile['userdata']) mqtt_client.on_connect = on_connect mqtt_client.on_message = on_message mqtt_client.on_publish = on_publish mqtt_client.on_disconnect = on_disconnect if mqtt_profile['username'] == 'default': print 'Please profile non-default client id to connect to' return False if mqtt_profile['transport'] == 'tls': server_cert = common.get_platform_delim().join([ '..', common.get_device_working_dir(), mqtt_profile['cert_directory'], mqtt_profile['ca_cert'] ]) client_cert = common.get_platform_delim().join([ '..', common.get_device_working_dir(), mqtt_profile['cert_directory'], mqtt_profile['client_cert'] ]) client_key = common.get_platform_delim().join([ '..', common.get_device_working_dir(), mqtt_profile['cert_directory'], mqtt_profile['client_key'] ]) mqtt_client.tls_set(server_cert, client_cert, client_key) mqtt_client.username_pw_set(mqtt_profile['username'], mqtt_profile['password']) mqtt_client.connect(srv_ip, srv_port, srv_keepalive) mqtt_profile['client'] = mqtt_client #vne::tbd:: failure handling or how to make it blocking with time limit print 'connection to broker successful' mqtt_client.loop_forever()
def _read_template(board_type, board, device_type, simulation = False): """ reads temperature template config file board_type: endpoint, gateway, cloud board: udo, rasppi3 device_type: temperature, lighting etc. """ global templates_dir fname = '.'.join([device_type, 'json']) fpath_vars = [] if simulation is True: fpath_vars = [templates_dir, 'common', 'simulator', 'device', fname] else: fpath_vars = [templates_dir, board_type, board, 'device', device_type, fname] fpath = common.get_platform_delim().join(fpath_vars) print 'reading template file: ', fpath if not os.path.exists(fpath): print 'Config file does not exist: ', fpath sys.exit('Config file does not exist') json_config = open(fpath).read() #tbd:: exception handling device_data = json.loads(json_config) return device_data
def initialize(): """ Entry point function - must be called first """ read_system_profile() global templates_dir templates_dir = common.get_platform_delim().join( ['..', 'config', 'templates']) global template_temp template_temp = _read_endpoint_template() """
def read_device_config(type, id): """ read device specific json config file """ #vne::tbd:: change to abs path #dfile = 'device_' + id + common.config_ext dfile = ''.join(['device_', id, common.config_ext]) device_file = common.get_platform_delim().join([common.device_config_dir, type, dfile]) json_config = open(device_file).read() #tbd:: exception handling device_data = json.loads(json_config) return device_data
def read_system_profile(): """ Reads the top level system template file """ fname = '.'.join(['system', 'json']) fpath = common.get_platform_delim().join(['..','config','templates', 'common', fname]) print fpath if not os.path.exists(fpath): print 'Config file does not exist: ', fpath sys.exit('Config file does not exist') json_config = open(fpath).read() #tbd:: exception handling global system_profile system_profile = json.loads(json_config)
def device_add(device_data): """ adds a new device to this endpoint device_data: dictionary of device parameters received Read template json for that device Add other current parameters Dump json data into json file for that device Add device to existing list """ retval = 0 device_id = device_data['id'] device_type = device_data['type'] if 'mode' in device_data and device_data['mode'] == 'simulation': device_data['sim_mode'] = True else: device_data['sim_mode'] = False device_template = templates.get_temperature_template( device_data['sim_mode']) device_template.update(device_data) # Set global device Id device_template['gid'] = config.get_device_global_id( device_type, device_id) device_template['data_topic'] = config.get_publish_id( device_type, 'data', device_id) #device_type['status'] = 'registered' global devices_dict devices_dict[device_type][device_id] = device_template set_device_status(device_type, device_id, device_status[1]) fname = 'device_' + device_id + common.config_ext device_file = common.get_platform_delim().join( [common.get_device_config_dir(), device_type, fname]) device_template['device_file'] = device_file common.create_json_file(device_template, device_file) publish_thread = Thread(target=device_publish_thread, args=(device_type, device_id)) publish_thread.start() print 'device added successfully', device_type, device_id return retval
def device_add(device_data): """ adds a new device to this endpoint device_data: dictionary of device parameters received Read template json for that device Add other current parameters Dump json data into json file for that device Add device to existing list """ retval = 0 #vne::tbd device_template = {} device_template = templates.get_temperature_template() device_template.update(device_data) device_id = device_data['id'] fname = 'device_' + device_id + common.config_ext device_type = device_data['type'] #device_type['status'] = 'registered' global devices_dict devices_dict[device_type][device_id] = device_template set_device_status(device_type, device_id, device_status[1]) device_file = common.get_platform_delim().join( [common.get_device_config_dir(), device_type, fname]) device_template['device_file'] = device_file common.create_json_file(device_template, device_file) publish_thread = Thread(target=device_publish_thread, args=( device_type, device_id, )) publish_thread.start() print 'device added successfully', device_type, device_id return retval