#
# Methods
#
def printDeviceDesc(dev):
	devId = dev.device_id
	devInfo = dev.device_info
	print("- Type: {} ID: {} Version: {} Name: {} Vendor: {}".format(devId.type, devId.id, devId.version, devInfo.name, devInfo.vendor))

	catIds = devInfo.categories
	catStrs = []
	for catId in catIds:
		cat = device_repository.get_device_category(catId)
		if cat is not None:
			catStrs.append(cat.name)
		else:
			catStrs.append("Id={}".format(catId))
	categories = ",".join(catStrs)

	familyIds = devInfo.families
	famStrs = []
	for familyId in familyIds:
		fam = device_repository.get_device_family(familyId)
		if fam is not None:
			famStrs.append(fam.name)
		else:
			famStrs.append("Id={}".format(familyId))
	families = ",".join(famStrs)

	print("     Categories: {}   Families: {}".format(categories, families))


def printVendorDesc(vendor):
	vendorInfo = vendor.vendor_info
	print("- ID: {} Name: {}".format(vendor.vendor_id, vendorInfo.name))


def printDeviceCategory(cat):
	print("- ID: {} Name: {} Description: {} Parent: {}".format(cat.category_id, cat.name, cat.description, cat.parent_category))


def printDeviceFamily(fam):
	print("- ID: {} Name: {} Description: {} Parent: {}".format(fam.family_id, fam.name, fam.description, fam.parent_family))


#
# Main program
#
proj = projects.primary

found = proj.find('Ethernet', True)
assert(found, 'No Modbus slave found')

dev = found[0]

allDevDescs = device_repository.get_all_devices()
for devDesc in allDevDescs:
	if devDesc.device_id.type == 81:
		printDeviceDesc(devDesc)

devId = device_repository.create_device_identification(81, "0x002A_0x0305_DIM 1", "SW=R01.11.00, HW=A1.0")

if devId:
	pn_control = dev.find("PN_Controller")[0]
	pn_control.add("Profinet_Slave1", devId)
	print("profinet slave added")

