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

fix: app crashes if data is read from sensor which is not connected #2605

Merged
merged 2 commits into from
Jan 5, 2025

Conversation

marcnause
Copy link
Contributor

@marcnause marcnause commented Jan 4, 2025

Fixes #2604

Changes

  • add return value to method which reads sensor data
  • skip UI update if sensor data was not read successfully

Screenshots / Recordings

N/A

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Bug Fixes:

  • Prevent UI updates if sensor data reading fails.

Previously it was assumed that dat was read successfully if the method was called at least once.
Copy link

sourcery-ai bot commented Jan 4, 2025

Reviewer's Guide by Sourcery

This pull request fixes a crash that occurs when attempting to read data from a disconnected sensor. It modifies the getSensorData method to return a boolean value indicating the success of the read operation. The UI is updated only if sensor data is successfully acquired.

Sequence diagram for sensor data acquisition and UI update flow

sequenceDiagram
    participant App
    participant SensorDataFetch
    participant Sensor
    participant UI

    App->>SensorDataFetch: execute()
    SensorDataFetch->>Sensor: getSensorData()
    alt Sensor connected and data read successful
        Sensor-->>SensorDataFetch: return sensor data
        SensorDataFetch->>SensorDataFetch: return true
        SensorDataFetch->>UI: updateUI()
        UI-->>SensorDataFetch: UI updated
    else Sensor disconnected or read failed
        Sensor-->>SensorDataFetch: return null/throw error
        SensorDataFetch->>SensorDataFetch: return false
        Note over SensorDataFetch,UI: Skip UI update
    end
Loading

Class diagram showing AbstractSensorActivity and SensorDataFetch relationship

classDiagram
    class AbstractSensorActivity {
        #getStartTime()
        #onOptionsItemSelected()
    }

    class SensorDataFetch {
        #getTimeElapsed()
        #execute()
        +getSensorData() boolean
        +updateUi()
    }

    AbstractSensorActivity *-- SensorDataFetch : contains

    note for SensorDataFetch "Modified to return boolean\nindicating success/failure\nof sensor data acquisition"
Loading

State diagram for sensor data acquisition process

stateDiagram-v2
    [*] --> AttemptRead
    AttemptRead --> ReadSuccess: Sensor connected
    AttemptRead --> ReadFailure: Sensor disconnected
    ReadSuccess --> UpdateUI: Update displays and charts
    ReadFailure --> Skip: Skip UI update
    UpdateUI --> [*]
    Skip --> [*]

    note right of ReadFailure: New error handling
    note right of Skip: Prevents crash
Loading

File-Level Changes

Change Details Files
Modified sensor data fetching logic to prevent app crash
  • Changed getSensorData method to return a boolean indicating success.
  • UI update is skipped if sensor data acquisition fails.
  • Removed isSensorDataAcquired method and its usage.
  • Added error handling for sensor read failures.
app/src/main/java/io/pslab/sensors/SensorMPU925X.java
app/src/main/java/io/pslab/sensors/SensorMPU6050.java
app/src/main/java/io/pslab/sensors/SensorBMP180.java
app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
app/src/main/java/io/pslab/sensors/SensorTSL2561.java
app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
app/src/main/java/io/pslab/sensors/SensorSHT21.java
app/src/main/java/io/pslab/sensors/SensorCCS811.java
app/src/main/java/io/pslab/sensors/SensorMLX90614.java
app/src/main/java/io/pslab/sensors/SensorVL53L0X.java
app/src/main/java/io/pslab/sensors/SensorADS1115.java
Updated UI update logic based on sensor data acquisition status
  • UI elements are updated only if sensor data is successfully read.
  • Conditional UI updates based on sensor data availability have been removed.
app/src/main/java/io/pslab/sensors/SensorMPU925X.java
app/src/main/java/io/pslab/sensors/SensorMPU6050.java
app/src/main/java/io/pslab/sensors/SensorBMP180.java
app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
app/src/main/java/io/pslab/sensors/SensorTSL2561.java
app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
app/src/main/java/io/pslab/sensors/SensorSHT21.java
app/src/main/java/io/pslab/sensors/SensorCCS811.java
app/src/main/java/io/pslab/sensors/SensorMLX90614.java
app/src/main/java/io/pslab/sensors/SensorVL53L0X.java
app/src/main/java/io/pslab/sensors/SensorADS1115.java

Assessment against linked issues

Issue Objective Addressed Explanation
#2604 Prevent app from crashing when sensor data is read even though no sensor is connected
#2604 Handle scenarios where sensor data cannot be retrieved gracefully

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @marcnause - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding null checks in updateUI() methods to avoid potential NPEs if sensor data objects are null after failed reads
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

github-actions bot commented Jan 4, 2025

@marcnause marcnause enabled auto-merge (rebase) January 4, 2025 23:50
@marcnause marcnause changed the title fix: app crash if data is read from sensor which is not connected fix: app crashes if data is read from sensor which is not connected Jan 4, 2025
@AsCress AsCress disabled auto-merge January 5, 2025 16:33
@AsCress AsCress enabled auto-merge (squash) January 5, 2025 16:33
@AsCress
Copy link
Collaborator

AsCress commented Jan 5, 2025

Changing this PR to use squash merge.

Copy link
Collaborator

@AsCress AsCress left a comment

Choose a reason for hiding this comment

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

@marcnause Everything looks good to me and the crash is fixed !

@AsCress AsCress merged commit 3d66bf5 into fossasia:development Jan 5, 2025
4 checks passed
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.

App crashes if sensor data is read even though no sensor is connected
2 participants