Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Need example of using IScriptDeviceRepository Interface

2016-06-18
2017-02-07
  • DavidCozens - 2016-06-18

    I want to use a python script to add and remove device descriptors. It appears that the IScriptDeviceRepository Interface added in 3.5.9.0 gives me the methods I require, but I've been unable to work out how to get an object that implements the interface.

     
  • DavidCozens - 2016-06-19

    I've got a bit further now, I found a global named . What I want to do is to ensure that I remove old versions of a set of device descriptors and add in new ones. I've managed to find the devices I want to remove, however either removing or importing devices is giving problems.

    devices = device_repository.get_all_devices("MyUniqueText")
    for device in devices:
        print("id: ", device.device_id, " info= ",device.device_info.name)
        device_repository.remove_device(device.device_id, None, True)
    for file in glob.glob("artefacts/*.devdesc.xml"):
        device_repository.import_device(file, None, True)
        print("Installed "+ file)
    

    Running this gives the error below for the line with remove_device, if I comment that line I get the same for import_device.

    System.ArgumentNullException: Traceback (most recent call last):
      File "C:\Users\David\Documents\scripts\CODESYS\CheckoutProject.py", line 50, in <module>
    TypeError: Value cannot be null.
    Parameter name: source
    

    However the documentation for the source parameter in import_device and remove_device both say

    Zitat:
    Remove the device from this repository source. If null the device is removed from the default repository source.

    I've spent quite a while digging through the API's and I haven't managed to work out another value that I could use for the parameter.
    Any help would be much appreciated.

     
  • mkeller - 2016-06-20

    Hi DavidCozens.

    DavidCozens hat geschrieben:
    However the documentation for the source parameter in import_device and remove_device both say
    I've spent quite a while digging through the API's and I haven't managed to work out another value that I could use for the parameter.
    Any help would be much appreciated.

    It is a bug. I added the Jira CDS-49921 to our bug tracking, so it will be fixed eventually.

    At the moment you need to specify a repository source to import or remove a device/vendor description.

    BR
    Martin

     
  • DavidCozens - 2016-06-20

    M.Keller hat geschrieben:
    Hi DavidCozens.
    It is a bug. I added the Jira CDS-49921 to our bug tracking, so it will be fixed eventually.
    At the moment you need to specify a repository source to import or remove a device/vendor description.
    BR
    Martin

    Hi Martin,

    Thank you. I have however been unable to find an API call to give me access to any repository source. How do I find the default repository source?

    regards David

     
  • mkeller - 2016-06-20

    Hi DavidCozens.

    You get the list of repository sources from the property (see IScriptRepositorySourceList) of the device repository.

    sources = device_repository.sources
    mySource = sources["My Device Repository"]
    

    BR
    Martin

     
  • DavidCozens - 2016-06-20

    M.Keller hat geschrieben:
    Hi DavidCozens.
    You get the list of repository sources from the property (see IScriptRepositorySourceList) of the device repository.

    sources = device_repository.sources
    mySource = sources["My Device Repository"]
    

    BR
    Martin

    Hi Martin,

    Thank you - I have that part of my script working now.

    regards David

     
  • jmohre - 2017-02-06

    Hi,
    sorry for capturing this topic, but my question might fit to that.

    What's the best way to remove all versions of all devices from the repository, which belong
    to a certain vendor?

    I did not really find an interface that lists all installed devices of a specific vendor.

    Regards

     
  • mkeller - 2017-02-06

    Hi jmohre.

    jmohre hat geschrieben:
    What's the best way to remove all versions of all devices from the repository, which belong
    to a certain vendor?

    The following example code iterates through the device sources, gets a list of all devices for a specific vendor and removes them from the device repository:

    try:
       for src in device_repository.sources:
          devs = device_repository.get_all_devices(src)
          devsVendor = devs.get_devices_of_vendor("Some company")
          try:
             for dev in devsVendor:
                device_repository.remove_device(dev.device_id, src, False)
          except:
             print("Failed to remove device description")
    finally:
       # Save the device cache because we used the remove_device without
       # saving the device cache because it is faster
       device_repository.save_device_cache()
    

    The call of is important because otherwise the devices still appear in the repository after a restart of CODESYS but the files of the device were deleted.

    BR
    Martin

     
  • jmohre - 2017-02-07

    Thanks Martin,

    I found "get_devices_of_vendor" this morning - it is a bit hidden .
    But I did not take care of the cache issue and the different repositories.

    Regards

    Jörg

     

Log in to post a comment.