Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementatation of the stream functionality #12

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

rgallor
Copy link
Collaborator

@rgallor rgallor commented Sep 3, 2024

This pull request introduces a Rust application that initiates an Astarte device, establishes an MQTT connection to a running Astarte instance, and sends data generated from a set of predefined mathematical functions.

Key Features:

  • Connection Configuration: The application requires a config.toml file, which contains the necessary information to connect the device to the Astarte instance.
  • Data Generation: Users can define mathematical functions and their parameters either through command-line interface (CLI) options or by setting the corresponding environment variables.

This work is inspired by and builds upon what has been done in the stream-qt5-test repository.

Closes #3

The interface allows generic sensors to stream samples data in double
format.
The interface is the same as the one defined in the
stream-qt5-test astarte-platform repository

Signed-off-by: Riccardo Gallo <[email protected]>
Introduce:
- `color_eyre` to have a structured report handler
- `tracing_subscriber` to collect scoped, structured and async-aware log data

Signed-off-by: Riccardo Gallo <[email protected]>
Copy link

codecov bot commented Sep 3, 2024

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@rgallor rgallor force-pushed the feat/implementatation branch 2 times, most recently from 0fcd3ec to a6f84a6 Compare September 9, 2024 13:31
src/main.rs Outdated Show resolved Hide resolved
@rgallor rgallor force-pushed the feat/implementatation branch 2 times, most recently from d775c12 to 7c6b1b5 Compare September 25, 2024 13:15
astarte-device-conf/.env Outdated Show resolved Hide resolved
astarte-device-conf/config.toml Outdated Show resolved Hide resolved
src/astarte.rs Show resolved Hide resolved
src/astarte.rs Show resolved Hide resolved
@rgallor rgallor requested a review from harlem88 October 1, 2024 15:57
@rgallor rgallor force-pushed the feat/implementatation branch 2 times, most recently from 835921f to 04d4e6a Compare October 4, 2024 15:18
.cargo/config.toml Outdated Show resolved Hide resolved
scripts/run.sh Outdated Show resolved Hide resolved
use tracing::{debug, error, info};

/// Stream Rust test node identifier
const STREAM_RUST_TEST_NODE_UUID: uuid::Uuid = uuid::uuid!("d72a6187-7cf1-44cc-87e8-e991936166dc");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be overridable? So you can test multiple connections to the message hub

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a first implementation, we decided to embed the UUID, consequently having only once connection to the message hub. However, I think this can be changed (either in this PR or in a new one) if we want. What do you think @harlem88?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you can change it in a new PR.

src/astarte.rs Outdated Show resolved Hide resolved
src/astarte.rs Outdated Show resolved Hide resolved
src/astarte.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/math.rs Outdated
}

fn random_spikes_sin(value: f64) -> f64 {
if value.sin() + rand::random::<f64>() / f64::MAX * 0.1 + rand::random::<f64>() / f64::MAX * 0.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls explain

confused crab noises

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In rust the range for float [0,1)

https://docs.rs/rand/latest/rand/fn.random.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the division by f64::MAX can be avoided

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, if I understood correctly from the stream-qt5-test code, the random_spike_sin function is a spike (spike(x) = 1 if 0<=x<1, 0 otherwise) computed on the value of a noise sine (which is sin(x) + rand())

src/math.rs Outdated
}

fn noise_sin(value: f64) -> f64 {
value.sin() + rand::random::<f64>() / f64::MAX * 0.2
Copy link
Contributor

@joshuachp joshuachp Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

{
100.0
} else {
0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to document this function since it's not clear from the name what it does.

From it, I would expect the sin(value) would be return, but I think the function actually returns spikes with PI frequency

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I don't think the function is actually correct, looking at the original code:

    } else if (m_function == QStringLiteral("randomspikessin")) {
        value = sin(x) + (((double) qrand()) / ((double) RAND_MAX) * 0.1) + ((((double) qrand()) / ((double) RAND_MAX)) > 0.999 ? 100 : 0);

@joshuachp
Copy link
Contributor

I would check the other functions like sinc they don't seem to do the same thing as in the original code

@rgallor
Copy link
Collaborator Author

rgallor commented Nov 7, 2024

I would check the other functions like sinc they don't seem to do the same thing as in the original code

Yes, for this one I used the original definition of the normalized sinc function, which differs from the implementation in the stream-qt5-test

This required also the introduction of some environment variables and
structures to handle the generation of the samples to be sent to
Astarte.
For simplicity, the math functions are the same as the ones used in the
stream-qt5-test repository.

Signed-off-by: Riccardo Gallo <[email protected]>
Use clap to start the application, also taking into account possible
environment variables set by the user.

Signed-off-by: Riccardo Gallo <[email protected]>
Now the configuration information to connect a device to Astarte are
provided inside a `astarte-device-DEVICE_ID_HERE-conf` directory, in
a `config.toml` file. Instead, all the information necessary to generate
samples to be sent to Astarte are provided via CLI or environment
variables.
Also the README have been update to explain how to configure the
application and build/run it.

Signed-off-by: Riccardo Gallo <[email protected]>
Signed-off-by: Riccardo Gallo <[email protected]>
Provide the Atsarte configuration via environment variables or config.toml file, prioritizing env vars

Signed-off-by: Riccardo Gallo <[email protected]>
Give more flexibility to choose the mqtt or grpc Astarte connection
configuration by using the CLI, environment variables and a config.toml
file

Signed-off-by: Riccardo Gallo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stream configuration via command line
3 participants