Azure / iot-plug-and-play-bridge

IoT Plug and Play bridge
Other
60 stars 28 forks source link

[Feature request]:callback function in adapter when a device is power up and gets configuration #51

Closed eddylawsurgere closed 1 year ago

eddylawsurgere commented 3 years ago

Is your feature request related to a problem? Please describe. I need a callback function in adapter when a device is power up and gets configuration from IoT hub/central. The configuration is all writable properties (desired property in device twin). Then the device automatically starts its sensor after configuring.

Describe the solution you'd like When I ran the sample Environmental Sensor, there's a message "Info: PnpBridge_PnpBridgeStateTelemetryCallback called, result=0, telemetry=PnpBridge configuration complete" in the log. It looks like to notify the configuration complete. If it can generate a callback function in adapter, that should work.

For example, there is a callback function code line in function "EnvironmentSensor_CreatePnpComponent" in file environmental_sensor_pnpbridge.c: _PnpComponentHandleSetPropertyUpdateCallback(BridgeComponentHandle, EnvironmentSensorProcessPropertyUpdate); Can we have a callback function like that for "Configuration Complete"?

Additional context Here is the log:

Info: Using configuration from specified file path: model/config_hub_eddytest01.json Info: Starting Azure PnpBridge Info: Pnp Bridge is running as am IoT egde device. Info: Pnp Bridge creation succeeded. Info: Connection_type is [connection_string] Info: Tracing is disabled Info: WARNING: SharedAccessKey is included in connection string. Ignoring symmetric_key in config file. Info: IoT Edge Device configuration initialized successfully Info: Building Pnp Bridge Adapter Manager, Adapters & Components Info: Adapter with identity impinj-r420-pnp-adapter does not have any associated global parameters. Proceeding with adapter creation. Info: Pnp Adapter with adapter ID impinj-r420-pnp-adapter has been created. Info: Pnp Adapter Manager created successfully. Info: Pnp components created successfully. Info: Pnp components built in model successfully. Info: Connected to Azure IoT Hub Info: Starting Pnp Component Info: Adapter:: Sending device information property to IoTHub. propertyName=DeviceState, propertyValue="INITIAL" Info: Pnp components started successfully. Info: Processing property update for the device or module twin Info: Received PnP property update for component=ImpinjR420Component, property=ReaderName Info: Get property:: PropertyName= PropertyValue= Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:Reader01a Info: Received PnP property update for component=ImpinjR420Component, property=Antenna1 Info: Get property value:1 Info: Get property value:1.000000 Info: Get property value:12.000000 Info: IoTHub client call to _SendReportedState succeeded Info: Received PnP property update for component=ImpinjR420Component, property=Session Info: Get property:: PropertyName= PropertyValue=<1.000000> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:1.000000 Info: Received PnP property update for component=ImpinjR420Component, property=SearchMode Info: Get property:: PropertyName= PropertyValue= Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:SINGLE_TARGET Info: Received PnP property update for component=ImpinjR420Component, property=RfMode Info: Get property:: PropertyName= PropertyValue=<3.000000> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:3.000000 Info: Received PnP property update for component=ImpinjR420Component, property=ReaderIp Info: Get property:: PropertyName= PropertyValue=<169.254.1.1> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:169.254.1.1 Info: Received PnP property update for component=ImpinjR420Component, property=SetClientId Info: Get property:: PropertyName= PropertyValue=<75.000000> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:75.000000 Info: Received PnP property update for component=ImpinjR420Component, property=UseKeepAlive Info: Get property:: PropertyName= PropertyValue= Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:1 Info: Received PnP property update for component=ImpinjR420Component, property=KeepAliveInterval Info: Get property:: PropertyName= PropertyValue=<3000.000000> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:3000.000000 Info: Received PnP property update for component=ImpinjR420Component, property=UseGpiDoorControl Info: Get property:: PropertyName= PropertyValue= Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:1 Info: Received PnP property update for component=ImpinjR420Component, property=GpiPhotoeye Info: Get property:: PropertyName= PropertyValue=<1.000000> Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:1.000000 _Info: PnpBridgePnpBridgeStateTelemetryCallback called, result=0, telemetry=PnpBridge configuration complete Info: PropertyCallback called, result=204, property name=DeviceState Info: PropertyCallback called, result=204, property name=����erName Info: PropertyCallback called, result=204, property name=����nna1 Info: PropertyCallback called, result=204, property name=����chMode Info: PropertyCallback called, result=204, property name=H{��ion Info: PropertyCallback called, result=204, property name=h{��de Info: PropertyCallback called, result=204, property name=H|��eepAlive Info: PropertyCallback called, result=204, property name=x{��erIp Info: PropertyCallback called, result=204, property name|��lientId Info: PropertyCallback called, result=204, property name=h|��AliveInterval Info: PropertyCallback called, result=204, property name=�|��piDoorControl Info: PropertyCallback called, result=204, property name=�{��hotoeye Info: Processing property update for the device or module twin Info: Received PnP property update for component=ImpinjR420Component, property=UseGpiDoorControl Info: Get property:: PropertyName= PropertyValue= Info: IoTHub client call to _SendReportedState succeeded Info: Get property value:0 Info: PropertyCallback called, result=204, property name=���� ^CError: Time:Tue Feb 9 13:58:20 2021 File:/home/etk/Projects/lib_sources/iot-plug-and-play-bridge/pnpbridge/deps/azure-iot-sdk-c-pnp/c-utility/adapters/threadapi_pthreads.c Func:ThreadAPI_Join Line:97 (result = THREADAPI_INVALID_ARG (2)) Info: Cleaning Pnp Bridge resources

Terminal will be reused by tasks, press any key to close it.

usivagna commented 3 years ago

Thanks for filing @eddylawsurgere, let me consult with @dipannita08 on this ask and get back to you.

usivagna commented 3 years ago

Based on feedback from Dipannita, there's already a desired property update callback in the adapter for when writable desired properties are successfully sent from device to cloud. From the perspective of the bridge whether it's configuration updates or any other writable properties, it would still only be a property update, so an additional callback wouldn't be required. there's already a desired property update callback in the adapter for when writable desired properties are successfully sent from device to cloud. From the perspective of the bridge whether it's configuration updates or any other writable properties, it would still only be a property update, so an additional callback may not be needed.

dipannita08 commented 3 years ago

If I understand your request correctly, you are looking for a way to be notified when device to cloud properties are written successfully, in order to make a decision from the adapter to start telemetry. If this is the case you can do the following:

  1. Register a callback from the adapter similar to SampleEnvironmentalSensor_PropertyCallback in the environmental sensor adapter, when reporting device twin properties from the device to cloud. Send in optional callback context.
  2. Along with this callback you will also be notified of the status, check for success on the "Configuration Update" property callback, and set an adapter "telemetry-ready" state.
  3. Before starting telemetry (StartComponent) from the adapter, ensure that the "telemetry-ready" state is set.

Also remember (more info in pnpadapter_api.h) that property and command callbacks can come in from the cloud to the device immediately after the client handle is created and the adapter should be ready to handle these, after successful client registration with IoT hub and before the flow of telemetry from device to cloud (StartComponent)

Please feel free to correct me if I misunderstood your concern.

eddylawsurgere commented 3 years ago

Let me give example. The desired properties got 2 writable properties and stored in cloud. It looks like this: "desired" : { "setTemperature" : 21, "setHumidity" : 50 }

When a device initially connects to IoT Central/Hub, the cloud sends the above desired properties as JSON to the device. This is case 1.

Here is case 2: If we change a writable property like "setTemperature" = 25 and click Save button on IoT Central Command page, the cloud only sends the property changed as JSON like: "desired" : { "setTemperature" : 25 }

Is there a feature (callback function) for case 1 to know that the whole desired properties have sent from cloud to the device?

Thanks

dipannita08 commented 3 years ago

In case 1, you will receive a cloud to device property update callback on the function you register from the adapter in "PnpComponentHandleSetPropertyUpdateCallback" with relevant information to deduce which property was updated.

eddylawsurgere commented 3 years ago

I found the function "PnpAdapterManager_PnpBridgeStateTelemetryCallback" in pbpadapter_manager.c. When I ran the pnpbridge app, tt's called back with a log message "Info: PnpBridge_PnpBridgeStateTelemetryCallback called, result=0, telemetry=PnpBridge configuration complete". It seems what I want. Does this function exists in pnpadapter_api?

Thanks

usivagna commented 1 year ago

Thank you for your interest in the IoT Plug and Play bridge, unfortunately due to new innovations in Azure IoT and changes in product strategy we have come to the decision to deprecate the IoT Plug and Play bridge. Thank you to the community for your support on this journey. The repo is planned to be archived as a read-only repository on November 30th 2023. Thank you to everyone who contributed bug fixes and new adapters for connecting devices with IoT Plug and Play. You can find the latest IoT innovations at https://techcommunity.microsoft.com/t5/internet-of-things-blog/bg-p/IoTBlog. Please note that any use of archived repositories introduces more risks from a security standpoint and should be done with caution.