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

Feature/hardware tests #149

Merged
merged 25 commits into from Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
32baa01
added .coveragerc file to control pythong coverage, added initial tests
ab7289 Mar 18, 2020
3589751
added .coveragerc file to control coverage
ab7289 Mar 18, 2020
8f4428e
fixed black violation
ab7289 Mar 18, 2020
b0ba5a8
removed .coveragerc as we need different run parameters depending on …
ab7289 Mar 18, 2020
d22ea21
modified Dockerfile and travis build
ab7289 Mar 19, 2020
4771dbd
Merge remote-tracking branch 'origin/hardware-team' into feature/hard…
ab7289 Mar 19, 2020
260532a
reverted coveragerc deletion
ab7289 Mar 19, 2020
849f146
Merge branch 'test-revert' into feature/hardware-tests
ab7289 Mar 19, 2020
c6f3f8c
added tests for CommunicationsPi.is_serial_usb
ab7289 Mar 19, 2020
c6754f4
modified travis config to utilize config files instead of passing in …
ab7289 Mar 19, 2020
48cd564
updated check.sh since we no longer need to pass in flags
ab7289 Mar 19, 2020
dccc0bf
added tests for extra_info and get_port in CommunicationsPi
ab7289 Mar 19, 2020
2fa674c
fixed black violations
ab7289 Mar 19, 2020
c584cce
Merge branch 'lint-config' into feature/hardware-tests
ab7289 Mar 20, 2020
5bc9027
fixed flake8 violations
ab7289 Mar 20, 2020
ce23b28
fixed black violations
ab7289 Mar 20, 2020
3d0c138
clean up
PrabhanshuAttri Mar 20, 2020
ce86d5a
Merge remote-tracking branch 'origin/feature/hardware-tests' into fea…
ab7289 Mar 20, 2020
fe32cf5
added explanatory comments
ab7289 Mar 20, 2020
9700f88
updated test-requirements.txt to add testfixtures module and added so…
ab7289 Mar 21, 2020
cf02f26
fixed black violations
ab7289 Mar 21, 2020
2b1b8c8
added basic tests for logger public methods and removed debug print s…
ab7289 Mar 21, 2020
da21fd9
updated flake8 line length to the django-recommended 119
ab7289 Mar 21, 2020
15acf81
added CommunicationsPi.utils tests and fixed a line in utils
ab7289 Mar 21, 2020
5b12f0b
commented out lines not in use
ab7289 Mar 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .coveragerc
@@ -0,0 +1,14 @@
# .coveragerc to control coverage.py
[run]
omit =
*/site-packages/*
*/distutils/*
*/tests/*
hardware/setup/*
source =
mysite
mercury
hardware
ag_data
hardware/CommunicationsPi
hardware/SensorPi
2 changes: 1 addition & 1 deletion .flake8
@@ -1,5 +1,5 @@
[flake8]
max-line-length = 92
max-line-length = 119
exclude =
.git,
__pycache__,
Expand Down
11 changes: 8 additions & 3 deletions .travis.yml
Expand Up @@ -34,9 +34,11 @@ jobs:
- python manage.py collectstatic --noinput

script:
- black --check --exclude "migrations/" mysite mercury ag_data
- flake8 mysite/ mercury/ ag_data/
- coverage run --source=mysite,mercury,ag_data --omit="migrations/*" manage.py test
- black --check --diff .
# modify .flake8 env file to update flake8 configuration
- flake8 .
# modify .coveragerc env file to update test configuration
- coverage run manage.py test

after_script:
- coveralls
Expand All @@ -45,6 +47,8 @@ jobs:
# run the hardware tests
- language: bash

# python: '3.6'

services:
- docker

Expand All @@ -57,5 +61,6 @@ jobs:
script:
# basic help world test to see if it's working
- docker run gcivil-nyu-org/spring2020-cs-gy-9223-class grep -q "Hello, Docker!" hello.txt
# - black --check --exclude "migrations/" hardware


4 changes: 2 additions & 2 deletions Dockerfile
Expand Up @@ -5,9 +5,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
raspi-config

# copy setup scripts
COPY ./hardware/setup .
COPY ./hardware .

# run setup
RUN bash raspberrypi-common.sh
RUN bash ./setup/raspberrypi-common.sh

RUN echo "Hello, Docker!" > hello.txt
Empty file.
42 changes: 21 additions & 21 deletions hardware/CommunicationsPi/find_port.py
Expand Up @@ -6,45 +6,45 @@


def is_usb_serial(port, args):
if port.vid is None:
if port["vid"] is None:
return False
if not args.vid is None:
if port.vid != args.vid:
if not args.get("vid") is None:
if port["vid"] != args.get("vid"):
return False
if not args.pid is None:
if port.pid != args.pid:
if not args.get("pid") is None:
if port.get("pid") != args.get("pid"):
return False
if not args.vendor is None:
if not port.manufacturer.startswith(args.vendor):
if not args.get("vendor") is None:
if not port["manufacturer"].startswith(args.get("vendor")):
return False
if not args.serial is None:
if not port.serial_number.startswith(args.serial):
if not args.get("serial") is None:
if not port["serial_number"].startswith(args.get("serial")):
return False
if not args.intf is None:
if port.interface is None or not args.intf in port.interface:
if not args.get("intf") is None:
if port["interface"] is None or not args.get("intf") in port.get("interface"):
return False
return True


def extra_info(port):
extra_items = []
if port.manufacturer:
extra_items.append("vendor '{}'".format(port.manufacturer))
if port.serial_number:
extra_items.append("serial '{}'".format(port.serial_number))
if port.interface:
extra_items.append("intf '{}'".format(port.interface))
if port.get("manufacturer"):
extra_items.append("vendor '{}'".format(port["manufacturer"]))
if port.get("serial_number"):
extra_items.append("serial '{}'".format(port["serial_number"]))
if port.get("interface"):
extra_items.append("intf '{}'".format(port["interface"]))
if extra_items:
return " with " + " ".join(extra_items)
return ""


def getPort():
def get_port():
for port in serial.tools.list_ports.comports():
if is_usb_serial(port, None):
if is_usb_serial(port, {}):
print(port)
print(port.device)
return
print(port["device"])
return "port found"
return


Expand Down
13 changes: 5 additions & 8 deletions hardware/CommunicationsPi/lan_client.py
Expand Up @@ -9,19 +9,16 @@

url = os.environ["LAN_SERVER"]

logging.info('Pinging')
logging.info("Pinging")
while True:
try:
payload = {
'key1': 'value1',
'key2': 'value2'
}
logging.info('data: ' + json.dumps(payload))
payload = {"key1": "value1", "key2": "value2"}
logging.info("data: " + json.dumps(payload))
response = requests.post(url, data=payload)
response.raise_for_status()
except HTTPError as http_err:
logging.error('HTTP error occurred: {}'.format(str(http_err)))
logging.error("HTTP error occurred: {}".format(str(http_err)))
except Exception as err:
logging.error('error occurred: {}'.format(str(err)))
logging.error("error occurred: {}".format(str(err)))
else:
time.sleep(1)
3 changes: 2 additions & 1 deletion hardware/CommunicationsPi/lan_server.py
Expand Up @@ -7,6 +7,7 @@
# Logs status in a file
log = get_logger("LAN_SERVER_LOG_FILE")


class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
Expand All @@ -31,7 +32,7 @@ def do_POST(self):
str(self.headers),
post_data.decode("utf-8"),
)
log.info('data: ' + str(post_data))
log.info("data: " + str(post_data))

self._set_response()
self.wfile.write("POST request for {}".format(self.path).encode("utf-8"))
Expand Down
5 changes: 3 additions & 2 deletions hardware/CommunicationsPi/logger.py
@@ -1,7 +1,8 @@
import sys
# import sys
import os
import logging
from logging import DEBUG, INFO, ERROR

from logging import INFO


class Logger(object):
Expand Down
2 changes: 1 addition & 1 deletion hardware/CommunicationsPi/main.py
@@ -1 +1 @@
print("Hello Comms")
# print("Hello Comms")
37 changes: 0 additions & 37 deletions hardware/CommunicationsPi/port.py
Expand Up @@ -4,10 +4,8 @@
import glob
import sys
import json

import serial
import serial_asyncio
from django.utils import dateparse


class AsyncSerialProtocol(asyncio.Protocol):
Expand All @@ -27,54 +25,19 @@ def data_received(self, data):
except json.JSONDecodeError:
print()

# print(repr(data))
# json_to_models(repr(data))
# if len(repr(data).split(";")) > 2:
self.transport.close()

def connection_lost(self, exc):
print("port closed")
asyncio.get_event_loop().stop()


def json_to_models(json_str, event_id):
"""
Json example:
{
sensors:{
ss_id : “Sensor id”,
ss_value : {
/*as many values as you wish*/
value_a_name : “value_a”,
value_b_name : “value_b”,
value_c_name : “value_c”
}
date : “2014-03-12T13:37:27+00:00” /*ISO 8601 dates*/
};
"""
res = []
sensors = json_str["sensors"]
ss_id = int(sensors["ss_id"])
ss_value = sensors["ss_value"]
date = dateparse.parse_datetime(sensors["date"])


ser = serial.Serial()
ports = glob.glob("/dev/tty.u*")
print(ports[0])
ser.port = ports[0]
ser.timeout = 1
ser.open()
# print(ser.port)
# print(ser.baudrate)
# print(ser.parity)
# print(ser.stopbits)
# print(ser.bytesize)
# print(ser.timeout)
# while 1:
# x = ser.readline()
# if x is not "":
# print(x)

loop = asyncio.new_event_loop()
coro = serial_asyncio.create_serial_connection(loop, AsyncSerialProtocol, ports[0])
Expand Down
4 changes: 2 additions & 2 deletions hardware/CommunicationsPi/serial_read.py
@@ -1,7 +1,7 @@
import os
import json
import serial
from utils import get_logger, get_serial_stream
from utils import get_logger

logging = get_logger("RECEIVER_LOG_FILE")

Expand All @@ -18,7 +18,7 @@
logging.info("listening")
while 1:
x = ser.readline().decode("utf-8")
if x is not "":
if x != "":
try:
message = json.loads(x)
logging.info(message)
Expand Down
1 change: 0 additions & 1 deletion hardware/CommunicationsPi/serial_write.py
@@ -1,7 +1,6 @@
import os
import time
import serial
import json

from utils import get_logger, get_serial_stream

Expand Down
2 changes: 1 addition & 1 deletion hardware/CommunicationsPi/utils.py
@@ -1,6 +1,6 @@
import os
import json
from logger import Logger
from .logger import Logger


def get_serial_stream(s):
Expand Down
Empty file added hardware/SensorPi/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion hardware/SensorPi/main.py
@@ -1 +1 @@
print("Hello sensors")
# print("Hello sensors")
Empty file added hardware/__init__.py
Empty file.
Empty file added hardware/tests/__init__.py
Empty file.