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

Refactor IoT service to use async functionality and fix MessageType #506

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

Conversation

Viacheslav417865
Copy link

1

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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


Thank you for your hard work on this project! 🌟 While you've made great progress, there are a few critical issues that need to be addressed to meet the core requirements of the task:

  1. Asynchronous Implementation: The methods connect, disconnect, and send_message need to be converted to asynchronous functions using async/await. This change is crucial for the task's requirements, as it ensures non-blocking operations.

  2. Device Registration: The task specifies using asyncio.gather for simultaneous device registration, which is not currently implemented. This is important for optimizing the registration process.

  3. Program Execution Order: The logical sequence of operations, such as using run_sequence and run_parallel, is not implemented. This is necessary to ensure that operations are executed in the correct order while maximizing parallel execution.

  4. Code Structure: The task suggests removing certain variables and using helper functions directly, which has not been done. This change is important for maintaining clean and efficient code.

Please address these issues to align the code with the task requirements. Once these changes are made, your solution will be much closer to completion. Keep up the great work, and don't hesitate to reach out if you need further assistance! 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.

app/main.py Outdated Show resolved Hide resolved
app/main.py Outdated Show resolved Hide resolved
app/main.py Outdated Show resolved Hide resolved
Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Thank you for your submission! 🎉 Your code demonstrates a solid understanding of asynchronous programming, and you've successfully implemented key features such as async/await and asyncio.gather for parallel operations. This aligns well with the task requirements.

However, there are a few areas for improvement:

  1. Message Type Handling: Ensure that the Message class uses MessageType for the msg_type attribute. This will help maintain type consistency and improve code readability.

  2. Device Protocol Compliance: The register_device method in IOTService should verify that the device being registered implements the Device protocol. Adding a type check or annotation can enforce this and prevent potential runtime errors.

  3. Asynchronous Message Sending: The send_message method should be asynchronous. Consider using async def send_message(self, message_type: MessageType, data: str = "") -> None: and replacing time.sleep(TIME_TO_SLEEP) with await asyncio.sleep(TIME_TO_SLEEP) to ensure non-blocking operations.

These improvements will enhance the robustness and maintainability of your code. Keep up the great work, and continue to refine your skills! Remember, addressing these issues will be crucial for future projects. 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines +19 to 25
def send_message(self, message_type: MessageType,
data: str = "") -> None:
print(
f"Hue Light handling message of type {message_type.name} with data [{data}]."
f"Hue Light handling message of type"
f" {message_type.name} with data [{data}]."
)
time.sleep(TIME_TO_SLEEP)

Choose a reason for hiding this comment

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

The send_message method should be asynchronous. Consider using async def send_message(self, message_type: MessageType, data: str = "") -> None: and replacing time.sleep(TIME_TO_SLEEP) with await asyncio.sleep(TIME_TO_SLEEP).

Comment on lines +40 to 46
def send_message(self, message_type: MessageType,
data: str = "") -> None:
print(
f"Smart Speaker handling message of type {message_type.name} with data [{data}]."
f"Smart Speaker handling message of type"
f" {message_type.name} with data [{data}]."
)
time.sleep(TIME_TO_SLEEP)

Choose a reason for hiding this comment

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

The send_message method should be asynchronous. Consider using async def send_message(self, message_type: MessageType, data: str = "") -> None: and replacing time.sleep(TIME_TO_SLEEP) with await asyncio.sleep(TIME_TO_SLEEP).

Comment on lines +61 to 68
def send_message(
self, message_type: MessageType,
data: str = "") -> None:
print(
f"Smart Toilet handling message of type {message_type.name} with data [{data}]."
f"Smart Toilet handling message of type "
f"{message_type.name} with data [{data}]."
)
time.sleep(TIME_TO_SLEEP)

Choose a reason for hiding this comment

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

The send_message method should be asynchronous. Consider using async def send_message(self, message_type: MessageType, data: str = "") -> None: and replacing time.sleep(TIME_TO_SLEEP) with await asyncio.sleep(TIME_TO_SLEEP).

msg_type: str,
content: str = None) -> None:
self.device_id = device_id
self.type = msg_type

Choose a reason for hiding this comment

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

The msg_type attribute in the Message class should be of type MessageType instead of str. Consider changing msg_type: str to msg_type: MessageType in the constructor.

Comment on lines +28 to +34
async def register_device(self, device: str) -> str:
"""Asynchronously register a device."""
device_id = str(len(self.devices) + 1)
self.devices[device_id] = device
print(f"Device {device.__class__.__name__} "
f"registered with ID {device_id}")
return device_id

Choose a reason for hiding this comment

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

The register_device method in IOTService should ensure that the device being registered implements the Device protocol. Consider adding a type check or annotation to enforce this.

Comment on lines +36 to +42
async def send_message(self, message: Message) -> None:
"""Asynchronously send a message to a device."""
print(f"Sending message to device "
f"{message.device_id}: {message.type}")
await asyncio.sleep(1)
print(f"Message sent: "
f"{message.device_id} - {message.type}")

Choose a reason for hiding this comment

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

The send_message method in IOTService should ensure that the message type is correctly handled. Verify that the Message class uses MessageType for the msg_type attribute.

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.

2 participants