Is it possible to restrict plugin to one thing instance?

Hi!

I’m just trying to get familiar with the nymea ecosystem by writing a plugin for LEDs connected to the GPIO pins of a Raspberry Pi. Due to hardware constraints, there can only be one such thing per Raspberry Pi hardware.

Is there a way, how to reflect this in the plugin setup? Meaning, is there a way, how to prevent that the user adds a second thing of the same kind - which could then not be configured or would interfere with the first instance?

Thank you,
HD

Hi @hd512,

Welcome to nymea!

So, right now your best option would be to return a failure in setupThing if you detect there is already such a thing present.

info->finish(Thing::ThingErrorHardwareUnavailable, QT_TR_NOOP("There can only be one blabla"));

Anyhow, would you mind elaborating why there can only be one? Shouldn’t it be possible to add multiple of them as long as there are free GPIOs available? (I don’t know much about this hardware so I might be wrong).

Thank you for this hint!

I realize that I wasn’t very specific regarding the hardware. The LEDs would be connected via SPI, where there is only one pin that is guaranteed to be available on the different Raspberry Pi versions. Some might have a second one, but firstly I’m not that far into it yet and secondly, I don’t have the capabilities to test this at the moment. Therefore, I would like to restrict the initial version to just one instance.

Ok. Sounds reasonable.

I’m not exactly experienced with SPI at this point. But perhaps there is the possibility to discover connected devices on the SPI bus? If so, you can also set your ThingClass’ createMethod to “discovery” only and a second discovery would just not return any results at all.

Same here, not yet advanced enough. If I find something like that, I would of course go for the discovery mode.

The SPI bus doesn’t allow discovery. The only thing you might discover are the number of available CS pins. I think the RPi has only 2 of them.

In the setup you can search for existing things with “myThings()”:

    if (!myThings().filterByThingClassId(insertYourThingClassId).isEmpty()) {
        info->finish(Thing::ThingErrorHardwareUnavailable, QT_TR_NOOP("There can only be one blabla"));
    }

Thank you!

Just a minor correction for future readers of this discussion: The proper error code is

Thing::ThingErrorHardwareNotAvailable

1 Like