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

Send dummy request before closing serial port #740

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

jonathanperret
Copy link
Contributor

@jonathanperret jonathanperret commented Dec 7, 2024

🐛 Problem

Hopefully fixes #662 .

💡 Proposed solution

One theory that is consistent with the observations in #662, is that the last cnfLine message sent from the desktop app to the firmware gets lost when the serial port is closed immediately afterwards. Here is an example of this happening in another context: serialport/serialport-rs#117

To avoid closing the serial port before the last data has been received by the firmware, in this PR we add an additional exchange initiated by the desktop app. A reqInfo is sent to the firmware, which immediately replies with a cnfInfo message. Because messages are sent and received in sequence, receiving this response confirms that previous messages have been transmitted and the serial port can be closed safely.

🤔 Alternatives considered

To make sure the last bytes are transmitted, I considered adding a delay before closing the port, but this wouldn't really guarantee data was sent.

A cleaner solution in the longer term would be to evolve the API so that all exchanges would be initiated by the desktop app, instead of having an exception for reqLine/cnfLine.

🔬How to test

This test release includes the change proposed in this PR: https://github.com/jonathanperret/ayab-desktop/releases/tag/1.0.0-safe-disconnect-1
It does not require a new firmware, so no need to reflash before testing.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new DISCONNECT state to improve the disconnection process after knitting operations.
    • Added a notification for users indicating that disconnection from the machine is in progress.
  • Improvements

    • Enhanced feedback mechanism with specific notifications related to the disconnection process.

Copy link
Contributor

coderabbitai bot commented Dec 7, 2024

Walkthrough

The changes introduce a new state called DISCONNECT to the State enumeration in the engine_fsm.py file, enhancing the finite state machine's ability to manage disconnections after knitting operations. The _API6_run_knit method is updated to transition to DISCONNECT upon completion of knitting, ensuring proper communication with the firmware. Additionally, a new method, _API6_disconnect, is added to handle disconnection logic. In the output.py file, a new output state DISCONNECTING_FROM_MACHINE is introduced, along with a method in FeedbackHandler to notify that disconnection is in progress.

Changes

File Path Change Summary
src/main/python/main/ayab/engine/engine_fsm.py - Added new state DISCONNECT to State enumeration.
- Modified _API6_run_knit to transition to DISCONNECT after knitting.
- Added method _API6_disconnect to handle disconnection logic.
src/main/python/main/ayab/engine/output.py - Added new enum value DISCONNECTING_FROM_MACHINE to Output class.
- Added method _disconnecting_from_machine to FeedbackHandler for disconnection notifications.
- Modified handle method for dynamic dispatching based on new output state.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant StateMachine
    participant Firmware
    participant FeedbackHandler

    User->>StateMachine: Start knitting
    StateMachine->>Firmware: Send knitting request
    Firmware-->>StateMachine: Knitting in progress
    StateMachine->>Firmware: Request final response
    Firmware-->>StateMachine: Final response received
    StateMachine->>StateMachine: Transition to DISCONNECT
    StateMachine->>FeedbackHandler: Notify disconnection in progress
    FeedbackHandler-->>User: Disconnection notification
    StateMachine->>StateMachine: Call _API6_disconnect
    StateMachine->>Firmware: Check for confirmation token
    Firmware-->>StateMachine: Confirmation token received
    StateMachine->>StateMachine: Transition to FINISHED
    StateMachine-->>User: Knitting finished
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
src/main/python/main/ayab/engine/engine_fsm.py (1)

54-54: Document the addition of the DISCONNECT state

The new DISCONNECT state enhances the state machine's capability to manage the disconnection process. Please update any relevant documentation or comments to include this new state and its role in the state machine.

src/main/python/main/ayab/engine/output.py (1)

36-36: Maintain logical grouping within the Output enumeration

Consider placing the new DISCONNECTING_FROM_MACHINE value adjacent to related states like CONNECTING_TO_MACHINE for better readability and logical grouping within the Output enum.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4bc241c and 1c2e6fe.

📒 Files selected for processing (2)
  • src/main/python/main/ayab/engine/engine_fsm.py (3 hunks)
  • src/main/python/main/ayab/engine/output.py (2 hunks)
🔇 Additional comments (1)
src/main/python/main/ayab/engine/output.py (1)

69-71: Verify dynamic method dispatch for new output state

Ensure that the handle method correctly dispatches to the _disconnecting_from_machine method when Output.DISCONNECTING_FROM_MACHINE is encountered. Since the dispatch relies on naming conventions, verify that the method name matches the enum member name in lowercase with a leading underscore.

To confirm, the method name _disconnecting_from_machine should match the enum member DISCONNECTING_FROM_MACHINE.

This avoids losing the last sent data with some USB/serial drivers.
Copy link

sonarqubecloud bot commented Dec 9, 2024

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/main/python/main/ayab/engine/engine_fsm.py (1)

200-211: Well-documented solution to prevent message loss

The implementation effectively addresses the issue of potentially dropped cnfLine messages by using reqInfo as a sentinel. The detailed comments explain the problem and solution clearly.

As mentioned in the PR objectives, consider evolving the API to ensure all exchanges are initiated by the desktop application in the future. This would provide a more robust communication pattern.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1c2e6fe and 787e068.

📒 Files selected for processing (2)
  • src/main/python/main/ayab/engine/engine_fsm.py (3 hunks)
  • src/main/python/main/ayab/engine/output.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/python/main/ayab/engine/output.py
🔇 Additional comments (2)
src/main/python/main/ayab/engine/engine_fsm.py (2)

54-54: LGTM: State enum addition is appropriate

The new DISCONNECT state provides a clear transitional state for handling the disconnection process.


251-261: LGTM: Clean implementation of disconnect handling

The method effectively handles the disconnection process by waiting for the cnfInfo response before transitioning to the FINISHED state. The implementation is simple and focused.

@dl1com
Copy link
Contributor

dl1com commented Dec 11, 2024

Review OK.
Before merging, we still require a test result if this really mitigates #662 .

For the future, the API should probably adapted for proper synchronuous communication rather this workaround (see lengthy discussions on Discord).

@jonathanperret
Copy link
Contributor Author

For the future, the API should probably adapted for proper synchronuous communication rather this workaround (see lengthy discussions on Discord).

Yes, this is to be kept in mind for the next iteration of the API which could be done just after 1.0 is out.
For 1.0 I favor keeping the API as-is if possible (noting that the proposed alternative of waiting for an extra indState message would be a breaking API change even though no message format is changed).

@dl1com dl1com merged commit 5f9e0d4 into AllYarnsAreBeautiful:main Dec 13, 2024
3 checks passed
@dl1com
Copy link
Contributor

dl1com commented Dec 13, 2024

Reported to be working by @Adrienne200 in Discord

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.

Patterning can get stuck on row n-1 and never finish
2 participants