macchina-io / macchina.io

macchina.io EDGE is a powerful C++ and JavaScript SDK for edge devices, multi-service IoT gateways and connected embedded systems.
https://macchina.io
GNU General Public License v3.0
512 stars 152 forks source link

Class remoting and getPropertyString in macchina.io #98

Closed zaleksa closed 2 years ago

zaleksa commented 4 years ago

Dear,

I need to MyClass have remoting, to be able for other bundles/javascript to get its methods. At the same time, I want to MyClass has properties such as 'device' and 'deviceType', so when objects of MyClass are created and registered in ServiceRegistry, it could be found by property (not just by findByName).

So, my idea was that MyClass should have attribute '//@ remote' and should inherit class DeviceImpl with 'Device' class as Super and 'MyClass' as Sub parameters:

// MyClass.h // //@ remote class MyClass: public IoT::Devices::DeviceImpl<IoT::Devices::Device, MyClass>

I use remote generator to get MyClassServerHelper for creating remote object and registering it to ServiceRegistry with 'device' and 'deviceType' properties.

Everything compiles without warning and errors and it works, but halfway:

I can reach MyClass methods from other bundles/javascript, but, since MyClass has property 'device', Sensors&Devices webUI is finding this property but it can't find getPropertyString method for this class. That is because I didn't use DeviceServerHelper class for creating remote object and registering to ServiceRegistry. If I do this, then Sensors&Devices webUI can find getPropertyString method, but I can not reach MyClass methods which are serialized.

Is there a way to get both things working?

p.s. I found workaround such as to copy getPropertyString method from DeviceImpl.h (and everything it depends on) and to use MyClassServerHelper for creating remote object and registering it to ServiceRegistry with 'device' and 'deviceType' properties. MyClass doesn't inherit any class. This way both things work as I expected, but it seems to me that it is not "clean" way to do that.

Best regards, Aleksandar

obiltschnig commented 4 years ago

The proper way would be to derive your class directly from IoT::Devices::Device and then implement the property/feature-related methods defined therein, possibly with the help of DeviceImpl. However, the Remoting code generator has a limitation with inheritance across different libraries/namespaces. The easiest workaround would be to add the interface for your device directly to the Devices library (and also generate the Remoting helper classes there), then implement the device in a different library, like it's done with the existing devices.

If you don't want to do that, "faking" the Device interface as you've done is also possible. However, you won't be able to properly use your class from C++ (via the Remoting framework).