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

Tests/main #650

Merged
merged 11 commits into from May 3, 2020
89 changes: 69 additions & 20 deletions hardware/main.py
Expand Up @@ -5,16 +5,12 @@
from dotenv import load_dotenv
from hardware.Utils.logger import Logger

logger = Logger(name="main.log", filename="main.log")
logger.info("Started hardware main.py")

PI_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
dotenv_file = os.path.join(PI_DIR, "hardware/env")
if os.path.isfile(dotenv_file): # pragma: no cover
load_dotenv(dotenv_path=dotenv_file)
else:
else: # pragma: no cover
print("dotenv_file was not a file")
logger.info("dotenv_file was not a file")

from hardware.CommunicationsPi.radio_transceiver import Transceiver # noqa: E402
from hardware.CommunicationsPi.comm_pi import CommPi # noqa: E402
Expand All @@ -25,13 +21,37 @@
from hardware.gpsPi.gps_reader import GPSReader # noqa: E402


if os.environ["HARDWARE_TYPE"] == "commPi":
print("CommunicationsPi")
logger.info("CommunicationsPi")
def main():
logger = Logger(name="main.log", filename="main.log")
logger.info("Started hardware main.py")
if os.environ["HARDWARE_TYPE"] == "commPi":
logger.info("CommunicationsPi")
handleComm()
elif os.environ["HARDWARE_TYPE"] == "sensePi":
logger.info("SensePi")
handleSense()
elif os.environ["HARDWARE_TYPE"] == "gpsPi":
logger.info("gpsPi")
handleGps()
else:
logger.info("Local Django Server")
handleLocal()


def handleComm():
"""
Starts up the CommunicationsPi server and starts listening for
traffic
"""
runServer(handler_class=CommPi)
elif os.environ["HARDWARE_TYPE"] == "sensePi":
print("SensePi")
logger.info("SensePi")


def handleSense():
"""
Starts up the SensorPi runtime, begins listening for SenseHat input,
establishing a connection to a local CommPi via LAN, and sending data
for transmission to the CommPi
"""
sensor_keys = get_sensor_keys()
sensor_ids = {}
sensor_ids[sensor_keys["TEMPERATURE"]] = 2
Expand All @@ -40,7 +60,7 @@
sensor_ids[sensor_keys["ACCELERATION"]] = 5
sensor_ids[sensor_keys["ORIENTATION"]] = 6
sensePi = SensePi(sensor_ids=sensor_ids)
gpsPi = GPSReader()

client = WebClient()

while True:
Expand All @@ -51,13 +71,8 @@
acc = sensePi.get_acceleration()
orie = sensePi.get_orientation()
all = sensePi.get_all()
coords = gpsPi.get_geolocation()
speed = gpsPi.get_speed_mph()

if coords is not None:
dataArr = [temp, pres, hum, acc, orie, coords, speed, all]
else:
dataArr = [temp, pres, hum, acc, orie, all]
dataArr = [temp, pres, hum, acc, orie, all]

for payload in dataArr:
payload = json.dumps(payload)
Expand All @@ -69,8 +84,38 @@
print("error occurred: {}".format(str(err)))
raise
time.sleep(1)
else:
print("Local Django Server")


def handleGps():
"""
Starts up the GPSPi runtime, begins listening for GPS Hat input,
establishing a connection to a local CommPi via LAN, and sending data
for transmission to the CommPi
"""
gpsPi = GPSReader()
client = WebClient()

while True:
print("gps loop")
coords = gpsPi.get_geolocation()
speed = gpsPi.get_speed_mph()
if coords is not None:
data = [coords, speed]
for i in data:
payload = json.loads(json.dumps(i))
try:
client.send(payload)
except Exception as err:
print("Error transmitting gps data: {}".format(str(err)))
raise
time.sleep(1)


def handleLocal():
"""
starts listening on the defined serial port and passing
received data along to the web client when received
"""
transceiver = Transceiver()
url = os.environ.get("DJANGO_SERVER_API_ENDPOINT")
if url:
Expand All @@ -83,3 +128,7 @@
client.send(payload)
else:
print("DJANGO_SERVER_API_ENDPOINT not set")


if __name__ == "__main__": # pragma: no cover
main()
1 change: 1 addition & 0 deletions hardware/tests/test_comm_pi.py
Expand Up @@ -77,6 +77,7 @@ def test_post_radio_with_internet(
"ENABLE_INTERNET_TRANSMISSION": "True",
"COMM_PI_LOG_FILE": "comm.log",
"LOG_DIRECTORY": "logs",
"ENABLE_RADIO_TRANSMISSION": "",
},
):
payload = '{"key": "value"}'
Expand Down