Skip to content

Conversation

MartinHjelmare
Copy link
Collaborator

Hi!

This branch implements a new feature that permits sending commands from the controller/gateway to a node. This gives the possibility of controlling switches connected to mysensors nodes.

The basis is a FIFO queue that enables comm with the SerialGateway thread from another thread. The queue is filled by a function fill_queue that puts a function and any arguments on the queue. The queue is handled by a function handle_queue that gets the functions from the queue and calls them, in FIFO order. Commands are issued from the controller by calling a new function in the Gateway class, set_child_value. This function calls the fill_queue function and puts the other set_child_value function from the Sensor class with correct arguments to the queue. I also changed the Sensor class set function to return a set message. The new set_child_value function in the Gateway class is for convenience.

When testing this branch I encountered problems when reconnecting to the Serial interface. My guess is that this has to do with pyserial 2.7 and onwards. Testing with pyserial 2.5 worked flawlessly as far as I could tell. Therefore I changed the required version of pyserial to 2.5. Since this problem might not effect everyone, it can be discussed if it's really a good idea to downgrade pyserial. There seems to be at least some other people that are having similar issues, if looking at pyserial repo. Maybe this part of my pull request should be removed?

The following points are a summary of my changes in the pull request.

  • Add queue as attribute to Gateway class
  • Add fill_queue function in Gateway class for putting functions in the
    queue.
  • Add handle_queue function in Gateway class for getting functions from
    the queue, and calling them.
  • Add set_child_value function in Gateway class as a convenience
    function. It will set a new value for a child and send the
    corresponding Message from the gateway to the child's node.
    This is not done directly but by putting a function with arguments to
    the FIFO queue. The function in the queue is the set_child_value
    function in the Sensor class.
  • Change set_child_value function in Sensor class to return a set
    Message.
  • Change the loop in the run function in SerialGateway class to handle
    queue first, then read from serial interface. If valid input is
    read from serial and decoded, add logic function with input as
    argument to the queue.
  • Add some debug logging to SerialGateway class.
  • Fix some minor PEP8 issues.
  • Fix problem of reading garbage after disconnecting and reconnecting,
    by changing pyserial required version to 2.5 or less.
  • Make serial connecting and disconnecting more robust, by sleeping for
    a while after connecting, and by checking if connection is open
    after connecting, and by closing connection when thread stops.
  • Add more logging to serial connection functions.
  • Change lowest logging level for log messages to INFO (home-assistant uses INFO as lowest).
  • Add function to set log level to debug if debug attribute of gateway
    is true.
  • Fix logging calls to use proper parameters instead of string
    formatting.
  • Add function docstrings to fill_queue, handle_queue and
    set_child_value functions in Gateway class.

As in my previous pull request, I've put the base branch to dev for merging. I don't know if that's what you would like, or if it should be the master branch, if you decide to merge this.

Best regards,
Martin

This feature will add support for mysensors switches.

* Add queue as attribute to Gateway class
* Add fill_queue function in Gateway class for putting functions in the
    queue.
* Add handle_queue function in Gateway class for getting functions from
    the queue, and calling them.
* Add set_child_value function in Gateway class as a convenience
    function. It will set a new value for a child and send the
    corresponding Message from the gateway to the child's node.
    This is not directly but by putting a function with arguments to
    the FIFO queue. The function in the queue is the set_child_value
    function in the Sensor class.
* Change set_child_value function in Sensor class to return a set
    Message.
* Change the loop in the run function in SerialGateway class to handle
    queue first, then read from serial interface. If valid input is
    read from serial and decoded, add logic function with input as
    argument to the queue.
* Add some debug logging to SerialGateway class.
* Fix some minor PEP8 issues.
* Fix problem of reading garbage after disconnecting and reconnecting,
    by changing pyserial required version to 2.5 or less.
* Make serial connecting and disconnecting more robust, by sleeping for
    a while after connecting, and by checking if connection is open
    after connecting, and by closing connection when thread stops.
* Add more logging to serial connection functions.
* Change lowest logging level for log messages to INFO.
* Add function to set log level to debug if debug attribute of gateway
    is true.
* Fix logging calls to use proper parameters instead of string
    formatting.
* Add function docstrings to fill_queue, handle_queue and
    set_child_value functions in Gateway class.
@MartinHjelmare
Copy link
Collaborator Author

I'll update my upstream, solve the conflicts and push again.

Conflicts:
	mysensors/mysensors.py

* Solved a couple of minor indentation style conflicts and merged a
	comment.
@maxdrift
Copy link

maxdrift commented Dec 7, 2015

+1 for this feature!

theolind added a commit that referenced this pull request Dec 16, 2015
Fifo queue, implementing switches
@theolind theolind merged commit 2aa8f32 into theolind:dev Dec 16, 2015
@theolind
Copy link
Owner

Thanks for your pull request @MartinHjelmare ! And sorry for the late merge.

@MartinHjelmare
Copy link
Collaborator Author

Great, thanks!

@MartinHjelmare MartinHjelmare deleted the fifo_queue branch January 16, 2016 13:58
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.

3 participants