import modbus
import json
import sqlite3

def discover_modbus_slaves(host,port):
	mydb=sqlite3.connect("../assets.db",check_same_thread=False)
	query=mydb.cursor()

	modbus_data=modbus.get_info(host,port,True)
	slave_data=json.loads(modbus_data) if modbus_data!=False else False

	if(slave_data):
		for slave_key in slave_data:
			insinventory='INSERT INTO modbus_devices\
					(sid,sid_data,device_info,ip_address)\
					VALUES\
					("'+slave_key+'","'+slave_data[slave_key]["Slave ID data"]+'","'+slave_data[slave_key]["Device identification"]+'","'+host+'")'
			query.execute(insinventory)
	query.close()
	mydb.commit()