Skip to content

Add class ArduinoIO#422

Merged
jamessynge merged 2 commits intopanoptes:developfrom
jamessynge:issue-420-arduino-io
Jan 29, 2018
Merged

Add class ArduinoIO#422
jamessynge merged 2 commits intopanoptes:developfrom
jamessynge:issue-420-arduino-io

Conversation

@jamessynge
Copy link
Copy Markdown
Contributor

pocs/sensors/arduino_io.py is based on peas/sensors.py, but
ArduinoIO handles only a single device, and writes readings
to a queue rather than to a database.

Moved protocol_arduinosimulator.py to a new folder,
pocs/serial_handlers. This will be the home of the other PySerial
handlers in the future.

pocs/sensors/arduino_io.py is based on peas/sensors.py, but
ArduinoIO handles only a single device, and writes readings
to a queue rather than to a database.

Moved protocol_arduinosimulator.py to a new folder,
pocs/serial_handlers. This will be the home of the other PySerial
handlers in the future.
@jamessynge
Copy link
Copy Markdown
Contributor Author

Partially address #420.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 28, 2018

Codecov Report

Merging #422 into develop will increase coverage by 0.4%.
The diff coverage is 75.52%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop     #422     +/-   ##
==========================================
+ Coverage    69.24%   69.65%   +0.4%     
==========================================
  Files           60       61      +1     
  Lines         4806     4943    +137     
  Branches       665      687     +22     
==========================================
+ Hits          3328     3443    +115     
- Misses        1301     1309      +8     
- Partials       177      191     +14
Impacted Files Coverage Δ
peas/tests/test_sensors.py 100% <100%> (ø) ⬆️
pocs/serial_handlers/protocol_arduinosimulator.py 74.8% <100%> (ø)
pocs/sensors/arduino_io.py 74.43% <74.43%> (ø)
pocs/utils/rs232.py 91.37% <75%> (-0.66%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c2cc1b...241e788. Read the comment docs.

Copy link
Copy Markdown
Member

@wtgee wtgee left a comment

Choose a reason for hiding this comment

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

Big question is about the queue with ArduinoIO and what the expected usage is like. Docstrings and other miscellany.

Comment thread pocs/utils/rs232.py Outdated
Returns: a list of PySerial's ListPortInfo objects. See:
https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports_common.py
"""
from serial.tools.list_ports import comports
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be at top of file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to top of file.

Comment thread pocs/utils/rs232.py Outdated
https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports_common.py
"""
from serial.tools.list_ports import comports
return sorted(comports(), key=operator.attrgetter('device'))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just key=lambda x: x.device?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That works too. Not sure which is "better", though yours is shorter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was thinking the docs recommended lambdas but looking at it now they go on to say it is such common usage that the operator module exists just for that. here :) So fine leaving as is.

Comment thread scripts/list_arduinos.py Outdated
if port_infos:
fmt = '{:20s} {:30s} {}'
print(fmt.format('Device', 'Manufacturer', 'Description'))
for pi in rs232.get_serial_port_info():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just for pi in ports_infos?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops, mistake.

Comment thread scripts/list_arduinos.py Outdated

print()
boards_and_ports = arduino_io.auto_detect_arduino_devices()
for (board, port) in boards_and_ports:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't think the parens are necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread scripts/list_arduinos.py
print("Arduino devices: {}".format(", ".join(devices)))
else:
print("No Arduino devices found.")
sys.exit(1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this used somewhere such that you want the 1 on exit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I figured this could be used by another script that is doing setup of a unit, so distinguishing between found some devices and found no devices is worthwhile. But, not used yet.

self._thread.daemon = True
self._thread.start()

def stop_reading(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docstring

else:
self._thread = None

def write(self, text):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docstrings (just mentioning for good measure 😄 )

with self._serial_data_lock:
if not self._serial_data.is_connected:
self._serial_data.connect()
return self._serial_data.write(text)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm guessing the lock context manager does the right thing here with a return statement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it does.

response = bytes(response)
self.logger.info('FakeArduinoSerialHandler.read({}) -> {!r}', size, response)
# if size > 1:
# self.logger.debug('FakeArduinoSerialHandler.read({}) -> {!r}', size, response)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removing commented out code.

It was too noisy because size=1 most of the time; this is because it gets called by readline in io.IOBase, the base of the serial impls. I experimented with only printing when size>1, but settled on adding a readline impl that prints the line that is read.

class ArduinoIO(object):
"""Reads the output from an arduino, and exposes the relays for change."""

def __init__(self, board_name, port, output_queue, serial_config=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the overall picture of how this will be used? I mostly ask because I think it's a bit odd to pass in a queue rather than to have it create a queue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The aim is for it to deliver readings to a thread that will take care of writing to the database (or wherever). The advantage of providing the queue to the object is that we can use the same queue for multiple Arduinos, and then the db writer thread only needs to read from a single queue.

Rename list_arduino_ports to get_arduino_ports.
Add missing docstrings.
@jamessynge
Copy link
Copy Markdown
Contributor Author

PTAL

Copy link
Copy Markdown
Member

@wtgee wtgee left a comment

Choose a reason for hiding this comment

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

LGTM

@jamessynge jamessynge merged commit 5fe9eee into panoptes:develop Jan 29, 2018
@jamessynge jamessynge deleted the issue-420-arduino-io branch January 29, 2018 18:20
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